Skip to content

Commit

Permalink
fix(feeByEvent): fix tip inclusion for partialFee (#986)
Browse files Browse the repository at this point in the history
* fix(feeByEvent): add tip inclusion to approximation

* set mock data for events using tips

* write tests and adjust logic to include tips into partial fee
  • Loading branch information
TarikGul committed Jul 20, 2022
1 parent cfc2fb4 commit 91dda83
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
33 changes: 27 additions & 6 deletions src/services/blocks/BlocksService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PromiseRpcResult } from '@polkadot/api-base/types/rpc';
import { GenericExtrinsic } from '@polkadot/types';
import { GenericCall } from '@polkadot/types/generic';
import { BlockHash, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { Compact } from '@polkadot/types-codec/base';
import { BadRequest } from 'http-errors';
import LRU from 'lru-cache';

Expand All @@ -44,6 +45,7 @@ import {
constructEvent,
treasuryEvent,
withdrawEvent,
withdrawEventForTip,
} from '../test-helpers/mock/data/mockEventData';
import { validators789629Hex } from '../test-helpers/mock/data/validators789629Hex';
import { parseNumberOrThrow } from '../test-helpers/mock/parseNumberOrThrow';
Expand Down Expand Up @@ -562,7 +564,8 @@ describe('BlocksService', () => {
it('Should retrieve the correct fee for balances::withdraw events', () => {
const response = blocksService['getPartialFeeByEvents'](
withdrawEvent,
partialFee
partialFee,
null
);

expect(response).toStrictEqual(expectedResponse);
Expand All @@ -571,7 +574,8 @@ describe('BlocksService', () => {
it('Should retrieve the correct fee for treasury::deposit events', () => {
const response = blocksService['getPartialFeeByEvents'](
treasuryEvent,
partialFee
partialFee,
null
);

expect(response).toStrictEqual(expectedResponse);
Expand All @@ -580,7 +584,21 @@ describe('BlocksService', () => {
it('Should retrieve the correct fee for balances::deposit events', () => {
const response = blocksService['getPartialFeeByEvents'](
balancesDepositEvent,
partialFee
partialFee,
null
);

expect(response).toStrictEqual(expectedResponse);
});

it('Should retrieve the correct fee for balances:withdraw events with a tip', () => {
const expectedResponse = { partialFee: '1681144907847007' };
const fee = polkadotRegistry.createType('Balance', '1675415067070856');
const tip = new Compact(polkadotRegistry, 'u64', 5729827274000);
const response = blocksService['getPartialFeeByEvents'](
withdrawEventForTip,
fee,
tip
);

expect(response).toStrictEqual(expectedResponse);
Expand All @@ -594,7 +612,8 @@ describe('BlocksService', () => {
const emptyArray = [] as unknown as ISanitizedEvent[];
const response = blocksService['getPartialFeeByEvents'](
emptyArray,
partialFee
partialFee,
null
);

expect(response).toStrictEqual(expectedResponseWithError);
Expand All @@ -611,7 +630,8 @@ describe('BlocksService', () => {
mockEvent,
'0x',
blockHash789629,
true
true,
null
);

expect(sanitizeNumbers(response)).toStrictEqual({
Expand All @@ -626,7 +646,8 @@ describe('BlocksService', () => {
mockEvent,
'0x',
blockHash789629,
false
false,
null
);

expect(sanitizeNumbers(response)).toStrictEqual({
Expand Down
28 changes: 18 additions & 10 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Header,
} from '@polkadot/types/interfaces';
import { AnyJson, Codec, Registry } from '@polkadot/types/types';
import { ICompact, INumber } from '@polkadot/types-codec/types/interfaces';
import { u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import BN from 'bn.js';
Expand Down Expand Up @@ -254,7 +255,8 @@ export class BlocksService extends AbstractService {
extrinsics[idx].events,
block.extrinsics[idx].toHex(),
hash,
getFeeByEvent
getFeeByEvent,
extrinsics[idx].tip
);

if (error) {
Expand Down Expand Up @@ -482,7 +484,8 @@ export class BlocksService extends AbstractService {
events: ISanitizedEvent[],
extrinsicHex: string,
hash: BlockHash,
getFeeByEvent: boolean
getFeeByEvent: boolean,
tip: ICompact<INumber> | null
) {
const { api } = this;
const { class: dispatchClass, partialFee } =
Expand All @@ -494,7 +497,7 @@ export class BlocksService extends AbstractService {
let fee: Balance | string = partialFee;
let error: string | undefined;
if (getFeeByEvent) {
const feeInfo = this.getPartialFeeByEvents(events, partialFee);
const feeInfo = this.getPartialFeeByEvents(events, partialFee, tip);
fee = feeInfo.partialFee;
error = feeInfo.error;
}
Expand All @@ -521,17 +524,20 @@ export class BlocksService extends AbstractService {
*/
private getPartialFeeByEvents(
events: ISanitizedEvent[],
partialFee: Balance
partialFee: Balance,
tip: ICompact<INumber> | null
): { partialFee: string; error?: string } {
// Check Event:Withdraw event for the balances pallet
const withdrawEvent = this.findEvent(events, 'balances', Event.withdraw);
if (withdrawEvent.length > 0 && withdrawEvent[0].data) {
const dataArr = withdrawEvent[0].data.toJSON();
if (Array.isArray(dataArr)) {
const fee = (dataArr as Array<number>)[dataArr.length - 1];

const adjustedPartialFee = tip
? tip.toBn().add(partialFee)
: partialFee;
// The difference between values is 00.00001% or less so they are alike.
if (this.areFeesSimilar(new BN(fee), partialFee)) {
if (this.areFeesSimilar(new BN(fee), adjustedPartialFee)) {
return {
partialFee: fee.toString(),
};
Expand All @@ -545,9 +551,11 @@ export class BlocksService extends AbstractService {
const dataArr = treasuryEvent[0].data.toJSON();
if (Array.isArray(dataArr)) {
const fee = (dataArr as Array<number>)[0];

const adjustedPartialFee = tip
? tip.toBn().add(partialFee)
: partialFee;
// The difference between values is 00.00001% or less so they are alike.
if (this.areFeesSimilar(new BN(fee), partialFee)) {
if (this.areFeesSimilar(new BN(fee), adjustedPartialFee)) {
return {
partialFee: fee.toString(),
};
Expand All @@ -563,9 +571,9 @@ export class BlocksService extends AbstractService {
({ data }) =>
(sumOfFees = sumOfFees.add(new BN(data[data.length - 1].toString())))
);

const adjustedPartialFee = tip ? tip.toBn().add(partialFee) : partialFee;
// The difference between values is 00.00001% or less so they are alike.
if (this.areFeesSimilar(sumOfFees, partialFee)) {
if (this.areFeesSimilar(sumOfFees, adjustedPartialFee)) {
return {
partialFee: sumOfFees.toString(),
};
Expand Down
4 changes: 4 additions & 0 deletions src/services/test-helpers/mock/data/mockEventData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const withdrawEvent = [
constructEvent('balances', 'Withdraw', ['0x', '2490128143']),
];

export const withdrawEventForTip = [
constructEvent('balances', 'Withdraw', ['0x', '1681144907847007']),
];

export const treasuryEvent = [
// Set the fee inside of the data for withdraw 1 decimal larger than expected.
constructEvent('balances', 'Withdraw', ['0x', '24901281430']),
Expand Down

0 comments on commit 91dda83

Please sign in to comment.