Skip to content

Commit

Permalink
Merge pull request #5735 from LiskHQ/5704_deduct_fee
Browse files Browse the repository at this point in the history
Update balance in beforeTransactionApply - Closes #5704
  • Loading branch information
shuse2 committed Aug 31, 2020
2 parents 10896f1 + f29cc36 commit eced17c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
13 changes: 11 additions & 2 deletions framework/src/modules/token/token_module.ts
Expand Up @@ -101,8 +101,11 @@ export class TokenModule extends BaseModule {
}

// eslint-disable-next-line @typescript-eslint/require-await
public async beforeTransactionApply({ transaction }: TransactionApplyContext): Promise<void> {
// Throw error if fee is lower than minimum fee (minFeePerBytes + baseFee)
public async beforeTransactionApply({
transaction,
stateStore,
}: TransactionApplyContext): Promise<void> {
// 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(
Expand All @@ -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.get<TokenAccount>(senderAddress);
sender.token.balance -= transaction.fee;
stateStore.account.set(senderAddress, sender);
}

public async afterTransactionApply({
Expand Down
21 changes: 21 additions & 0 deletions framework/test/unit/modules/token/token_module.spec.ts
Expand Up @@ -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', () => {
Expand Down

0 comments on commit eced17c

Please sign in to comment.