From 68e665804c9ebc89a3e03bff334f99ff44848b10 Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Fri, 9 Dec 2022 11:24:21 +0300 Subject: [PATCH] add compute fee --- src/pages/SharedData.jsx | 4 ++- src/store/store.js | 14 ++++++--- src/utils/Utils.js | 68 ++++++++++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/src/pages/SharedData.jsx b/src/pages/SharedData.jsx index 34c56fd..7f35782 100644 --- a/src/pages/SharedData.jsx +++ b/src/pages/SharedData.jsx @@ -66,7 +66,9 @@ function SharedData() { BigInt(store.accountInfo?.sequence?.nonce || 0), store.pubKey, store.nodeInfo.networkIdentifier, - store.passPhrase + store.passPhrase, + store.nodeInfo?.genesisConfig?.minFeePerByte, + store.nodeInfo?.genesisConfig?.baseFees ); const signedTx = builder.validate( diff --git a/src/store/store.js b/src/store/store.js index 465b0a4..53af6e8 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -4,7 +4,7 @@ import { onBecomeObserved, onBecomeUnobserved, } from 'mobx'; -import { cryptography } from '@liskhq/lisk-client'; +import { cryptography, transactions } from '@liskhq/lisk-client'; import { passphrase } from '@liskhq/lisk-client'; import { getNodeInfo } from '../api/node'; import { fetchWrapper } from '../shared/fetchWrapper'; @@ -210,7 +210,9 @@ class Store { BigInt(this.accountInfo?.sequence?.nonce || 0), this.addressAndPubKey.publicKey, this.nodeInfo.networkIdentifier, - this.passPhrase + this.passPhrase, + this.nodeInfo?.genesisConfig?.minFeePerByte, + this.nodeInfo?.genesisConfig?.baseFees ); let signedTx = null; @@ -232,7 +234,9 @@ class Store { BigInt(this.accountInfo?.sequence?.nonce || 0), this.addressAndPubKey.publicKey, this.nodeInfo.networkIdentifier, - this.passPhrase + this.passPhrase, + this.nodeInfo?.genesisConfig?.minFeePerByte, + this.nodeInfo?.genesisConfig?.baseFees ); const signedTx = builder.vote(delegateAddress, amount); @@ -251,7 +255,9 @@ class Store { BigInt(this.accountInfo?.sequence?.nonce || 0), this.addressAndPubKey.publicKey, this.nodeInfo.networkIdentifier, - this.passPhrase + this.passPhrase, + this.nodeInfo?.genesisConfig?.minFeePerByte, + this.nodeInfo?.genesisConfig?.baseFees ); const unlockObjects = this.accountLockedVotesCanReturn[delegateAddress]; diff --git a/src/utils/Utils.js b/src/utils/Utils.js index 06e8565..a074b72 100644 --- a/src/utils/Utils.js +++ b/src/utils/Utils.js @@ -156,7 +156,8 @@ export const generateTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000) + minFeePerByte = 0, + baseFees = [] ) => { return { update: (features) => @@ -166,7 +167,8 @@ export const generateTransaction = ( senderPublicKey, networkIdentifier, passPhrase, - fee + minFeePerByte, + baseFees ), remove: (features) => generateRemoveTransaction( @@ -175,7 +177,8 @@ export const generateTransaction = ( senderPublicKey, networkIdentifier, passPhrase, - fee + minFeePerByte, + baseFees ), validate: (features, recipientAddress) => generateValidateTransaction( @@ -184,7 +187,8 @@ export const generateTransaction = ( senderPublicKey, networkIdentifier, passPhrase, - fee, + minFeePerByte, + baseFees, recipientAddress ), vote: (delegateAddress, amount) => @@ -193,7 +197,8 @@ export const generateTransaction = ( senderPublicKey, networkIdentifier, passPhrase, - fee, + minFeePerByte, + baseFees, delegateAddress, amount ), @@ -203,7 +208,8 @@ export const generateTransaction = ( senderPublicKey, networkIdentifier, passPhrase, - fee, + minFeePerByte, + baseFees, delegateAddress, unlockObjects ), @@ -216,19 +222,25 @@ export const generateSetTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000) + minFeePerByte = 0, + baseFees = [] ) => { const tx = { moduleID: 1001, assetID: 1, nonce, senderPublicKey, - fee, asset: { features, }, }; + tx.fee = transactions.computeMinFee(setFeatureAssetSchema, tx, { + minFeePerByte, + baseFees, + numberOfSignatures: 1, + }); + if (features.length === 0) return; const signedTx = transactions.signTransaction( @@ -256,18 +268,24 @@ export const generateRemoveTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000) + minFeePerByte = 0, + baseFees = [] ) => { const tx = { moduleID: 1001, assetID: 2, nonce, senderPublicKey, - fee, asset: { features, }, }; + + tx.fee = transactions.computeMinFee(removeFeatureAssetSchema, tx, { + minFeePerByte, + baseFees, + numberOfSignatures: 1, + }); const signedTx = transactions.signTransaction( removeFeatureAssetSchema, tx, @@ -289,7 +307,8 @@ const generateValidateTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000), + minFeePerByte = 0, + baseFees = [], recipientAddress = '' ) => { const tx = { @@ -297,13 +316,18 @@ const generateValidateTransaction = ( assetID: 11, nonce, senderPublicKey: Buffer.from(senderPublicKey, 'hex'), - fee, asset: { features, recipientAddress: Buffer.from(recipientAddress, 'hex'), }, }; + tx.fee = transactions.computeMinFee(validateFeatureAssetSchema, tx, { + minFeePerByte, + baseFees, + numberOfSignatures: 1, + }); + if (features.length === 0) return; const signedTx = transactions.signTransaction( @@ -332,7 +356,8 @@ const generateVoteTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000), + minFeePerByte = 0, + baseFees = [], delegateAddress = '', amount = 0n ) => { @@ -341,7 +366,6 @@ const generateVoteTransaction = ( assetID: 1, nonce, senderPublicKey: Buffer.from(senderPublicKey, 'hex'), - fee, asset: { votes: [ { @@ -352,6 +376,12 @@ const generateVoteTransaction = ( }, }; + tx.fee = transactions.computeMinFee(voteDelegateAssetSchema, tx, { + minFeePerByte, + baseFees, + numberOfSignatures: 1, + }); + const signedTx = transactions.signTransaction( voteDelegateAssetSchema, tx, @@ -376,7 +406,8 @@ const generateUnlockTransaction = ( senderPublicKey = '', networkIdentifier = '', passPhrase = '', - fee = BigInt(500000), + minFeePerByte = 0, + baseFees = [], delegateAddress = '', unlockObjects = [] ) => { @@ -385,7 +416,6 @@ const generateUnlockTransaction = ( assetID: 2, nonce, senderPublicKey: Buffer.from(senderPublicKey, 'hex'), - fee, asset: { unlockObjects: unlockObjects.map((e) => ({ delegateAddress: Buffer.from(delegateAddress, 'hex'), @@ -395,6 +425,12 @@ const generateUnlockTransaction = ( }, }; + tx.fee = transactions.computeMinFee(unlockDelegateAssetSchema, tx, { + minFeePerByte, + baseFees, + numberOfSignatures: 1, + }); + const signedTx = transactions.signTransaction( unlockDelegateAssetSchema, tx,