| description | 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.
{% page-ref page="../tutorials/smart-contracts/deploy-a-smart-contract-on-avalanche-using-remix-and-metamask.md" %}
To interact with C-Chain via the JSON-RPC endpoint:
/ext/bc/C/rpcTo interact with other instances of the EVM via the JSON-RPC endpoint:
/ext/bc/blockchainID/rpcwhere blockchainID is the ID of the blockchain running the EVM.
To interact with C-Chain via the websocket endpoint:
/ext/bc/C/wsFor example, to interact with the C-Chain's Ethereum APIs via websocket on localhost you can use:
ws://127.0.0.1:9650/ext/bc/C/wsTo interact with other instances of the EVM via the websocket endpoint:
/ext/bc/blockchainID/wswhere blockchainID is the ID of the blockchain running the EVM.
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.
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
eth_getAssetBalance({
address: string,
blk: BlkNrOrHash,
assetID: string,
}) -> {balance: int}addressowner of the assetblkis the block number or hash at which to retrieve the balanceassetIDid of the asset for which the balance is requested
Example Call
curl -X POST --data '{
"jsonrpc": "2.0",
"method": "eth_getAssetBalance",
"params": [
"0x8723e5773847A4Eb5FeEDabD9320802c5c812F46",
"latest",
"3RvKBAmQnfYionFXMfW5P8TDZgZiogKbHjM8cjpu16LKAgF5T"
],
"id": 1
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/rpcExample Response
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1388"
}To interact with the avax specific RPC calls on the C-Chain:
/ext/bc/C/avaxTo interact with other instances of the EVM AVAX endpoints:
/ext/bc/blockchainID/avaxExport 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.
avax.export({
to: string,
amount: int,
assetID: string,
username: string,
password:string,
}) -> {txID: string}tois the X-Chain address the asset is sent to.amountis the amount of the asset to send.assetIDis the ID of the asset. To export AVAX use"AVAX"as theassetID.- The asset is sent from addresses controlled by
usernameandpassword.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.export",
"params" :{
"to":"X-avax1q9c6ltuxpsqz7ul8j0h0d0ha439qt70sr3x2m0",
"amount": 500,
"assetID": "2nzgmhZLuVq8jc7NNu2eahkKwoJcbFWXWJCxHBVWAJEZkhquoK",
"username":"myUsername",
"password":"myPassword"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"txID": "2W5JuFENitZKTpJsy9igBpTcEeBKxBHHGAUkgsSUnkjVVGQ9i8"
},
"id": 1
}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.
avax.exportAVAX({
to: string,
amount: int,
destinationChain: string,
from: []string, //optional
changeAddr: string, //optional
username: string,
password:string,
}) -> {txID: string}Request
fromis the C-Chain addresses the AVAX is sent from. They should be in hex format.tois the X-Chain address the AVAX is sent to. It should be in bech32 format.amountis the amount of nAVAX to send.destinationChainis the chain the AVAX is sent to. To export funds to the X-Chain, use"X".changeAddris 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
txIDis the txid of the completed ExportTx.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.exportAVAX",
"params" :{
"from": ["0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"],
"to":"X-avax1q9c6ltuxpsqz7ul8j0h0d0ha439qt70sr3x2m0",
"amount": 500,
"destinationChain": "X",
"changeAddr": "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC",
"username":"myUsername",
"password":"myPassword"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"txID": "2ffcxdkiKXXA4JdyRoS38dd7zoThkapNPeZuGPmmLBbiuBBHDa"
},
"id": 1
}Get the private key that controls a given address. The returned private key can be added to a user with avax.importKey.
avax.exportKey({
username: string,
password:string,
address:string
}) -> {privateKey: string}Request
usernamemust controladdress.addressis the address for which you want to export the corresponding private key. It should be in hex format.
Response
privateKeyis the CB58 endcoded string representation of the private key that controlsaddress. It has aPrivateKey-prefix and can be used to import a key viaavax.importKey.privateKeyHexis the hex string representation of the private key that controlsaddress. It can be used to import an account into Metamask.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.exportKey",
"params" :{
"username" :"myUsername",
"password":"myPassword",
"address": "0xc876DF0F099b3eb32cBB78820d39F5813f73E18C"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"privateKey": "PrivateKey-2o2uPgTSf3aR5nW6yLHjBEAiatAFKEhApvYzsjvAJKRXVWCYkE",
"privateKeyHex": "0xec381fb8d32168be4cf7f8d4ce9d8ca892d77ba574264f3665ad5edb89710157"
},
"id": 1
}}Gets the UTXOs that reference a given address.
avax.getUTXOs(
{
addresses: string,
limit: int, //optional
startIndex: { //optional
address: string,
utxo: string
},
sourceChain: string,
encoding: string, //optional
},
) ->
{
numFetched: int,
utxos: []string,
endIndex: {
address: string,
utxo: string
}
}utxosis a list of UTXOs such that each UTXO references at least one address inaddresses.- At most
limitUTXOs are returned. Iflimitis omitted or greater than 1024, it is set to 1024. - This method supports pagination.
endIndexdenotes the last UTXO returned. To get the next set of UTXOs, use the value ofendIndexasstartIndexin the next call. - If
startIndexis omitted, will fetch all UTXOs up tolimit. - When using pagination (ie when
startIndexis 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.
encodingsets the format for the returned UTXOs. Can be either "cb58" or "hex". Defaults to "cb58".
Suppose we want all UTXOs that reference at least one of C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.getUTXOs",
"params" :{
"addresses":["C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf"],
"sourceChain": "X",
"startIndex": {
"address": "C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf",
"utxo": "22RXW7SWjBrrxu2vzDkd8uza7fuEmNpgbj58CxBob9UbP37HSB"
},
"encoding": "cb58"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avaxThis gives response:
{
"jsonrpc": "2.0",
"result": {
"numFetched": "3",
"utxos": [
"11QEQTor9xZ1TyCyq8aFVShdP7YjM1ug9KuPUuMpgvQVz5qjEzo244NbJomjciNUPqUr1cD455dXhVrVNopnMXTQrTFY5kqrEVAQ3Ng9AnapQrYVEYiWc32F5CQuD3N5sB1EhQmMdJr5pis1QLjMmRQmut7Maafwup1vEU",
"11Eo6c9iUz3ERtmHbdUb3nzzMaqFffFQStshEsSTiFQP5xqfmeaeCFHCBajmoJUdQRHtkChGAmPucDfuCyBAEyGmmv2w8b7dX5sATxV7HxHZE4eak14GMGVEr7v3ij1B8mE82cymTJJz1X3PpRk2pTaxwEnLWfh1aAiTFC",
"118mpEHsia5sYYvKUx4j56mA7i1yvmLNyynm7LcmehcJJwMVY65smT4kGQgyc9DULwuaLTrUcsqbQutCdajoJXBdPVqvHMkYBTYQKs7WSmTXH8v7iUVqZfphMnS7VxVjGU1zykeTnbuAoZt4cFMUJzd8JaZk5eC82zmLmT"
],
"endIndex": {
"address": "C-avax1yzt57wd8me6xmy3t42lz8m5lg6yruy79m6whsf",
"utxo": "27q6nsuvtyT4mvXVnQQAXw1YKoTxCow5Qm91GZ678TU1SvUiC2"
},
"encoding": "cb58"
},
"id": 1
}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.
avax.import({
to: string,
sourceChain: string,
username: string,
password:string,
}) -> {txID: string}Request
tois the address the asset is sent to. This must be the same as thetoargument in the corresponding call to the C-Chain'sexport.sourceChainis the ID or alias of the chain the asset is being imported from. To import funds from the X-Chain, use"X".usernameis the user that controlsto.
Response
txIDis the ID of the completed ImportTx.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.import",
"params" :{
"to":"0x4b879aff6b3d24352Ac1985c1F45BA4c3493A398",
"sourceChain":"X",
"username":"myUsername",
"password":"myPassword"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"txID": "6bJq9dbqhiQvoshT3uSUbg9oB24n7Ei6MLnxvrdmao78oHR9t"
},
"id": 1
}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.
avax.importAVAX({
to: string,
sourceChain: string,
username: string,
password:string,
}) -> {txID: string}Request
tois the address the AVAX is sent to. It should be in hex format.sourceChainis the ID or alias of the chain the AVAX is being imported from. To import funds from the X-Chain, use"X".usernameis the user that controlsto.
Response
txIDis the ID of the completed ImportTx.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.importAVAX",
"params" :{
"to":"0x4b879aff6b3d24352Ac1985c1F45BA4c3493A398",
"sourceChain":"X",
"username":"myUsername",
"password":"myPassword"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"txID": "LWTRsiKnEUJC58y8ezAk6hhzmSMUCtemLvm3LZFw8fxDQpns3"
},
"id": 1
}Give a user control over an address by providing the private key that controls the address.
avax.importKey({
username: string,
password:string,
privateKey:string
}) -> {address: string}Request
- Add
privateKeytousername's set of private keys.
Response
addressis the addressusernamenow controls with the private key. It will be in hex format.
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.importKey",
"params" :{
"username" :"myUsername",
"password":"myPassword",
"privateKey":"PrivateKey-2o2uPgTSf3aR5nW6yLHjBEAiatAFKEhApvYzsjvAJKRXVWCYkE"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc": "2.0",
"result": {
"address": "0xc876DF0F099b3eb32cBB78820d39F5813f73E18C"
},
"id": 1
}Send a signed transaction to the network. encoding specifies the format of the signed transaction. Can be either "cb58" or "hex". Defaults to "cb58".
avax.issueTx({
tx: string,
encoding: string, //optional
}) -> {
txID: string
}curl -X POST --data '{
"jsonrpc":"2.0",
"id" : 1,
"method" :"avax.issueTx",
"params" :{
"tx":"6sTENqXfk3gahxkJbEPsmX9eJTEFZRSRw83cRJqoHWBiaeAhVbz9QV4i6SLd6Dek4eLsojeR8FbT3arFtsGz9ycpHFaWHLX69edJPEmj2tPApsEqsFd7wDVp7fFxkG6HmySR",
"encoding": "cb58"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc":"2.0",
"id" :1,
"result" :{
"txID":"NUPLwbt2hsYxpQg4H2o451hmTWQ4JZx2zMzM4SinwtHgAdX1JLPHXvWSXEnpecStLj"
}
}Get the status of an atomic transaction sent to the network.
avax.getAtomicTxStatus({txID: string}) -> {
status: string,
blockHeight: string // returned when status is Accepted
}status is one of:
Accepted: The transaction is (or will be) accepted by every node. Check theblockHeightpropertyProcessing: The transaction is being voted on by this nodeDropped: The transaction was dropped by this node because it thought the transaction invalidUnknown: The transaction hasn’t been seen by this node
curl -X POST --data '{
"jsonrpc":"2.0",
"id" :1,
"method" :"avax.getAtomicTxStatus",
"params" :{
"txID":"2QouvFWUbjuySRxeX5xMbNCuAaKWfbk5FeEa2JmoF85RKLk2dD"
}
}' -H 'content-type:application/json;' 127.0.0.1:9650/ext/bc/C/avax{
"jsonrpc":"2.0",
"id" :1,
"result" :{
"status":"Accepted",
"blockHeight": "1"
}
}