从tic.common转化而来,彻底脱离对 Base/*, _Ling.js 的依赖,只保留并提供 Action及其子类。

This commit is contained in:
luk.lu
2018-11-27 10:42:05 +08:00
commit 89975eea2c
5 changed files with 187 additions and 0 deletions

40
ling/ActTransfer.js Normal file
View File

@@ -0,0 +1,40 @@
const Action = require('./Action.js')
const Ticrypto = wo&&wo.Crypto?wo.Crypto:require('tic.crypto')
/******************** Public of instance ********************/
const DAD=module.exports=function ActTransfer(prop) {
this._class='ActTransfer'
this.setProp(prop) // 没有定义 ActTransfer.prototype._model因此继承了上级Action.prototype._model因此通过this.setProp继承了上级Action定义的实例自有数据。另一个方案是调用 Action.call(this, prop)
this.type='ActTransfer'
}
DAD.__proto__= Action
// DAD._table=DAD.name // 注释掉从而继承父类Action的数据库表格名
const MOM=DAD.prototype
MOM.__proto__=Action.prototype
/******************** Shared by instances ********************/
MOM.validate=function(){
// return Ticrypto.isAddress(this.toAddress)
return Ticrypto.isAddress(this.toAddress) && this.fee>=wo.Config.MIN_FEE_ActTransfer
// && wo.Account.accountPool[this.actorAddress].balance>this.amount+this.fee //Todo:引入缓存账户
&&this.toAddress != this.actorAddress
}
MOM.execute=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: sender.balance-this.amount-this.fee }, 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: getter.balance+this.amount }, 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={}