Skip to content

Commit 758dd2f

Browse files
committed
support web3.js BigNumber input
1 parent b28833c commit 758dd2f

34 files changed

+248
-141
lines changed

packages/target-web3-v1-test/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"web3": "^1.6.0",
2222
"web3-eth-contract": "^1.6.0",
2323
"web3-core": "^1",
24-
"@types/bn.js": "^4.11.6"
24+
"@types/bn.js": "^4.11.6",
25+
"bignumber.js": "^9"
2526
}
2627
}

packages/target-web3-v1-test/test/DataTypesInput.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import BigNumber from 'bignumber.js'
12
import BN from 'bn.js'
23
import { q18, typedAssert } from 'test-utils'
34

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

10-
const bn = (s: string) => new BN(s)
11-
1211
it('works', async () => {
1312
const { contract, web3 } = chain
1413

1514
typedAssert(await contract.methods.input_uint8('42').call(), '42')
1615
typedAssert(await contract.methods.input_uint8(42).call(), '42')
17-
typedAssert(await contract.methods.input_uint8(bn('42')).call(), '42')
16+
typedAssert(await contract.methods.input_uint8(new BN('42')).call(), '42')
17+
typedAssert(await contract.methods.input_uint8(BigNumber('42')).call(), '42')
1818

1919
typedAssert(await contract.methods.input_uint256(q18(1)).call(), q18(1))
2020
typedAssert(await contract.methods.input_uint256(1).call(), '1')
21-
typedAssert(await contract.methods.input_uint256(bn(q18(1))).call(), q18(1))
21+
typedAssert(await contract.methods.input_uint256(new BN(q18(1))).call(), q18(1))
22+
typedAssert(await contract.methods.input_uint256(BigNumber(q18(1))).call(), q18(1))
2223

2324
typedAssert(await contract.methods.input_int8('42').call(), '42')
2425
typedAssert(await contract.methods.input_int8(42).call(), '42')
25-
typedAssert(await contract.methods.input_int8(bn('42')).call(), '42')
26+
typedAssert(await contract.methods.input_int8(new BN('42')).call(), '42')
27+
typedAssert(await contract.methods.input_int8(BigNumber('42')).call(), '42')
2628

2729
typedAssert(await contract.methods.input_int256(q18(1)).call(), q18(1))
2830
typedAssert(await contract.methods.input_int256(1).call(), '1')
29-
typedAssert(await contract.methods.input_int256(bn(q18(1))).call(), q18(1))
31+
typedAssert(await contract.methods.input_int256(new BN(q18(1))).call(), q18(1))
32+
typedAssert(await contract.methods.input_int256(BigNumber(q18(1))).call(), q18(1))
3033

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

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

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

5556
typedAssert(await contract.methods.input_tuple('1', '2').call(), { 0: '1', 1: '2' })
5657
typedAssert(await contract.methods.input_tuple(1, 2).call(), { 0: '1', 1: '2' })
57-
typedAssert(await contract.methods.input_tuple(bn('1'), bn('2')).call(), { 0: '1', 1: '2' })
58+
typedAssert(await contract.methods.input_tuple(new BN('1'), new BN('2')).call(), { 0: '1', 1: '2' })
59+
typedAssert(await contract.methods.input_tuple(BigNumber('1'), BigNumber('2')).call(), { 0: '1', 1: '2' })
5860

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

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

6568
typedAssert(await contract.methods.input_enum('1').call(), '1')
6669
typedAssert(await contract.methods.input_enum(1).call(), '1')
67-
typedAssert(await contract.methods.input_enum(bn('1')).call(), '1')
70+
typedAssert(await contract.methods.input_enum(new BN('1')).call(), '1')
71+
typedAssert(await contract.methods.input_enum(BigNumber('1')).call(), '1')
6872
})
6973
})

packages/target-web3-v1-test/test/Events.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import BigNumber from 'bn.js'
1+
import BigNumber from 'bignumber.js'
2+
import BN from 'bn.js'
23
import { asyncWithDoneCase, typedAssert } from 'test-utils'
34

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

109+
await contract.methods.emit_event1().send({
110+
from: accounts[0],
111+
gas: GAS_LIMIT_STANDARD,
112+
maxFeePerGas: new BN(10 ** 9),
113+
maxPriorityFeePerGas: new BN(10 ** 9),
114+
})
115+
// doesn't throw error
116+
108117
await contract.methods.emit_event1().send({
109118
from: accounts[0],
110119
gas: GAS_LIMIT_STANDARD,

packages/target-web3-v1-test/types/types.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
/* tslint:disable */
33
/* eslint-disable */
44
import type BN from "bn.js";
5+
import type BigNumber from "bignumber.js";
56
import type { EventEmitter } from "events";
67
import type { EventLog, PromiEvent, TransactionReceipt } from "web3-core/types";
78
import type { Contract } from "web3-eth-contract";
89

910
export interface EstimateGasOptions {
1011
from?: string;
1112
gas?: number;
12-
value?: number | string | BN;
13+
value?: number | string | BN | BigNumber;
1314
}
1415

1516
export interface EventOptions {
@@ -32,19 +33,19 @@ export interface ContractEventEmitter<T> extends EventEmitter {
3233
}
3334

3435
export interface NonPayableTx {
35-
nonce?: string | number | BN;
36-
chainId?: string | number | BN;
36+
nonce?: string | number | BN | BigNumber;
37+
chainId?: string | number | BN | BigNumber;
3738
from?: string;
3839
to?: string;
3940
data?: string;
40-
gas?: string | number | BN;
41-
maxPriorityFeePerGas?: string | number | BN;
42-
maxFeePerGas?: string | number | BN;
43-
gasPrice?: string | number | BN;
41+
gas?: string | number | BN | BigNumber;
42+
maxPriorityFeePerGas?: string | number | BN | BigNumber;
43+
maxFeePerGas?: string | number | BN | BigNumber;
44+
gasPrice?: string | number | BN | BigNumber;
4445
}
4546

4647
export interface PayableTx extends NonPayableTx {
47-
value?: string | number | BN;
48+
value?: string | number | BN | BigNumber;
4849
}
4950

5051
export interface NonPayableTransactionObject<T> {
@@ -69,5 +70,6 @@ export type BlockType =
6970
| "genesis"
7071
| "earliest"
7172
| number
72-
| BN;
73+
| BN
74+
| BigNumber;
7375
export type BaseContract = Omit<Contract, "clone" | "once">;

packages/target-web3-v1-test/types/v0.6.4/DataTypesInput.ts

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

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

4243
input_enum(
43-
input1: number | string | BN
44+
input1: number | string | BN | BigNumber
4445
): NonPayableTransactionObject<string>;
4546

4647
input_fixedarray_array_fixedarray(
47-
input1: (number | string | BN)[][][]
48+
input1: (number | string | BN | BigNumber)[][][]
4849
): NonPayableTransactionObject<string[][][]>;
4950

5051
input_int256(
51-
input1: number | string | BN
52+
input1: number | string | BN | BigNumber
5253
): NonPayableTransactionObject<string>;
5354

5455
input_int8(
55-
input1: number | string | BN
56+
input1: number | string | BN | BigNumber
5657
): NonPayableTransactionObject<string>;
5758

5859
input_multiple_structs_with_same_name(
59-
info1: [number | string | BN, number | string | BN]
60+
info1: [
61+
number | string | BN | BigNumber,
62+
number | string | BN | BigNumber
63+
]
6064
): NonPayableTransactionObject<[string, string]>;
6165

6266
input_stat_array(
63-
input1: (number | string | BN)[]
67+
input1: (number | string | BN | BigNumber)[]
6468
): NonPayableTransactionObject<string[]>;
6569

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

6872
input_struct(
69-
input1: [number | string | BN, number | string | BN]
73+
input1: [
74+
number | string | BN | BigNumber,
75+
number | string | BN | BigNumber
76+
]
7077
): NonPayableTransactionObject<[string, string]>;
7178

7279
input_struct2(
7380
input1: [
74-
number | string | BN,
75-
[number | string | BN, number | string | BN]
81+
number | string | BN | BigNumber,
82+
[number | string | BN | BigNumber, number | string | BN | BigNumber]
7683
]
7784
): NonPayableTransactionObject<[string, [string, string]]>;
7885

7986
input_struct2_array(
8087
input1: [
81-
number | string | BN,
82-
[number | string | BN, number | string | BN]
88+
number | string | BN | BigNumber,
89+
[number | string | BN | BigNumber, number | string | BN | BigNumber]
8390
][]
8491
): NonPayableTransactionObject<[string, [string, string]][]>;
8592

8693
input_struct2_tuple(
8794
input: [
88-
number | string | BN,
89-
[number | string | BN, number | string | BN]
95+
number | string | BN | BigNumber,
96+
[number | string | BN | BigNumber, number | string | BN | BigNumber]
9097
][]
9198
): NonPayableTransactionObject<[string, [string, string]][]>;
9299

93100
input_struct3_array(
94-
input1: [(number | string | BN)[]][]
101+
input1: [(number | string | BN | BigNumber)[]][]
95102
): NonPayableTransactionObject<[string[]][]>;
96103

97104
input_struct_array(
98-
input1: [number | string | BN, number | string | BN][]
105+
input1: [
106+
number | string | BN | BigNumber,
107+
number | string | BN | BigNumber
108+
][]
99109
): NonPayableTransactionObject<[string, string][]>;
100110

101111
input_struct_array_array(
102-
input1: [number | string | BN, number | string | BN][][]
112+
input1: [
113+
number | string | BN | BigNumber,
114+
number | string | BN | BigNumber
115+
][][]
103116
): NonPayableTransactionObject<[string, string][][]>;
104117

105118
input_struct_array_array_array(
106-
input1: [number | string | BN, number | string | BN][][][]
119+
input1: [
120+
number | string | BN | BigNumber,
121+
number | string | BN | BigNumber
122+
][][][]
107123
): NonPayableTransactionObject<[string, string][][][]>;
108124

109125
input_struct_array_fixedarray(
110-
input1: [number | string | BN, number | string | BN][][]
126+
input1: [
127+
number | string | BN | BigNumber,
128+
number | string | BN | BigNumber
129+
][][]
111130
): NonPayableTransactionObject<[string, string][][]>;
112131

113132
input_struct_fixedarray_array(
114-
input1: [number | string | BN, number | string | BN][][]
133+
input1: [
134+
number | string | BN | BigNumber,
135+
number | string | BN | BigNumber
136+
][][]
115137
): NonPayableTransactionObject<[string, string][][]>;
116138

117139
input_struct_fixedarray_array_fixedarray(
118-
input1: [number | string | BN, number | string | BN][][][]
140+
input1: [
141+
number | string | BN | BigNumber,
142+
number | string | BN | BigNumber
143+
][][][]
119144
): NonPayableTransactionObject<[string, string][][][]>;
120145

121146
input_struct_fixedarray_array_fixedarray_array_fixedarray(
122-
input1: [number | string | BN, number | string | BN][][][][][]
147+
input1: [
148+
number | string | BN | BigNumber,
149+
number | string | BN | BigNumber
150+
][][][][][]
123151
): NonPayableTransactionObject<[string, string][][][][][]>;
124152

125153
input_struct_fixedarray_fixedarray(
126-
input1: [number | string | BN, number | string | BN][][]
154+
input1: [
155+
number | string | BN | BigNumber,
156+
number | string | BN | BigNumber
157+
][][]
127158
): NonPayableTransactionObject<[string, string][][]>;
128159

129160
input_tuple(
130-
input1: number | string | BN,
131-
input2: number | string | BN
161+
input1: number | string | BN | BigNumber,
162+
input2: number | string | BN | BigNumber
132163
): NonPayableTransactionObject<{
133164
0: string;
134165
1: string;
135166
}>;
136167

137168
input_uint256(
138-
input1: number | string | BN
169+
input1: number | string | BN | BigNumber
139170
): NonPayableTransactionObject<string>;
140171

141172
input_uint8(
142-
input1: number | string | BN
173+
input1: number | string | BN | BigNumber
143174
): NonPayableTransactionObject<string>;
144175

145176
input_uint_array(
146-
input1: (number | string | BN)[]
177+
input1: (number | string | BN | BigNumber)[]
147178
): NonPayableTransactionObject<string[]>;
148179
};
149180
events: {

packages/target-web3-v1-test/types/v0.6.4/DataTypesPure.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

55
import type BN from "bn.js";
6+
import type BigNumber from "bignumber.js";
67
import type { ContractOptions } from "web3-eth-contract";
78
import type { EventLog } from "web3-core";
89
import type { EventEmitter } from "events";

packages/target-web3-v1-test/types/v0.6.4/DataTypesView.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

55
import type BN from "bn.js";
6+
import type BigNumber from "bignumber.js";
67
import type { ContractOptions } from "web3-eth-contract";
78
import type { EventLog } from "web3-core";
89
import type { EventEmitter } from "events";

packages/target-web3-v1-test/types/v0.6.4/Events.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

55
import type BN from "bn.js";
6+
import type BigNumber from "bignumber.js";
67
import type { ContractOptions } from "web3-eth-contract";
78
import type { EventLog } from "web3-core";
89
import type { EventEmitter } from "events";

packages/target-web3-v1-test/types/v0.6.4/Issue428_Reproduction/A.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

55
import type BN from "bn.js";
6+
import type BigNumber from "bignumber.js";
67
import type { ContractOptions } from "web3-eth-contract";
78
import type { EventLog } from "web3-core";
89
import type { EventEmitter } from "events";

packages/target-web3-v1-test/types/v0.6.4/Issue428_Reproduction/B.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* eslint-disable */
44

55
import type BN from "bn.js";
6+
import type BigNumber from "bignumber.js";
67
import type { ContractOptions } from "web3-eth-contract";
78
import type { EventLog } from "web3-core";
89
import type { EventEmitter } from "events";

0 commit comments

Comments
 (0)