Contract Chain (C-Chain) API
The C-Chain is an instance of the Ethereum Virtual Machine (EVM)
Note: Ethereum has its own notion of networkID and chainID. These have no relationship to Avalanche’s view of networkID and chainID and are purely internal to the C-Chain. On Mainnet, the C-Chain uses 1 and 43114 for these values. On the Fuji Testnet, it uses 1 and 43113 for these values. networkID and chainID can also be obtained using the net_version and eth_chainId methods.

Deploying a Smart Contract

Ethereum APIs

Ethereum API Endpoints

JSON-RPC Endpoints

To interact with C-Chain via the JSON-RPC endpoint:
1
/ext/bc/C/rpc
Copied!
To interact with other instances of the EVM via the JSON-RPC endpoint:
1
/ext/bc/blockchainID/rpc
Copied!
where blockchainID is the ID of the blockchain running the EVM.

WebSocket Endpoints

To interact with C-Chain via the websocket endpoint:
1
/ext/bc/C/ws
Copied!
For example, to interact with the C-Chain's Ethereum APIs via websocket on localhost you can use:
1
ws://127.0.0.1:9650/ext/bc/C/ws
Copied!
Note: on localhost, use ws://. When using the Public API or another host that supports encryption, use wss://.
To interact with other instances of the EVM via the websocket endpoint:
1
/ext/bc/blockchainID/ws
Copied!
where blockchainID is the ID of the blockchain running the EVM.

Methods

Standard Ethereum APIs

Avalanche offers an API interface identical to Geth's API except that it only supports the following services:
    web3_
    net_
    eth_
    personal_
    txpool_
    debug_
You can interact with these services the same exact way you’d interact with Geth. See the Ethereum Wiki’s JSON-RPC Documentation and Geth’s JSON-RPC Documentation for a full description of this API.

eth_getAssetBalance

In addition to the standard Ethereum APIs, Avalanche offers eth_getAssetBalance to retrieve the balance of first class Avalanche Native Tokens on the C-Chain (excluding AVAX, which must be fetched with eth_getBalance).
Signature
1
eth_getAssetBalance({
2
address: string,
3
blk: BlkNrOrHash,
4
assetID: string,
5
}) -> {balance: int}
Copied!
    address owner of the asset
    blk is the block number or hash at which to retrieve the balance
    assetID id of the asset for which the balance is requested
Example Call
1
curl -X POST --data '{
2
"jsonrpc": "2.0",
3
"method": "eth_getAssetBalance",
4
"params": [
5
"0x8723e5773847A4Eb5FeEDabD9320802c5c812F46",
6
"latest",
7
"3RvKBAmQnfYionFXMfW5P8TDZgZiogKbHjM8cjpu16LKAgF5T"
8
],
9
"id": 1
10
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/rpc
Copied!
Example Response
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0x1388"
5
}
Copied!

eth_baseFee

Get the base fee for the next block.

Signature

1
eth_baseFee() -> {}
Copied!
result is the hex value of the base fee for the next block.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"eth_baseFee",
5
"params" :{}
6
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/rpc
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0x34630b8a00"
5
}
Copied!

eth_maxPriorityFeePerGas

Get the priority fee needed to be included in a block.

Signature

1
eth_maxPriorityFeePerGas() -> {}
Copied!
result is hex value of the priority fee needed to be included in a block.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"eth_maxPriorityFeePerGas",
5
"params" :{}
6
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/rpc
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0x2540be400"
5
}
Copied!
For more information on dynamic fees see the C-Chain section of the transaction fee documentation.

Avalanche Specific APIs

Avalanche Specific API Endpoints

To interact with the avax specific RPC calls on the C-Chain:
1
/ext/bc/C/avax
Copied!
To interact with other instances of the EVM AVAX endpoints:
1
/ext/bc/blockchainID/avax
Copied!

avax.getAtomicTx

Gets a transaction by its ID. Optional encoding parameter to specify the format for the returned transaction. Can be either cb58 or hex. Defaults to cb58.

Signature

1
avax.getAtomicTx({
2
txID: string,
3
encoding: string, //optional
4
}) -> {
5
tx: string,
6
encoding: string,
7
blockHeight: string
8
}
Copied!
Request
    txID is the transacion ID. It should be in cb58 format.
    encoding is the encoding format to use. Can be either cb58 or hex. Defaults to cb58.
Response
    tx is the transaction encoded to encoding.
    encoding is the encoding.
    blockHeight is the height of the block which the transaction was included in.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.getAtomicTx",
5
"params" :{
6
"txID":"2GD5SRYJQr2kw5jE73trBFiAgVQyrCaeg223TaTyJFYXf2kPty",
7
"encoding": "cb58"
8
}
9
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"tx": "111111115k3oJsP1JGxvsZPFh1WXzSYNVDtvgvZ4qDWtAs5ccogA1RtT3Me5x8xgkj7cyxaNGEHuMv5U34qo94fnvHweLeSRf31ggt3MoD7MHSDw6LbiXeaJa3uwBDHzd6tPxw17478X13Ff7DkHtbWYYx2WTcJYk4nVP2swCHjBE3uQjmu6RdhtgZCxvnD6YVpEsXqvam6cDzpf5BLaosYCSt5p8SmLU2ppaSb6DPA4EW4679ygUxiDNP3SFagjUvzSrfBJRFCzsan4ZJqH8haYqpJL42TUN4q3eFKvscZfp2v2WWEEwJYmJP4Nc1P7wndeMxPFEm3vjkBaVUZ5k25TpYtghq6Kx897dVNaMSsTAoudwqTR1cCUGiR3bLfi82MgnvuApsYqtRfaD9deSHc8UA1ohPehkj9eaY",
5
"encoding": "cb58",
6
"blockHeight": "1"
7
},
8
"id": 1
9
}
Copied!

avax.export

Export an asset from the C-Chain to the X-Chain. After calling this method, you must call avm.import on the X-Chain to complete the transfer.

Signature

1
avax.export({
2
to: string,
3
amount: int,
4
assetID: string,
5
username: string,
6
password:string,
7
}) -> {txID: string}
Copied!
    to is the X-Chain address the asset is sent to.
    amount is the amount of the asset to send.
    assetID is the ID of the asset. To export AVAX use "AVAX" as the assetID.
    The asset is sent from addresses controlled by username and password.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.export",
5
"params" :{
6
"to":"X-avax1q9c6ltuxpsqz7ul8j0h0d0ha439qt70sr3x2m0",
7
"amount": 500,
8
"assetID": "2nzgmhZLuVq8jc7NNu2eahkKwoJcbFWXWJCxHBVWAJEZkhquoK",
9
"username":"myUsername",
10
"password":"myPassword"
11
}
12
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"txID": "2W5JuFENitZKTpJsy9igBpTcEeBKxBHHGAUkgsSUnkjVVGQ9i8"
5
},
6
"id": 1
7
}
Copied!

avax.exportAVAX

DEPRECATED—instead use avax.export.
Send AVAX from the C-Chain to the X-Chain. After calling this method, you must call avm.importAVAX on the X-Chain to complete the transfer.

Signature

1
avax.exportAVAX({
2
to: string,
3
amount: int,
4
destinationChain: string,
5
from: []string, //optional
6
changeAddr: string, //optional
7
username: string,
8
password:string,
9
}) -> {txID: string}
Copied!
Request
    from is the C-Chain addresses the AVAX is sent from. They should be in hex format.
    to is the X-Chain address the AVAX is sent to. It should be in bech32 format.
    amount is the amount of nAVAX to send.
    destinationChain is the chain the AVAX is sent to. To export funds to the X-Chain, use "X".
    changeAddr is the C-Chain address where any change is sent to. It should be in hex format.
    The AVAX is sent from addresses controlled by username
Response
    txID is the txid of the completed ExportTx.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.exportAVAX",
5
"params" :{
6
"from": ["0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"],
7
"to":"X-avax1q9c6ltuxpsqz7ul8j0h0d0ha439qt70sr3x2m0",
8
"amount": 500,
9
"destinationChain": "X",
10
"changeAddr": "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC",
11
"username":"myUsername",
12
"password":"myPassword"
13
}
14
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"txID": "2ffcxdkiKXXA4JdyRoS38dd7zoThkapNPeZuGPmmLBbiuBBHDa"
5
},
6
"id": 1
7
}
Copied!

avax.exportKey

Get the private key that controls a given address. The returned private key can be added to a user with avax.importKey.

Signature

1
avax.exportKey({
2
username: string,
3
password:string,
4
address:string
5
}) -> {privateKey: string}
Copied!
Request
    username must control address.
    address is the address for which you want to export the corresponding private key. It should be in hex format.
Response
    privateKey is the CB58 endcoded string representation of the private key that controls address. It has a PrivateKey- prefix and can be used to import a key via avax.importKey.
    privateKeyHex is the hex string representation of the private key that controls address. It can be used to import an account into Metamask.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.exportKey",
5
"params" :{
6
"username" :"myUsername",
7
"password":"myPassword",
8
"address": "0xc876DF0F099b3eb32cBB78820d39F5813f73E18C"
9
}
10
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"privateKey": "PrivateKey-2o2uPgTSf3aR5nW6yLHjBEAiatAFKEhApvYzsjvAJKRXVWCYkE",
5
"privateKeyHex": "0xec381fb8d32168be4cf7f8d4ce9d8ca892d77ba574264f3665ad5edb89710157"
6
},
7
"id": 1
8
}}
Copied!

avax.getUTXOs

Gets the UTXOs that reference a given address.

Signature

1
avax.getUTXOs(
2
{
3
addresses: string,
4
limit: int, //optional
5
startIndex: { //optional
6
address: string,
7
utxo: string
8
},
9
sourceChain: string,
10
encoding: string, //optional
11
},
12
) ->
13
{
14
numFetched: int,
15
utxos: []string,
16
endIndex: {
17
address: string,
18
utxo: string
19
}
20
}
Copied!
    utxos is a list of UTXOs such that each UTXO references at least one address in addresses.
    At most limit UTXOs are returned. If limit is omitted or greater than 1024, it is set to 1024.
    This method supports pagination. endIndex denotes the last UTXO returned. To get the next set of UTXOs, use the value of endIndex as startIndex in the next call.
    If startIndex is omitted, will fetch all UTXOs up to limit.
    When using pagination (ie when startIndex is provided), UTXOs are not guaranteed to be unique across multiple calls. That is, a UTXO may appear in the result of the first call, and then again in the second call.
    When using pagination, consistency is not guaranteed across multiple calls. That is, the UTXO set of the addresses may have changed between calls.
    encoding sets the format for the returned UTXOs. Can be either "cb58" or "hex". Defaults to "cb58".

Example

Suppose we want all UTXOs that reference at least one of C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf.
1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.getUTXOs",
5
"params" :{
6
"addresses":["C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf"],
7
"sourceChain": "X",
8
"startIndex": {
9
"address": "C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf",
10
"utxo": "22RXW7SWjBrrxu2vzDkd8uza7fuEmNpgbj58CxBob9UbP37HSB"
11
},
12
"encoding": "cb58"
13
}
14
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!
This gives response:
1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"numFetched": "3",
5
"utxos": [
6
"11QEQTor9xZ1TyCyq8aFVShdP7YjM1ug9KuPUuMpgvQVz5qjEzo244NbJomjciNUPqUr1cD455dXhVrVNopnMXTQrTFY5kqrEVAQ3Ng9AnapQrYVEYiWc32F5CQuD3N5sB1EhQmMdJr5pis1QLjMmRQmut7Maafwup1vEU",
7
"11Eo6c9iUz3ERtmHbdUb3nzzMaqFffFQStshEsSTiFQP5xqfmeaeCFHCBajmoJUdQRHtkChGAmPucDfuCyBAEyGmmv2w8b7dX5sATxV7HxHZE4eak14GMGVEr7v3ij1B8mE82cymTJJz1X3PpRk2pTaxwEnLWfh1aAiTFC",
8
"118mpEHsia5sYYvKUx4j56mA7i1yvmLNyynm7LcmehcJJwMVY65smT4kGQgyc9DULwuaLTrUcsqbQutCdajoJXBdPVqvHMkYBTYQKs7WSmTXH8v7iUVqZfphMnS7VxVjGU1zykeTnbuAoZt4cFMUJzd8JaZk5eC82zmLmT"
9
],
10
"endIndex": {
11
"address": "C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf",
12
"utxo": "27q6nsuvtyT4mvXVnQQAXw1YKoTxCow5Qm91GZ678TU1SvUiC2"
13
},
14
"encoding": "cb58"
15
},
16
"id": 1
17
}
Copied!

avax.import

Finalize the transfer of a non-AVAX or AVAX from the X-Chain to the C-Chain. Before this method is called, you must call the X-Chain's avm.export method to initiate the transfer.

Signature

1
avax.import({
2
to: string,
3
sourceChain: string,
4
username: string,
5
password:string,
6
}) -> {txID: string}
Copied!
Request
    to is the address the asset is sent to. This must be the same as the to argument in the corresponding call to the C-Chain's export.
    sourceChain is the ID or alias of the chain the asset is being imported from. To import funds from the X-Chain, use "X".
    username is the user that controls to.
Response
    txID is the ID of the completed ImportTx.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.import",
5
"params" :{
6
"to":"0x4b879aff6b3d24352Ac1985c1F45BA4c3493A398",
7
"sourceChain":"X",
8
"username":"myUsername",
9
"password":"myPassword"
10
}
11
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"txID": "6bJq9dbqhiQvoshT3uSUbg9oB24n7Ei6MLnxvrdmao78oHR9t"
5
},
6
"id": 1
7
}
Copied!

avax.importAVAX

DEPRECATED—instead use avax.import
Finalize a transfer of AVAX from the X-Chain to the C-Chain. Before this method is called, you must call the X-Chain's avm.exportAVAX method to initiate the transfer.

Signature

1
avax.importAVAX({
2
to: string,
3
sourceChain: string,
4
username: string,
5
password:string,
6
}) -> {txID: string}
Copied!
Request
    to is the address the AVAX is sent to. It should be in hex format.
    sourceChain is the ID or alias of the chain the AVAX is being imported from. To import funds from the X-Chain, use "X".
    username is the user that controls to.
Response
    txID is the ID of the completed ImportTx.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.importAVAX",
5
"params" :{
6
"to":"0x4b879aff6b3d24352Ac1985c1F45BA4c3493A398",
7
"sourceChain":"X",
8
"username":"myUsername",
9
"password":"myPassword"
10
}
11
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"txID": "LWTRsiKnEUJC58y8ezAk6hhzmSMUCtemLvm3LZFw8fxDQpns3"
5
},
6
"id": 1
7
}
Copied!

avax.importKey

Give a user control over an address by providing the private key that controls the address.

Signature

1
avax.importKey({
2
username: string,
3
password:string,
4
privateKey:string
5
}) -> {address: string}
Copied!
Request
    Add privateKey to username's set of private keys.
Response
    address is the address username now controls with the private key. It will be in hex format.

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.importKey",
5
"params" :{
6
"username" :"myUsername",
7
"password":"myPassword",
8
"privateKey":"PrivateKey-2o2uPgTSf3aR5nW6yLHjBEAiatAFKEhApvYzsjvAJKRXVWCYkE"
9
}
10
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc": "2.0",
3
"result": {
4
"address": "0xc876DF0F099b3eb32cBB78820d39F5813f73E18C"
5
},
6
"id": 1
7
}
Copied!

avax.issueTx

Send a signed transaction to the network. encoding specifies the format of the signed transaction. Can be either "cb58" or "hex". Defaults to "cb58".

Signature

1
avax.issueTx({
2
tx: string,
3
encoding: string, //optional
4
}) -> {
5
txID: string
6
}
Copied!

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" : 1,
4
"method" :"avax.issueTx",
5
"params" :{
6
"tx":"6sTENqXfk3gahxkJbEPsmX9eJTEFZRSRw83cRJqoHWBiaeAhVbz9QV4i6SLd6Dek4eLsojeR8FbT3arFtsGz9ycpHFaWHLX69edJPEmj2tPApsEqsFd7wDVp7fFxkG6HmySR",
7
"encoding": "cb58"
8
}
9
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"result" :{
5
"txID":"NUPLwbt2hsYxpQg4H2o451hmTWQ4JZx2zMzM4SinwtHgAdX1JLPHXvWSXEnpecStLj"
6
}
7
}
Copied!

avax.getAtomicTxStatus

Get the status of an atomic transaction sent to the network.

Signature

1
avax.getAtomicTxStatus({txID: string}) -> {
2
status: string,
3
blockHeight: string // returned when status is Accepted
4
}
Copied!
status is one of:
    Accepted: The transaction is (or will be) accepted by every node. Check the blockHeight property
    Processing: The transaction is being voted on by this node
    Dropped: The transaction was dropped by this node because it thought the transaction invalid
    Unknown: The transaction hasn’t been seen by this node

Example Call

1
curl -X POST --data '{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"method" :"avax.getAtomicTxStatus",
5
"params" :{
6
"txID":"2QouvFWUbjuySRxeX5xMbNCuAaKWfbk5FeEa2JmoF85RKLk2dD"
7
}
8
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax
Copied!

Example Response

1
{
2
"jsonrpc":"2.0",
3
"id" :1,
4
"result" :{
5
"status":"Accepted",
6
"blockHeight": "1"
7
}
8
}
Copied!
Last modified 7d ago