Skip to content

Commit

Permalink
chore: add comment + altered test
Browse files Browse the repository at this point in the history
  • Loading branch information
petertonysmith94 committed Dec 20, 2024
1 parent 2a4d6cf commit a0f983f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/abi-coder/src/encoding/coders/BigNumberCoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ describe('BigNumberCoder', () => {
expect(data).toEqual(expected);
});

it('should throw an error when encoding [number more than max safe integer]', () => {
const coder = new BigNumberCoder('u64');
const value: number = Number.MAX_SAFE_INTEGER + 1;

expect(() => coder.encode(value)).toThrow(
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - number value is too large.')
);
});

it('should encode a u64 [very big number - as string]', () => {
const coder = new BigNumberCoder('u64');
const value: string = (Number.MAX_SAFE_INTEGER + 1).toString();
const expected = new Uint8Array([0, 32, 0, 0, 0, 0, 0, 0]);
const value: string = '76472027892439376';
const expected = new Uint8Array([1, 15, 174, 231, 121, 200, 89, 80]);

const data = coder.encode(value);

Expand All @@ -45,7 +54,7 @@ describe('BigNumberCoder', () => {

it('should throw an error when encoding [number more than max safe integer]', () => {
const coder = new BigNumberCoder('u64');
const value: number = Number.MAX_SAFE_INTEGER + 1;
const value: number = 76472027892439376;

expect(() => coder.encode(value)).toThrow(
new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid u64 type - number value is too large.')
Expand Down
3 changes: 3 additions & 0 deletions packages/abi-coder/src/encoding/coders/BigNumberCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class BigNumberCoder extends Coder<BNInput, BN> {
encode(value: BNInput): Uint8Array {
let bytes;

// We throw an error if the value is a number and it's more than the max safe integer
// This is because we can experience some odd behavior with integers more than the max safe integer
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER#description
if (typeof value === 'number' && value > Number.MAX_SAFE_INTEGER) {
throw new FuelError(
ErrorCode.ENCODE_ERROR,
Expand Down

0 comments on commit a0f983f

Please sign in to comment.