这样消除了同一份代码出现在两处的不良结构,避免了同步的困难。 当 node.server 需要临时修改 ActionXxx.js 时,只要在 server.js 里临时 require('../tic.action').ActionXxx 即可。一处修改,到处可用。
77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
const Action = require('./Action.js')
|
|
const Methods = ['create', 'transfer', 'exchange', 'mount']
|
|
async function actValidator (action) {
|
|
switch (action.data.method) {
|
|
case 'create':
|
|
if (action.data.name && action.data.symbol && action.data.decimals &&
|
|
Number.isSafeInteger(Number(action.data.decimals)) &&
|
|
!await wo.Tac.getOne({ Tac: { name: action.data.name, symbol: action.data.symbol } })
|
|
) { return true }
|
|
return false
|
|
case 'transfer':
|
|
return action.amount && action.amount > 0 && action.actorAddress && action.toAddress && action.actorAddress !== action.toAddress
|
|
case 'exchange':
|
|
return true
|
|
case 'mount':
|
|
return true
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
|
|
class ActionTac extends Action {
|
|
constructor (prop) {
|
|
super()
|
|
Object.defineProperty(this, '_class', {
|
|
value: 'ActionTac',
|
|
enumerable: true,
|
|
writable: false
|
|
}),
|
|
Object.defineProperty(this, 'type', {
|
|
value: 'ActionTac',
|
|
enumerable: true,
|
|
writable: false
|
|
}),
|
|
Object.defineProperty(this, 'data', {
|
|
value: prop,
|
|
enumerable: true,
|
|
writable: false
|
|
})
|
|
}
|
|
static async validate (action) { // todo 20190409: MOM.validateMe
|
|
return Methods.includes(action.data.method) && await actValidator(action)
|
|
}
|
|
|
|
static async execute (action) { // todo 20190409: MOM.executeMe
|
|
if (action && action.data.method) {
|
|
switch (action.data.method) {
|
|
case 'create':
|
|
delete action._class
|
|
let tac = new wo.Tac(
|
|
Object.assign(action.data,
|
|
action.actorAddress,
|
|
action.actorPubkey,
|
|
action.actorSignature
|
|
))
|
|
tac.address = wo.Crypto.pubkey2address(wo.Crypto.hash(action.actorSignature, action.hash))
|
|
return await tac.addMe()
|
|
case 'transfer':
|
|
// 内部交易,转发到应用链进程来处理
|
|
await wo.Store.decrease(action.actorAddress, 0 - action.amount, action.address)
|
|
await wo.Store.increase(action.toAddress, action.amount, action.address)
|
|
return true
|
|
case 'exchange':
|
|
// Bancor类型
|
|
return wo.Tac.exchange(action)
|
|
case 'mount':
|
|
return wo.Tac.mount(action)
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
}
|
|
|
|
module.exports = ActionTac
|