UNPKG

14.1 kBTypeScriptView Raw
1import { AbiDecoder, BigNumber } from '@0x/utils';
2import { BlockParam, BlockWithoutTransactionData, BlockWithTransactionData, CallData, FilterObject, JSONRPCRequestPayload, LogEntry, SupportedProvider, TraceParams, Transaction, TransactionReceipt, TransactionReceiptWithDecodedLogs, TransactionTrace, TxAccessListWithGas, TxData } from 'ethereum-types';
3import { NodeType } from './types';
4/**
5 * An alternative to the Web3.js library that provides a consistent, clean, promise-based interface.
6 */
7export declare class Web3Wrapper {
8 /**
9 * Flag to check if this instance is of type Web3Wrapper
10 */
11 isZeroExWeb3Wrapper: boolean;
12 abiDecoder: AbiDecoder;
13 private _provider;
14 private readonly _supportedProvider;
15 private readonly _callAndTxnDefaults;
16 private _jsonRpcRequestId;
17 /**
18 * Check if an address is a valid Ethereum address
19 * @param address Address to check
20 * @returns Whether the address is a valid Ethereum address
21 */
22 static isAddress(address: string): boolean;
23 /**
24 * A unit amount is defined as the amount of a token above the specified decimal places (integer part).
25 * E.g: If a currency has 18 decimal places, 1e18 or one quintillion of the currency is equivalent
26 * to 1 unit.
27 * @param amount The amount in baseUnits that you would like converted to units.
28 * @param decimals The number of decimal places the unit amount has.
29 * @return The amount in units.
30 */
31 static toUnitAmount(amount: BigNumber, decimals: number): BigNumber;
32 /**
33 * A baseUnit is defined as the smallest denomination of a token. An amount expressed in baseUnits
34 * is the amount expressed in the smallest denomination.
35 * E.g: 1 unit of a token with 18 decimal places is expressed in baseUnits as 1000000000000000000
36 * @param amount The amount of units that you would like converted to baseUnits.
37 * @param decimals The number of decimal places the unit amount has.
38 * @return The amount in baseUnits.
39 */
40 static toBaseUnitAmount(amount: BigNumber | number, decimals: number): BigNumber;
41 /**
42 * Convert an Ether amount from ETH to Wei
43 * @param ethAmount Amount of Ether to convert to wei
44 * @returns Amount in wei
45 */
46 static toWei(ethAmount: BigNumber): BigNumber;
47 private static _assertBlockParam;
48 private static _assertBlockParamOrString;
49 private static _normalizeTxReceiptStatus;
50 /**
51 * Instantiates a new Web3Wrapper.
52 * @param provider The Web3 provider instance you would like the Web3Wrapper to use for interacting with
53 * the backing Ethereum node.
54 * @param callAndTxnDefaults Override Call and Txn Data defaults sent with RPC requests to the backing Ethereum node.
55 * @return An instance of the Web3Wrapper class.
56 */
57 constructor(supportedProvider: SupportedProvider, callAndTxnDefaults?: Partial<CallData>);
58 /**
59 * Get the contract defaults set to the Web3Wrapper instance
60 * @return CallAndTxnData defaults (e.g gas, gasPrice, nonce, etc...)
61 */
62 getContractDefaults(): Partial<CallData> | undefined;
63 /**
64 * Retrieve the Web3 provider
65 * @return Web3 provider instance
66 */
67 getProvider(): SupportedProvider;
68 /**
69 * Update the used Web3 provider
70 * @param provider The new Web3 provider to be set
71 */
72 setProvider(supportedProvider: SupportedProvider): void;
73 /**
74 * Check whether an address is available through the backing provider. This can be
75 * useful if you want to know whether a user can sign messages or transactions from
76 * a given Ethereum address.
77 * @param senderAddress Address to check availability for
78 * @returns Whether the address is available through the provider.
79 */
80 isSenderAddressAvailableAsync(senderAddress: string): Promise<boolean>;
81 /**
82 * Fetch the backing Ethereum node's version string (e.g `MetaMask/v4.2.0`)
83 * @returns Ethereum node's version string
84 */
85 getNodeVersionAsync(): Promise<string>;
86 /**
87 * Fetches the networkId of the backing Ethereum node
88 * @returns The network id
89 */
90 getNetworkIdAsync(): Promise<number>;
91 /**
92 * Fetches the chainId of the backing Ethereum node
93 * @returns The chain id
94 */
95 getChainIdAsync(): Promise<number>;
96 /**
97 * Fetch the current gas price.
98 * For post-London hardfork chains, this will be baseFeePerGas + maxPriorityFeePerGas
99 */
100 getGasPriceAsync(): Promise<BigNumber>;
101 /**
102 * Fetch the base fee per gas for the pending block.
103 */
104 getBaseFeePerGasAsync(): Promise<BigNumber>;
105 /**
106 * Fetch the current max piority fee per gas. This is the suggested miner tip
107 * to get mined in the current block.
108 */
109 getMaxPriorityFeePerGasAsync(): Promise<BigNumber>;
110 /**
111 * Retrieves the transaction receipt for a given transaction hash if found
112 * @param txHash Transaction hash
113 * @returns The transaction receipt, including it's status (0: failed, 1: succeeded). Returns undefined if transaction not found.
114 */
115 getTransactionReceiptIfExistsAsync(txHash: string): Promise<TransactionReceipt | undefined>;
116 /**
117 * Retrieves the transaction data for a given transaction
118 * @param txHash Transaction hash
119 * @returns The raw transaction data
120 */
121 getTransactionByHashAsync(txHash: string): Promise<Transaction>;
122 /**
123 * Retrieves an accounts Ether balance in wei
124 * @param owner Account whose balance you wish to check
125 * @param defaultBlock The block depth at which to fetch the balance (default=latest)
126 * @returns Balance in wei
127 */
128 getBalanceInWeiAsync(owner: string, defaultBlock?: BlockParam): Promise<BigNumber>;
129 /**
130 * Check if a contract exists at a given address
131 * @param address Address to which to check
132 * @returns Whether or not contract code was found at the supplied address
133 */
134 doesContractExistAtAddressAsync(address: string): Promise<boolean>;
135 /**
136 * Gets the contract code by address
137 * @param address Address of the contract
138 * @param defaultBlock Block height at which to make the call. Defaults to `latest`
139 * @return Code of the contract
140 */
141 getContractCodeAsync(address: string, defaultBlock?: BlockParam): Promise<string>;
142 /**
143 * Gets the debug trace of a transaction
144 * @param txHash Hash of the transactuon to get a trace for
145 * @param traceParams Config object allowing you to specify if you need memory/storage/stack traces.
146 * @return Transaction trace
147 */
148 getTransactionTraceAsync(txHash: string, traceParams: TraceParams): Promise<TransactionTrace>;
149 /**
150 * Sign a message with a specific address's private key (`eth_sign`)
151 * @param address Address of signer
152 * @param message Message to sign
153 * @returns Signature string (might be VRS or RSV depending on the Signer)
154 */
155 signMessageAsync(address: string, message: string): Promise<string>;
156 /**
157 * Sign an EIP712 typed data message with a specific address's private key (`eth_signTypedData`)
158 * @param address Address of signer
159 * @param typedData Typed data message to sign
160 * @returns Signature string (as RSV)
161 */
162 signTypedDataAsync(address: string, typedData: any): Promise<string>;
163 /**
164 * Fetches the latest block number
165 * @returns Block number
166 */
167 getBlockNumberAsync(): Promise<number>;
168 /**
169 * Fetches the nonce for an account (transaction count for EOAs).
170 * @param address Address of account.
171 * @param defaultBlock Block height at which to make the call. Defaults to `latest`
172 * @returns Account nonce.
173 */
174 getAccountNonceAsync(address: string, defaultBlock?: BlockParam): Promise<number>;
175 /**
176 * Fetch a specific Ethereum block without transaction data
177 * @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral)
178 * @returns The requested block without transaction data, or undefined if block was not found
179 * (e.g the node isn't fully synced, there was a block re-org and the requested block was uncles, etc...)
180 */
181 getBlockIfExistsAsync(blockParam: string | BlockParam): Promise<BlockWithoutTransactionData | undefined>;
182 /**
183 * Fetch a specific Ethereum block with transaction data
184 * @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral)
185 * @returns The requested block with transaction data
186 */
187 getBlockWithTransactionDataAsync(blockParam: string | BlockParam): Promise<BlockWithTransactionData>;
188 /**
189 * Fetch a block's timestamp
190 * @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral)
191 * @returns The block's timestamp
192 */
193 getBlockTimestampAsync(blockParam: string | BlockParam): Promise<number>;
194 /**
195 * Retrieve the user addresses available through the backing provider
196 * @returns Available user addresses
197 */
198 getAvailableAddressesAsync(): Promise<string[]>;
199 /**
200 * Take a snapshot of the blockchain state on a TestRPC/Ganache local node
201 * @returns The snapshot id. This can be used to revert to this snapshot
202 */
203 takeSnapshotAsync(): Promise<number>;
204 /**
205 * Revert the blockchain state to a previous snapshot state on TestRPC/Ganache local node
206 * @param snapshotId snapshot id to revert to
207 * @returns Whether the revert was successful
208 */
209 revertSnapshotAsync(snapshotId: number): Promise<boolean>;
210 /**
211 * Mine a block on a TestRPC/Ganache local node
212 */
213 mineBlockAsync(): Promise<void>;
214 /**
215 * Increase the next blocks timestamp on TestRPC/Ganache or Geth local node.
216 * Will throw if provider is neither TestRPC/Ganache or Geth.
217 * @param timeDelta Amount of time to add in seconds
218 */
219 increaseTimeAsync(timeDelta: number): Promise<number>;
220 /**
221 * Retrieve smart contract logs for a given filter
222 * @param filter Parameters by which to filter which logs to retrieve
223 * @returns The corresponding log entries
224 */
225 getLogsAsync(filter: FilterObject): Promise<LogEntry[]>;
226 /**
227 * Calculate the estimated gas cost for a given transaction
228 * @param txData Transaction data
229 * @returns Estimated gas cost
230 */
231 estimateGasAsync(txData: Partial<TxData>): Promise<number>;
232 /**
233 * Generate an access list for an ethereum call and also compute the gas used.
234 * @param callData Call data
235 * @param defaultBlock Block height at which to make the call. Defaults to 'latest'.
236 * @returns The access list and gas used.
237 */
238 createAccessListAsync(callData: CallData, defaultBlock?: BlockParam): Promise<TxAccessListWithGas>;
239 /**
240 * Call a smart contract method at a given block height
241 * @param callData Call data
242 * @param defaultBlock Block height at which to make the call. Defaults to `latest`
243 * @returns The raw call result
244 */
245 callAsync(callData: CallData, defaultBlock?: BlockParam): Promise<string>;
246 /**
247 * Send a transaction
248 * @param txData Transaction data
249 * @returns Transaction hash
250 */
251 sendTransactionAsync(txData: TxData): Promise<string>;
252 /**
253 * Waits for a transaction to be mined and returns the transaction receipt.
254 * Note that just because a transaction was mined does not mean it was
255 * successful. You need to check the status code of the transaction receipt
256 * to find out if it was successful, or use the helper method
257 * awaitTransactionSuccessAsync.
258 * @param txHash Transaction hash
259 * @param pollingIntervalMs How often (in ms) should we check if the transaction is mined.
260 * @param timeoutMs How long (in ms) to poll for transaction mined until aborting.
261 * @return Transaction receipt with decoded log args.
262 */
263 awaitTransactionMinedAsync(txHash: string, pollingIntervalMs?: number, timeoutMs?: number): Promise<TransactionReceiptWithDecodedLogs>;
264 /**
265 * Waits for a transaction to be mined and returns the transaction receipt.
266 * Unlike awaitTransactionMinedAsync, it will throw if the receipt has a
267 * status that is not equal to 1. A status of 0 or null indicates that the
268 * transaction was mined, but failed. See:
269 * https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgettransactionreceipt
270 * @param txHash Transaction hash
271 * @param pollingIntervalMs How often (in ms) should we check if the transaction is mined.
272 * @param timeoutMs How long (in ms) to poll for transaction mined until aborting.
273 * @return Transaction receipt with decoded log args.
274 */
275 awaitTransactionSuccessAsync(txHash: string, pollingIntervalMs?: number, timeoutMs?: number): Promise<TransactionReceiptWithDecodedLogs>;
276 /**
277 * Calls the 'debug_setHead' JSON RPC method, which sets the current head of
278 * the local chain by block number. Note, this is a destructive action and
279 * may severely damage your chain. Use with extreme caution. As of now, this
280 * is only supported by Geth. It sill throw if the 'debug_setHead' method is
281 * not supported.
282 * @param blockNumber The block number to reset to.
283 */
284 setHeadAsync(blockNumber: number): Promise<void>;
285 /**
286 * Sends a raw Ethereum JSON RPC payload and returns the response's `result` key
287 * @param payload A partial JSON RPC payload. No need to include version, id, params (if none needed)
288 * @return The contents nested under the result key of the response body
289 */
290 sendRawPayloadAsync<A>(payload: Partial<JSONRPCRequestPayload>): Promise<A>;
291 /**
292 * Returns either NodeType.Geth or NodeType.Ganache depending on the type of
293 * the backing Ethereum node. Throws for any other type of node.
294 */
295 getNodeTypeAsync(): Promise<NodeType>;
296}
297//# sourceMappingURL=web3_wrapper.d.ts.map
\No newline at end of file