From 0c311382cd9f787c99f3282337d7a3932ce8b208 Mon Sep 17 00:00:00 2001 From: mitsuaki-u Date: Mon, 31 Aug 2020 14:34:56 +0200 Subject: [PATCH 1/2] Deduct tx fee from account --- framework/src/modules/token/token_module.ts | 13 ++++++++++-- .../unit/modules/token/token_module.spec.ts | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/framework/src/modules/token/token_module.ts b/framework/src/modules/token/token_module.ts index 69f7fadfacc..e0618830806 100644 --- a/framework/src/modules/token/token_module.ts +++ b/framework/src/modules/token/token_module.ts @@ -101,8 +101,11 @@ export class TokenModule extends BaseModule { } // eslint-disable-next-line @typescript-eslint/require-await - public async beforeTransactionApply({ transaction }: TransactionApplyContext): Promise { - // Throw error if fee is lower than minimum fee (minFeePerBytes + baseFee) + public async beforeTransactionApply({ + transaction, + stateStore, + }: TransactionApplyContext): Promise { + // Check if transaction fee is lower than the minimum required fee (minFeePerBytes + baseFee) const minFee = BigInt(this.config.minFeePerByte) * BigInt(transaction.getBytes().length); const baseFee = this.config.baseFees.find( @@ -114,6 +117,12 @@ export class TokenModule extends BaseModule { `Insufficient transaction fee. Minimum required fee is: ${minimumRequiredFee.toString()}`, ); } + + // Deduct transaction fee from sender balance + const senderAddress = transaction.senderID; + const sender = await stateStore.account.getOrDefault(senderAddress); + sender.token.balance -= transaction.fee; + stateStore.account.set(senderAddress, sender); } public async afterTransactionApply({ diff --git a/framework/test/unit/modules/token/token_module.spec.ts b/framework/test/unit/modules/token/token_module.spec.ts index 404f780e30c..ab387f6125c 100644 --- a/framework/test/unit/modules/token/token_module.spec.ts +++ b/framework/test/unit/modules/token/token_module.spec.ts @@ -238,6 +238,27 @@ describe('token module', () => { ), ); }); + + it('should deduct transaction fee from sender account', async () => { + const expectedSenderAccount = { + ...senderAccount, + token: { + ...senderAccount.token, + balance: senderAccount.token.balance - validTransaction.fee, + }, + }; + + await tokenModule.beforeTransactionApply({ + stateStore, + transaction: validTransaction, + reducerHandler, + }); + + expect(stateStore.account.set).toHaveBeenCalledWith( + senderAccount.address, + expectedSenderAccount, + ); + }); }); describe('#afterTransactionApply', () => { From 9d628223152c2d5bac6427b06438ec91519a128a Mon Sep 17 00:00:00 2001 From: mitsuaki-u Date: Mon, 31 Aug 2020 15:47:49 +0200 Subject: [PATCH 2/2] Use state store get --- framework/src/modules/token/token_module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/modules/token/token_module.ts b/framework/src/modules/token/token_module.ts index e0618830806..ecf85961a81 100644 --- a/framework/src/modules/token/token_module.ts +++ b/framework/src/modules/token/token_module.ts @@ -120,7 +120,7 @@ export class TokenModule extends BaseModule { // Deduct transaction fee from sender balance const senderAddress = transaction.senderID; - const sender = await stateStore.account.getOrDefault(senderAddress); + const sender = await stateStore.account.get(senderAddress); sender.token.balance -= transaction.fee; stateStore.account.set(senderAddress, sender); }