UNPKG

web3-eth

Version:

Web3 module to interact with the Ethereum blockchain and smart contracts.

989 lines (986 loc) 105 kB
"use strict"; /* 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/>. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web3Eth = exports.registeredSubscriptions = void 0; // Disabling because returnTypes must be last param to match 1.x params /* eslint-disable default-param-last */ const web3_types_1 = require("web3-types"); const web3_core_1 = require("web3-core"); const web3_errors_1 = require("web3-errors"); const web3_utils_1 = require("web3-utils"); const web3_rpc_methods_1 = require("web3-rpc-methods"); const rpcMethodsWrappers = __importStar(require("./rpc_method_wrappers.js")); const filteringRpcMethodsWrappers = __importStar(require("./filtering_rpc_method_wrappers.js")); const web3_subscriptions_js_1 = require("./web3_subscriptions.js"); exports.registeredSubscriptions = { logs: web3_subscriptions_js_1.LogsSubscription, newPendingTransactions: web3_subscriptions_js_1.NewPendingTransactionsSubscription, newHeads: web3_subscriptions_js_1.NewHeadsSubscription, syncing: web3_subscriptions_js_1.SyncingSubscription, pendingTransactions: web3_subscriptions_js_1.NewPendingTransactionsSubscription, // the same as newPendingTransactions. just for support API like in version 1.x newBlockHeaders: web3_subscriptions_js_1.NewHeadsSubscription, // the same as newHeads. just for support API like in version 1.x }; /** * * The Web3Eth allows you to interact with an Ethereum blockchain. * * For using Web3 Eth functions, first install Web3 package using `npm i web3` or `yarn add web3` based on your package manager usage. * After that, Web3 Eth functions will be available as mentioned in following snippet. * ```ts * import { Web3 } from 'web3'; * const web3 = new Web3('https://mainnet.infura.io/v3/<YOURPROJID>'); * * const block = await web3.eth.getBlock(0); * * ``` * * For using individual package install `web3-eth` package using `npm i web3-eth` or `yarn add web3-eth` and only import required functions. * This is more efficient approach for building lightweight applications. * ```ts * import { Web3Eth } from 'web3-eth'; * * const eth = new Web3Eth('https://mainnet.infura.io/v3/<YOURPROJID>'); * const block = await eth.getBlock(0); * * ``` */ class Web3Eth extends web3_core_1.Web3Context { constructor( // eslint-disable-next-line @typescript-eslint/no-explicit-any providerOrContext) { if (typeof providerOrContext === 'string' || // eslint-disable-next-line @typescript-eslint/no-explicit-any (0, web3_core_1.isSupportedProvider)(providerOrContext)) { // @ts-expect-error disable the error: "A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers." super({ // eslint-disable-next-line @typescript-eslint/no-explicit-any provider: providerOrContext, registeredSubscriptions: exports.registeredSubscriptions, }); return; } if (providerOrContext.registeredSubscriptions) { super(providerOrContext); return; } super(Object.assign(Object.assign({}, providerOrContext), { registeredSubscriptions: exports.registeredSubscriptions })); // an alias for calculateFeeData // eslint-disable-next-line this.getFeeData = this.calculateFeeData; } setTransactionMiddleware(transactionMiddleware) { this.transactionMiddleware = transactionMiddleware; } getTransactionMiddleware() { return this.transactionMiddleware; } /** * @returns Returns the ethereum protocol version of the node. * * ```ts * web3.eth.getProtocolVersion().then(console.log); * > "63" * ``` */ getProtocolVersion() { return __awaiter(this, void 0, void 0, function* () { return web3_rpc_methods_1.ethRpcMethods.getProtocolVersion(this.requestManager); }); } // TODO Add returnFormat parameter /** * Checks if the node is currently syncing. * * @returns Either a {@link SyncingStatusAPI}, or `false`. * * ```ts * web3.eth.isSyncing().then(console.log); * > { * startingBlock: 100, * currentBlock: 312, * highestBlock: 512, * knownStates: 234566, * pulledStates: 123455 * } * ``` */ isSyncing() { return __awaiter(this, void 0, void 0, function* () { return web3_rpc_methods_1.ethRpcMethods.getSyncing(this.requestManager); }); } // TODO consider adding returnFormat parameter (to format address as bytes) /** * @returns Returns the coinbase address to which mining rewards will go. * * ```ts * web3.eth.getCoinbase().then(console.log); * > "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe" * ``` */ getCoinbase() { return __awaiter(this, void 0, void 0, function* () { return web3_rpc_methods_1.ethRpcMethods.getCoinbase(this.requestManager); }); } /** * Checks whether the node is mining or not. * * @returns `true` if the node is mining, otherwise `false`. * * ```ts * web3.eth.isMining().then(console.log); * > true * ``` */ isMining() { return __awaiter(this, void 0, void 0, function* () { return web3_rpc_methods_1.ethRpcMethods.getMining(this.requestManager); }); } /** * @deprecated Will be removed in the future, please use {@link Web3Eth.getHashRate} method instead. * * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The number of hashes per second that the node is mining with. * * ```ts * web3.eth.getHashrate().then(console.log); * > 493736n * * web3.eth.getHashrate({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log); * > "0x788a8" * ``` */ getHashrate(returnFormat) { return __awaiter(this, void 0, void 0, function* () { var _a; if (returnFormat === void 0) { returnFormat = ((_a = this.defaultReturnFormat) !== null && _a !== void 0 ? _a : web3_types_1.DEFAULT_RETURN_FORMAT); } return this.getHashRate(returnFormat); }); } /** * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The number of hashes per second that the node is mining with. * * ```ts * web3.eth.getHashRate().then(console.log); * > 493736n * * web3.eth.getHashRate({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log); * > "0x788a8" * ``` */ getHashRate() { return __awaiter(this, arguments, void 0, function* (returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getHashRate(this, returnFormat); }); } /** * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The gas price determined by the last few blocks median gas price. * * ```ts * web3.eth.getGasPrice().then(console.log); * > 20000000000n * * web3.eth.getGasPrice({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log); * > "0x4a817c800" * ``` */ getGasPrice() { return __awaiter(this, arguments, void 0, function* (returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getGasPrice(this, returnFormat); }); } /** * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns the current maxPriorityFeePerGas per gas in wei. * * ```ts * web3.eth.getMaxPriorityFeePerGas().then(console.log); * > 20000000000n * * web3.eth.getMaxPriorityFeePerGas({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log); * > "0x4a817c800" * ``` */ getMaxPriorityFeePerGas() { return __awaiter(this, arguments, void 0, function* (returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getMaxPriorityFeePerGas(this, returnFormat); }); } /** * Calculates the current Fee Data. * If the node supports EIP-1559, then `baseFeePerGas` and `maxPriorityFeePerGas` will be returned along with the calculated `maxFeePerGas` value. * `maxFeePerGas` is calculated as `baseFeePerGas` * `baseFeePerGasFactor` + `maxPriorityFeePerGas`. * If the node does not support EIP-1559, then the `gasPrice` will be returned and the other values will be undefined. * * @param baseFeePerGasFactor (optional) The factor to multiply the `baseFeePerGas` with when calculating `maxFeePerGas`, if the node supports EIP-1559. This can be a `bigint` for precise calculation or a `number` to support decimals. The default value is 2 (BigInt). * If a `number` is provided, it will be converted to `bigint` with three decimal precision. * @param alternativeMaxPriorityFeePerGas (optional) The alternative `maxPriorityFeePerGas` to use when calculating `maxFeePerGas`, if the node supports EIP-1559 but does not support the method `eth_maxPriorityFeePerGas`. The default value is 1 gwei. * @returns The current fee data. * * @example * web3.eth.calculateFeeData().then(console.log); * > { * gasPrice: 20000000000n, * maxFeePerGas: 60000000000n, * maxPriorityFeePerGas: 20000000000n, * baseFeePerGas: 20000000000n * } * * @example * // Using a `bigint` for baseFeePerGasFactor * web3.eth.calculateFeeData(1n).then(console.log); * > { * gasPrice: 20000000000n, * maxFeePerGas: 40000000000n, * maxPriorityFeePerGas: 20000000000n, * baseFeePerGas: 20000000000n * } * * @example * // Using a `number` for baseFeePerGasFactor (with decimals) * web3.eth.calculateFeeData(1.5).then(console.log); * > { * gasPrice: 20000000000n, * maxFeePerGas: 50000000000n, // baseFeePerGasFactor is converted to BigInt(1.500) * maxPriorityFeePerGas: 20000000000n, * baseFeePerGas: 20000000000n * } * * @example * web3.eth.calculateFeeData(3n).then(console.log); * > { * gasPrice: 20000000000n, * maxFeePerGas: 80000000000n, * maxPriorityFeePerGas: 20000000000n, * baseFeePerGas: 20000000000n * } */ calculateFeeData() { return __awaiter(this, arguments, void 0, function* (baseFeePerGasFactor = BigInt(2), alternativeMaxPriorityFeePerGas = web3_utils_1.ethUnitMap.Gwei) { var _a; const block = yield this.getBlock(undefined, false); const baseFeePerGas = (_a = block === null || block === void 0 ? void 0 : block.baseFeePerGas) !== null && _a !== void 0 ? _a : undefined; // use undefined if it was null let gasPrice; try { gasPrice = yield this.getGasPrice(); } catch (error) { // do nothing } let maxPriorityFeePerGas; try { maxPriorityFeePerGas = yield this.getMaxPriorityFeePerGas(); } catch (error) { // do nothing } let maxFeePerGas; // if the `block.baseFeePerGas` is available, then EIP-1559 is supported // and we can calculate the `maxFeePerGas` from the `block.baseFeePerGas` if (baseFeePerGas) { // tip the miner with alternativeMaxPriorityFeePerGas, if no value available from getMaxPriorityFeePerGas maxPriorityFeePerGas = maxPriorityFeePerGas !== null && maxPriorityFeePerGas !== void 0 ? maxPriorityFeePerGas : alternativeMaxPriorityFeePerGas; // basically maxFeePerGas = (baseFeePerGas +- 12.5%) + maxPriorityFeePerGas // and we multiply the `baseFeePerGas` by `baseFeePerGasFactor`, to allow // trying to include the transaction in the next few blocks even if the // baseFeePerGas is increasing fast let baseFeeMultiplier; if (typeof baseFeePerGasFactor === 'number') { // Convert number to bigint with three decimal places baseFeeMultiplier = BigInt(Math.floor(baseFeePerGasFactor * 1000)) / BigInt(1000); } else { // It's already a BigInt, so just use it as-is baseFeeMultiplier = baseFeePerGasFactor; } maxFeePerGas = baseFeePerGas * baseFeeMultiplier + maxPriorityFeePerGas; } return { gasPrice, maxFeePerGas, maxPriorityFeePerGas, baseFeePerGas }; }); } /** * @returns A list of accounts the node controls (addresses are checksummed). * * ```ts * web3.eth.getAccounts().then(console.log); * > ["0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "0xDCc6960376d6C6dEa93647383FfB245CfCed97Cf"] * ``` */ getAccounts() { return __awaiter(this, void 0, void 0, function* () { var _a; const hexAddresses = (_a = (yield web3_rpc_methods_1.ethRpcMethods.getAccounts(this.requestManager))) !== null && _a !== void 0 ? _a : []; return hexAddresses.map(address => (0, web3_utils_1.toChecksumAddress)(address)); }); } /** * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The current block number. * * ```ts * web3.eth.getBlockNumber().then(console.log); * > 2744n * * web3.eth.getBlockNumber({ number: FMT_NUMBER.HEX , bytes: FMT_BYTES.HEX }).then(console.log); * > "0xab8" * ``` */ getBlockNumber() { return __awaiter(this, arguments, void 0, function* (returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getBlockNumber(this, returnFormat); }); } /** * Get the balance of an address at a given block. * * @param address The address to get the balance of. * @param blockNumber ({@link BlockNumberOrTag} defaults to {@link Web3Eth.defaultBlock}) Specifies what block to use as the current state for the balance query. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The current balance for the given address in `wei`. * * ```ts * web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); * > 1000000000000n * * web3.eth.getBalance("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); * > "0xe8d4a51000" * ``` */ getBalance(address_1) { return __awaiter(this, arguments, void 0, function* (address, blockNumber = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getBalance(this, address, blockNumber, returnFormat); }); } /** * Get the storage at a specific position of an address. * * @param address The address to get the storage from. * @param storageSlot The index position of the storage. * @param blockNumber ({@link BlockNumberOrTag} defaults to {@link Web3Eth.defaultBlock}) Specifies what block to use as the current state for the storage query. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The value in storage at the given position. * * ```ts * web3.eth.getStorageAt("0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234", 0).then(console.log); * > "0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234" * * web3.eth.getStorageAt( * "0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234", * 0, * undefined, * { number: FMT_NUMBER.HEX , bytes: FMT_BYTES.UINT8ARRAY } * ).then(console.log); * > Uint8Array(31) [ * 3, 52, 86, 115, 33, 35, 255, 255, * 35, 66, 52, 45, 209, 35, 66, 67, * 67, 36, 35, 66, 52, 253, 35, 79, * 210, 63, 212, 242, 61, 66, 52 * ] * ``` */ getStorageAt(address_1, storageSlot_1) { return __awaiter(this, arguments, void 0, function* (address, storageSlot, blockNumber = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getStorageAt(this, address, storageSlot, blockNumber, returnFormat); }); } /** * Get the code at a specific address. * * @param address The address to get the code from. * @param blockNumber ({@link BlockNumberOrTag} defaults to {@link Web3Eth.defaultBlock}) Specifies what block to use as the current state for the code query. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The [data](https://ethereum.org/en/developers/docs/transactions/#the-data-field) at the provided `address`. * * ```ts * web3.eth.getCode("0x033456732123ffff2342342dd12342434324234234fd234fd23fd4f23d4234").then(console.log); * > "0x600160008035811a818181146012578301005b601b6001356025565b8060005260206000f25b600060078202905091905056" * * web3.eth.getCode( * "0x407d73d8a49eeb85d32cf465507dd71d507100c1", * undefined, * { number: FMT_NUMBER.HEX , bytes: FMT_BYTES.UINT8ARRAY } * ).then(console.log); * > Uint8Array(50) [ * 96, 1, 96, 0, 128, 53, 129, 26, 129, 129, 129, * 20, 96, 18, 87, 131, 1, 0, 91, 96, 27, 96, * 1, 53, 96, 37, 86, 91, 128, 96, 0, 82, 96, * 32, 96, 0, 242, 91, 96, 0, 96, 7, 130, 2, * 144, 80, 145, 144, 80, 86 * ] * ``` */ getCode(address_1) { return __awaiter(this, arguments, void 0, function* (address, blockNumber = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getCode(this, address, blockNumber, returnFormat); }); } /** * Retrieves a {@link Block} matching the provided block number, block hash or block tag. * * @param block The {@link BlockNumberOrTag} (defaults to {@link Web3Eth.defaultBlock}) or block hash of the desired block. * @param hydrated If specified `true`, the returned block will contain all transactions as objects. If `false` it will only contain transaction hashes. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted (does not format transaction objects or hashes). * @returns A {@link Block} object matching the provided block number or block hash. * * ```ts * web3.eth.getBlock(0).then(console.log); * > { * hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d', * parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', * miner: '0x0000000000000000000000000000000000000000', * stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4', * transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * difficulty: 1n, * number: 0n, * gasLimit: 30000000n, * gasUsed: 0n, * timestamp: 1658281638n, * extraData: '0x', * mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * nonce: 0n, * totalDifficulty: 1n, * baseFeePerGas: 1000000000n, * size: 514n, * transactions: [], * uncles: [] * } * * web3.eth.getBlock( * "0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d", * false, * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > { * hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d', * parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', * miner: '0x0000000000000000000000000000000000000000', * stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4', * transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * difficulty: 1, * number: 0, * gasLimit: 30000000, * gasUsed: 0, * timestamp: 1658281638, * extraData: '0x', * mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * nonce: 0, * totalDifficulty: 1, * baseFeePerGas: 1000000000, * size: 514, * transactions: [], * uncles: [] * } * ``` */ getBlock() { return __awaiter(this, arguments, void 0, function* (block = this.defaultBlock, hydrated = false, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getBlock(this, block, hydrated, returnFormat); }); } /** * @param block The {@link BlockNumberOrTag} (defaults to {@link Web3Eth.defaultBlock}) or block hash of the desired block. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The number of transactions in the provided block. * * ```ts * web3.eth.getBlockTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); * > 1n * * web3.eth.getBlockTransactionCount( * "0x407d73d8a49eeb85d32cf465507dd71d507100c1", * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > 1 * ``` */ getBlockTransactionCount() { return __awaiter(this, arguments, void 0, function* (block = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getBlockTransactionCount(this, block, returnFormat); }); } /** * @param block The {@link BlockNumberOrTag} (defaults to {@link Web3Eth.defaultBlock}) or block hash of the desired block. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The number of [uncles](https://ethereum.org/en/glossary/#ommer) in the provided block. * * ```ts * web3.eth.getBlockUncleCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); * > 1n * * web3.eth.getBlockUncleCount( * "0x407d73d8a49eeb85d32cf465507dd71d507100c1", * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > 1 * ``` */ getBlockUncleCount() { return __awaiter(this, arguments, void 0, function* (block = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getBlockUncleCount(this, block, returnFormat); }); } /** * * @param block The {@link BlockNumberOrTag} (defaults to {@link Web3Eth.defaultBlock}) or block hash of the desired block. * @param uncleIndex The index position of the uncle. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns A blocks [uncle](https://ethereum.org/en/glossary/#ommer) by a given uncle index position. * * ```ts * web3.eth.getUncle(0, 1).then(console.log); * > { * hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d', * parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', * miner: '0x0000000000000000000000000000000000000000', * stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4', * transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * difficulty: 1n, * number: 0n, * gasLimit: 30000000n, * gasUsed: 0n, * timestamp: 1658281638n, * extraData: '0x', * mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * nonce: 0n, * totalDifficulty: 1n, * baseFeePerGas: 1000000000n, * size: 514n, * transactions: [], * uncles: [] * } * * web3.eth.getUncle( * "0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d", * 1, * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > { * hash: '0x7dbfdc6a7a67a670cb9b0c3f81ca60c007762f1e4e598cb027a470678ff26d0d', * parentHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * sha3Uncles: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347', * miner: '0x0000000000000000000000000000000000000000', * stateRoot: '0x5ed9882897d363c4632a6e67fba6203df61bd994813dcf048da59be442a9c6c4', * transactionsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * receiptsRoot: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * difficulty: 1, * number: 0, * gasLimit: 30000000, * gasUsed: 0, * timestamp: 1658281638, * extraData: '0x', * mixHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * nonce: 0, * totalDifficulty: 1, * baseFeePerGas: 1000000000, * size: 514, * transactions: [], * uncles: [] * } * ``` */ getUncle() { return __awaiter(this, arguments, void 0, function* (block = this.defaultBlock, uncleIndex, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getUncle(this, block, uncleIndex, returnFormat); }); } /** * @param transactionHash The hash of the desired transaction. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The desired transaction object. * * ```ts * web3.eth.getTransaction('0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc').then(console.log); * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0n, * nonce: 0n, * blockHash: '0x43202bd16b6bd54bea1b310736bd78bdbe93a64ad940f7586739d9eb25ad8d00', * blockNumber: 1n, * transactionIndex: 0n, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1n, * gas: 90000n, * gasPrice: 2000000000n, * input: '0x', * v: 2709n, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * } * * web3.eth.getTransaction( * web3.utils.hexToBytes("0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400"), * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0, * nonce: 0, * blockHash: '0x43202bd16b6bd54bea1b310736bd78bdbe93a64ad940f7586739d9eb25ad8d00', * blockNumber: 1, * transactionIndex: 0, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1, * gas: 90000, * gasPrice: 2000000000, * input: '0x', * v: 2709, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * } * ``` */ getTransaction(transactionHash_1) { return __awaiter(this, arguments, void 0, function* (transactionHash, returnFormat = this.defaultReturnFormat) { const response = yield rpcMethodsWrappers.getTransaction(this, transactionHash, returnFormat); if (!response) throw new web3_errors_1.TransactionNotFound(); return response; }); } /** * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns A list of pending transactions. * * ```ts * web3.eth.getPendingTransactions().then(console.log); * > [ * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0n, * nonce: 0n, * blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * blockNumber: null, * transactionIndex: 0n, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1n, * gas: 90000n, * gasPrice: 2000000000n, * input: '0x', * v: 2709n, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * }, * { * hash: '0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f', * type: 0n, * nonce: 1n, * blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * blockNumber: null, * transactionIndex: 0n, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1n, * gas: 90000n, * gasPrice: 2000000000n, * input: '0x', * v: 2710n, * r: '0x55ac19fade21db035a1b7ea0a8d49e265e05dbb926e75f273f836ad67ce5c96a', * s: '0x6550036a7c3fd426d5c3d35d96a7075cd673957620b7889846a980d2d017ec08' * } * ] * * * web3.eth.getPendingTransactions({ number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX }).then(console.log); * > [ * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0, * nonce: 0, * blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * blockNumber: null, * transactionIndex: 0, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1, * gas: 90000, * gasPrice: 2000000000, * input: '0x', * v: 2709, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * }, * { * hash: '0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f', * type: 0, * nonce: 1, * blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000', * blockNumber: null, * transactionIndex: 0, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1, * gas: 90000, * gasPrice: 2000000000, * input: '0x', * v: 2710, * r: '0x55ac19fade21db035a1b7ea0a8d49e265e05dbb926e75f273f836ad67ce5c96a', * s: '0x6550036a7c3fd426d5c3d35d96a7075cd673957620b7889846a980d2d017ec08' * } * ] * ``` */ getPendingTransactions() { return __awaiter(this, arguments, void 0, function* (returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getPendingTransactions(this, returnFormat); }); } /** * @param block The {@link BlockNumberOrTag} (defaults to {@link Web3Eth.defaultBlock}) or block hash of the desired block. * @param transactionIndex The index position of the transaction. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The desired transaction object. * * ```ts * web3.eth.getTransactionFromBlock('0x43202bd16b6bd54bea1b310736bd78bdbe93a64ad940f7586739d9eb25ad8d00', 0).then(console.log); * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0n, * nonce: 0n, * blockHash: '0x43202bd16b6bd54bea1b310736bd78bdbe93a64ad940f7586739d9eb25ad8d00', * blockNumber: 1n, * transactionIndex: 0n, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1n, * gas: 90000n, * gasPrice: 2000000000n, * input: '0x', * v: 2709n, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * } * * web3.eth.getTransactionFromBlock( * hexToBytes("0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400"), * 0, * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * { * hash: '0x73aea70e969941f23f9d24103e91aa1f55c7964eb13daf1c9360c308a72686dc', * type: 0, * nonce: 0, * blockHash: '0x43202bd16b6bd54bea1b310736bd78bdbe93a64ad940f7586739d9eb25ad8d00', * blockNumber: 1, * transactionIndex: 0, * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * value: 1, * gas: 90000, * gasPrice: 2000000000, * input: '0x', * v: 2709, * r: '0x8b336c290f6d7b2af3ccb2c02203a8356cc7d5b150ab19cce549d55636a3a78c', * s: '0x5a83c6f816befc5cd4b0c997a347224a8aa002e5799c4b082a3ec726d0e9531d' * } * ``` */ getTransactionFromBlock() { return __awaiter(this, arguments, void 0, function* (block = this.defaultBlock, transactionIndex, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getTransactionFromBlock(this, block, transactionIndex, returnFormat); }); } /** * @param transactionHash Hash of the transaction to retrieve the receipt for. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The desired {@link TransactionReceipt} object. * * ```ts * web3.eth.getTransactionReceipt("0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f").then(console.log); * > { * transactionHash: '0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f', * transactionIndex: 0n, * blockNumber: 2n, * blockHash: '0xeb1565a08b23429552dafa92e32409f42eb43944f7611963c63ce40e7243941a', * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * cumulativeGasUsed: 21000n, * gasUsed: 21000n, * logs: [], * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * status: 1n, * effectiveGasPrice: 2000000000n, * type: 0n * } * * web3.eth.getTransactionReceipt( * "0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f", * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > { * transactionHash: '0xdf7756865c2056ce34c4eabe4eff42ad251a9f920a1c620c00b4ea0988731d3f', * transactionIndex: 0, * blockNumber: 2, * blockHash: '0xeb1565a08b23429552dafa92e32409f42eb43944f7611963c63ce40e7243941a', * from: '0x6e599da0bff7a6598ac1224e4985430bf16458a4', * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * cumulativeGasUsed: 21000, * gasUsed: 21000, * logs: [], * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * status: 1, * effectiveGasPrice: 2000000000, * type: 0n * } * ``` */ getTransactionReceipt(transactionHash_1) { return __awaiter(this, arguments, void 0, function* (transactionHash, returnFormat = this.defaultReturnFormat) { const response = yield rpcMethodsWrappers.getTransactionReceipt(this, transactionHash, returnFormat); if (!response) throw new web3_errors_1.TransactionNotFound(); return response; }); } /** * @param address The address to get the number of transactions for. * @param blockNumber ({@link BlockNumberOrTag} defaults to {@link Web3Eth.defaultBlock}) Specifies what block to use as the current state for the query. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @returns The number of transactions sent from the provided address. * * ```ts * web3.eth.getTransactionCount("0x407d73d8a49eeb85d32cf465507dd71d507100c1").then(console.log); * > 1n * * web3.eth.getTransactionCount( * "0x407d73d8a49eeb85d32cf465507dd71d507100c1", * undefined, * { number: FMT_NUMBER.NUMBER , bytes: FMT_BYTES.HEX } * ).then(console.log); * > 1 * ``` */ getTransactionCount(address_1) { return __awaiter(this, arguments, void 0, function* (address, blockNumber = this.defaultBlock, returnFormat = this.defaultReturnFormat) { return rpcMethodsWrappers.getTransactionCount(this, address, blockNumber, returnFormat); }); } /** * @param transaction The {@link Transaction}, {@link TransactionWithFromLocalWalletIndex}, {@link TransactionWithToLocalWalletIndex} or {@link TransactionWithFromAndToLocalWalletIndex} to send. __Note:__ In the `to` and `from` fields when hex strings are used, it is assumed they are addresses, for any other form (number, string number, etc.) it is assumed they are wallet indexes. * @param returnFormat ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) Specifies how the return data should be formatted. * @param options A configuration object used to change the behavior of the `sendTransaction` method. * @returns If `await`ed or `.then`d (i.e. the promise resolves), the transaction hash is returned. * ```ts * const transaction = { * from: '0x6E599DA0bfF7A6598AC1224E4985430Bf16458a4', * to: '0x6f1DF96865D09d21e8f3f9a7fbA3b17A11c7C53C', * value: '0x1' * } * * const transactionReceipt = await web3.eth.sendTransaction(transaction); * console.log(transactionReceipt); * > { * blockHash: '0x39cee0da843293ae3136cee0de4c0803745868b6e72b7cd05fba395bffa0ee85', * blockNumber: 6659547n, * cumulativeGasUsed: 1029036n, * effectiveGasPrice: 6765796845n, * from: '0x6E599DA0bfF7A6598AC1224E4985430Bf16458a4', * gasUsed: 21000n, * logs: [], * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * status: 1n, * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * transactionHash: '0x619de868dd73c07bd0c096adcd405c93c1e924fdf741e684a127a52324c28bb9', * transactionIndex: 16n, * type: 2n *} * * web3.eth.sendTransaction(transaction).then(console.log); * > { * blockHash: '0x39cee0da843293ae3136cee0de4c0803745868b6e72b7cd05fba395bffa0ee85', * blockNumber: 6659547n, * cumulativeGasUsed: 1029036n, * effectiveGasPrice: 6765796845n, * from: '0x6E599DA0bfF7A6598AC1224E4985430Bf16458a4', * gasUsed: 21000n, * logs: [], * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * status: 1n, * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * transactionHash: '0x619de868dd73c07bd0c096adcd405c93c1e924fdf741e684a127a52324c28bb9', * transactionIndex: 16n, * type: 2n *} * * web3.eth.sendTransaction(transaction).catch(console.log); * > <Some TransactionError> * * // Example using options.ignoreGasPricing = true * web3.eth.sendTransaction(transaction, undefined, { ignoreGasPricing: true }).then(console.log); * > { * blockHash: '0x39cee0da843293ae3136cee0de4c0803745868b6e72b7cd05fba395bffa0ee85', * blockNumber: 6659547n, * cumulativeGasUsed: 1029036n, * effectiveGasPrice: 6765796845n, * from: '0x6E599DA0bfF7A6598AC1224E4985430Bf16458a4', * gasUsed: 21000n, * logs: [], * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', * status: 1n, * to: '0x6f1df96865d09d21e8f3f9a7fba3b17a11c7c53c', * transactionHash: '0x619de868dd73c07bd0c096adcd405c93c1e924fdf741e684a127a52324c28bb9', * transactionIndex: 16n, * type: 2n *} * ``` * *