Smart contract methods
Node RPC call example:
curl -X POST --data '{
"jsonrpc":"2.0",
"key":"myAPIkey",
"id":1,
"method":"contract_call",
"params": [
{
"from": "0x...",
"contract": "0x...",
"method": "transfer",
"args": [
{
"index": 0,
"format": "hex",
"value": "0xcbb98....91"
},
{
"index": 1,
"format": "dna",
"value": "1000"
}
]
}
]
}'
Args
Smart contract methods may have a dynamic list of parameters args
that is specific to each smart contract:
[
{
"index": 0,
"format": "hex",
"value": "0x11"
},
{
"index": 1,
"format": "uint64",
"value": "11"
}
]
Index
: index of parameterFormat
: format of parameterbyte
,uint64
,string
,bigint
,hex
(default),dna
(float representation).Value
: parameter value interpreted according to the specified format
contract_deploy
and contract_estimateDeploy
methods
Method contract_deploy
creates DeployContractTx
transaction to deploy a custom contract specified by code
and nonce
or a predefined contract specified by codeHash
. Use contract_estimateDeploy
method to estimate gas consumption of the future DeployContractTx
transaction and address of the future contract.
Parameters for custom contract:
from
: sender addresscode
: compiled WebAssembly code of a custom contractargs
: dynamic list of parameters of constructor which is specific to a particular contractnonce
: unique nonce for the custom contract (allows you to generate unique addresses for contracts with the samecode
andargs
)maxFee
: must cover a sum oftxFee
+gasCost
(see more aboutmaxFee
)
Example:
{
"method": "contract_estimateDeploy",
"params": [
{
"from": "<address>",
"code": "0x0061736d01000000013...",
"nonce": "0x01",
"maxFee": 1,
"args": [
{
"index": 0,
"format": "uint64",
"value": "0"
}
]
}
],
"id": 1,
"key": "<key>"
}
Parameters for predefined contract:
from
: sender addresscodeHash
: predefined smart contract codeamount
: amount of coins that will be blocked at the smart contract stakeargs
: dynamic list of parameters relevant to the specified smart contractmaxFee
: must cover a sum oftxFee
+gasCost
(see more aboutmaxFee
)
Example:
{
"method": "contract_estimateDeploy",
"params": [
{
"from": "<address>",
"codeHash": "0x01",
"amount": 1100,
"maxFee": 1,
"args": [
{
"index": 0,
"format": "uint64",
"value": "0"
}
]
}
],
"id": 1,
"key": "<key>"
}
contract_call
and contract_estimateCall
methods
Method contract_call
creates CallContractTx
transaction to call specified contract's method. Use contract_estimateCall
method to estimate gas consumption of the future CallContractTx
transaction.
Parameters:
from
: sender addresscontract
: contract addressmethod
: name of the called contract's methodamount
: amount of coins transferred to the contract addressargs
: dynamic list of parameters relevant to specified contract's methodbroadcastBlock
: block number when a postponed transaction should be published by the node
Example:
{
{
"method": "contract_estimateCall",
"params": [
{
"from": "<sender address>",
"contract": "<smart contract address>",
"method": "transfer",
"amount": 123,
"maxFee": 1,
"args": [
{
"index": 0,
"format": "hex",
"value": "<address>"
},
{
"index": 1,
"format": "dna",
"value": "123"
}
]
}
],
"id": 1,
"key": "<key>"
}
contract_terminate
and contract_estimateTerminate
methods
Method contract_terminate
creates TerminateContractTx
transaction to terminate the contract. Use contract_estimateTerminate
method to estimates gas consumption of the future TerminateContractTx
transaction.
Parameters:
from
: sender addresscontract
: contract addressargs
: dynamic list of parameters relevant to specified contract's method
Example:
{
"method": "contract_estimateTerminate",
"params": [
{
"from": "<sender address>",
"contract": "<smart contract address>",
"maxFee": 1,
"args": [
{
"index": 0,
"format": "hex",
"value": "<Destination address>"
}
]
}
]
}
contract_readData
method
Returns requested data of the smart contract's state.
Parameters:
contract
: smart contract addresskey
: key of the requested dataformat
: data format
Example:
{
"method": "contract_readData",
"params": ["<smart contract address>", "key", "hex"]
}
contract_iterateMap
method
Returns requested array data (map
) of the smart contract's state and continuation token to iterate the data.
Parameters:
contract
: smart contract addressmap
: the name of the requested array data (see maps available for the specific smart contract)continuationToken
: iteration token should benull
for the first callkeyFormat
: key format of the requested array datavalueFormat
: data format of the requested array data
Example:
{
"method": "contract_iterateMap",
"params": [
"<smart contract address>",
"addr", //map name
null, //continuationToken
"hex", //keyFormat
"hex", //valueFormat
10
]
}
contract_readMap
method
Returns requested value for the given key from the array data (map
) of the smart contract's state.
Parameters:
contract
: smart contract addressmap
: the name of the requested array data (see maps available for the specific smart contract)key
: key of the requested array datavalueFormat
: data format of the requested array data
Example:
{
"method": "contract_readMap",
"params": [
"<smart contract address>",
"addr",
"0x725....70", //key in the array data
"hex"
]
}
contract_readonlyCall
method
Calls specified smart contract's method without changing the state.
Parameters:
contract
: smart contract addressmethod
: key of the requested dataformat
: data formatargs
: dynamic list of parameters
Example:
{
"method": "contract_readonlyCall",
"params": [
{
"contract": "<smart contract address>",
"method": "getHash",
"format": "hex",
"args": [
{
"index": 0,
"format": "hex",
"value": "123"
}
]
}
]
}
contract_getStake
method
Returns amount of coins blocked at the stake of the smart contract
Parameters:
Contract
: smart contract address
Example:
{
"method": "contract_getStake",
"params": ["<smart contract address>"]
}
bcn_txReceipt
method
Returns receipt of the specified transaction
Parameters:
hash
: hash of mined transaction
Example:
{
"method": "bcn_txReceipt",
"params": ["<transaction hash>"]
}
Response example:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"contract": "...",
"method": "...",
"success": false,
"gasUsed": 200,
"txHash": "0x217...",
"error": "index out of range",
"gasCost": "0.00032916392364",
"txFee": "0.002320605661662"
}
}
contract
: address of the called contractmethod
: called method of the smart contractsuccess
: whether transaction changed the state or notgasUsed
: amount of gas usedtxHash
: contract transaction hasherror
: error textgasCost
: gas cost, iDNAtxFee
: transaction cost, iDNA
Errors:
index out of range
:args
array has a missing element with requiredindex
.
contract_events
method
Returns the list of events of the specified smart contract
Parameters:
contract
: smart contract address
Example:
{
"method": "contract_events",
"params": [
{
"contract": "<smart contract address>"
}
]
}
contract_subscribeToEvent
and contract_unsubscribeFromEvent
methods
Subscribes/unsubscribes from the specified event of the smart contract
Parameters:
contract
: smart contract addressevent
: event name
Example:
{
"method": "contract_subscribeToEvent",
"params": ["<smart contract address>", "<event name>"]
}
bcn_feePerGas
method
The method returns the current gasPrice
.
Example:
{
"method": "bcn_feePerGas",
"params": [],
"id": 1,
"key": "<API key>"
}
Example:
{
"result": 1651527663100 //0.0000016515276631 iDNA
}
The minimum gasPrice
is 0.01
/NetworkSize
.