Skip to content

Commit

Permalink
fix: don't enforce bigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Jun 20, 2022
1 parent 94b2fff commit efef507
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
11 changes: 9 additions & 2 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ describe('defaultProvider', () => {
test(`getBlock(blockHash=undefined, blockNumber=${exampleBlockNumber})`, () => {
return expect(provider.getBlock(exampleBlockNumber)).resolves.not.toThrow();
});
test('getBlock(blockHash=undefined, blockNumber=null)', () => {
return expect(provider.getBlock()).resolves.not.toThrow();
test('getBlock(blockHash=undefined, blockNumber=null)', async () => {
const block = await provider.getBlock();

expect(block).not.toBeNull();

const { block_number, timestamp } = block;

expect(typeof block_number).toEqual('number');
expect(typeof timestamp).toEqual('number');
});
test('getBlock() -> { blockNumber }', async () => {
const block = await provider.getBlock();
Expand Down
4 changes: 2 additions & 2 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TransactionReceiptResponse,
} from '../types';
import { getSelectorFromName } from '../utils/hash';
import { parse, stringify } from '../utils/json';
import { parse, parseAsBig, stringify } from '../utils/json';
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
import { compressProgram, randomAddress } from '../utils/stark';
import { ProviderInterface } from './interface';
Expand Down Expand Up @@ -167,7 +167,7 @@ export class Provider implements ProviderInterface {
})
.then((res) => {
if (endpoint === 'estimate_fee') {
return parse(res, (_, v) => {
return parseAsBig(res, (_, v) => {
if (v && typeof v === 'bigint') {
return toBN(v.toString());
}
Expand Down
18 changes: 11 additions & 7 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Json from 'json-bigint';

const { parse, stringify } = Json({
alwaysParseAsBig: true,
useNativeBigInt: true,
protoAction: 'preserve',
constructorAction: 'preserve',
});
const json = (alwaysParseAsBig: boolean) => {
return Json({
alwaysParseAsBig,
useNativeBigInt: true,
protoAction: 'preserve',
constructorAction: 'preserve',
});
};

export const { parse, stringify } = json(false);
export const { parse: parseAsBig, stringify: stringifyAsBig } = json(true);

export { parse, stringify };
export default { parse, stringify };

0 comments on commit efef507

Please sign in to comment.