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

[web3.js] support BigNumber.js #784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quiet-rules-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@typechain/web3-v1-test": patch
"@typechain/web3-v1": patch
---

[web3.js] support BigNumber.js
3 changes: 2 additions & 1 deletion packages/target-web3-v1-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"web3": "^1.6.0",
"web3-eth-contract": "^1.6.0",
"web3-core": "^1",
"@types/bn.js": "^4.11.6"
"@types/bn.js": "^4.11.6",
"bignumber.js": "^9"
}
}
30 changes: 17 additions & 13 deletions packages/target-web3-v1-test/test/DataTypesInput.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js'
import BN from 'bn.js'
import { q18, typedAssert } from 'test-utils'

Expand All @@ -7,26 +8,28 @@ import { createNewBlockchain } from './common'
describe('DataTypesInput', () => {
const chain = createNewBlockchain<DataTypesInput>('DataTypesInput')

const bn = (s: string) => new BN(s)

it('works', async () => {
const { contract, web3 } = chain

typedAssert(await contract.methods.input_uint8('42').call(), '42')
typedAssert(await contract.methods.input_uint8(42).call(), '42')
typedAssert(await contract.methods.input_uint8(bn('42')).call(), '42')
typedAssert(await contract.methods.input_uint8(new BN('42')).call(), '42')
typedAssert(await contract.methods.input_uint8(BigNumber('42')).call(), '42')

typedAssert(await contract.methods.input_uint256(q18(1)).call(), q18(1))
typedAssert(await contract.methods.input_uint256(1).call(), '1')
typedAssert(await contract.methods.input_uint256(bn(q18(1))).call(), q18(1))
typedAssert(await contract.methods.input_uint256(new BN(q18(1))).call(), q18(1))
typedAssert(await contract.methods.input_uint256(BigNumber(q18(1))).call(), q18(1))

typedAssert(await contract.methods.input_int8('42').call(), '42')
typedAssert(await contract.methods.input_int8(42).call(), '42')
typedAssert(await contract.methods.input_int8(bn('42')).call(), '42')
typedAssert(await contract.methods.input_int8(new BN('42')).call(), '42')
typedAssert(await contract.methods.input_int8(BigNumber('42')).call(), '42')

typedAssert(await contract.methods.input_int256(q18(1)).call(), q18(1))
typedAssert(await contract.methods.input_int256(1).call(), '1')
typedAssert(await contract.methods.input_int256(bn(q18(1))).call(), q18(1))
typedAssert(await contract.methods.input_int256(new BN(q18(1))).call(), q18(1))
typedAssert(await contract.methods.input_int256(BigNumber(q18(1))).call(), q18(1))

typedAssert(await contract.methods.input_bool(true).call(), true)

Expand All @@ -47,23 +50,24 @@ describe('DataTypesInput', () => {
typedAssert(await contract.methods.input_stat_array([1, 2, 3]).call(), ['1', '2', '3'])

// TODO this fails due to an issue in web3 abi coder handling of inner BN (see https://github.com/ChainSafe/web3.js/issues/3920)
// typedAssert(
// await contract.methods.input_stat_array([bn('1'), bn('2'), bn('3')]).call(),
// ['1', '2', '3'],
// )
// typedAssert(await contract.methods.input_stat_array([new BN('1'), new BN('2'), new BN('3')]).call(),['1', '2', '3'],)
// typedAssert(await contract.methods.input_stat_array([BigNumber('1'), BigNumber('2'), BigNumber('3')]).call(),['1', '2', '3'],)

typedAssert(await contract.methods.input_tuple('1', '2').call(), { 0: '1', 1: '2' })
typedAssert(await contract.methods.input_tuple(1, 2).call(), { 0: '1', 1: '2' })
typedAssert(await contract.methods.input_tuple(bn('1'), bn('2')).call(), { 0: '1', 1: '2' })
typedAssert(await contract.methods.input_tuple(new BN('1'), new BN('2')).call(), { 0: '1', 1: '2' })
typedAssert(await contract.methods.input_tuple(BigNumber('1'), BigNumber('2')).call(), { 0: '1', 1: '2' })

typedAssert(await contract.methods.input_struct(['1', '2']).call(), ['1', '2'])
typedAssert(await contract.methods.input_struct([1, 2]).call(), ['1', '2'])

// TODO this fails due to an issue in web3 abi coder handling of inner BN (see https://github.com/ChainSafe/web3.js/issues/3920)
// typedAssert(await contract.methods.input_struct([bn('1'), bn('2')]).call(), ['1', '2'])
// typedAssert(await contract.methods.input_struct([new BN('1'), new BN('2')]).call(), ['1', '2'])
// typedAssert(await contract.methods.input_struct([BigNumber('1'), BigNumber('2')]).call(), ['1', '2'])

typedAssert(await contract.methods.input_enum('1').call(), '1')
typedAssert(await contract.methods.input_enum(1).call(), '1')
typedAssert(await contract.methods.input_enum(bn('1')).call(), '1')
typedAssert(await contract.methods.input_enum(new BN('1')).call(), '1')
typedAssert(await contract.methods.input_enum(BigNumber('1')).call(), '1')
})
})
11 changes: 10 additions & 1 deletion packages/target-web3-v1-test/test/Events.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BigNumber from 'bn.js'
import BigNumber from 'bignumber.js'
import BN from 'bn.js'
import { asyncWithDoneCase, typedAssert } from 'test-utils'

import type { Events } from '../types/v0.6.4/Events'
Expand Down Expand Up @@ -105,6 +106,14 @@ describe('Events', () => {
it('works', async () => {
const { accounts, contract } = chain

await contract.methods.emit_event1().send({
from: accounts[0],
gas: GAS_LIMIT_STANDARD,
maxFeePerGas: new BN(10 ** 9),
maxPriorityFeePerGas: new BN(10 ** 9),
})
// doesn't throw error

await contract.methods.emit_event1().send({
from: accounts[0],
gas: GAS_LIMIT_STANDARD,
Expand Down
20 changes: 11 additions & 9 deletions packages/target-web3-v1-test/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
/* tslint:disable */
/* eslint-disable */
import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { EventEmitter } from "events";
import type { EventLog, PromiEvent, TransactionReceipt } from "web3-core/types";
import type { Contract } from "web3-eth-contract";

export interface EstimateGasOptions {
from?: string;
gas?: number;
value?: number | string | BN;
value?: number | string | BN | BigNumber;
}

export interface EventOptions {
Expand All @@ -32,19 +33,19 @@ export interface ContractEventEmitter<T> extends EventEmitter {
}

export interface NonPayableTx {
nonce?: string | number | BN;
chainId?: string | number | BN;
nonce?: string | number | BN | BigNumber;
chainId?: string | number | BN | BigNumber;
from?: string;
to?: string;
data?: string;
gas?: string | number | BN;
maxPriorityFeePerGas?: string | number | BN;
maxFeePerGas?: string | number | BN;
gasPrice?: string | number | BN;
gas?: string | number | BN | BigNumber;
maxPriorityFeePerGas?: string | number | BN | BigNumber;
maxFeePerGas?: string | number | BN | BigNumber;
gasPrice?: string | number | BN | BigNumber;
}

export interface PayableTx extends NonPayableTx {
value?: string | number | BN;
value?: string | number | BN | BigNumber;
}

export interface NonPayableTransactionObject<T> {
Expand All @@ -69,5 +70,6 @@ export type BlockType =
| "genesis"
| "earliest"
| number
| BN;
| BN
| BigNumber;
export type BaseContract = Omit<Contract, "clone" | "once">;
85 changes: 58 additions & 27 deletions packages/target-web3-v1-test/types/v0.6.4/DataTypesInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down Expand Up @@ -40,110 +41,140 @@ export interface DataTypesInput extends BaseContract {
): NonPayableTransactionObject<string>;

input_enum(
input1: number | string | BN
input1: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;

input_fixedarray_array_fixedarray(
input1: (number | string | BN)[][][]
input1: (number | string | BN | BigNumber)[][][]
): NonPayableTransactionObject<string[][][]>;

input_int256(
input1: number | string | BN
input1: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;

input_int8(
input1: number | string | BN
input1: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;

input_multiple_structs_with_same_name(
info1: [number | string | BN, number | string | BN]
info1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
]
): NonPayableTransactionObject<[string, string]>;

input_stat_array(
input1: (number | string | BN)[]
input1: (number | string | BN | BigNumber)[]
): NonPayableTransactionObject<string[]>;

input_string(input1: string): NonPayableTransactionObject<string>;

input_struct(
input1: [number | string | BN, number | string | BN]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
]
): NonPayableTransactionObject<[string, string]>;

input_struct2(
input1: [
number | string | BN,
[number | string | BN, number | string | BN]
number | string | BN | BigNumber,
[number | string | BN | BigNumber, number | string | BN | BigNumber]
]
): NonPayableTransactionObject<[string, [string, string]]>;

input_struct2_array(
input1: [
number | string | BN,
[number | string | BN, number | string | BN]
number | string | BN | BigNumber,
[number | string | BN | BigNumber, number | string | BN | BigNumber]
][]
): NonPayableTransactionObject<[string, [string, string]][]>;

input_struct2_tuple(
input: [
number | string | BN,
[number | string | BN, number | string | BN]
number | string | BN | BigNumber,
[number | string | BN | BigNumber, number | string | BN | BigNumber]
][]
): NonPayableTransactionObject<[string, [string, string]][]>;

input_struct3_array(
input1: [(number | string | BN)[]][]
input1: [(number | string | BN | BigNumber)[]][]
): NonPayableTransactionObject<[string[]][]>;

input_struct_array(
input1: [number | string | BN, number | string | BN][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][]
): NonPayableTransactionObject<[string, string][]>;

input_struct_array_array(
input1: [number | string | BN, number | string | BN][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][]
): NonPayableTransactionObject<[string, string][][]>;

input_struct_array_array_array(
input1: [number | string | BN, number | string | BN][][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][][]
): NonPayableTransactionObject<[string, string][][][]>;

input_struct_array_fixedarray(
input1: [number | string | BN, number | string | BN][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][]
): NonPayableTransactionObject<[string, string][][]>;

input_struct_fixedarray_array(
input1: [number | string | BN, number | string | BN][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][]
): NonPayableTransactionObject<[string, string][][]>;

input_struct_fixedarray_array_fixedarray(
input1: [number | string | BN, number | string | BN][][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][][]
): NonPayableTransactionObject<[string, string][][][]>;

input_struct_fixedarray_array_fixedarray_array_fixedarray(
input1: [number | string | BN, number | string | BN][][][][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][][][][]
): NonPayableTransactionObject<[string, string][][][][][]>;

input_struct_fixedarray_fixedarray(
input1: [number | string | BN, number | string | BN][][]
input1: [
number | string | BN | BigNumber,
number | string | BN | BigNumber
][][]
): NonPayableTransactionObject<[string, string][][]>;

input_tuple(
input1: number | string | BN,
input2: number | string | BN
input1: number | string | BN | BigNumber,
input2: number | string | BN | BigNumber
): NonPayableTransactionObject<{
0: string;
1: string;
}>;

input_uint256(
input1: number | string | BN
input1: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;

input_uint8(
input1: number | string | BN
input1: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;

input_uint_array(
input1: (number | string | BN)[]
input1: (number | string | BN | BigNumber)[]
): NonPayableTransactionObject<string[]>;
};
events: {
Expand Down
1 change: 1 addition & 0 deletions packages/target-web3-v1-test/types/v0.6.4/DataTypesPure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down
1 change: 1 addition & 0 deletions packages/target-web3-v1-test/types/v0.6.4/DataTypesView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down
1 change: 1 addition & 0 deletions packages/target-web3-v1-test/types/v0.6.4/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand Down
5 changes: 4 additions & 1 deletion packages/target-web3-v1-test/types/v0.6.4/Library/Lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable */

import type BN from "bn.js";
import type BigNumber from "bignumber.js";
import type { ContractOptions } from "web3-eth-contract";
import type { EventLog } from "web3-core";
import type { EventEmitter } from "events";
Expand All @@ -29,7 +30,9 @@ export interface Lib extends BaseContract {
): Lib;
clone(): Lib;
methods: {
other(b: number | string | BN): NonPayableTransactionObject<string>;
other(
b: number | string | BN | BigNumber
): NonPayableTransactionObject<string>;
};
events: {
allEvents(options?: EventOptions, cb?: Callback<EventLog>): EventEmitter;
Expand Down