cfx Namespace
JSON-RPC Spec
There is a JSON-RPC API spec of cfx namespace on GitHub. You can view it in open-rpc playground
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 using the most compact representation and prefix with "0x"
. Zero should be represented as "0x0"
. 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
(missing0x
prefix)
Unformatted data
When encoding UNFORMATTED DATA (byte arrays, hashes, bytecode arrays): encode as hex using two hex digits per byte and prefix with "0x"
. Examples:
0x41
(size 1,"A"
)0x004200
(size 3,"\0B\0"
)0x
(size 0,""
)- WRONG:
0xf0f0f
(must be even number of digits) - WRONG:
004200
(missing0x
prefix)
Note that block and transaction hashes are represented using 32 bytes.
Base32 Address
BASE32
: Base32 addresses should be encoded as an ASCII string of 42-characters plus network prefix, separators, and optional fields. Please note the following constraints for base32 addresses as RPC parameters:
- The network-prefix should match the node's network, i.e.
cfx:acc7uawf5ubtnmezvhu9dhc6sghea0403y2dgpyfjp
can be sent to mainnet nodes andcfxtest:acc7uawf5ubtnmezvhu9dhc6sghea0403ywjz6wtpg
can be sent to testnet nodes. Note that these two example addresses correspond to the same account on different networks. - Including and omitting the address-type are both accepted, i.e.
cfx:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg
andcfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg
are equivalent. However, addresses with an incorrect type, e.g.cfx:type.contract:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg
, are rejected. - Both lowercase (
cfx:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg
) and uppercase (CFX:AARC9ABYCUE0HHZGYRR53M6CXEDGCCRMMYYBJGH4XG
) addresses are accepted. Mixed-case addresses are rejected.
Refer to Addresses for more knowledge about Base32 addresses.
The default epochNumber parameter
Several RPC methods have an epoch number parameter. The concept of epochs in Conflux is somewhat analogous to the concept of block numbers (height) in other ledgers, but one epoch might contain multiple blocks.
The epoch number specifies a point in time and the corresponding state of the system, subject to consensus. The following options are possible for the epoch number parameter:
HEX String
- an integer epoch number. For example,0x3e8
is epoch 1000.String "earliest"
for the epoch of the genesis block.String "latest_checkpoint"
for the earliest epoch stored in memory.String "latest_finalized"
- for the latest finalized (by PoS) epoch. (Added from conflux-rustv2.0.0
)String "latest_confirmed"
- for the latest confirmed epoch (using the confirmation meter's estimate).String "latest_state"
- for the latest epoch that has been executed.String "latest_mined"
- for the latest known epoch.
Please note that due to performance optimization, the latest mined epochs are not executed, so there is no state available for these epochs. For most RPCs related to state query, "latest_state"
is recommended. (Refer to transaction lifecycle for more information about transaction life cycle in Conflux).
EIP-1898 style Conflux epochNumber parameter
Conflux core space supports epoch number parameter in EIP-1898 style for certain RPCs. The EIP-1898 style epoch number parameter is an object with 3 optional fields:
epochNumber
. Corresponding to EIP-1898 definedblockNumber
blockHash
. Same as EIP-1898blockHash
requirePivot
. Corresponding to EIP-1898requireCanonical
. Defaults totrue
Por ejemplo:
{
"blockHash": "0x692373025c7315fa18b2d02139d08e987cd7016025920f59ada4969c24e44e06",
"requirePivot": false
}
The EIP-1898 style epoch number parameter is now usable in following RPCs:
CURL EXAMPLES
Examples of using the JSON_RPC API by making curl requests to an Conflux 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:12537). A complete curl request including these additional data takes the following form:
$ curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"cfx_clientVersion","params":[],"id":67}' 127.0.0.1:12537
The examples in the rest of this document will use the HTTP endpoint.
State and transaction availability
Conflux archive and full nodes remove historical state tries to reduce storage use. Full nodes will also discard transactions and receipts for historical blocks. As a result, some RPC interfaces might be unavailable for historical queries.
Below is a list of the Conflux RPC APIs and their availability on archive and full nodes. "recent" means that the RPC is only supported for recent items, "OK" means that it should work for any valid input.
RPC | archive node | full node |
---|---|---|
cfx_call | recent | recent |
cfx_checkBalanceAgainstTransaction | recent | recent |
cfx_clientVersion | OK | OK |
cfx_epochNumber | OK | OK |
cfx_estimateGasAndCollateral | recent | recent |
cfx_gasPrice | OK | OK |
cfx_getAccount | recent | recent |
cfx_getAccumulateInterestRate | OK | OK |
cfx_getAdmin | recent | recent |
cfx_getBalance | recent | recent |
cfx_getBestBlockHash | OK | OK |
cfx_getBlockByEpochNumber | OK | recent |
cfx_getBlockByHash | OK | recent |
cfx_getBlockByHashWithPivotAssumption | OK | recent |
cfx_getBlockRewardInfo | OK | recent |
cfx_getBlocksByEpoch | OK | OK |
cfx_getCode | recent | recent |
cfx_getCollateralForStorage | recent | recent |
cfx_getConfirmationRiskByHash | OK | recent |
cfx_getDepositList | recent | recent |
cfx_getInterestRate | recent | recent |
cfx_getLogs | OK | recent |
cfx_getNextNonce | recent | recent |
cfx_getSkippedBlocksByEpoch | OK | OK |
cfx_getSponsorInfo | recent | recent |
cfx_getStakingBalance | recent | recent |
cfx_getStatus | OK | OK |
cfx_getStorageAt | recent | recent |
cfx_getStorageRoot | recent | recent |
cfx_getTransactionByHash | OK | recent |
cfx_getTransactionReceipt | OK | recent |
cfx_getVoteList | recent | recent |
cfx_sendRawTransaction | OK | OK |
If you query a state entry that is unavailable on the node, you will get an error response:
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getBalance","params":["cfx:type.user:aarc9abycue0hhzgyrr53m6cxedgccrmmyybjgh4xg", "earliest"],"id":1}' -H "Content-Type: application/json" localhost:12539
// Result
{
"jsonrpc": "2.0",
"error": {
"code": -32016,
"message": "Error processing request: State for epoch (number=0 hash=0x24dcc768132dc7f651d7cb35c52e7bba632eda073d8743f81cfe905ff7e4157a) does not exist: out-of-bound StateAvailabilityBoundary { synced_state_height: 0, lower_bound: 9510001, upper_bound: 9569393, optimistic_executed_height: Some(9569392) }"
},
"id": 1
}
In this example, we are told that the earliest available state is at epoch 9510001
(0x911c71
).
Migrating from Ethereum JSON-RPC
There is a correspondence between some JSON-RPCs from Ethereum and Conflux. Even though the details of JSON-RPC may differ, the following mapping table could be helpful when migrating from Ethereum to Conflux:
Ethereum | Conflux |
---|---|
eth_blockNumber | cfx_epochNumber |
eth_call | cfx_call |
eth_estimateGas | cfx_estimateGasAndCollateral |
eth_gasPrice | cfx_gasPrice |
eth_getBalance | cfx_getBalance |
eth_getBlockByHash | cfx_getBlockByHash |
eth_getBlockByNumber | cfx_getBlockByEpochNumber |
eth_getCode | cfx_getCode |
eth_getLogs | cfx_getLogs |
eth_getStorageAt | cfx_getStorageAt |
eth_getTransactionByHash | cfx_getTransactionByHash |
eth_getTransactionCount | cfx_getNextNonce |
eth_getTransactionReceipt | cfx_getTransactionReceipt |
eth_sendRawTransaction | cfx_sendRawTransaction |
GOSSIP, STATE, HISTORY
A handful of core JSON-RPC methods require data from the Conflux 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 Method
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.
- cfx_getStatus
- cfx_epochNumber
- cfx_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.
- cfx_getBalance
- cfx_getStorageAt
- cfx_getNonce
- cfx_getCode
- cfx_call
- cfx_estimateGasAndCollateral
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, and transaction receipts.
- cfx_getBlockByHash
- cfx_getBlockByEpochNumber
- cfx_getTransactionByHash
- cfx_getTransactionReceipt
JSON-RPC methods
cfx_getTransactionByHash
Returns information about a transaction, identified by its hash.
Parameters
DATA
, 32 Bytes - hash of a transaction
params: [
"0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"
]
Returns
Object
- a transaction object, or null
when no transaction was found:
blockHash
:DATA
, 32 Bytes - hash of the block where this transaction was in and got executed.null
when the transaction is pending.chainId
:QUANTITY
- the chain ID specified by the sender.contractCreated
:BASE32
- address of the contract created.null
when it is not a contract deployment transaction.data
:DATA
- the data sent along with the transaction.epochHeight
:QUANTITY
- the epoch proposed by the sender. Note that this is NOT the epoch of the block containing this transaction.from
:BASE32
- address of the sender.gas
:QUANTITY
- gas provided by the sender.gasPrice
:QUANTITY
- gas price provided by the sender in Drip.hash
:DATA
, 32 Bytes - hash of the transaction.nonce
:QUANTITY
- the number of transactions made by the sender prior to this one.r
:DATA
, 32 Bytes - ECDSA signature r.s
:DATA
, 32 Bytes - ECDSA signature s.status
:QUANTITY
- 0 for success, 1 if an error occurred, 2 for skipped,null
when the transaction is skipped or not packed.storageLimit
:QUANTITY
- the storage limit specified by the sender.to
:BASE32
- address of the receiver.null
when it is a contract deployment transaction.transactionIndex
:QUANTITY
- the transaction's position in the block.null
when the transaction is pending.v
:QUANTITY
- ECDSA recovery id.value
:QUANTITY
- value transferred in Drip.
Note that the fields blockHash
, contractCreated
, status
, and transactionIndex
are provided by the node as they depend on the transaction's position within the ledger. The rest of the fields are included in or derived from the original transaction.
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getTransactionByHash","params":["0x497755f45baef13a35347933c48c0b8940f2cc3347477b5ed9f165581b082699"],"id":1}' -H "Content-Type: application/json" localhost:12539
// Result
{
"jsonrpc": "2.0",
"result": {
"blockHash": "0x564750c06c7afb10de8beebcf24411cc73439295d5abb1264d2c9b90eee5606f",
"chainId": "0x2",
"contractCreated": null,
"data": "0x0",
"epochHeight": "0x909c9f",
"from": "CFX:TYPE.USER:AARC9ABYCUE0HHZGYRR53M6CXEDGCCRMMYYBJGH4XG",
"gas": "0xf4240",
"gasPrice": "0x174876e800",
"hash": "0x497755f45baef13a35347933c48c0b8940f2cc3347477b5ed9f165581b082699",
"nonce": "0x3b518",
"r": "0x14da6cff1a3cd864b04d1b16f480fa023f449322e318b04bb1109b5754b516ce",
"s": "0x304070abe6488c3532ecb66f4be32b88ee35ce48a4607b8d0c86461987a79fc7",
"status": "0x0",
"storageLimit": "0x100",
"to": "CFX:TYPE.CONTRACT:ACC7UAWF5UBTNMEZVHU9DHC6SGHEA0403Y2DGPYFJP",
"transactionIndex": "0x0",
"v": "0x1",
"value": "0x3635c9adc5dea00000"
},
"id": 1
}
cfx_getBlockByHash
Returns information about a block, identified by its hash.
Parameters
DATA
, 32 Bytes - hash of a block.Boolean
- iftrue
, it returns the full transaction objects. Iffalse
, only the hashes of the transactions are returned.
params: [
"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
true
]
Returns
Object
- A block object, or null
when no block was found:
adaptive
:Boolean
-true
if the weight of the block is adaptive under the GHAST rule.blame
:QUANTITY
- if 0, then this block does not blame any blocks on its parent path. If it isn > 0
, then this block blames itsn
predecessors on its parent path, e.g. whenn = 1
, then the block blames its parent but not its parent's parent.deferredLogsBloomHash
:DATA
, 32 Bytes - the hash of the logs bloom after deferred execution at the block's epoch (assuming it is the pivot block).deferredReceiptsRoot
:DATA
, 32 Bytes - the Merkle root of the receipts after deferred execution at the block's epoch (assuming it is the pivot block).deferredStateRoot
:DATA
, 32 Bytes - the hash of the state trie root triplet after deferred execution at the block's epoch (assuming it is the pivot block).difficulty
:QUANTITY
- the PoW difficulty of this block.epochNumber
:QUANTITY
- the number of the epoch containing this block in the node's view of the ledger.null
when the epoch number is not determined (e.g. the block is not in the best block's past set).gasLimit
:QUANTITY
- the maximum gas allowed in this block.gasUsed
:QUANTITY
- the total gas used in this block.null
when the block is pending.hash
:DATA
, 32 Bytes - hash of the block.height
:QUANTITY
- the height of the block.miner
:BASE32
- the address of the beneficiary to whom the mining rewards were given.nonce
:DATA
, 8 Bytes - hash of the generated proof-of-work.parentHash
:DATA
, 32 Bytes - hash of the parent block.powQuality
:DATA
- the PoW quality.null
when the block is pending.refereeHashes
:Array
- array of referee block hashes.size
:QUANTITY
- the size of this block in bytes, excluding the block header.timestamp
:QUANTITY
- the unix timestamp for when the block was created.transactions
:Array
- array of transaction objects, or 32-byte transaction hashes, depending on the second parameter.transactionsRoot
:DATA
, 32 Bytes - the Merkle root of the transactions in this block.custom
:Array
- customized information. Note from v2.0custom
's type has changed from array ofnumber array
to array ofhex string
.blockNumber
:QUANTITY
- the number of this block's total order in the tree-graph.null
when the order is not determined. Added fromConflux-rust v1.1.5
posReference
:DATA
, 32 Bytes - the hash of the PoS newest committed block. Added fromConflux-rust v2.0.0
Note that the fields epochNumber
and gasUsed
are provided by the node as they depend on the ledger. The rest of the fields are included in or derived from the block header directly.
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getBlockByHash","params":["0x692373025c7315fa18b2d02139d08e987cd7016025920f59ada4969c24e44e06", false],"id":1}' -H "Content-Type: application/json" localhost:12539
// Result
{
"jsonrpc": "2.0",
"result": {
"adaptive": false,
"blame": 0,
"deferredLogsBloomHash": "0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5",
"deferredReceiptsRoot": "0x522717233b96e0a03d85f02f8127aa0e23ef2e0865c95bb7ac577ee3754875e4",
"deferredStateRoot": "0xd449df4ba49f5ab02abf261e976197beecf93c5198a6f0b6bd2713d84115c4ec",
"difficulty": "0xeee440",
"epochNumber": "0x1394cb",
"gasLimit": "0xb2d05e00",
"gasUsed": "0xad5ae8",
"hash": "0x692373025c7315fa18b2d02139d08e987cd7016025920f59ada4969c24e44e06",
"height": "0x1394c9",
"miner": "CFX:TYPE.USER:AARC9ABYCUE0HHZGYRR53M6CXEDGCCRMMYYBJGH4XG",
"nonce": "0x329243b1063c6773",
"parentHash": "0xd1c2ff79834f86eb4bc98e0e526de475144a13719afba6385cf62a4023c02ae3",
"powQuality": "0x2ab0c3513",
"refereeHashes": [
"0xcc103077ede14825a5667bddad79482d7bbf1f1a658fed6894fa0e9287fc6be1"
],
"size": "0x180",
"timestamp": "0x5e8d32a1",
"transactions": [
"0xedfa5b9c38ba51e791cc72b8f75ff758533c8c38f426eddee3fd95d984dd59ff"
],
"custom": ["0x12"],
"transactionsRoot": "0xfb245dae4539ea49812e822adbffa9dd2ee9b3de8f3d9a7d186d351dcc9a6ed4",
"posReference": "0xd1c2ff79834f86eb4bc98e0e526de475144a13719afba6385cf62a4023c02ae3",
},
"id": 1
}
cfx_getBlockByEpochNumber
Returns information about a block, identified by its epoch number.
Parameters
QUANTITY|TAG
- the epoch number, or the string"latest_mined"
,"latest_state"
,"latest_confirmed"
,"latest_checkpoint"
or"earliest"
, see the epoch number parameter.Boolean
- iftrue
, it returns the full transaction objects. Iffalse
, only the hashes of the transactions are returned
params: [
"latest_mined",
true
]
Returns
See cfx_getBlockByHash.
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getBlockByEpochNumber","params":["latest_mined", false],"id":1}' -H "Content-Type: application/json" localhost:12539
Result see cfx_getBlockByHash.
cfx_getBestBlockHash
Returns the hash of the best block.
Parameters
None.
Returns
DATA
, 32 Bytes - hash of the best block.
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getBestBlockHash","id":1}' -H "Content-Type: application/json" localhost:12539
// Result
{
"jsonrpc" : "2.0",
"result" : "0x7d54c03f4fe971d5c45d95dddc770a0ec8d5bd27d57c049ce8adc469269e35a4",
"id" : 1
}
cfx_epochNumber
Returns the epoch number corresponding to the given tag.
Parameters
TAG
- (optional, default:"latest_mined"
) String"latest_mined"
,"latest_state"
,"latest_confirmed"
,"latest_checkpoint"
or"earliest"
, see the epoch number parameter.
Returns
QUANTITY
- the integer epoch number corresponding to the given tag.
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_epochNumber","params":["latest_mined"],"id":1}' -H "Content-Type: application/json" localhost:12539
// Result
{
"jsonrpc" : "2.0",
"result" : "0x49",
"id" : 1
}
cfx_gasPrice
Returns the current price per gas in Drip.
Parameters
None.