Convenience libraries
While you may choose to interact directly with Parallax clients via the JSON-RPC API, there are often easier options for dapp developers. Many JavaScript libraries exist to provide wrappers on top of the JSON-RPC API. With these libraries, developers can write intuitive, one-line methods in the programming language of their choice to initialize JSON-RPC requests (under the hood) that interact with Parallax.Conventions
Hex value encoding
Two key data types get passed over JSON: unformatted byte arrays and quantities. Both are passed with a hex encoding but with different requirements for formatting. Quantities When encoding quantities (integers, numbers): encode as hex, prefix with “0x”, the most compact representation (slight exception: zero should be represented as “0x0”). Here are some examples:- 0x41 (65 in decimal)
- 0x400 (1024 in decimal)
- WRONG: 0x (should always have at least one digit - zero is “0x0”)
- WRONG: 0x0400 (no leading zeroes allowed)
- WRONG: ff (must be prefixed 0x)
- 0x41 (size 1, “A”)
- 0x004200 (size 3, “\0B\0”)
- 0x (size 0, "")
- WRONG: 0xf0f0f (must be even number of digits)
- WRONG: 004200 (must be prefixed 0x)
- eth_getBalance
- eth_getCode
- eth_getTransactionCount
- eth_getStorageAt
- eth_call
defaultBlock
parameter:
HEX String
- an integer block numberString "earliest"
for the earliest/genesis blockString "latest"
- for the latest mined blockString "safe"
- for the latest safe head blockString "finalized"
- for the latest finalized blockString "pending"
- for the pending state/transactions
Examples
On this page we provide examples of how to use individual JSON_RPC API endpoints using the command line tool, curl↗. These individual endpoint examples are found below in the Curl examples section. Further down the page, we also provide an end-to-end example for compiling and deploying a smart contract using a Parallax node, the JSON_RPC API and curl.Curl examples
Examples of using the JSON_RPC API by making curl↗ requests to a Parallax node are provided below. Each example includes a description of the specific endpoint, its parameters, return type, and a worked example of how it should be used. The curl requests might return an error message relating to the content type. This is because the--data
option sets the content type to application/x-www-form-urlencoded
. If your node does complain about this, manually set the header by placing -H "Content-Type: application/json"
at the start of the call. The examples also do not include the URL/IP & port combination which must be the last argument given to curl (e.g. 127.0.0.1:8545
). A complete curl request including these additional data takes the following form:
Gossip, State, History
A handful of core JSON-RPC methods require data from the Parallax network, and fall neatly into three main categories: Gossip, State, and History. Use the links in these sections to jump to each method, or use the table of contents to explore the whole list of methods.Gossip Methods
These methods track the head of the chain. This is how transactions make their way around the network, find their way into blocks, and how clients find out about new blocks.
- eth_blockNumber
- eth_sendRawTransaction
State Methods
Methods that report the current state of all the data stored. The “state” is like one big shared piece of RAM, and includes account balances, contract data, and gas estimations.
- eth_getBalance
- eth_getStorageAt
- eth_getTransactionCount
- eth_getCode
- eth_call
- eth_estimateGas
History Methods
Fetches historical records of every block back to genesis. This is like one large append-only file, and includes all block headers, block bodies, uncle blocks, and transaction receipts.
- eth_getBlockTransactionCountByHash
- eth_getBlockTransactionCountByNumber
- eth_getUncleCountByBlockHash
- eth_getUncleCountByBlockNumber
- eth_getBlockByHash
- eth_getBlockByNumber
- eth_getTransactionByHash
- eth_getTransactionByBlockHashAndIndex
- eth_getTransactionByBlockNumberAndIndex
- eth_getTransactionReceipt
- eth_getUncleByBlockHashAndIndex
- eth_getUncleByBlockNumberAndIndex
JSON-RPC API Methods
web3_clientVersion
Returns the current client version. Parameters None ReturnsString
- The current client version
Example
web3_sha3
Returns Keccak-256 (not the standardized SHA3-256) of the given data. ParametersDATA
- the data to convert into a SHA3 hash
DATA
- The SHA3 result of the given string.
Example
net_version
Returns the current network id. Parameters None ReturnsString
- The current network id.
The full list of current network IDs is available at chainlist.org↗. Some common ones are:
2110
: Parallax main network2111
: Parallax test network
net_listening
Returnstrue
if client is actively listening for network connections.
Parameters
None
Returns
Boolean
- true
when listening, otherwise false
.
Example
net_peerCount
Returns number of peers currently connected to the client. Parameters None ReturnsQUANTITY
- integer of the number of connected peers.
Example
eth_syncing
Returns an object with data about the sync status orfalse
.
Parameters
None
Returns
Object|Boolean
, An object with sync status data or FALSE
, when not syncing:
startingBlock
:QUANTITY
- The block at which the import started (will only be reset, after the sync reached his head)currentBlock
:QUANTITY
- The current block, same as eth_blockNumberhighestBlock
:QUANTITY
- The estimated highest block
eth_coinbase
Returns the client coinbase address. Parameters None ReturnsDATA
, 20 bytes - the current coinbase address.
Example
eth_chainId
Returns the chain ID used for signing replay-protected transactions. Parameters None ReturnschainId
, hexadecimal value as a string representing the integer of the current chain id.
Example
eth_mining
Returnstrue
if client is actively mining new blocks.
Parameters
None
Returns
Boolean
- returns true
of the client is mining, otherwise false
.
Example
eth_hashrate
Returns the number of hashes per second that the node is mining with. Parameters None ReturnsQUANTITY
- number of hashes per second.
Example
eth_gasPrice
Returns the current price per gas in wei. Parameters None ReturnsQUANTITY
- integer of the current gas price in wei.
Example
eth_accounts
Returns a list of addresses owned by client. Parameters None ReturnsArray of DATA
, 20 Bytes - addresses owned by the client.
Example
eth_blockNumber
Returns the number of most recent block. Parameters None ReturnsQUANTITY
- integer of the current block number the client is on.
Example
eth_getBalance
Returns the balance of the account of given address. ParametersDATA
, 20 Bytes - address to check for balance.QUANTITY|TAG
- integer block number, or the string"latest"
,"earliest"
or"pending"
, see the default block parameter
QUANTITY
- integer of the current balance in wei.
Example
eth_getStorageAt
Returns the value from a storage position at a given address. ParametersDATA
, 20 Bytes - address of the storage.QUANTITY
- integer of the position in the storage.QUANTITY|TAG
- integer block number, or the string"latest"
,"earliest"
or"pending"
, see the default block parameter
DATA
- the value at this storage position.
Example Calculating the correct position depends on the storage to retrieve. Consider the following contract deployed at 0x295a70b2de5e3953354a6a8344e616ed314d7251
by address 0x391694e7e0b0cce554cb130d723a9d27458f9298
.
eth_getTransactionCount
Returns the number of transactions sent from an address. ParametersDATA
, 20 Bytes - address.QUANTITY|TAG
- integer block number, or the string"latest"
,"earliest"
or"pending"
, see the default block parameter
QUANTITY
- integer of the number of transactions send from this address.
Example
eth_getBlockTransactionCountByHash
Returns the number of transactions in a block from a block matching the given block hash. ParametersDATA
, 32 Bytes - hash of a block
QUANTITY
- integer of the number of transactions in this block.
Example
eth_getBlockTransactionCountByNumber
Returns the number of transactions in a block matching the given block number. ParametersQUANTITY|TAG
- integer of a block number, or the string"earliest"
,"latest"
or"pending"
, as in the default block parameter.
QUANTITY
- integer of the number of transactions in this block.
Example
eth_getUncleCountByBlockHash
Returns the number of uncles in a block from a block matching the given block hash. ParametersDATA
, 32 Bytes - hash of a block
QUANTITY
- integer of the number of uncles in this block.
Example
eth_getUncleCountByBlockNumber
Returns the number of uncles in a block from a block matching the given block number. ParametersQUANTITY|TAG
- integer of a block number, or the string “latest”, “earliest” or “pending”, see the default block parameter
QUANTITY
- integer of the number of uncles in this block.
Example
eth_getCode
Returns code at a given address. ParametersDATA
, 20 Bytes - addressQUANTITY|TAG
- integer block number, or the string"latest"
,"earliest"
or"pending"
, see the default block parameter
DATA
- the code from the given address.
Example
eth_sign
The sign method calculates an Ethereum specific signature with:sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + 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.
Parameters
DATA
, 20 Bytes - addressDATA
, N Bytes - message to sign
DATA
: Signature
Example
eth_signTransaction
Signs a transaction that can be submitted to the network at a later time using with eth_sendRawTransaction. ParametersObject
- The transaction object
from
:DATA
, 20 Bytes - The address the transaction is sent from.to
:DATA
, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.gas
:QUANTITY
- (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.gasPrice
:QUANTITY
- (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas, in Wei.value
:QUANTITY
- (optional) Integer of the value sent with this transaction, in Wei.data
:DATA
- The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.nonce
:QUANTITY
- (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
DATA
, The signed transaction object.
Example
eth_sendTransaction
Creates new message call transaction or a contract creation, if the data field contains code. ParametersObject
- The transaction object
from
:DATA
, 20 Bytes - The address the transaction is sent from.to
:DATA
, 20 Bytes - (optional when creating new contract) The address the transaction is directed to.gas
:QUANTITY
- (optional, default: 90000) Integer of the gas provided for the transaction execution. It will return unused gas.gasPrice
:QUANTITY
- (optional, default: To-Be-Determined) Integer of the gasPrice used for each paid gas.value
:QUANTITY
- (optional) Integer of the value sent with this transaction.data
:DATA
- The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.nonce
:QUANTITY
- (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
DATA
, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
Example
eth_sendRawTransaction
Creates new message call transaction or a contract creation for signed transactions. ParametersDATA
, The signed transaction data.
DATA
, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
Use eth_getTransactionReceipt to get the contract address, after the transaction was mined, when you created a contract.
Example
eth_call
Executes a new message call immediately without creating a transaction on the block chain. ParametersObject
- The transaction call object
from
:DATA
, 20 Bytes - (optional) The address the transaction is sent from.to
:DATA
, 20 Bytes - The address the transaction is directed to.gas
:QUANTITY
- (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions.gasPrice
:QUANTITY
- (optional) Integer of the gasPrice used for each paid gasvalue
:QUANTITY
- (optional) Integer of the value sent with this transactiondata
:DATA
- (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI in the Solidity documentation↗
QUANTITY|TAG
- integer block number, or the string"latest"
,"earliest"
or"pending"
, see the default block parameter
DATA
- the return value of executed contract.
Example
eth_estimateGas
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 PVM mechanics and node performance. Parameters See eth_call parameters, except that all properties are optional. If no gas limit is specified Parallax client uses the block gas limit from the pending block as an upper bound. As a result the returned estimate might not be enough to executed the call/transaction when the amount of gas is higher than the pending block gas limit. ReturnsQUANTITY
- the amount of gas used.
Example
eth_getBlockByHash
Returns information about a block by hash. ParametersDATA
, 32 Bytes - Hash of a block.Boolean
- Iftrue
it returns the full transaction objects, iffalse
only the hashes of the transactions.
Object
- A block object, or null
when no block was found:
number
:QUANTITY
- the block number.null
when its pending block.hash
:DATA
, 32 Bytes - hash of the block.null
when its pending block.parentHash
:DATA
, 32 Bytes - hash of the parent block.nonce
:DATA
, 8 Bytes - hash of the generated proof-of-work.null
when its pending block.sha3Uncles
:DATA
, 32 Bytes - SHA3 of the uncles data in the block.logsBloom
:DATA
, 256 Bytes - the bloom filter for the logs of the block.null
when its pending block.transactionsRoot
:DATA
, 32 Bytes - the root of the transaction trie of the block.stateRoot
:DATA
, 32 Bytes - the root of the final state trie of the block.receiptsRoot
:DATA
, 32 Bytes - the root of the receipts trie of the block.miner
:DATA
, 20 Bytes - the address of the beneficiary to whom the mining rewards were given.difficulty
:QUANTITY
- integer of the difficulty for this block.totalDifficulty
:QUANTITY
- integer of the total difficulty of the chain until this block.extraData
:DATA
- the “extra data” field of this block.size
:QUANTITY
- integer the size of this block in bytes.gasLimit
:QUANTITY
- the maximum gas allowed in this block.gasUsed
:QUANTITY
- the total used gas by all transactions in this block.timestamp
:QUANTITY
- the unix timestamp for when the block was collated.transactions
:Array
- Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.uncles
:Array
- Array of uncle hashes.
eth_getBlockByNumber
Returns information about a block by block number. ParametersQUANTITY|TAG
- integer of a block number, or the string"earliest"
,"latest"
or"pending"
, as in the default block parameter.Boolean
- Iftrue
it returns the full transaction objects, iffalse
only the hashes of the transactions.
eth_getTransactionByHash
Returns the information about a transaction requested by transaction hash. ParametersDATA
, 32 Bytes - hash of a transaction
Object
- A transaction object, or null
when no transaction was found:
blockHash
:DATA
, 32 Bytes - hash of the block where this transaction was in.null
when its pending.blockNumber
:QUANTITY
- block number where this transaction was in.null
when its pending.from
:DATA
, 20 Bytes - address of the sender.gas
:QUANTITY
- gas provided by the sender.gasPrice
:QUANTITY
- gas price provided by the sender in Wei.hash
:DATA
, 32 Bytes - hash of the transaction.input
:DATA
- the data send along with the transaction.nonce
:QUANTITY
- the number of transactions made by the sender prior to this one.to
:DATA
, 20 Bytes - address of the receiver.null
when its a contract creation transaction.transactionIndex
:QUANTITY
- integer of the transactions index position in the block.null
when its pending.value
:QUANTITY
- value transferred in Wei.v
:QUANTITY
- ECDSA recovery idr
:QUANTITY
- ECDSA signature rs
:QUANTITY
- ECDSA signature s
eth_getTransactionByBlockHashAndIndex
Returns information about a transaction by block hash and transaction index position. ParametersDATA
, 32 Bytes - hash of a block.QUANTITY
- integer of the transaction index position.
eth_getTransactionByBlockNumberAndIndex
Returns information about a transaction by block number and transaction index position. ParametersQUANTITY|TAG
- a block number, or the string"earliest"
,"latest"
or"pending"
, as in the default block parameter.QUANTITY
- the transaction index position.
eth_getTransactionReceipt
Returns the receipt of a transaction by transaction hash. Note That the receipt is not available for pending transactions. ParametersDATA
, 32 Bytes - hash of a transaction
Object
- A transaction receipt object, or null
when no receipt was found:
transactionHash
:DATA
, 32 Bytes - hash of the transaction.transactionIndex
:QUANTITY
- integer of the transactions index position in the block.blockHash
:DATA
, 32 Bytes - hash of the block where this transaction was in.blockNumber
:QUANTITY
- block number where this transaction was in.from
:DATA
, 20 Bytes - address of the sender.to
:DATA
, 20 Bytes - address of the receiver. null when its a contract creation transaction.cumulativeGasUsed
:QUANTITY
- The total amount of gas used when this transaction was executed in the block.effectiveGasPrice
:QUANTITY
- The sum of the base fee and tip paid per unit of gas.gasUsed
:QUANTITY
- The amount of gas used by this specific transaction alone.contractAddress
:DATA
, 20 Bytes - The contract address created, if the transaction was a contract creation, otherwisenull
.logs
:Array
- Array of log objects, which this transaction generated.logsBloom
:DATA
, 256 Bytes - Bloom filter for light clients to quickly retrieve related logs.type
:QUANTITY
- integer of the transaction type,0x0
for legacy transactions,0x1
for access list types,0x2
for dynamic fees.
root
:DATA
32 bytes of post-transaction stateroot (pre Byzantium)status
:QUANTITY
either1
(success) or0
(failure)
eth_getUncleByBlockHashAndIndex
Returns information about a uncle of a block by hash and uncle index position. ParametersDATA
, 32 Bytes - The hash of a block.QUANTITY
- The uncle’s index position.
QUANTITY|TAG
- a block number, or the string"earliest"
,"latest"
or"pending"
, as in the default block parameter.QUANTITY
- the uncle’s index position.
eth_getCompilers
Returns a list of available compilers in the client. Parameters None ReturnsArray
- Array of available compilers.
Example
eth_compileSolidity
Returns compiled solidity code. ParametersString
- The source code.
DATA
- The compiled source code.
Example
eth_compileLLL
Returns compiled LLL code. ParametersString
- The source code.
DATA
- The compiled source code.
Example
eth_compileSerpent
Returns compiled serpent code. ParametersString
- The source code.
DATA
- The compiled source code.
Example
eth_newFilter
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. 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)”- Parameters
Object
- The filter options:
fromBlock
:QUANTITY|TAG
- (optional, default:"latest"
) Integer block number, or"latest"
for the last mined block or"pending"
,"earliest"
for not yet mined transactions.toBlock
:QUANTITY|TAG
- (optional, default:"latest"
) Integer block number, or"latest"
for the last mined block or"pending"
,"earliest"
for not yet mined transactions.address
:DATA|Array
, 20 Bytes - (optional) Contract address or a list of addresses from which logs should originate.topics
:Array of DATA
, - (optional) Array of 32 BytesDATA
topics. Topics are order-dependent. Each topic can also be an array of DATA with “or” options.
QUANTITY
- A filter id.
Example
eth_newBlockFilter
Creates a filter in the node, to notify when a new block arrives. To check if the state has changed, call eth_getFilterChanges. Parameters None ReturnsQUANTITY
- A filter id.
Example
eth_newPendingTransactionFilter
Creates a filter in the node, to notify when new pending transactions arrive. To check if the state has changed, call eth_getFilterChanges. Parameters None ReturnsQUANTITY
- A filter id.
Example
eth_uninstallFilter
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 eth_getFilterChanges for a period of time. ParametersQUANTITY
- The filter id.
Boolean
- true
if the filter was successfully uninstalled, otherwise false
.
Example
QUANTITY
- the filter id.
Array
- Array of log objects, or an empty array if nothing has changed since last poll.
- For filters created with
eth_newBlockFilter
the return are block hashes (DATA
, 32 Bytes), e.g.["0x3454645634534..."]
. - For filters created with
eth_newPendingTransactionFilter
the return are transaction hashes (DATA
, 32 Bytes), e.g.["0x6345343454645..."]
. - For filters created with
eth_newFilter
logs are objects with following params:removed
:TAG
-true
when the log was removed, due to a chain reorganization.false
if its a valid log.logIndex
:QUANTITY
- integer of the log index position in the block.null
when its pending log.transactionIndex
:QUANTITY
- integer of the transactions index position log was created from.null
when its pending log.transactionHash
:DATA
, 32 Bytes - hash of the transactions this log was created from.null
when its pending log.blockHash
:DATA
, 32 Bytes - hash of the block where this log was in.null
when its pending.null
when its pending log.blockNumber
:QUANTITY
- the block number where this log was in.null
when its pending.null
when its pending log.address
:DATA
, 20 Bytes - address from which this 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. (In solidity: The first topic is the hash of the signature of the event (e.g.Deposit(address,bytes32,uint256)
), except you declared the event with theanonymous
specifier.)
- Example
eth_getFilterLogs
Returns an array of all logs matching filter with given id. ParametersQUANTITY
- The filter id.
eth_getLogs
Returns an array of all logs matching a given filter object. ParametersObject
- The filter options:
fromBlock
:QUANTITY|TAG
- (optional, default:"latest"
) Integer block number, or"latest"
for the last mined block or"pending"
,"earliest"
for not yet mined transactions.toBlock
:QUANTITY|TAG
- (optional, default:"latest"
) Integer block number, or"latest"
for the last mined block or"pending"
,"earliest"
for not yet mined transactions.address
:DATA|Array
, 20 Bytes - (optional) Contract address or a list of addresses from which logs should originate.topics
:Array of DATA
, - (optional) Array of 32 BytesDATA
topics. Topics are order-dependent. Each topic can also be an array of DATA with “or” options.blockhash
:DATA
, 32 Bytes - (optional, future) With the addition of EIP-234,blockHash
will be a new filter option which restricts the logs returned to the single block with the 32-byte hashblockHash
. UsingblockHash
is equivalent tofromBlock
=toBlock
= the block number with hashblockHash
. IfblockHash
is present in the filter criteria, then neitherfromBlock
nortoBlock
are allowed.
eth_getWork
Returns the hash of the current block, the seedHash, and the boundary condition to be met (“target”). Parameters None ReturnsArray
- Array with the following properties:
DATA
, 32 Bytes - current block header pow-hashDATA
, 32 Bytes - the seed hash used for the DAG.DATA
, 32 Bytes - the boundary condition (“target”), 2^256 / difficulty.
eth_submitWork
Used for submitting a proof-of-work solution. ParametersDATA
, 8 Bytes - The nonce found (64 bits)DATA
, 32 Bytes - The header’s pow-hash (256 bits)DATA
, 32 Bytes - The mix digest (256 bits)
Boolean
- returns true
if the provided solution is valid, otherwise false
.
Example
eth_submitHashrate
Used for submitting mining hashrate. ParametersHashrate
, a hexadecimal string representation (32 bytes) of the hashrateID
, String - A random hexadecimal(32 bytes) ID identifying the client
Boolean
- returns true
if submitting went through successfully and false
otherwise.
Example
Usage example
Deploying a contract using JSON_RPC
This section includes a demonstration of how to deploy a contract using only the RPC interface. There are alternative routes to deploying contracts where this complexity is abstracted away—for example, using libraries built on top of the RPC interface such as web3.js↗ and web3.py↗. These abstractions are generally easier to understand and less error-prone, but it is still helpful to understand what is happening under the hood. The following is a straightforward smart contract calledMultiply7
that will be deployed using the JSON-RPC interface to a Parallax node. This tutorial assumes the reader is already running a Parallax node. More information on nodes and clients is available here. Please refer to the Parallax client documentation to see how to start the HTTP JSON-RPC. Most clients default to serving on localhost:8545
.
--http
flag on startup. In this example we use the Parallax node on a private development chain. Using this approach we don’t need ether on the real network.
http://localhost:8545
.
We can verify that the interface is running by retrieving the Coinbase address and balance using curl↗. Please note that data in these examples will differ on your local node. If you want to try these commands, replace the request params in the second curl request with the result returned from the first.
solc
release to match the version of compiler used for our example.)
The next step is to compile the Multiply7 contract to byte code that can be send to the PVM.
eth_estimateGas
method that will give us an estimate.
eth_getTransactionReceipt
RPC method.
0x4d03d617d700cf81935d7f797f4e2ae719648262
. A null result instead of a receipt means the transaction has not been included in a block yet. Wait for a moment and check if your miner is running and retry it.
Interacting with smart contracts
In this example we will be sending a transaction usingeth_sendTransaction
to the multiply
method of the contract.
eth_sendTransaction
requires several arguments, specifically from
, to
and data
. From
is the public address of our account, and to
is the contract address. The data
argument contains a payload that defines which method must be called and with which arguments. This is where the ABI (application binary interface)↗ comes into play. The ABI is a JSON file that defines how to define and encode data for the PVM.
The bytes of the payload defines which method in the contract is called. This is the first 4 bytes from the Keccak hash over the function name and its argument types, hex encoded. The multiply function accepts an uint which is an alias for uint256. This leaves us with:
int<M>: enc(X)
is the big-endian two’s complement encoding of X, padded on the higher-order (left) side with 0xff for negative X and with zero > bytes for positive X such that the length is a multiple of 32 bytes.
This encodes to 0000000000000000000000000000000000000000000000000000000000000006
.
Combining the function selector and the encoded argument our data will be 0xc6888fa10000000000000000000000000000000000000000000000000000000000000006
.
This can now be sent to the node:
multiply
function shows that the Print
event was raised with the input times 7. Since the argument for the Print
event was a uint256 we can decode it according to the ABI rules which will leave us with the expected decimal 42. Apart from the data it is worth noting that topics can be used to determine which event created the log: