-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
types.ts
439 lines (421 loc) · 17.6 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { Web3ContextInitOptions, Web3PromiEvent } from 'web3-core';
import {
AccessListResult,
BlockNumberOrTag,
EthExecutionAPI,
HexString,
Numbers,
TransactionReceipt,
NonPayableCallOptions,
PayableCallOptions,
DataFormat,
DEFAULT_RETURN_FORMAT,
FormatType,
} from 'web3-types';
import { NewHeadsSubscription, SendTransactionEvents } from 'web3-eth';
import { LogsSubscription } from './log_subscription.js';
export type NonPayableTxOptions = NonPayableCallOptions;
export type PayableTxOptions = PayableCallOptions;
export { ContractAbiWithSignature, EventLog, ContractOptions } from 'web3-types';
export interface ContractEventOptions {
/**
* Let you filter events by indexed parameters, e.g. `{filter: {myNumber: [12,13]}}` means all events where `myNumber` is `12` or `13`.
*/
filter?: Record<string, unknown>;
/**
* The block number (greater than or equal to) from which to get events on. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized` can also be used. For specific range use {@link Contract.getPastEvents}.
*/
fromBlock?: BlockNumberOrTag;
/**
* This allows to manually set the topics for the event filter. If given the filter property and event signature, (topic[0]) will not be set automatically. Each topic can also be a nested array of topics that behaves as `or` operation between the given nested topics.
*/
topics?: string[];
}
export interface NonPayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
arguments: Inputs;
/**
* This will call a method and execute its smart contract method in the EVM without sending any transaction. Note calling cannot alter the smart contract state.
*
* ```ts
* // using the promise
* const result = await myContract.methods.myMethod(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
* // MULTI-ARGUMENT RETURN:
* // Solidity
* contract MyContract {
* function myFunction() returns(uint256 myNumber, string myString) {
* return (23456, "Hello!%");
* }
* }
*
* // web3.js
* var MyContract = new web3.eth.Contract(abi, address);
* const result = MyContract.methods.myFunction().call()
* console.log(result)
* > Result {
* myNumber: '23456',
* myString: 'Hello!%',
* 0: '23456', // these are here as fallbacks if the name is not know or given
* 1: 'Hello!%'
* }
*
*
* // SINGLE-ARGUMENT RETURN:
* // Solidity
* contract MyContract {
* function myFunction() returns(string myString) {
* return "Hello!%";
* }
* }
*
* // web3.js
* const MyContract = new web3.eth.Contract(abi, address);
* const result = await MyContract.methods.myFunction().call();
* console.log(result);
* > "Hello!%"
* ```
*
* @param tx - The options used for calling.
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns - The return value(s) of the smart contract method. If it returns a single value, it’s returned as is. If it has multiple return values they are returned as an object with properties and indices.
*/
call<SpecialOutput = Outputs>(
tx?: NonPayableCallOptions,
block?: BlockNumberOrTag,
): Promise<SpecialOutput>;
/**
* This will send a transaction to the smart contract and execute its method. Note this can alter the smart contract state.
*
* ```ts
* await myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
* const receipt = await myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
*
* // using the event emitter
* const sendObj = myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
* sendObj.on('transactionHash', function(hash){
* ...
* });
*
* sendObj.on('confirmation', function(confirmationNumber, receipt){
* ...
* });
*
* sendObj.on('receipt', function(receipt){
* // receipt example
* console.log(receipt);
* > {
* "transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
* "transactionIndex": 0,
* "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
* "blockNumber": 3,
* "contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
* "cumulativeGasUsed": 314159,
* "gasUsed": 30234,
* "events": {
* "MyEvent": {
* returnValues: {
* myIndexedParam: 20,
* myOtherIndexedParam: '0x123456789...',
* myNonIndexParam: 'My String'
* },
* raw: {
* data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
* topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
* },
* event: 'MyEvent',
* signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
* logIndex: 0,
* transactionIndex: 0,
* transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
* blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
* blockNumber: 1234,
* address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
* },
* "MyOtherEvent": {
* ...
* },
* "MyMultipleEvent":[{...}, {...}] // If there are multiple of the same event, they will be in an array
* }
* }
* });
*
* sendObj.on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
* ...
* });
* ```
*
* @param tx - The options used for sending.
* @returns - Returns a {@link PromiEvent} resolved with transaction receipt.
*/
send(
tx?: NonPayableTxOptions,
): Web3PromiEvent<
FormatType<TransactionReceipt, typeof DEFAULT_RETURN_FORMAT>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;
/**
* Returns the amount of gas consumed by executing the method locally without creating a new transaction on the blockchain.
* The returned amount can be used as a gas estimate for executing the transaction publicly. The actual gas used can be
* different when sending the transaction later, as the state of the smart contract can be different at that time.
*
* ```ts
* const gasAmount = await myContract.methods.myMethod(123).estimateGas({gas: 5000000});
* if(gasAmount == 5000000) {
* console.log('Method ran out of gas');
* }
* ```
*
* @param options - The options used for calling
* @param returnFormat - The data format you want the output in.
* @returns - The gas amount estimated.
*/
estimateGas<ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
options?: NonPayableCallOptions,
returnFormat?: ReturnFormat,
): Promise<FormatType<Numbers, ReturnFormat>>;
/**
* Encodes the ABI for this method. The resulting hex string is 32-bit function signature hash plus the passed parameters in Solidity tightly packed format.
* This can be used to send a transaction, call a method, or pass it into another smart contract’s method as arguments.
* Set the data field on `web3.eth.sendTransaction` options as the encodeABI() result and it is the same as calling the contract method with `contract.myMethod.send()`.
*
* Some use cases for encodeABI() include: preparing a smart contract transaction for a multi signature wallet,
* working with offline wallets and cold storage and creating transaction payload for complex smart contract proxy calls.
*
* @returns - The encoded ABI byte code to send via a transaction or call.
*/
encodeABI(): string;
/**
* This method generates an access list for a transaction. You must specify a `from` address and `gas` if it’s not specified in options.
*
* @param options - The options used for createAccessList.
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns The returned data of the createAccessList, e.g. The generated access list for transaction.
*
* ```ts
* const result = await MyContract.methods.myFunction().createAccessList();
* console.log(result);
*
* > {
* "accessList": [
* {
* "address": "0x15859bdf5aff2080a9968f6a410361e9598df62f",
* "storageKeys": [
* "0x0000000000000000000000000000000000000000000000000000000000000000"
* ]
* }
* ],
* "gasUsed": "0x7671"
* }
* ```
*/
createAccessList(
tx?: NonPayableCallOptions,
block?: BlockNumberOrTag,
): Promise<AccessListResult>;
}
export interface PayableMethodObject<Inputs = unknown[], Outputs = unknown[]> {
arguments: Inputs;
/**
* Will call a method and execute its smart contract method in the EVM without sending any transaction. Note calling cannot alter the smart contract state.
*
* ```ts
* // using the promise
* const result = await myContract.methods.myMethod(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
* // MULTI-ARGUMENT RETURN:
* // Solidity
* contract MyContract {
* function myFunction() returns(uint256 myNumber, string myString) {
* return (23456, "Hello!%");
* }
* }
*
* // web3.js
* var MyContract = new web3.eth.Contract(abi, address);
* const result = MyContract.methods.myFunction().call()
* console.log(result)
* > Result {
* myNumber: '23456',
* myString: 'Hello!%',
* 0: '23456', // these are here as fallbacks if the name is not know or given
* 1: 'Hello!%'
* }
*
*
* // SINGLE-ARGUMENT RETURN:
* // Solidity
* contract MyContract {
* function myFunction() returns(string myString) {
* return "Hello!%";
* }
* }
*
* // web3.js
* const MyContract = new web3.eth.Contract(abi, address);
* const result = await MyContract.methods.myFunction().call();
* console.log(result);
* > "Hello!%"
* ```
*
* @param tx - The options used for calling.
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns - The return value(s) of the smart contract method. If it returns a single value, it’s returned as is. If it has multiple return values they are returned as an object with properties and indices.
*/
call<SpecialOutput = Outputs>(
tx?: PayableCallOptions,
block?: BlockNumberOrTag,
): Promise<SpecialOutput>;
/**
* Will send a transaction to the smart contract and execute its method. Note this can alter the smart contract state.
*
* ```ts
* await myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
* const receipt = await myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'});
*
*
* // using the event emitter
* const sendObj = myContract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
* sendObj.on('transactionHash', function(hash){
* ...
* });
*
* sendObj.on('confirmation', function(confirmationNumber, receipt){
* ...
* });
*
* sendObj.on('receipt', function(receipt){
* // receipt example
* console.log(receipt);
* > {
* "transactionHash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
* "transactionIndex": 0,
* "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46",
* "blockNumber": 3,
* "contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe",
* "cumulativeGasUsed": 314159,
* "gasUsed": 30234,
* "events": {
* "MyEvent": {
* returnValues: {
* myIndexedParam: 20,
* myOtherIndexedParam: '0x123456789...',
* myNonIndexParam: 'My String'
* },
* raw: {
* data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
* topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
* },
* event: 'MyEvent',
* signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
* logIndex: 0,
* transactionIndex: 0,
* transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
* blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
* blockNumber: 1234,
* address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
* },
* "MyOtherEvent": {
* ...
* },
* "MyMultipleEvent":[{...}, {...}] // If there are multiple of the same event, they will be in an array
* }
* }
* });
*
* sendObj.on('error', function(error, receipt) { // If the transaction was rejected by the network with a receipt, the second parameter will be the receipt.
* ...
* });
* ```
*
* @param tx - The options used for sending.
* @returns - Returns a {@link PromiEvent} object resolved with transaction receipt.
*/
send(
tx?: PayableTxOptions,
): Web3PromiEvent<
FormatType<TransactionReceipt, typeof DEFAULT_RETURN_FORMAT>,
SendTransactionEvents<typeof DEFAULT_RETURN_FORMAT>
>;
/**
* Returns the amount of gas consumed by executing the method locally without creating a new transaction on the blockchain.
* The returned amount can be used as a gas estimate for executing the transaction publicly. The actual gas used can be
* different when sending the transaction later, as the state of the smart contract can be different at that time.
*
* ```ts
* const gasAmount = await myContract.methods.myMethod(123).estimateGas({gas: 5000000});
* if(gasAmount == 5000000) {
* console.log('Method ran out of gas');
* }
* ```
*
* @param options - The options used for calling
* @param returnFormat - The data format you want the output in.
* @returns - The gas amount estimated.
*/
estimateGas<ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
options?: PayableCallOptions,
returnFormat?: ReturnFormat,
): Promise<FormatType<Numbers, ReturnFormat>>;
/**
* Encodes the ABI for this method. The resulting hex string is 32-bit function signature hash plus the passed parameters in Solidity tightly packed format.
* This can be used to send a transaction, call a method, or pass it into another smart contract’s method as arguments.
* Set the data field on `web3.eth.sendTransaction` options as the encodeABI() result and it is the same as calling the contract method with `contract.myMethod.send()`.
*
* Some use cases for encodeABI() include: preparing a smart contract transaction for a multi signature wallet,
* working with offline wallets and cold storage and creating transaction payload for complex smart contract proxy calls.
*
* @returns - The encoded ABI byte code to send via a transaction or call.
*/
encodeABI(): HexString;
/**
* This method generates an access list for a transaction. You must specify a `from` address and `gas` if it’s not specified in options.
*
* @param options - The options used for createAccessList.
* @param block - If you pass this parameter it will not use the default block set with contract.defaultBlock. Pre-defined block numbers as `earliest`, `latest`, `pending`, `safe` or `finalized can also be used. Useful for requesting data from or replaying transactions in past blocks.
* @returns The returned data of the createAccessList, e.g. The generated access list for transaction.
*
* ```ts
* const result = await MyContract.methods.myFunction().createAccessList();
* console.log(result);
*
* > {
* "accessList": [
* {
* "address": "0x15859bdf5aff2080a9968f6a410361e9598df62f",
* "storageKeys": [
* "0x0000000000000000000000000000000000000000000000000000000000000000"
* ]
* }
* ],
* "gasUsed": "0x7671"
* }
*```
*/
createAccessList(tx?: PayableCallOptions, block?: BlockNumberOrTag): Promise<AccessListResult>;
}
export type Web3ContractContext = Partial<
Web3ContextInitOptions<
EthExecutionAPI,
{
logs: typeof LogsSubscription;
newHeads: typeof NewHeadsSubscription;
newBlockHeaders: typeof NewHeadsSubscription;
}
>
>;