Skip to content

Commit 81fd628

Browse files
committed
Added BigNumber.toBigInt method (ethers-io#1415).
1 parent f9dd099 commit 81fd628

File tree

7 files changed

+44
-34
lines changed

7 files changed

+44
-34
lines changed

packages/abi/src.ts/abi-coder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export class AbiCoder {
9191
return new Writer(this._getWordSize());
9292
}
9393

94-
getDefaultValue(types: Array<string | ParamType>): Result {
94+
getDefaultValue(types: ReadonlyArray<string | ParamType>): Result {
9595
const coders: Array<Coder> = types.map((type) => this._getCoder(ParamType.from(type)));
9696
const coder = new TupleCoder(coders, "_");
9797
return coder.defaultValue();
9898
}
9999

100-
encode(types: Array<string | ParamType>, values: Array<any>): string {
100+
encode(types: ReadonlyArray<string | ParamType>, values: ReadonlyArray<any>): string {
101101
if (types.length !== values.length) {
102102
logger.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, {
103103
count: { types: types.length, values: values.length },
@@ -113,7 +113,7 @@ export class AbiCoder {
113113
return writer.data;
114114
}
115115

116-
decode(types: Array<string | ParamType>, data: BytesLike, loose?: boolean): Result {
116+
decode(types: ReadonlyArray<string | ParamType>, data: BytesLike, loose?: boolean): Result {
117117
const coders: Array<Coder> = types.map((type) => this._getCoder(ParamType.from(type)));
118118
const coder = new TupleCoder(coders, "_");
119119
return coder.decode(this._getReader(arrayify(data), loose));

packages/abi/src.ts/fragments.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ import { version } from "./_version";
88
const logger = new Logger(version);
99

1010
export interface JsonFragmentType {
11-
name?: string;
12-
indexed?: boolean;
13-
type?: string;
14-
components?: Array<JsonFragmentType>;
11+
readonly name?: string;
12+
readonly indexed?: boolean;
13+
readonly type?: string;
14+
readonly components?: ReadonlyArray<JsonFragmentType>;
1515
}
1616

1717
export interface JsonFragment {
18-
name?: string;
19-
type?: string;
18+
readonly name?: string;
19+
readonly type?: string;
2020

21-
anonymous?: boolean;
21+
readonly anonymous?: boolean;
2222

23-
payable?: boolean;
24-
constant?: boolean;
25-
stateMutability?: string;
23+
readonly payable?: boolean;
24+
readonly constant?: boolean;
25+
readonly stateMutability?: string;
2626

27-
inputs?: Array<JsonFragmentType>;
28-
outputs?: Array<JsonFragmentType>;
27+
readonly inputs?: ReadonlyArray<JsonFragmentType>;
28+
readonly outputs?: ReadonlyArray<JsonFragmentType>;
2929

30-
gas?: string;
30+
readonly gas?: string;
3131
};
3232

3333

@@ -399,7 +399,7 @@ type TypeCheck<T> = { -readonly [ K in keyof T ]: T[K] };
399399
interface _Fragment {
400400
readonly type: string;
401401
readonly name: string;
402-
readonly inputs: Array<ParamType>;
402+
readonly inputs: ReadonlyArray<ParamType>;
403403
}
404404

405405
export abstract class Fragment {

packages/abi/src.ts/interface.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function checkNames(fragment: Fragment, type: "input" | "output", params: Array<
6363
}
6464
*/
6565
export class Interface {
66-
readonly fragments: Array<Fragment>;
66+
readonly fragments: ReadonlyArray<Fragment>;
6767

6868
readonly errors: { [ name: string ]: any };
6969
readonly events: { [ name: string ]: EventFragment };
@@ -76,10 +76,10 @@ export class Interface {
7676

7777
readonly _isInterface: boolean;
7878

79-
constructor(fragments: string | Array<Fragment | JsonFragment | string>) {
79+
constructor(fragments: string | ReadonlyArray<Fragment | JsonFragment | string>) {
8080
logger.checkNew(new.target, Interface);
8181

82-
let abi: Array<Fragment | JsonFragment | string> = [ ];
82+
let abi: ReadonlyArray<Fragment | JsonFragment | string> = [ ];
8383
if (typeof(fragments) === "string") {
8484
abi = JSON.parse(fragments);
8585
} else {
@@ -259,15 +259,15 @@ export class Interface {
259259
}
260260

261261

262-
_decodeParams(params: Array<ParamType>, data: BytesLike): Result {
262+
_decodeParams(params: ReadonlyArray<ParamType>, data: BytesLike): Result {
263263
return this._abiCoder.decode(params, data)
264264
}
265265

266-
_encodeParams(params: Array<ParamType>, values: Array<any>): string {
266+
_encodeParams(params: ReadonlyArray<ParamType>, values: ReadonlyArray<any>): string {
267267
return this._abiCoder.encode(params, values)
268268
}
269269

270-
encodeDeploy(values?: Array<any>): string {
270+
encodeDeploy(values?: ReadonlyArray<any>): string {
271271
return this._encodeParams(this.deploy.inputs, values || [ ]);
272272
}
273273

@@ -287,7 +287,7 @@ export class Interface {
287287
}
288288

289289
// Encode the data for a function call (e.g. tx.data)
290-
encodeFunctionData(functionFragment: FunctionFragment | string, values?: Array<any>): string {
290+
encodeFunctionData(functionFragment: FunctionFragment | string, values?: ReadonlyArray<any>): string {
291291
if (typeof(functionFragment) === "string") {
292292
functionFragment = this.getFunction(functionFragment);
293293
}
@@ -332,7 +332,7 @@ export class Interface {
332332
}
333333

334334
// Encode the result for a function call (e.g. for eth_call)
335-
encodeFunctionResult(functionFragment: FunctionFragment | string, values?: Array<any>): string {
335+
encodeFunctionResult(functionFragment: FunctionFragment | string, values?: ReadonlyArray<any>): string {
336336
if (typeof(functionFragment) === "string") {
337337
functionFragment = this.getFunction(functionFragment);
338338
}
@@ -341,7 +341,7 @@ export class Interface {
341341
}
342342

343343
// Create the filter for the event with search criteria (e.g. for eth_filterLog)
344-
encodeFilterTopics(eventFragment: EventFragment, values: Array<any>): Array<string | Array<string>> {
344+
encodeFilterTopics(eventFragment: EventFragment, values: ReadonlyArray<any>): Array<string | Array<string>> {
345345
if (typeof(eventFragment) === "string") {
346346
eventFragment = this.getEvent(eventFragment);
347347
}
@@ -398,7 +398,7 @@ export class Interface {
398398
return topics;
399399
}
400400

401-
encodeEventLog(eventFragment: EventFragment, values: Array<any>): { data: string, topics: Array<string> } {
401+
encodeEventLog(eventFragment: EventFragment, values: ReadonlyArray<any>): { data: string, topics: Array<string> } {
402402
if (typeof(eventFragment) === "string") {
403403
eventFragment = this.getEvent(eventFragment);
404404
}
@@ -442,7 +442,7 @@ export class Interface {
442442
}
443443

444444
// Decode a filter for the event and the search criteria
445-
decodeEventLog(eventFragment: EventFragment | string, data: BytesLike, topics?: Array<string>): Result {
445+
decodeEventLog(eventFragment: EventFragment | string, data: BytesLike, topics?: ReadonlyArray<string>): Result {
446446
if (typeof(eventFragment) === "string") {
447447
eventFragment = this.getEvent(eventFragment);
448448
}

packages/bignumber/src.ts/bignumber.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ export class BigNumber implements Hexable {
190190
return null;
191191
}
192192

193+
toBigInt(): BigInt {
194+
try {
195+
return BigInt(this.toString());
196+
} catch (e) { }
197+
198+
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
199+
value: this.toString()
200+
});
201+
}
202+
193203
toString(): string {
194204
// Lots of people expect this, which we do not support, so check (See: #889)
195205
if (arguments.length > 0) {

packages/bytes/src.ts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function arrayify(value: BytesLike | Hexable | number, options?: DataOpti
136136
return logger.throwArgumentError("invalid arrayify value", "value", value);
137137
}
138138

139-
export function concat(items: Array<BytesLike>): Uint8Array {
139+
export function concat(items: ReadonlyArray<BytesLike>): Uint8Array {
140140
const objects = items.map(item => arrayify(item));
141141
const length = objects.reduce((accum, item) => (accum + item.length), 0);
142142

@@ -275,7 +275,7 @@ export function hexDataSlice(data: BytesLike, offset: number, endOffset?: number
275275
return "0x" + data.substring(offset);
276276
}
277277

278-
export function hexConcat(items: Array<BytesLike>): string {
278+
export function hexConcat(items: ReadonlyArray<BytesLike>): string {
279279
let result = "0x";
280280
items.forEach((item) => {
281281
result += hexlify(item).substring(2);

packages/contracts/src.ts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ class WildcardRunningEvent extends RunningEvent {
578578
}
579579
}
580580

581-
export type ContractInterface = string | Array<Fragment | JsonFragment | string> | Interface;
581+
export type ContractInterface = string | ReadonlyArray<Fragment | JsonFragment | string> | Interface;
582582

583583
type InterfaceFunc = (contractInterface: ContractInterface) => Interface;
584584

packages/solidity/src.ts/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
7272

7373
// @TODO: Array Enum
7474

75-
export function pack(types: Array<string>, values: Array<any>) {
75+
export function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
7676
if (types.length != values.length) { throw new Error("type/value count mismatch"); }
7777
const tight: Array<Uint8Array> = [];
7878
types.forEach(function(type, index) {
@@ -81,10 +81,10 @@ export function pack(types: Array<string>, values: Array<any>) {
8181
return hexlify(concat(tight));
8282
}
8383

84-
export function keccak256(types: Array<string>, values: Array<any>) {
84+
export function keccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
8585
return hashKeccak256(pack(types, values));
8686
}
8787

88-
export function sha256(types: Array<string>, values: Array<any>) {
88+
export function sha256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
8989
return hashSha256(pack(types, values));
9090
}

0 commit comments

Comments
 (0)