Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(feeByEvent): sanitize fee for hex values #990

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/services/blocks/BlocksService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} 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 { hexToNumber, isHex, u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import BN from 'bn.js';
import { BadRequest, InternalServerError } from 'http-errors';
Expand Down Expand Up @@ -532,7 +532,9 @@ export class BlocksService extends AbstractService {
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 fee = this.sanitizeFee(
(dataArr as Array<string>)[dataArr.length - 1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the shape change here or do we want to have it seen as string for the sanitizeFee/isHex call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the shape was inaccurately type casted for fees (technically number or string work here for BN.js but I changed it to its proper type to be more accurate). The type returned from .toJSON() should be a string, because any u128/u64 value interpreted by polkadot-js will be returned as a string.

);
const adjustedPartialFee = tip
? tip.toBn().add(partialFee)
: partialFee;
Expand All @@ -550,7 +552,7 @@ export class BlocksService extends AbstractService {
if (treasuryEvent.length > 0 && treasuryEvent[0].data) {
const dataArr = treasuryEvent[0].data.toJSON();
if (Array.isArray(dataArr)) {
const fee = (dataArr as Array<number>)[0];
const fee = this.sanitizeFee((dataArr as Array<string>)[0]);
const adjustedPartialFee = tip
? tip.toBn().add(partialFee)
: partialFee;
Expand All @@ -569,7 +571,9 @@ export class BlocksService extends AbstractService {
let sumOfFees = new BN(0);
depositEvents.forEach(
({ data }) =>
(sumOfFees = sumOfFees.add(new BN(data[data.length - 1].toString())))
(sumOfFees = sumOfFees.add(
new BN(this.sanitizeFee(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.
Expand Down Expand Up @@ -607,6 +611,20 @@ export class BlocksService extends AbstractService {
);
}

/**
* Sanitize a given fee
*
* @param fee
*/
private sanitizeFee(fee: string): string {
if (isHex(fee)) {
const numFee = hexToNumber(fee);
return numFee.toString(10);
}

return fee;
}

/**
* Checks to see if the value in an event is within 00.00001% accuracy of
* the queried `partialFee` from `rpc::payment::queryInfo`.
Expand Down