41 lines
2.0 KiB
JavaScript
41 lines
2.0 KiB
JavaScript
const Action = require('./Action.js')
|
||
const ticCrypto = require('tic.crypto')
|
||
|
||
const DAD = module.exports = function ActionTransfer (prop) {
|
||
this._class = this.constructor.name
|
||
this.setProp(prop) // 没有定义 ActionTransfer.prototype._model,因此继承了上级Action.prototype._model,因此通过this.setProp,继承了上级Action定义的实例自有数据。另一个方案是,调用 Action.call(this, prop)
|
||
this.type = this.constructor.name
|
||
}
|
||
DAD.__proto__ = Action
|
||
|
||
const MOM = DAD.prototype
|
||
MOM.__proto__ = Action.prototype
|
||
|
||
MOM.validateMe = function () {
|
||
// if (sender && sender.type !== 'multisig' && action.toAddress != action.actorAddress && sender.balance >= action.amount + action.fee){
|
||
return this.actorPubkey && this.toAddress && ticCrypto.pubkey2address(this.actorPubkey)!== this.toAddress // 不能转帐给自己。
|
||
&& this.amount && this.amount > 0 && (this.fee >= 0)
|
||
}
|
||
|
||
MOM.executableMe = async function() {
|
||
let balance = await wo.Account.getBalance(this.actorAddress)
|
||
return balance >= this.amount + this.fee
|
||
}
|
||
|
||
MOM.executeMe = async function () {
|
||
let sender= await wo.Account.getOne({Account: { address: this.actorAddress }})
|
||
if (sender && sender.type !== 'multisig' && this.toAddress != this.actorAddress && sender.balance >= this.amount + this.fee){
|
||
await sender.setMe({Account:{ balance: Number(sender.balance)-Number(this.amount)-Number(this.fee), countAction: sender.countAction+1 }, cond:{ address:sender.address}})
|
||
let getter= await wo.Account.getOne({Account: { address: this.toAddress }}) || await wo.Account.addOne({Account: { address: this.toAddress }})
|
||
await getter.setMe({Account:{ balance: Number(getter.balance)+Number(this.amount), countAction: getter.countAction+1 }, cond:{ address:getter.address}})
|
||
mylog.info('Excecuted action='+JSON.stringify(this))
|
||
return this
|
||
}
|
||
// mylog.info('balance('+sender.address+')='+sender.balance+' is less than '+this.amount+', 无法转账')
|
||
return null
|
||
}
|
||
|
||
/** ****************** Public of class ********************/
|
||
|
||
DAD.api = {}
|