Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 90d1eb8

Browse files
committed
add default return format
1 parent c4e039a commit 90d1eb8

File tree

14 files changed

+390
-158
lines changed

14 files changed

+390
-158
lines changed

packages/web3-core/src/web3_config.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import { Numbers, HexString, BlockNumberOrTag, Common } from 'web3-types';
18+
import {
19+
Numbers,
20+
HexString,
21+
BlockNumberOrTag,
22+
Common,
23+
DEFAULT_RETURN_FORMAT,
24+
DataFormat,
25+
} from 'web3-types';
1926
import { ConfigHardforkMismatchError, ConfigChainMismatchError } from 'web3-errors';
2027
import { isNullish, toHex } from 'web3-utils';
2128
import { TransactionTypeParser } from './types.js';
@@ -52,6 +59,7 @@ export interface Web3ConfigOptions {
5259
};
5360
transactionBuilder?: TransactionBuilder;
5461
transactionTypeParser?: TransactionTypeParser;
62+
defaultReturnFormat?: DataFormat;
5563
}
5664

5765
type ConfigEvent<T, P extends keyof T = keyof T> = P extends unknown
@@ -93,6 +101,7 @@ export abstract class Web3Config
93101
},
94102
transactionBuilder: undefined,
95103
transactionTypeParser: undefined,
104+
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
96105
};
97106

98107
public constructor(options?: Partial<Web3ConfigOptions>) {
@@ -348,6 +357,15 @@ export abstract class Web3Config
348357
this.config.maxListenersWarningThreshold = val;
349358
}
350359

360+
public get defaultReturnFormat() {
361+
return this.config.defaultReturnFormat;
362+
}
363+
public set defaultReturnFormat(val) {
364+
this._triggerConfigChange('defaultReturnFormat', val);
365+
366+
this.config.defaultReturnFormat = val;
367+
}
368+
351369
public get defaultNetworkId() {
352370
return this.config.defaultNetworkId;
353371
}

packages/web3-core/test/unit/__snapshots__/web3_context.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ exports[`Web3Context getContextObject should return correct context object 1`] =
1313
"defaultHardfork": "london",
1414
"defaultMaxPriorityFeePerGas": "0x9502f900",
1515
"defaultNetworkId": undefined,
16+
"defaultReturnFormat": {
17+
"bytes": "BYTES_HEX",
18+
"number": "NUMBER_BIGINT",
19+
},
1620
"defaultTransactionType": "0x2",
1721
"enableExperimentalFeatures": {
1822
"useRpcCallSpecification": false,

packages/web3-core/test/unit/web3_config.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
import { toHex } from 'web3-utils';
19+
import { DEFAULT_RETURN_FORMAT } from 'web3-types';
1920
import { Web3Config, Web3ConfigEvent } from '../../src/web3_config';
2021

2122
class MyConfigObject extends Web3Config {}
@@ -44,6 +45,7 @@ const defaultConfig = {
4445
transactionConfirmationPollingInterval: undefined,
4546
defaultTransactionType: '0x2',
4647
defaultMaxPriorityFeePerGas: toHex(2500000000),
48+
defaultReturnFormat: DEFAULT_RETURN_FORMAT,
4749
};
4850
const setValue = {
4951
string: 'newValue',

packages/web3-eth-accounts/test/config/setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ require('jest-extended');
2323

2424
process.env.NODE_ENV = 'test';
2525

26-
const jestTimeout = 10000;
26+
const jestTimeout = 15000;
2727

2828
jest.setTimeout(jestTimeout);

packages/web3-eth-contract/src/contract.ts

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ export type ContractMethodsInterface<Abi extends ContractAbi> = {
157157
} & { [key: string]: ContractBoundMethod<any> };
158158

159159
export type ContractMethodSend = Web3PromiEvent<
160-
FormatType<TransactionReceipt, typeof DEFAULT_RETURN_FORMAT>,
161-
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
160+
FormatType<TransactionReceipt, DataFormat>,
161+
SendTransactionEvents<DataFormat>
162162
>;
163163
export type ContractDeploySend<Abi extends ContractAbi> = Web3PromiEvent<
164164
// eslint-disable-next-line no-use-before-define
165165
Contract<Abi>,
166-
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
166+
SendTransactionEvents<DataFormat>
167167
>;
168168

169169
/**
@@ -526,7 +526,7 @@ export class Contract<Abi extends ContractAbi>
526526
? contextOrReturnFormat
527527
: isDataFormat(optionsOrContextOrReturnFormat)
528528
? optionsOrContextOrReturnFormat
529-
: returnFormat ?? DEFAULT_RETURN_FORMAT;
529+
: returnFormat ?? this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT;
530530
const address =
531531
typeof addressOrOptionsOrContext === 'string' ? addressOrOptionsOrContext : undefined;
532532
this.config.contractDataInputFill =
@@ -799,7 +799,8 @@ export class Contract<Abi extends ContractAbi>
799799
},
800800
estimateGas: async <ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
801801
options?: PayableCallOptions,
802-
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
802+
returnFormat: ReturnFormat = (this.defaultReturnFormat ??
803+
DEFAULT_RETURN_FORMAT) as ReturnFormat,
803804
) => {
804805
const modifiedOptions = { ...options };
805806
return this._contractMethodEstimateGas({
@@ -814,7 +815,11 @@ export class Contract<Abi extends ContractAbi>
814815
encodeMethodABI(
815816
abi as AbiFunctionFragment,
816817
args as unknown[],
817-
format({ format: 'bytes' }, deployData as Bytes, DEFAULT_RETURN_FORMAT),
818+
format(
819+
{ format: 'bytes' },
820+
deployData as Bytes,
821+
this.defaultReturnFormat as typeof DEFAULT_RETURN_FORMAT,
822+
),
818823
),
819824
};
820825
}
@@ -898,7 +903,7 @@ export class Contract<Abi extends ContractAbi>
898903
? param1
899904
: isDataFormat(param2)
900905
? param2
901-
: param3 ?? DEFAULT_RETURN_FORMAT;
906+
: param3 ?? (this.defaultReturnFormat as DataFormat);
902907

903908
const abi =
904909
eventName === 'allEvents' || eventName === ALL_EVENTS
@@ -959,15 +964,18 @@ export class Contract<Abi extends ContractAbi>
959964
return decodedLogs;
960965
}
961966

962-
private _parseAndSetAddress(value?: Address, returnFormat: DataFormat = DEFAULT_RETURN_FORMAT) {
967+
private _parseAndSetAddress(
968+
value?: Address,
969+
returnFormat: DataFormat = this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
970+
) {
963971
this._address = value
964972
? toChecksumAddress(format({ format: 'address' }, value, returnFormat))
965973
: value;
966974
}
967975

968976
private _parseAndSetJsonInterface(
969977
abis: ContractAbi,
970-
returnFormat: DataFormat = DEFAULT_RETURN_FORMAT,
978+
returnFormat: DataFormat = this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
971979
) {
972980
this._functions = {};
973981
this._methods = {} as ContractMethodsInterface<Abi>;
@@ -1119,7 +1127,8 @@ export class Contract<Abi extends ContractAbi>
11191127

11201128
estimateGas: async <ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
11211129
options?: PayableCallOptions | NonPayableCallOptions,
1122-
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
1130+
returnFormat: ReturnFormat = (this.defaultReturnFormat ??
1131+
DEFAULT_RETURN_FORMAT) as unknown as ReturnFormat,
11231132
) =>
11241133
this._contractMethodEstimateGas({
11251134
abi: methodAbi,
@@ -1176,7 +1185,12 @@ export class Contract<Abi extends ContractAbi>
11761185
},
11771186
});
11781187
try {
1179-
const result = await call(this, tx, block, DEFAULT_RETURN_FORMAT);
1188+
const result = await call(
1189+
this,
1190+
tx,
1191+
block,
1192+
(this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT) as typeof DEFAULT_RETURN_FORMAT,
1193+
);
11801194
return decodeMethodReturn(abi, result);
11811195
} catch (error: unknown) {
11821196
if (error instanceof ContractExecutionError) {
@@ -1207,7 +1221,12 @@ export class Contract<Abi extends ContractAbi>
12071221
});
12081222

12091223
try {
1210-
return createAccessList(this, tx, block, DEFAULT_RETURN_FORMAT);
1224+
return createAccessList(
1225+
this,
1226+
tx,
1227+
block,
1228+
this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
1229+
);
12111230
} catch (error: unknown) {
12121231
if (error instanceof ContractExecutionError) {
12131232
// this will parse the error data by trying to decode the ABI error inputs according to EIP-838
@@ -1237,11 +1256,16 @@ export class Contract<Abi extends ContractAbi>
12371256
contractOptions: modifiedContractOptions,
12381257
});
12391258

1240-
const transactionToSend = sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, {
1241-
// TODO Should make this configurable by the user
1242-
checkRevertBeforeSending: false,
1243-
contractAbi: this._jsonInterface,
1244-
});
1259+
const transactionToSend = sendTransaction(
1260+
this,
1261+
tx,
1262+
this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
1263+
{
1264+
// TODO Should make this configurable by the user
1265+
checkRevertBeforeSending: false,
1266+
contractAbi: this._jsonInterface,
1267+
},
1268+
);
12451269

12461270
// eslint-disable-next-line no-void
12471271
void transactionToSend.on('error', (error: unknown) => {
@@ -1267,10 +1291,10 @@ export class Contract<Abi extends ContractAbi>
12671291
const tx = getSendTxParams({
12681292
abi,
12691293
params,
1270-
options: { ...options, dataInputFill: this.config.contractDataInputFill },
1294+
options: { ...options, dataInputFill: this.contractDataInputFill },
12711295
contractOptions: modifiedContractOptions,
12721296
});
1273-
return sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, {
1297+
return sendTransaction(this, tx, this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT, {
12741298
transactionResolver: receipt => {
12751299
if (receipt.status === BigInt(0)) {
12761300
throw new Web3ContractError("code couldn't be stored", receipt);
@@ -1310,13 +1334,18 @@ export class Contract<Abi extends ContractAbi>
13101334
options: { ...options, dataInputFill: this.config.contractDataInputFill },
13111335
contractOptions: contractOptions ?? this.options,
13121336
});
1313-
return estimateGas(this, tx, BlockTags.LATEST, returnFormat);
1337+
return estimateGas(
1338+
this,
1339+
tx,
1340+
BlockTags.LATEST,
1341+
returnFormat ?? this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
1342+
);
13141343
}
13151344

13161345
// eslint-disable-next-line class-methods-use-this
13171346
private _createContractEvent(
13181347
abi: AbiEventFragment & { signature: HexString },
1319-
returnFormat: DataFormat = DEFAULT_RETURN_FORMAT,
1348+
returnFormat: DataFormat = this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT,
13201349
): ContractBoundEvent {
13211350
return (...params: unknown[]) => {
13221351
const { topics, fromBlock } = encodeEventABI(

packages/web3-eth-ens/src/ens.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
import { Web3Context, Web3ContextObject } from 'web3-core';
19-
import { ENSNetworkNotSyncedError, ENSUnsupportedNetworkError, RevertInstructionError } from 'web3-errors';
19+
import {
20+
ENSNetworkNotSyncedError,
21+
ENSUnsupportedNetworkError,
22+
RevertInstructionError,
23+
} from 'web3-errors';
2024
import { isSyncing } from 'web3-eth';
2125
import { Contract } from 'web3-eth-contract';
2226
import { getId } from 'web3-net';
@@ -37,25 +41,25 @@ import { Resolver } from './resolver.js';
3741

3842
/**
3943
* This class is designed to interact with the ENS system on the Ethereum blockchain.
40-
* For using ENS package, first install Web3 package using: `npm i web3` or `yarn add web3` based on your package manager, after that ENS features can be used as mentioned in following snippet.
41-
* ```ts
42-
*
43-
* import { Web3 } from 'web3';
44-
*
45-
* const web3 = new Web3('https://127.0.0.1:4545');
46-
*
47-
* console.log(await web3.eth.ens.getAddress('ethereum.eth'))
48-
* ```
49-
* For using individual package install `web3-eth-ens` packages using: `npm i web3-eth-ens` or `yarn add web3-eth-ens`. This is more efficient approach for building lightweight applications.
50-
*
51-
* ```ts
52-
*import { ENS } from 'web3-eth-ens';
53-
*
54-
* const ens = new ENS(undefined,'https://127.0.0.1:4545');
55-
*
56-
* console.log(await ens.getAddress('vitalik.eth'));
57-
* ```
58-
*/
44+
* For using ENS package, first install Web3 package using: `npm i web3` or `yarn add web3` based on your package manager, after that ENS features can be used as mentioned in following snippet.
45+
* ```ts
46+
*
47+
* import { Web3 } from 'web3';
48+
*
49+
* const web3 = new Web3('https://127.0.0.1:4545');
50+
*
51+
* console.log(await web3.eth.ens.getAddress('ethereum.eth'))
52+
* ```
53+
* For using individual package install `web3-eth-ens` packages using: `npm i web3-eth-ens` or `yarn add web3-eth-ens`. This is more efficient approach for building lightweight applications.
54+
*
55+
* ```ts
56+
*import { ENS } from 'web3-eth-ens';
57+
*
58+
* const ens = new ENS(undefined,'https://127.0.0.1:4545');
59+
*
60+
* console.log(await ens.getAddress('vitalik.eth'));
61+
* ```
62+
*/
5963
export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
6064
/**
6165
* The registryAddress property can be used to define a custom registry address when you are connected to an unknown chain. It defaults to the main registry address.
@@ -175,7 +179,6 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
175179
return this._resolver.getText(ENSName, key);
176180
}
177181

178-
179182
/**
180183
* Resolves the name of an ENS node.
181184
* @param ENSName - The node to resolve
@@ -246,7 +249,7 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
246249
return this._detectedAddress;
247250
}
248251
const networkType = await getId(this, {
249-
...DEFAULT_RETURN_FORMAT,
252+
...(this.defaultReturnFormat ?? DEFAULT_RETURN_FORMAT),
250253
number: FMT_NUMBER.HEX,
251254
}); // get the network from provider
252255
const addr = registryAddresses[networkIds[networkType]];
@@ -292,10 +295,10 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
292295
* const receipt = await ens.setAddress('web3js.eth','0xe2597eb05cf9a87eb1309e86750c903ec38e527e');
293296
*```
294297
*/
295-
public async setAddress(
298+
public async setAddress(
296299
name: string,
297300
address: Address,
298-
txConfig: PayableCallOptions
301+
txConfig: PayableCallOptions,
299302
): Promise<TransactionReceipt | RevertInstructionError> {
300303
return this._resolver.setAddress(name, address, txConfig);
301304
}

0 commit comments

Comments
 (0)