Skip to content

Commit

Permalink
feat: 🎸 Update transaction fees algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
limsbase committed Jul 30, 2020
1 parent 840182c commit 61e172f
Show file tree
Hide file tree
Showing 77 changed files with 597 additions and 691 deletions.
75 changes: 45 additions & 30 deletions examples/fun-tests/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ module.exports = {
// Fees for transacton
fees: {
// base
send: '10000000',
vote: '10000000',
secondSignature: '500000000',
delegate: '10000000000',
multiSignature: '500000000',
transfer: '0.1',
vote: '0.1',
delegate: '100',
signature: '5',
multiSignature: '5',
lock: 'o.1',

// aob
aob_issuer: '100',
Expand All @@ -60,20 +61,21 @@ module.exports = {
aob_transfer: '0',

// dapp
dappRegistration: '2500000000',
dappWithdrawal: '10000000',
dappDeposit: '10000000',
data: '10000000',
dapp: '100',
dapp_out: '0.1',
dapp_in: '0.1',

// todo: 测试中提供的费用
username: '10000000',
multiTransfer: '10000000',
dapp: '10000000000',
username: '0.1',

// dao
evidence: '10000000', // fixme
org: '10000000',
exchange: '10000000'
dao_confirmation: '1',
dao_contribution: '1',
dao_exchange: '0.1',
dao_org: '0.1',

// evidence
evidence: '0.1' // fixme
}
},

Expand All @@ -94,25 +96,38 @@ module.exports = {

// Fees for transacton
fees: {
send: '10000000', // Bignum update
vote: '100000000',
secondSignature: '500000000',
delegate: '2500000000',
multisignature: '500000000',
dappRegistration: '2500000000',
dappWithdrawal: '10000000',
dappDeposit: '10000000',
data: '10000000',
// base
transfer: '0.1',
vote: '0.1',
delegate: '100',
signature: '5',
multiSignature: '5',
lock: 'o.1',

// aob
aob_issuer: '100',
aob_asset: '500',
aob_flag: '0.1',
aob_acl: '0.1',
aob_issue: '0.1',
aob_transfer: '0',

// dapp
dapp: '100',
dapp_out: '0.1',
dapp_in: '0.1',

// todo: 测试中提供的费用
username: '10000000',
multiTransfer: '10000000',
dapp: '10000000000',
username: '0.1',

// dao
evidence: '10000000', // fixme
org: '10000000',
exchange: '10000000'
dao_confirmation: '1',
dao_contribution: '1',
dao_exchange: '0.1',
dao_org: '0.1',

// evidence
evidence: '0.1' // fixme
}
}
}
2 changes: 1 addition & 1 deletion examples/fun-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"node": ">=8.6.0"
},
"scripts": {
"start": "cross-env NODE_ENV=development node --harmony app.js",
"start": "cross-env NODE_ENV=development node app.js",
"test": "./node_modules/.bin/mocha -r intelli-espower-loader test/**/*.test.js",
"build": "cross-env NODE_ENV=production ./node_modules/.bin/gulp linux-build-main",
"build-testnet": "./node_modules/.bin/gulp linux-build-test",
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-aob/src/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class Acl extends Asset.Base {
return bb.toBuffer()
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_acl, this.constants.fixedPoint)
}

async verify (trs, sender) {
await super.verify(trs, sender)

Expand Down
2 changes: 1 addition & 1 deletion packages/asset-aob/src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Aob extends Asset.Base {
}

async calculateFee () {
return DdnUtils.bignum.multiply(500, this.constants.fixedPoint)
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_asset, this.constants.fixedPoint)
}

async verify (trs, sender) {
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-aob/src/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Flags extends Asset.Base {
return buffer
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_flag, this.constants.fixedPoint)
}

async verify (trs, sender) {
await super.verify(trs, sender)

Expand Down
4 changes: 4 additions & 0 deletions packages/asset-aob/src/issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Issue extends Asset.Base {
return buffer
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_issue, this.constants.fixedPoint)
}

async verify (trs, sender) {
const assetIssue = await this.getAssetObject(trs)

Expand Down
2 changes: 1 addition & 1 deletion packages/asset-aob/src/issuer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Issuer extends Asset.Base {
}

async calculateFee () {
return DdnUtils.bignum.multiply(100, this.constants.fixedPoint)
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_issuer, this.constants.fixedPoint)
}

async verify (trs, sender) {
Expand Down
3 changes: 1 addition & 2 deletions packages/asset-aob/src/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Transfer extends Asset.Base {
}

async calculateFee () {
console.log('this.constants[this.config.net].fees.aob_transfer', this.constants[this.config.net].fees.aob_transfer)
return DdnUtils.bignum.multiply(this.constants[this.config.net].fees.aob_transfer, this.constants.fixedPoint)
return DdnUtils.bignum.multiply(this.constants.net.fees.aob_transfer, this.constants.fixedPoint)
}

async create (data, trs) {
Expand Down
42 changes: 20 additions & 22 deletions packages/asset-base/src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,27 @@ class AssetBase {
}

/**
* 计算该类型资产交易的手续费(方法内不允许使用context对象内容)
* @param {*} trs
* @param {*} sender
*/
* 计算该类型资产交易的手续费,如果子类没有定义该方法,默认是转账交易费,即:0.1 DDN
* @param {*} trs
* @param {*} sender
*/
async calculateFee () {
console.log('base.js...................')
// return DdnUtils.bignum.multiply(0.0002, 100) // fixme: 这里应该可以定制 2020.5.31
return DdnUtils.bignum.multiply(this.constants.net.fees.transfer, this.constants.fixedPoint)
}

/**
* 定义资产属性和字段的对应关系
* 最多支持定义15个属性
* 字符串类型10个,名称分别是str1,str2,str3...str10,长度分别是32,64,64,128,128,256,256,512,512,1024,前4个有索引
* 整数类型3个,名称分别是int1,int2,int3,类型为INT,前2个有索引
* 时间戳类型2个,分别是timestamp1,timestamp2
* 扩展类无上限,名称使用str_ext, int_ext, timestamp_ext,分别定义不同类型
*
* 以下属于系统属性,不可使用
* amount:转账金额,默认为0,字符串类型
* recipientId:收款地址,默认为null
* message:备注信息
*/
* 定义资产属性和字段的对应关系
* 最多支持定义15个属性
* 字符串类型10个,名称分别是str1,str2,str3...str10,长度分别是32,64,64,128,128,256,256,512,512,1024,前4个有索引
* 整数类型3个,名称分别是int1,int2,int3,类型为INT,前2个有索引
* 时间戳类型2个,分别是timestamp1,timestamp2
* 扩展类无上限,名称使用str_ext, int_ext, timestamp_ext,分别定义不同类型
*
* 以下属于系统属性,不可使用
* amount:转账金额,默认为0,字符串类型
* recipientId:收款地址,默认为null
* message:备注信息
*/
async propsMapping () {
throw new Error('AssetBase子类必须重载propsMapping方法。')

Expand All @@ -144,12 +143,11 @@ class AssetBase {
* 自定义资产Api
* @param {*} router
*/
async attachApi () {
}
async attachApi () { }

/**
* 判断是否包含Json扩展属性
*/
* 判断是否包含Json扩展属性
*/
async hasExtProps () {
if (this.isHasExtProps !== null && typeof (this.isHasExtProps) !== 'undefined') {
return this.isHasExtProps
Expand Down
6 changes: 2 additions & 4 deletions packages/asset-dao/src/confirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ class Confirmation extends Asset.Base {

async calculateFee (trs) {
const confirmation = await this.getAssetObject(trs)
let feeBase = '1'
let feeBase = this.constants.net.fees.dao_confirmation
if (confirmation.state === 0) {
feeBase = '0' // 拒绝稿件时手续费为0
}

const result = DdnUtils.bignum.multiply(feeBase, this.constants.fixedPoint).toString()

return result
return DdnUtils.bignum.multiply(feeBase, this.constants.fixedPoint).toString()
}

async verify (trs) {
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-dao/src/contribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class Contribution extends Asset.Base {
return bb.toBuffer()
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.dao_contribution, this.constants.fixedPoint)
}

async dbSave (trs, dbTrans) {
const contribution = await this.getAssetObject(trs)
contribution.url = (contribution.url + '').toLowerCase()
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-dao/src/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ class Exchange extends Asset.Base {
return bb.toBuffer()
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.dao_exchange, this.constants.fixedPoint)
}

async applyUnconfirmed (trs, sender, dbTrans) {
const assetObj = await this.getAssetObject(trs)
const key = `${sender.address}:${trs.type}:${assetObj.org_id}:${assetObj.state}`
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-dapp/src/dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Dapp extends Asset.Base {
}

async calculateFee () {
return DdnUtils.bignum.multiply(100, this.constants.fixedPoint)
return DdnUtils.bignum.multiply(this.constants.net.fees.dapp, this.constants.fixedPoint)
}

async verify (trs) {
Expand Down
4 changes: 4 additions & 0 deletions packages/asset-dapp/src/in-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class InTransfer extends Asset.Base {
]
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.dapp_in, this.constants.fixedPoint)
}

async create (data, trs) {
trs.recipientId = null

Expand Down
4 changes: 4 additions & 0 deletions packages/asset-dapp/src/out-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class OutTransfer extends Asset.Base {
return buf
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.dapp_out, this.constants.fixedPoint)
}

async create (data, trs) {
trs.recipientId = data.recipientId
trs.amount = '0'
Expand Down
25 changes: 13 additions & 12 deletions packages/asset-evidence/src/evidence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ByteBuffer from 'bytebuffer'
import Asset from '@ddn/asset-base'
import DdnUtils from '@ddn/utils'

class Evidence extends Asset.Base {
// eslint-disable-next-line no-useless-constructor
Expand Down Expand Up @@ -67,6 +68,10 @@ class Evidence extends Asset.Base {
return bb.toBuffer()
}

async calculateFee () {
return DdnUtils.bignum.multiply(this.constants.net.fees.evidence, this.constants.fixedPoint)
}

async verify (trs, sender) {
const trans = await super.verify(trs, sender)
const assetObj = await this.getAssetObject(trs)
Expand All @@ -89,12 +94,10 @@ class Evidence extends Asset.Base {
['senderId'],
async (err, { senderId }) => {
if (err) {
reject(err)
return reject(err)
} else {
if (senderId !== sender.address) {
return reject(
`The evidence ipid ${assetObj.ipid} has been registered by ${senderId}`
)
return reject(new Error(`The evidence ipid ${assetObj.ipid} has been registered by ${senderId})`))
} else {
let results2
try {
Expand All @@ -113,17 +116,17 @@ class Evidence extends Asset.Base {
}

if (results2 && results2.length > 0) {
return reject(
`The evidence hash already exists: ${assetObj.hash}`
)
return reject(new Error(`The evidence hash already exists: ${assetObj.hash}`))
} else {
resolve(trans)
return resolve(trans)
}
}
}
}

)
})
}
)
} else {
return trans
}
Expand All @@ -133,9 +136,7 @@ class Evidence extends Asset.Base {
const assetObj = await this.getAssetObject(trs)
const key = `${sender.address}:${trs.type}:${assetObj.ipid}`
if (this.oneoff.has(key)) {
throw new Error(
`The evidence ${assetObj.ipid} is in process already.`
)
throw new Error(`The evidence ${assetObj.ipid} is in process already.`)
}

await super.applyUnconfirmed(trs, sender, dbTrans)
Expand Down
Loading

0 comments on commit 61e172f

Please sign in to comment.