Ganache Ethereum JSON-RPC documentation.
bzz_hive(): Promise<any[]>
Returns the kademlia table in a readable table format.
Promise<any[]>
: Returns the kademlia table in a readable table format.
console.log(await provider.send("bzz_hive"));
bzz_info(): Promise<any[]>
Returns details about the swarm node.
Promise<any[]>
: Returns details about the swarm node.
console.log(await provider.send("bzz_info"));
db_getHex(dbName: string, key: string): Promise<string>
Returns binary data from the local database.
dbName: string
: Database name.key: string
: Key name.
Promise<string>
: The previously stored data.
console.log(await provider.send("db_getHex", ["testDb", "testKey"] ));
db_getString(dbName: string, key: string): Promise<string>
Returns string from the local database.
dbName: string
: Database name.key: string
: Key name.
Promise<string>
: The previously stored string.
console.log(await provider.send("db_getString", ["testDb", "testKey"] ));
db_putHex(dbName: string, key: string, data: DATA): Promise<boolean>
Stores binary data in the local database.
dbName: string
: Database name.key: string
: Key name.data: DATA
: Data to store.
Promise<boolean>
: true if the value was stored, otherwise false.
console.log(await provider.send("db_putHex", ["testDb", "testKey", "0x0"] ));
db_putString(dbName: string, key: string, value: string): Promise<boolean>
Stores a string in the local database.
dbName: string
: Database name.key: string
: Key name.value: string
: String to store.
Promise<boolean>
: returns true if the value was stored, otherwise false.
console.log(await provider.send("db_putString", ["testDb", "testKey", "testValue"] ));
debug_storageRangeAt(blockHash: DATA, transactionIndex: number, contractAddress: DATA, startKey: DATA, maxResult: number): Promise<StorageRangeResult>
Attempts to replay the transaction as it was executed on the network and return storage data given a starting key and max number of entries to return.
blockHash: DATA
: Hash of a block.transactionIndex: number
: Integer of the transaction index position.contractAddress: DATA
: Address of the contract.startKey: DATA
: Hash of the start key for grabbing storage entries.maxResult: number
: Integer of maximum number of storage entries to return.
Promise<StorageRangeResult>
: Returns a storage object with the keys being keccak-256 hashes of the storage keys, and the values being the raw, unhashed key and value for that specific storage slot. Also returns a next key which is the keccak-256 hash of the next key in storage for continuous downloading.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const initialTxHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const {contractAddress} = await provider.request({ method: "eth_getTransactionReceipt", params: [initialTxHash] });
// set value to 19
const data = "0x552410770000000000000000000000000000000000000000000000000000000000000019";
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to: contractAddress, data }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const { blockHash, transactionIndex } = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const storage = await provider.request({ method: "debug_storageRangeAt", params: [blockHash, transactionIndex, contractAddress, "0x01", 1] });
console.log(storage);
debug_traceTransaction(transactionHash: DATA, options?: TransactionTraceOptions): Promise<TraceTransactionResult>
Attempt to run the transaction in the exact same manner as it was executed on the network. It will replay any transaction that may have been executed prior to this one before it will finally attempt to execute the transaction that corresponds to the given hash.
In addition to the hash of the transaction you may give it a secondary optional argument, which specifies the options for this specific call. The possible options are:
disableStorage
: {boolean} Setting this totrue
will disable storage capture (default =false
).disableMemory
: {boolean} Setting this totrue
will disable memory capture (default =false
).disableStack
: {boolean} Setting this totrue
will disable stack capture (default =false
).
transactionHash: DATA
: Hash of the transaction to trace.options?: TransactionTraceOptions
: See options in source.
Promise<TraceTransactionResult>
: Returns the gas
, structLogs
, and returnValue
for the traced transaction.
The structLogs
are an array of logs, which contains the following fields:
depth
: The execution depth.error
: Information about an error, if one occurred.gas
: The number of gas remaining.gasCost
: The cost of gas in wei.memory
: An array containing the contract's memory data.op
: The current opcode.pc
: The current program counter.stack
: The EVM execution stack.storage
: An object containing the contract's storage data.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const transactionTrace = await provider.request({ method: "debug_traceTransaction", params: [txHash] });
console.log(transactionTrace);
eth_accounts(): Promise<string[]>
Returns a list of addresses owned by client.
Promise<string[]>
: Array of 20 Bytes - addresses owned by the client.
const accounts = await provider.request({ method: "eth_accounts", params: [] });
console.log(accounts);
eth_blockNumber(): Promise<QUANTITY>
Returns the number of the most recent block.
Promise<QUANTITY>
: The current block number the client is on.
const blockNumber = await provider.request({ method: "eth_blockNumber" });
console.log(blockNumber);
eth_call(transaction: any, blockNumber: QUANTITY | TAG): Promise<DATA>
Executes a new message call immediately without creating a transaction on the block chain.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: any
: The transaction call object as seen in source.blockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<DATA>
: The return value of executed contract.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
const txObj = { from, gas: "0x5b8d80", gasPrice: "0x1dfd14000", value:"0x0", data: simpleSol };
const result = await provider.request({ method: "eth_call", params: [txObj, "latest"] });
console.log(result);
eth_chainId(): Promise<QUANTITY>
Returns the currently configured chain id, a value used in replay-protected transaction signing as introduced by EIP-155.
Promise<QUANTITY>
: The chain id as a string.
const chainId = await provider.send("eth_chainId");
console.log(chainId);
eth_coinbase(): Promise<Address>
Returns the client coinbase address.
Promise<Address>
: The current coinbase address.
const coinbaseAddress = await provider.request({ method: "eth_coinbase" });
console.log(coinbaseAddress);
eth_estimateGas(transaction: TypedRpcTransaction, blockNumber: QUANTITY | TAG): Promise<QUANTITY>
Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain. Note that the estimate may be significantly more than the amount of gas actually used by the transaction, for a variety of reasons including EVM mechanics and node performance.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: TypedRpcTransaction
: The transaction call object as seen in source.blockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<QUANTITY>
: The amount of gas used.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
const gasEstimate = await provider.request({ method: "eth_estimateGas", params: [{ from, to }, "latest" ] });
console.log(gasEstimate);
eth_gasPrice(): Promise<QUANTITY>
Returns the current price per gas in wei.
Promise<QUANTITY>
: Integer of the current gas price in wei.
const gasPrice = await provider.request({ method: "eth_gasPrice", params: [] });
console.log(gasPrice);
eth_getBalance(address: DATA, blockNumber: QUANTITY | TAG): Promise<QUANTITY>
Returns the balance of the account of given address.
address: DATA
: Address to check for balance.blockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<QUANTITY>
: Integer of the account balance in wei.
const accounts = await provider.request({ method: "eth_accounts", params: [] });
const balance = await provider.request({ method: "eth_getBalance", params: [accounts[0], "latest"] });
console.log(balance);
eth_getBlockByHash(hash: DATA, transactions: boolean): Promise<object>
Returns information about a block by block hash.
hash: DATA
: Hash of a block.transactions: boolean
: Iftrue
it returns the full transaction objects, iffalse
only the hashes of the transactions.
Promise<object>
: The block, null
if the block doesn't exist.
hash
:DATA
, 32 Bytes - Hash of the block.null
when pending.parentHash
:DATA
, 32 Bytes - Hash of the parent block.sha3Uncles
:DATA
, 32 Bytes - SHA3 of the uncles data in the block.miner
:DATA
, 20 Bytes - Address of the miner.stateRoot
:DATA
, 32 Bytes - The root of the state trie of the block.transactionsRoot
:DATA
, 32 Bytes - The root of the transaction trie of the block.receiptsRoot
:DATA
, 32 Bytes - The root of the receipts trie of the block.logsBloom
:DATA
, 256 Bytes - The bloom filter for the logs of the block.null
when pending.difficulty
:QUANTITY
- Integer of the difficulty of this block.number
:QUANTITY
- The block number.null
when pending.gasLimit
:QUANTITY
- The maximum gas allowed in the block.gasUsed
:QUANTITY
- Total gas used by all transactions in the block.timestamp
:QUANTITY
- The unix timestamp for when the block was collated.extraData
:DATA
- Extra data for the block.mixHash
:DATA
, 256 Bytes - Hash identifier for the block.nonce
:DATA
, 8 Bytes - Hash of the generated proof-of-work.null
when pending.totalDifficulty
:QUANTITY
- Integer of the total difficulty of the chain until this block.size
:QUANTITY
- Integer the size of the block in bytes.transactions
:Array
- Array of transaction objects or 32 Bytes transaction hashes depending on the last parameter.uncles
:Array
- Array of uncle hashes.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txReceipt = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const block = await provider.request({ method: "eth_getBlockByHash", params: [txReceipt.blockHash, true] });
console.log(block);
eth_getBlockByNumber(number: QUANTITY | TAG, transactions: boolean): Promise<object>
Returns information about a block by block number.
number: QUANTITY | TAG
: Integer of a block number, or the string "earliest", "latest" or "pending", as in the default block parameter.transactions: boolean
: Iftrue
it returns the full transaction objects, iffalse
only the hashes of the transactions.
Promise<object>
: The block, null
if the block doesn't exist.
hash
:DATA
, 32 Bytes - Hash of the block.null
when pending.parentHash
:DATA
, 32 Bytes - Hash of the parent block.sha3Uncles
:DATA
, 32 Bytes - SHA3 of the uncles data in the block.miner
:DATA
, 20 Bytes - Address of the miner.stateRoot
:DATA
, 32 Bytes - The root of the state trie of the block.transactionsRoot
:DATA
, 32 Bytes - The root of the transaction trie of the block.receiptsRoot
:DATA
, 32 Bytes - The root of the receipts trie of the block.logsBloom
:DATA
, 256 Bytes - The bloom filter for the logs of the block.null
when pending.difficulty
:QUANTITY
- Integer of the difficulty of this block.number
:QUANTITY
- The block number.null
when pending.gasLimit
:QUANTITY
- The maximum gas allowed in the block.gasUsed
:QUANTITY
- Total gas used by all transactions in the block.timestamp
:QUANTITY
- The unix timestamp for when the block was collated.extraData
:DATA
- Extra data for the block.mixHash
:DATA
, 256 Bytes - Hash identifier for the block.nonce
:DATA
, 8 Bytes - Hash of the generated proof-of-work.null
when pending.totalDifficulty
:QUANTITY
- Integer of the total difficulty of the chain until this block.size
:QUANTITY
- Integer the size of the block in bytes.transactions
:Array
- Array of transaction objects or 32 Bytes transaction hashes depending on the last parameter.uncles
:Array
- Array of uncle hashes.
const block = await provider.request({ method: "eth_getBlockByNumber", params: ["0x0", false] });
console.log(block);
eth_getBlockTransactionCountByHash(hash: DATA): Promise<QUANTITY>
Returns the number of transactions in a block from a block matching the given block hash.
hash: DATA
: Hash of a block.
Promise<QUANTITY>
: Number of transactions in the block.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txReceipt = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const txCount = await provider.request({ method: "eth_getBlockTransactionCountByHash", params: [txReceipt.blockHash] });
console.log(txCount);
eth_getBlockTransactionCountByNumber(blockNumber: QUANTITY | TAG): Promise<QUANTITY>
Returns the number of transactions in a block from a block matching the given block number.
blockNumber: QUANTITY | TAG
Promise<QUANTITY>
: Integer of the number of transactions in the block.
const txCount = await provider.request({ method: "eth_getBlockTransactionCountByNumber", params: ["0x0"] });
console.log(txCount);
eth_getCode(address: DATA, blockNumber: QUANTITY | TAG): Promise<DATA>
Returns code at a given address.
address: DATA
: Address.blockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<DATA>
: The code from the given address.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txReceipt = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const code = await provider.request({ method: "eth_getCode", params: [txReceipt.contractAddress, "latest"] });
console.log(code);
eth_getCompilers(): Promise<string[]>
Returns a list of available compilers.
Promise<string[]>
: List of available compilers.
const compilers = await provider.send("eth_getCompilers");
console.log(compilers);
eth_getFilterChanges(filterId: QUANTITY): Promise<DATA[]>
Polling method for a filter, which returns an array of logs, block hashes, or transaction hashes, depending on the filter type, which occurred since last poll.
filterId: QUANTITY
: The filter id.
Promise<DATA[]>
: An array of logs, block hashes, or transaction hashes, depending on the filter type, which occurred since last poll.
For filters created with eth_newBlockFilter
the return are block hashes (DATA
, 32 Bytes).
For filters created with eth_newPendingTransactionFilter
the return are transaction hashes (DATA
, 32 Bytes).
For filters created with eth_newFilter
the return are log objects with the following parameters:
removed
:TAG
-true
when the log was removed,false
if its a valid log.logIndex
:QUANTITY
- Integer of the log index position in the block.null
when pending.transactionIndex
:QUANTITY
- Integer of the transactions index position.null
when pending.transactionHash
:DATA
, 32 Bytes - Hash of the transaction where the log was.null
when pending.blockHash
:DATA
, 32 Bytes - Hash of the block where the log was.null
when pending.blockNumber
:QUANTITY
- The block number where the log was in.null
when pending.address
:DATA
, 20 Bytes - The address from which the log originated.data
:DATA
- Contains one or more 32 Bytes non-indexed arguments of the log.topics
:Array of DATA
- Array of 0 to 4 32 BytesDATA
of indexed log arguments.
// Logs.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
// contract Logs {
// event Event(uint256 indexed first, uint256 indexed second);
// constructor() {
// emit Event(1, 2);
// }
//
// function logNTimes(uint8 n) public {
// for (uint8 i = 0; i < n; i++) {
// emit Event(i, i);
// }
// }
// }
const logsContract = "0x608060405234801561001057600080fd5b50600260017f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a360e58061004f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80635e19e69f14602d575b600080fd5b605960048036036020811015604157600080fd5b81019080803560ff169060200190929190505050605b565b005b60005b8160ff168160ff16101560ab578060ff168160ff167f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a38080600101915050605e565b505056fea26469706673582212201af9c13c7b00e2b628c1258d45f9f62d2aad8cd32fc32fd9515d8ad1e792679064736f6c63430007040033";
const [from] = await provider.send("eth_accounts");
const filterId = await provider.send("eth_newFilter");
const subscriptionId = await provider.send("eth_subscribe", ["newHeads"]);
await provider.send("eth_sendTransaction", [{ from, data: logsContract, gas: "0x5b8d80" }] );
await provider.once("message");
const changes = await provider.request({ method: "eth_getFilterChanges", params: [filterId] });
console.log(changes);
await provider.send("eth_unsubscribe", [subscriptionId]);
eth_getFilterLogs(filterId: QUANTITY): Promise<object[]>
Returns an array of all logs matching filter with given id.
filterId: QUANTITY
: The filter id.
Promise<object[]>
: Array of log objects, or an empty array.
// Logs.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
// contract Logs {
// event Event(uint256 indexed first, uint256 indexed second);
// constructor() {
// emit Event(1, 2);
// }
//
// function logNTimes(uint8 n) public {
// for (uint8 i = 0; i < n; i++) {
// emit Event(i, i);
// }
// }
// }
const logsContract = "0x608060405234801561001057600080fd5b50600260017f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a360e58061004f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80635e19e69f14602d575b600080fd5b605960048036036020811015604157600080fd5b81019080803560ff169060200190929190505050605b565b005b60005b8160ff168160ff16101560ab578060ff168160ff167f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a38080600101915050605e565b505056fea26469706673582212201af9c13c7b00e2b628c1258d45f9f62d2aad8cd32fc32fd9515d8ad1e792679064736f6c63430007040033";
const [from] = await provider.send("eth_accounts");
const filterId = await provider.send("eth_newFilter");
await provider.send("eth_subscribe", ["newHeads"]);
await provider.send("eth_sendTransaction", [{ from, data: logsContract, gas: "0x5b8d80" }] );
await provider.once("message");
const logs = await provider.request({ method: "eth_getFilterLogs", params: [filterId] });
console.log(logs);
eth_getLogs(filter: FilterArgs): Promise<object[]>
Returns an array of all logs matching a given filter object.
Filter options:
fromBlock
:QUANTITY | TAG
(optional) - Integer block number, or the string "latest", "earliest" or "pending".toBlock
:QUANTITY | TAG
(optional) - Integer block number, or the string "latest", "earliest" or "pending".address
:DATA | Array
(optional) - Contract address or a list of addresses from which the logs should originate.topics
:Array of DATA
(optional) - Array of 32 BytesDATA
topcis. Topics are order-dependent. Each topic can also be an array ofDATA
with "or" options.blockHash
:DATA
, 32 Bytes (optional) - Hash of the block to restrict logs from. IfblockHash
is present, then neitherfromBlock
ortoBlock
are allowed.
filter: FilterArgs
: The filter options as seen in source.
Promise<object[]>
: Array of log objects, or an empty array.
// Logs.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
// contract Logs {
// event Event(uint256 indexed first, uint256 indexed second);
// constructor() {
// emit Event(1, 2);
// }
//
// function logNTimes(uint8 n) public {
// for (uint8 i = 0; i < n; i++) {
// emit Event(i, i);
// }
// }
// }
const logsContract = "0x608060405234801561001057600080fd5b50600260017f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a360e58061004f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80635e19e69f14602d575b600080fd5b605960048036036020811015604157600080fd5b81019080803560ff169060200190929190505050605b565b005b60005b8160ff168160ff16101560ab578060ff168160ff167f34e802e5ebd1f132e05852c5064046c1b535831ec52f1c4997fc6fdc4d5345b360405160405180910390a38080600101915050605e565b505056fea26469706673582212201af9c13c7b00e2b628c1258d45f9f62d2aad8cd32fc32fd9515d8ad1e792679064736f6c63430007040033";
const [from] = await provider.send("eth_accounts");
await provider.send("eth_subscribe", ["newHeads"]);
const txHash = await provider.send("eth_sendTransaction", [{ from, data: logsContract, gas: "0x5b8d80" }] );
await provider.once("message");
const { contractAddress } = await provider.send("eth_getTransactionReceipt", [txHash] );
const logs = await provider.request({ method: "eth_getLogs", params: [{ address: contractAddress }] });
console.log(logs);
eth_getStorageAt(address: DATA, position: QUANTITY, blockNumber: QUANTITY | TAG): Promise<DATA>
Returns the value from a storage position at a given address.
address: DATA
: Address of the storage.position: QUANTITY
: Integer of the position in the storage.blockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<DATA>
: The value in storage at the requested position.
// Simple.sol
// // SPDX-License-Identifier: MIT
// pragma solidity ^0.7.4;
//
// contract Simple {
// uint256 public value;
// constructor() payable {
// value = 5;
// }
// }
const simpleSol = "0x6080604052600560008190555060858060196000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea26469706673582212200897f7766689bf7a145227297912838b19bcad29039258a293be78e3bf58e20264736f6c63430007040033";
const [from] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, gas: "0x5b8d80", data: simpleSol }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txReceipt = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const storageValue = await provider.request({ method: "eth_getStorageAt", params: [txReceipt.contractAddress, "0x0", "latest"] });
console.log(storageValue);
eth_getTransactionByBlockHashAndIndex(hash: DATA, index: QUANTITY): Promise<object>
Returns information about a transaction by block hash and transaction index position.
hash: DATA
: Hash of a block.index: QUANTITY
: Integer of the transaction index position.
Promise<object>
: The transaction object or null
if no transaction was found.
hash
:DATA
, 32 Bytes - The transaction hash.nonce
:QUANTITY
- The number of transactions made by the sender prior to this one.blockHash
:DATA
, 32 Bytes - The hash of the block the transaction is in.null
when pending.blockNumber
:QUANTITY
- The number of the block the transaction is in.null
when pending.transactionIndex
:QUANTITY
- The index position of the transaction in the block.from
:DATA
, 20 Bytes - The address the transaction is sent from.to
:DATA
, 20 Bytes - The address the transaction is sent to.value
:QUANTITY
- The value transferred in wei.gas
:QUANTITY
- The gas provided by the sender.gasPrice
:QUANTITY
- The price of gas in wei.input
:DATA
- The data sent along with the transaction.v
:QUANTITY
- ECDSA recovery id.r
:DATA
, 32 Bytes - ECDSA signature r.s
:DATA
, 32 Bytes - ECDSA signature s.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const { blockHash, transactionIndex } = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const tx = await provider.request({ method: "eth_getTransactionByBlockHashAndIndex", params: [ blockHash, transactionIndex ] });
console.log(tx);
eth_getTransactionByBlockNumberAndIndex(number: QUANTITY | TAG, index: QUANTITY): Promise<object>
Returns information about a transaction by block number and transaction index position.
number: QUANTITY | TAG
: A block number, or the string "earliest", "latest" or "pending".index: QUANTITY
: Integer of the transaction index position.
Promise<object>
: The transaction object or null
if no transaction was found.
hash
:DATA
, 32 Bytes - The transaction hash.nonce
:QUANTITY
- The number of transactions made by the sender prior to this one.blockHash
:DATA
, 32 Bytes - The hash of the block the transaction is in.null
when pending.blockNumber
:QUANTITY
- The number of the block the transaction is in.null
when pending.transactionIndex
:QUANTITY
- The index position of the transaction in the block.from
:DATA
, 20 Bytes - The address the transaction is sent from.to
:DATA
, 20 Bytes - The address the transaction is sent to.value
:QUANTITY
- The value transferred in wei.gas
:QUANTITY
- The gas provided by the sender.gasPrice
:QUANTITY
- The price of gas in wei.input
:DATA
- The data sent along with the transaction.v
:QUANTITY
- ECDSA recovery id.r
:DATA
, 32 Bytes - ECDSA signature r.s
:DATA
, 32 Bytes - ECDSA signature s.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const { transactionIndex } = await provider.request({ method: "eth_getTransactionReceipt", params: [txHash] });
const tx = await provider.request({ method: "eth_getTransactionByBlockNumberAndIndex", params: [ "latest", transactionIndex ] });
console.log(tx);
eth_getTransactionByHash(transactionHash: DATA): Promise<object>
Returns the information about a transaction requested by transaction hash.
transactionHash: DATA
: Hash of a transaction.
Promise<object>
: The transaction object or null
if no transaction was found.
hash
:DATA
, 32 Bytes - The transaction hash.nonce
:QUANTITY
- The number of transactions made by the sender prior to this one.blockHash
:DATA
, 32 Bytes - The hash of the block the transaction is in.null
when pending.blockNumber
:QUANTITY
- The number of the block the transaction is in.null
when pending.transactionIndex
:QUANTITY
- The index position of the transaction in the block.from
:DATA
, 20 Bytes - The address the transaction is sent from.to
:DATA
, 20 Bytes - The address the transaction is sent to.value
:QUANTITY
- The value transferred in wei.gas
:QUANTITY
- The gas provided by the sender.gasPrice
:QUANTITY
- The price of gas in wei.input
:DATA
- The data sent along with the transaction.v
:QUANTITY
- ECDSA recovery id.r
:DATA
, 32 Bytes - ECDSA signature r.s
:DATA
, 32 Bytes - ECDSA signature s.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const tx = await provider.request({ method: "eth_getTransactionByHash", params: [ txHash ] });
console.log(tx);
eth_getTransactionCount(address: DATA, blockNumber: QUANTITY | TAG): Promise<QUANTITY>
Returns the number of transactions sent from an address.
address: DATA
:DATA
, 20 Bytes - The address to get number of transactions sent fromblockNumber: QUANTITY | TAG
: Integer block number, or the string "latest", "earliest" or "pending".
Promise<QUANTITY>
: Number of transactions sent from this address.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txCount = await provider.request({ method: "eth_getTransactionCount", params: [ from, "latest" ] });
console.log(txCount);
eth_getTransactionReceipt(transactionHash: DATA): Promise<TransactionReceiptJSON>
Returns the receipt of a transaction by transaction hash.
Note: The receipt is not available for pending transactions.
transactionHash: DATA
: Hash of a transaction.
Promise<TransactionReceiptJSON>
: Returns the receipt of a transaction by transaction hash.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
const txReceipt = await provider.request({ method: "eth_getTransactionReceipt", params: [ txHash ] });
console.log(txReceipt);
eth_getUncleByBlockHashAndIndex(hash: DATA, index: QUANTITY): Promise<object>
Returns information about a uncle of a block by hash and uncle index position.
hash: DATA
: Hash of a block.index: QUANTITY
: The uncle's index position.
Promise<object>
: A block object or null
when no block is found.
hash
:DATA
, 32 Bytes - Hash of the block.null
when pending.parentHash
:DATA
, 32 Bytes - Hash of the parent block.sha3Uncles
:DATA
, 32 Bytes - SHA3 of the uncles data in the block.miner
:DATA
, 20 Bytes - Address of the miner.stateRoot
:DATA
, 32 Bytes - The root of the state trie of the block.transactionsRoot
:DATA
, 32 Bytes - The root of the transaction trie of the block.receiptsRoot
:DATA
, 32 Bytes - The root of the receipts trie of the block.logsBloom
:DATA
, 256 Bytes - The bloom filter for the logs of the block.null
when pending.difficulty
:QUANTITY
- Integer of the difficulty of this block.number
:QUANTITY
- The block number.null
when pending.gasLimit
:QUANTITY
- The maximum gas allowed in the block.gasUsed
:QUANTITY
- Total gas used by all transactions in the block.timestamp
:QUANTITY
- The unix timestamp for when the block was collated.extraData
:DATA
- Extra data for the block.mixHash
:DATA
, 256 Bytes - Hash identifier for the block.nonce
:DATA
, 8 Bytes - Hash of the generated proof-of-work.null
when pending.totalDifficulty
:QUANTITY
- Integer of the total difficulty of the chain until this block.size
:QUANTITY
- Integer the size of the block in bytes.transactions
:Array
- Array of transaction objects or 32 Bytes transaction hashes depending on the last parameter.uncles
:Array
- Array of uncle hashes.
const blockHash = await provider.send("eth_getBlockByNumber", ["latest"] );
const block = await provider.send("eth_getUncleByBlockHashAndIndex", [blockHash, "0x0"] );
console.log(block);
eth_getUncleByBlockNumberAndIndex(blockNumber: QUANTITY | TAG, uncleIndex: QUANTITY): Promise<object>
Returns information about a uncle of a block by hash and uncle index position.
blockNumber: QUANTITY | TAG
: A block number, or the string "earliest", "latest" or "pending".uncleIndex: QUANTITY
: The uncle's index position.
Promise<object>
: A block object or null
when no block is found.
hash
:DATA
, 32 Bytes - Hash of the block.null
when pending.parentHash
:DATA
, 32 Bytes - Hash of the parent block.sha3Uncles
:DATA
, 32 Bytes - SHA3 of the uncles data in the block.miner
:DATA
, 20 Bytes - Address of the miner.stateRoot
:DATA
, 32 Bytes - The root of the state trie of the block.transactionsRoot
:DATA
, 32 Bytes - The root of the transaction trie of the block.receiptsRoot
:DATA
, 32 Bytes - The root of the receipts trie of the block.logsBloom
:DATA
, 256 Bytes - The bloom filter for the logs of the block.null
when pending.difficulty
:QUANTITY
- Integer of the difficulty of this block.number
:QUANTITY
- The block number.null
when pending.gasLimit
:QUANTITY
- The maximum gas allowed in the block.gasUsed
:QUANTITY
- Total gas used by all transactions in the block.timestamp
:QUANTITY
- The unix timestamp for when the block was collated.extraData
:DATA
- Extra data for the block.mixHash
:DATA
, 256 Bytes - Hash identifier for the block.nonce
:DATA
, 8 Bytes - Hash of the generated proof-of-work.null
when pending.totalDifficulty
:QUANTITY
- Integer of the total difficulty of the chain until this block.size
:QUANTITY
- Integer the size of the block in bytes.transactions
:Array
- Array of transaction objects or 32 Bytes transaction hashes depending on the last parameter.uncles
:Array
- Array of uncle hashes.
const block = await provider.send("eth_getUncleByBlockNumberAndIndex", ["latest", "0x0"] );
console.log(block);
eth_getUncleCountByBlockHash(hash: DATA): Promise<QUANTITY>
Returns the number of uncles in a block from a block matching the given block hash.
hash: DATA
: Hash of a block.
Promise<QUANTITY>
: The number of uncles in a block.
const blockHash = await provider.send("eth_getBlockByNumber", ["latest"] );
const uncleCount = await provider.send("eth_getUncleCountByBlockHash", [blockHash] );
console.log(uncleCount);
eth_getUncleCountByBlockNumber(blockNumber: QUANTITY | TAG): Promise<QUANTITY>
Returns the number of uncles in a block from a block matching the given block hash.
blockNumber: QUANTITY | TAG
: A block number, or the string "earliest", "latest" or "pending".
Promise<QUANTITY>
: The number of uncles in a block.
const uncleCount = await provider.send("eth_getUncleCountByBlockNumber", ["latest"] );
console.log(uncleCount);
eth_getWork(filterId: QUANTITY): Promise<[] | [string, string, string]>
Returns: An Array with the following elements
1: DATA
, 32 Bytes - current block header pow-hash
2: DATA
, 32 Bytes - the seed hash used for the DAG.
3: DATA
, 32 Bytes - the boundary condition ("target"), 2^256 / difficulty.
filterId: QUANTITY
: A filter id.
Promise<[] | [string, string, string]>
: The hash of the current block, the seedHash, and the boundary condition to be met ("target").
console.log(await provider.send("eth_getWork", ["0x0"] ));
eth_hashrate(): Promise<QUANTITY>
Returns the number of hashes per second that the node is mining with.
Promise<QUANTITY>
: Number of hashes per second.
const hashrate = await provider.request({ method: "eth_hashrate", params: [] });
console.log(hashrate);
eth_mining(): Promise<boolean>
Returns true
if client is actively mining new blocks.
Promise<boolean>
: returns true
if the client is mining, otherwise false
.
const isMining = await provider.request({ method: "eth_mining", params: [] });
console.log(isMining);
eth_newBlockFilter(): Promise<QUANTITY>
Creates a filter in the node, to notify when a new block arrives. To check
if the state has changed, call eth_getFilterChanges
.
Promise<QUANTITY>
: A filter id.
const filterId = await provider.request({ method: "eth_newBlockFilter", params: [] });
console.log(filterId);
eth_newFilter(filter?: RangeFilterArgs): Promise<QUANTITY>
Creates a filter object, based on filter options, to notify when the state
changes (logs). To check if the state has changed, call
eth_getFilterChanges
.
If the from fromBlock
or toBlock
option are equal to "latest" the
filter continually append logs for whatever block is seen as latest at the
time the block was mined, not just for the block that was "latest" when the
filter was created.
A note on specifying topic filters:
Topics are order-dependent. A transaction with a log with topics [A, B] will be matched by the following topic filters:
[]
“anything”[A]
“A in first position (and anything after)”[null, B]
“anything in first position AND B in second position (and anything after)”[A, B]
“A in first position AND B in second position (and anything after)”[[A, B], [A, B]]
“(A OR B) in first position AND (A OR B) in second position (and anything after)”
Filter options:
fromBlock
:QUANTITY | TAG
(optional) - Integer block number, or the string "latest", "earliest" or "pending".toBlock
:QUANTITY | TAG
(optional) - Integer block number, or the string "latest", "earliest" or "pending".address
:DATA | Array
(optional) - Contract address or a list of addresses from which the logs should originate.topics
:Array of DATA
(optional) - Array of 32 BytesDATA
topcis. Topics are order-dependent. Each topic can also be an array ofDATA
with "or" options.
filter?: RangeFilterArgs
: The filter options as seen in source.
Promise<QUANTITY>
: A filter id.
const filterId = await provider.request({ method: "eth_newFilter", params: [] });
console.log(filterId);
eth_newPendingTransactionFilter(): Promise<QUANTITY>
Creates a filter in the node, to notify when new pending transactions
arrive. To check if the state has changed, call eth_getFilterChanges
.
Promise<QUANTITY>
: A filter id.
const filterId = await provider.request({ method: "eth_newPendingTransactionFilter", params: [] });
console.log(filterId);
eth_protocolVersion(): Promise<DATA>
Returns the current ethereum protocol version.
Promise<DATA>
: The current ethereum protocol version.
const version = await provider.request({ method: "eth_protocolVersion", params: [] });
console.log(version);
eth_sendRawTransaction(transaction: string): Promise<DATA>
Creates new message call transaction or a contract creation for signed transactions.
transaction: string
: The signed transaction data.
Promise<DATA>
: The transaction hash.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
const signedTx = await provider.request({ method: "eth_signTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
const txHash = await provider.send("eth_sendRawTransaction", [signedTx] );
console.log(txHash);
eth_sendTransaction(transaction: TypedRpcTransaction): Promise<DATA>
Creates new message call transaction or a contract creation, if the data field contains code.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: TypedRpcTransaction
: The transaction call object as seen in source.
Promise<DATA>
: The transaction hash.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const txHash = await provider.request({ method: "eth_sendTransaction", params: [{ from, to, gas: "0x5b8d80" }] });
await provider.once("message"); // Note: `await provider.once` is non-standard
console.log(txHash);
eth_sign(address: DATA, message: DATA): Promise<string>
The sign method calculates an Ethereum specific signature with:
sign(keccak256("\x19Ethereum Signed Message:\n" + message.length + message)))
.
By adding a prefix to the message makes the calculated signature recognizable as an Ethereum specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
Note the address to sign with must be unlocked.
address: DATA
: Address to sign with.message: DATA
: Message to sign.
Promise<string>
: Signature - a hex encoded 129 byte array
starting with 0x
. It encodes the r
, s
, and v
parameters from
appendix F of the yellow paper
in big-endian format. Bytes 0...64 contain the r
parameter, bytes
64...128 the s
parameter, and the last byte the v
parameter. Note
that the v
parameter includes the chain id as specified in EIP-155.
const [account] = await provider.request({ method: "eth_accounts", params: [] });
const msg = "0x307866666666666666666666";
const signature = await provider.request({ method: "eth_sign", params: [account, msg] });
console.log(signature);
eth_signTransaction(transaction: TypedRpcTransaction): Promise<string>
Signs a transaction that can be submitted to the network at a later time using eth_sendRawTransaction
.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: TypedRpcTransaction
: The transaction call object as seen in source.
Promise<string>
: The raw, signed transaction.
const [from, to] = await provider.request({ method: "eth_accounts", params: [] });
const signedTx = await provider.request({ method: "eth_signTransaction", params: [{ from, to }] });
console.log(signedTx)
eth_signTypedData(address: DATA, typedData: TypedData): Promise<string>
Identical to eth_signTypedData_v4.
address: DATA
: Address of the account that will sign the messages.typedData: TypedData
: Typed structured data to be signed.
Promise<string>
: Signature. As in eth_sign
, it is a hex encoded 129 byte array
starting with 0x
. It encodes the r
, s
, and v
parameters from
appendix F of the yellow paper
in big-endian format. Bytes 0...64 contain the r
parameter, bytes
64...128 the s
parameter, and the last byte the v
parameter. Note
that the v
parameter includes the chain id as specified in EIP-155.
const [account] = await provider.request({ method: "eth_accounts", params: [] });
const typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
],
},
primaryType: 'Mail',
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
};
const signature = await provider.request({ method: "eth_signTypedData", params: [account, typedData] });
console.log(signature);
eth_signTypedData_v4(address: DATA, typedData: TypedData): Promise<string>
address: DATA
: Address of the account that will sign the messages.typedData: TypedData
: Typed structured data to be signed.
Promise<string>
: Signature. As in eth_sign
, it is a hex encoded 129 byte array
starting with 0x
. It encodes the r
, s
, and v
parameters from
appendix F of the yellow paper
in big-endian format. Bytes 0...64 contain the r
parameter, bytes
64...128 the s
parameter, and the last byte the v
parameter. Note
that the v
parameter includes the chain id as specified in EIP-155.
const [account] = await provider.request({ method: "eth_accounts", params: [] });
const typedData = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' }
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' }
],
},
primaryType: 'Mail',
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
};
const signature = await provider.request({ method: "eth_signTypedData_v4", params: [account, typedData] });
console.log(signature);
eth_submitHashrate(hashRate: DATA, clientID: DATA): Promise<boolean>
Used for submitting mining hashrate.
hashRate: DATA
: A hexadecimal string representation (32 bytes) of the hash rate.clientID: DATA
: A random hexadecimal(32 bytes) ID identifying the client.
Promise<boolean>
: true
if submitting went through succesfully and false
otherwise.
const hashRate = "0x0000000000000000000000000000000000000000000000000000000000000001";
const clientId = "0xb2222a74119abd18dbcb7d1f661c6578b7bbeb4984c50e66ed538347f606b971";
const result = await provider.request({ method: "eth_submitHashrate", params: [hashRate, clientId] });
console.log(result);
eth_submitWork(nonce: DATA, powHash: DATA, digest: DATA): Promise<boolean>
Used for submitting a proof-of-work solution.
nonce: DATA
: The nonce found (64 bits).powHash: DATA
: The header's pow-hash (256 bits).digest: DATA
: The mix digest (256 bits).
Promise<boolean>
: true
if the provided solution is valid, otherwise false
.
const nonce = "0xe0df4bd14ab39a71";
const powHash = "0x0000000000000000000000000000000000000000000000000000000000000001";
const digest = "0xb2222a74119abd18dbcb7d1f661c6578b7bbeb4984c50e66ed538347f606b971";
const result = await provider.request({ method: "eth_submitWork", params: [nonce, powHash, digest] });
console.log(result);
eth_subscribe(subscriptionName: SubscriptionName): PromiEvent<QUANTITY>
Starts a subscription to a particular event. For every event that matches the subscription a JSON-RPC notification with event details and subscription ID will be sent to a client.
subscriptionName: SubscriptionName
: Name for the subscription.
PromiEvent<QUANTITY>
: A subscription id.
const subscriptionId = await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
console.log(subscriptionId);
eth_syncing(): Promise<boolean>
Returns an object containing data about the sync status or false
when not syncing.
Promise<boolean>
: An object with sync status data or false
, when not syncing.
startingBlock
: {bigint} The block at which the import started (will only be reset, after the sync reached his head).currentBlock
: {bigint} The current block, same aseth_blockNumber
.highestBlock
: {bigint} The estimated highest block.
const result = await provider.request({ method: "eth_syncing", params: [] });
console.log(result);
eth_uninstallFilter(filterId: QUANTITY): Promise<boolean>
Uninstalls a filter with given id. Should always be called when watch is no longer needed.
filterId: QUANTITY
: The filter id.
Promise<boolean>
: true
if the filter was successfully uninstalled, otherwise
false
.
const filterId = await provider.request({ method: "eth_newFilter", params: [] });
const result = await provider.request({ method: "eth_uninstallFilter", params: [filterId] });
console.log(result);
eth_unsubscribe(subscriptionId: SubscriptionId): Promise<boolean>
Cancel a subscription to a particular event. Returns a boolean indicating if the subscription was successfully cancelled.
subscriptionId: SubscriptionId
: The ID of the subscription to unsubscribe to.
Promise<boolean>
: true
if subscription was cancelled successfully, otherwise false
.
const subscriptionId = await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const result = await provider.request({ method: "eth_unsubscribe", params: [subscriptionId] });
console.log(result);
evm_increaseTime(seconds: number | QUANTITY): Promise<number>
Jump forward in time by the given amount of time, in seconds.
seconds: number | QUANTITY
: Number of seconds to jump forward in time by. Must be greater than or equal to0
.
Promise<number>
: Returns the total time adjustment, in seconds.
const seconds = 10;
const timeAdjustment = await provider.send("evm_increaseTime", [seconds] );
console.log(timeAdjustment);
evm_lockUnknownAccount(address: DATA): Promise<boolean>
Locks any unknown account.
Note: accounts known to the personal
namespace and accounts returned by
eth_accounts
cannot be locked using this method.
address: DATA
: The address of the account to lock.
Promise<boolean>
: true
if the account was locked successfully, false
if the
account was already locked. Throws an error if the account could not be
locked.
const address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
const result = await provider.send("evm_lockUnknownAccount", [address] );
console.log(result);
evm_mine(timestamp: number): Promise<"0x0">
Force a single block to be mined.
Mines a block independent of whether or not mining is started or stopped. Will mine an empty block if there are no available transactions to mine.
timestamp: number
: the timestamp the block should be mined with. EXPERIMENTAL: Optionally, specify anoptions
object withtimestamp
and/orblocks
fields. Ifblocks
is given, it will mine exactlyblocks
number of blocks, regardless of any other blocks mined or reverted during it's operation. This behavior is subject to change!
Promise<"0x0">
: The string "0x0"
. May return additional meta-data in the future.
console.log("start", await provider.send("eth_blockNumber"));
await provider.send("evm_mine", [{blocks: 5}] ); // mines 5 blocks
console.log("end", await provider.send("eth_blockNumber"));
evm_revert(snapshotId: QUANTITY): Promise<boolean>
Revert the state of the blockchain to a previous snapshot. Takes a single parameter, which is the snapshot id to revert to. This deletes the given snapshot, as well as any snapshots taken after (e.g.: reverting to id 0x1 will delete snapshots with ids 0x1, 0x2, etc.)
snapshotId: QUANTITY
: The snapshot id to revert.
Promise<boolean>
: true
if a snapshot was reverted, otherwise false
.
const [from, to] = await provider.send("eth_accounts");
const startingBalance = BigInt(await provider.send("eth_getBalance", [from] ));
// take a snapshot
const snapshotId = await provider.send("evm_snapshot");
// send value to another account (over-simplified example)
await provider.send("eth_subscribe", ["newHeads"] );
await provider.send("eth_sendTransaction", [{from, to, value: "0xffff"}] );
await provider.once("message"); // Note: `await provider.once` is non-standard
// ensure balance has updated
const newBalance = await provider.send("eth_getBalance", [from] );
assert(BigInt(newBalance) < startingBalance);
// revert the snapshot
const isReverted = await provider.send("evm_revert", [snapshotId] );
assert(isReverted);
console.log({isReverted: isReverted});
// ensure balance has reverted
const endingBalance = await provider.send("eth_getBalance", [from] );
const isBalanceReverted = assert.strictEqual(BigInt(endingBalance), startingBalance);
console.log({isBalanceReverted: isBalanceReverted});
evm_setAccountNonce(address: DATA, nonce: QUANTITY): Promise<boolean>
Sets the given account's nonce to the specified value. Mines a new block before returning.
Warning: this will result in an invalid state tree.
address: DATA
: The account address to update.nonce: QUANTITY
: The nonce value to be set.
Promise<boolean>
: true
if it worked, otherwise false
.
const nonce = "0x3e8";
const [address] = await provider.request({ method: "eth_accounts", params: [] });
const result = await provider.send("evm_setAccountNonce", [address, nonce] );
console.log(result);
evm_setTime(time: number | QUANTITY | Date): Promise<number>
Sets the internal clock time to the given timestamp.
Warning: This will allow you to move backwards in time, which may cause new blocks to appear to be mined before old blocks. This is will result in an invalid state.
time: number | QUANTITY | Date
: JavaScript timestamp (millisecond precision).
Promise<number>
: The amount of seconds between the given timestamp and now.
const currentDate = Date.now();
setTimeout(async () => {
const time = await provider.send("evm_setTime", [currentDate] );
console.log(time); // should be about two seconds ago
}, 1000);
evm_snapshot(): Promise<QUANTITY>
Snapshot the state of the blockchain at the current block. Takes no
parameters. Returns the id of the snapshot that was created. A snapshot can
only be reverted once. After a successful evm_revert
, the same snapshot
id cannot be used again. Consider creating a new snapshot after each
evm_revert
if you need to revert to the same point multiple times.
Promise<QUANTITY>
: The hex-encoded identifier for this snapshot.
const provider = ganache.provider();
const [from, to] = await provider.send("eth_accounts");
const startingBalance = BigInt(await provider.send("eth_getBalance", [from] ));
// take a snapshot
const snapshotId = await provider.send("evm_snapshot");
// send value to another account (over-simplified example)
await provider.send("eth_subscribe", ["newHeads"] );
await provider.send("eth_sendTransaction", [{from, to, value: "0xffff"}] );
await provider.once("message"); // Note: `await provider.once` is non-standard
// ensure balance has updated
const newBalance = await provider.send("eth_getBalance", [from] );
assert(BigInt(newBalance) < startingBalance);
// revert the snapshot
const isReverted = await provider.send("evm_revert", [snapshotId] );
assert(isReverted);
// ensure balance has reverted
const endingBalance = await provider.send("eth_getBalance", [from] );
const isBalanceReverted = assert.strictEqual(BigInt(endingBalance), startingBalance);
console.log({isBalanceReverted: isBalanceReverted});
evm_unlockUnknownAccount(address: DATA, duration: number): Promise<boolean>
Unlocks any unknown account.
Note: accounts known to the personal
namespace and accounts returned by
eth_accounts
cannot be unlocked using this method.
address: DATA
: The address of the account to unlock.duration: number
: (Default: disabled) Duration in seconds how long the account should remain unlocked for. Set to 0 to disable automatic locking.
Promise<boolean>
: true
if the account was unlocked successfully, false
if the
account was already unlocked. Throws an error if the account could not be
unlocked.
const address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
const result = await provider.send("evm_unlockUnknownAccount", [address] );
console.log(result);
miner_setEtherbase(address: DATA): Promise<boolean>
Sets the etherbase, where mining rewards will go.
address: DATA
: The address where the mining rewards will go.
Promise<boolean>
: true
.
const [account] = await provider.request({ method: "eth_accounts", params: [] });
console.log(await provider.send("miner_setEtherbase", [account] ));
miner_setExtra(extra: DATA): Promise<boolean>
Set the extraData block header field a miner can include.
extra: DATA
: TheextraData
to include.
Promise<boolean>
: If successfully set returns true
, otherwise returns an error.
console.log(await provider.send("miner_setExtra", ["0x0"] ));
miner_setGasPrice(number: QUANTITY): Promise<boolean>
Sets the default accepted gas price when mining transactions. Any transactions that don't specify a gas price will use this amount. Transactions that are below this limit are excluded from the mining process.
number: QUANTITY
: Default accepted gas price.
Promise<boolean>
: true
.
console.log(await provider.send("miner_setGasPrice", [300000] ));
miner_start(threads: number): Promise<boolean>
Resume the CPU mining process with the given number of threads.
Note: threads
is ignored.
threads: number
: Number of threads to resume the CPU mining process with.
Promise<boolean>
: true
.
await provider.send("miner_stop");
// check that eth_mining returns false
console.log(await provider.send("eth_mining"));
await provider.send("miner_start");
// check that eth_mining returns true
console.log(await provider.send("eth_mining"));
miner_stop(): Promise<boolean>
Stop the CPU mining operation.
Promise<boolean>
: true
.
// check that eth_mining returns true
console.log(await provider.send("eth_mining"));
await provider.send("miner_stop");
// check that eth_mining returns false
console.log(await provider.send("eth_mining"));
net_listening(): Promise<boolean>
Returns true
if client is actively listening for network connections.
Promise<boolean>
: true
when listening, otherwise false
.
console.log(await provider.send("net_listening"));
net_peerCount(): Promise<QUANTITY>
Returns number of peers currently connected to the client.
Promise<QUANTITY>
: Number of connected peers.
console.log(await provider.send("net_peerCount"));
net_version(): Promise<string>
Returns the current network id.
Promise<string>
: The current network id. This value should NOT be JSON-RPC Quantity/Data encoded.
console.log(await provider.send("net_version"));
personal_importRawKey(rawKey: DATA, passphrase: string): Promise<Address>
Imports the given unencrypted private key (hex string) into the key store, encrypting it with the passphrase.
rawKey: DATA
: The raw, unencrypted private key to import.passphrase: string
: The passphrase to encrypt with.
Promise<Address>
: Returns the address of the new account.
const rawKey = "0x0123456789012345678901234567890123456789012345678901234567890123";
const passphrase = "passphrase";
const address = await provider.send("personal_importRawKey",[rawKey, passphrase] );
console.log(address);
personal_listAccounts(): Promise<string[]>
Returns all the Ethereum account addresses of all keys that have been added.
Promise<string[]>
: The Ethereum account addresses of all keys that have been added.
console.log(await provider.send("personal_listAccounts"));
personal_lockAccount(address: DATA): Promise<boolean>
Locks the account. The account can no longer be used to send transactions.
address: DATA
: The account address to be locked.
Promise<boolean>
: Returns true
if the account was locked, otherwise false
.
const [account] = await provider.send("personal_listAccounts");
const isLocked = await provider.send("personal_lockAccount", [account] );
console.log(isLocked);
personal_newAccount(passphrase: string): Promise<Address>
Generates a new account with private key. Returns the address of the new account.
passphrase: string
: The passphrase to encrypt the private key with.
Promise<Address>
: The new account's address.
const passphrase = "passphrase";
const address = await provider.send("personal_newAccount", [passphrase] );
console.log(address);
personal_sendTransaction(transaction: TypedRpcTransaction, passphrase: string): Promise<DATA>
Validate the given passphrase and submit transaction.
The transaction is the same argument as for eth_sendTransaction
and
contains the from address. If the passphrase can be used to decrypt the
private key belonging to tx.from
the transaction is verified, signed and
send onto the network. The account is not unlocked globally in the node
and cannot be used in other RPC calls.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: TypedRpcTransaction
passphrase: string
: The passphrase to decrpyt the private key belonging totx.from
.
Promise<DATA>
: The transaction hash or if unsuccessful an error.
const passphrase = "passphrase";
const newAccount = await provider.send("personal_newAccount", [passphrase] );
const [to] = await provider.send("personal_listAccounts");
// use account and passphrase to send the transaction
const txHash = await provider.send("personal_sendTransaction", [{ from: newAccount, to, gasLimit: "0x5b8d80" }, passphrase] );
console.log(txHash);
personal_signTransaction(transaction: TypedRpcTransaction, passphrase: string): Promise<string>
Validates the given passphrase and signs a transaction that can be
submitted to the network at a later time using eth_sendRawTransaction
.
The transaction is the same argument as for eth_signTransaction
and
contains the from address. If the passphrase can be used to decrypt the
private key belonging to tx.from
the transaction is verified and signed.
The account is not unlocked globally in the node and cannot be used in other RPC calls.
Transaction call object:
from
:DATA
, 20 bytes (optional) - The address the transaction is sent from.to
:DATA
, 20 bytes - The address the transaction is sent to.gas
:QUANTITY
(optional) - Integer of the maximum gas allowance for the transaction.gasPrice
:QUANTITY
(optional) - Integer of the price of gas in wei.value
:QUANTITY
(optional) - Integer of the value in wei.data
:DATA
(optional) - Hash of the method signature and the ABI encoded parameters.
transaction: TypedRpcTransaction
: The transaction call object as seen in source.passphrase: string
Promise<string>
: The raw, signed transaction.
const [to] = await provider.request({ method: "eth_accounts", params: [] });
const passphrase = "passphrase";
const from = await provider.send("personal_newAccount", [passphrase] );
await provider.request({ method: "eth_subscribe", params: ["newHeads"] });
const signedTx = await provider.request({ method: "personal_signTransaction", params: [{ from, to }, passphrase] });
console.log(signedTx)
personal_unlockAccount(address: DATA, passphrase: string, duration: number): Promise<boolean>
Unlocks the account for use.
The unencrypted key will be held in memory until the unlock duration expires. The unlock duration defaults to 300 seconds. An explicit duration of zero seconds unlocks the key until geth exits.
The account can be used with eth_sign
and eth_sendTransaction
while it is
unlocked.
address: DATA
: 20 Bytes - The address of the account to unlock.passphrase: string
: Passphrase to unlock the account.duration: number
: (default: 300) Duration in seconds how long the account should remain unlocked for. Set to 0 to disable automatic locking.
Promise<boolean>
: true
if it worked. Throws an error or returns false
if it did not.
// generate an account
const passphrase = "passphrase";
const newAccount = await provider.send("personal_newAccount", [passphrase] );
const isUnlocked = await provider.send("personal_unlockAccount", [newAccount, passphrase] );
console.log(isUnlocked);
rpc_modules(): Promise<object>
Returns object of RPC modules.
Promise<object>
: RPC modules.
console.log(await provider.send("rpc_modules"));
shh_addToGroup(address: DATA): Promise<boolean>
Adds a whisper identity to the group.
address: DATA
: The identity address to add to a group.
Promise<boolean>
: true
if the identity was successfully added to the group, otherwise false
.
console.log(await provider.send("shh_addToGroup", ["0x0"] ));
shh_getFilterChanges(id: QUANTITY): Promise<any[]>
Polling method for whisper filters. Returns new messages since the last call of this method.
id: QUANTITY
: The filter id. Ex: "0x7"
Promise<any[]>
: More Info: https://github.com/ethereum/wiki/wiki/JSON-RPC#shh_getfilterchanges
console.log(await provider.send("shh_getFilterChanges", ["0x0"] ));
shh_getMessages(id: QUANTITY): Promise<boolean>
Get all messages matching a filter. Unlike shh_getFilterChanges this returns all messages.
id: QUANTITY
: The filter id. Ex: "0x7"
Promise<boolean>
: See: shh_getFilterChanges
.
console.log(await provider.send("shh_getMessages", ["0x0"] ));
shh_hasIdentity(address: DATA): Promise<boolean>
Checks if the client hold the private keys for a given identity.
address: DATA
: The identity address to check.
Promise<boolean>
: Returns true
if the client holds the private key for that identity, otherwise false
.
console.log(await provider.send("shh_hasIdentity", ["0x0"] ));
shh_newFilter(to: DATA, topics: DATA[]): Promise<boolean>
Creates filter to notify, when client receives whisper message matching the filter options.
to: DATA
: (optional) Identity of the receiver. When present it will try to decrypt any incoming message if the client holds the private key to this identity.topics: DATA[]
: Array of topics which the incoming message's topics should match.
Promise<boolean>
: Returns true
if the identity was successfully added to the group, otherwise false
.
console.log(await provider.send("shh_newFilter", ["0x0", []] ));
shh_newGroup(): Promise<string>
Creates a new group.
Promise<string>
: The address of the new group.
shh_newIdentity(): Promise<string>
Creates new whisper identity in the client.
Promise<string>
: - The address of the new identity.
console.log(await provider.send("shh_newIdentity"));
shh_post(postData: WhisperPostObject): Promise<boolean>
Creates a whisper message and injects it into the network for distribution.
postData: WhisperPostObject
Promise<boolean>
: Returns true
if the message was sent, otherwise false
.
console.log(await provider.send("shh_post", [{}] ));
shh_uninstallFilter(id: QUANTITY): Promise<boolean>
Uninstalls a filter with given id. Should always be called when watch is no longer needed.
Additionally filters timeout when they aren't requested with shh_getFilterChanges
for a period of time.
id: QUANTITY
: The filter id. Ex: "0x7"
Promise<boolean>
: true
if the filter was successfully uninstalled, otherwise false
.
console.log(await provider.send("shh_uninstallFilter", ["0x0"] ));
shh_version(): Promise<string>
Returns the current whisper protocol version.
Promise<string>
: The current whisper protocol version.
console.log(await provider.send("shh_version"));
web3_clientVersion(): Promise<string>
Returns the current client version.
Promise<string>
: The current client version.
console.log(await provider.send("web3_clientVersion"));
web3_sha3(data: DATA): Promise<DATA>
Returns Keccak-256 (not the standardized SHA3-256) of the given data.
data: DATA
: the data to convert into a SHA3 hash.
Promise<DATA>
: The SHA3 result of the given string.
const data = "hello trufflers";
const sha3 = await provider.send("web3_sha3", [data] );
console.log(sha3);