Skip to content

Commit

Permalink
fix: estimatedFeeToMaxFee with integers only
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Apr 8, 2022
1 parent 50a7951 commit 71d19a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
10 changes: 10 additions & 0 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ describe('computeHashOnElements()', () => {
);
});
});
describe('estimatedFeeToMaxFee()', () => {
test('should return maxFee for 0', () => {
const res = stark.estimatedFeeToMaxFee(0, 0.15).toNumber();
expect(res).toBe(0);
});
test('should return maxFee for 10_000', () => {
const res = stark.estimatedFeeToMaxFee(10_000, 0.15).toNumber();
expect(res).toBe(11_500);
});
});
26 changes: 16 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ export function compileCalldata(args: RawArgs): Calldata {
}

export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.15): BN {
return toBN(estimatedFee).mul(toBN(overhead).add(toBN(1)));
// BN can only handle Integers, so we need to do all calulations with integers
const overHeadPercent = Math.round((1 + overhead) * 100);
return toBN(estimatedFee).mul(toBN(overHeadPercent)).div(toBN(100));
}

0 comments on commit 71d19a7

Please sign in to comment.