# Contract
# Create Contract
Essentially, creating contract is sending a special transaction whose transaction type is 1 with contract's code and specified parameters. It has the following steps.
- Call RPC method
contract_getCreateContractToAddress
to generate a new contract address - Encode passed-in parameters according to ABI. This step can be completed by calling either
abi.encodeParameters
in vite.js, orcontract_getCreateContractParams
in RPC - Call RPC method
contract_getCreateContractData
to generate transaction data - Call RPC method
tx_sendRawTx
to create contract. Here parametertoAddress
is the contract address generated in Step 1.data
is transaction data created in Step 3.blockType
is set to 1, indicating creating contract transaction.amount
andtokenId
stand for the amount of token that are transferred to the contract during creation.fee
is the cost, currently 10 VITE in the Mainnet.
Tips
Above steps are implemented in method builtinTxBlock.createContract
in vite.js
# Call Contract
Essentially, calling contract is sending a special transaction to smart contract's account, specifying calling method name and parameters.
- Encode method name and passed-in parameters according to ABI. This step can be completed by calling either
abi.encodeFunctionCall
method in vite.js (recommended), orcontract_getCallContractData
in RPC - Call RPC method
tx_sendRawTx
to send a transaction to contract. Here parametertoAddress
is the contract address.data
is transaction data created in Step 1.blockType
is set to 2 for calling contract.amount
andtokenId
stand for the amount of token that are transferred to the contract.fee
is 0, standing for free.
Tips
Above steps are implemented in method builtinTxBlock.callContract
in vite.js
# Read States Off-chain
Contract's states can be accessed off-chain through getter
methods. The ABI definition of getter
and off-chain code are generated when the contract is compiled.
- Encode
getter
method name and passed-in parameters according to ABI. This step can be completed by calling eitherabi.encodeFunctionCall
method in vite.js (recommended), orcontract_getCallOffChainData
in RPC - Call RPC method
contract_callOffChainMethod
to visit contract's state
# Call Built-in Contract
Similar to calling common contract, calling built-in contract is sending a special transaction to built-in contract's account, specifying calling method name and parameters. Vite has implemented Staking, Token Issuance and Consensus 3 built-in smart contracts.
- Generate transaction data by calling relevant
xxx_getxxxData
RPC method. For example, callmintage_getMintData
when issuing new token. - Call RPC method
tx_sendRawTx
to send the transaction to built-in contract.
Tips
Most methods of built-in contracts are provided in builtinTxBlock
in vite.js
# API
# contract_getCreateContractToAddress
Return a new address for contract
Parameters:
Address
: The address of requesteruint64
: Current account block heightHash
: The hash of previous account block
Returns:
Address
New contract address
Example:
# contract_getCreateContractParams
Encode passed-in parameters for creating contract
Parameters:
string
: Contract's ABI definition[]string
: Passed-in parameters. Element is either a string for simple type or JSON string for complex
Returns:
[]byte
Data
Example:
# contract_getCreateContractData
Generate request data for creating contract
Parameters:
gid
:Gid
: The id of delegated consensus group. A contract must assign a consensus group to have contract transaction executed. For example, global delegated consensus group has id "00000000000000000002"confirmTime
:uint8
: The number of snapshot blocks (between 0-75) by which request sent to this contract is confirmed before responding to the transaction. 0 stands for no additional confirmation required. This parameter must be positive if random number, timestamp or snapshot block height is used in the contract. If , additional quota will be consumed for responseseedCount
:uint8
: The number of snapshot blocks (between 0-75) having random seed by which request sent to this contract is confirmed before responding to the transaction. 0 stands for no additional confirmation required. This parameter must be positive if random number is used in the contract. HavingquotaRatio
:uint8
: Quota multiply factor between 10-100. Additional quota will be consumed for contract's caller when a number above 10 is passed in. For example, quotaRatio=15 means 1.5 times of quota will be charged for caller.hexCode
:string
: The hex code of contractparams
:[]byte
: Encoded parameters, returned bycontract_getCreateContractParams
Returns:
[]byte
Data
Example:
# contract_getCallContractData
Generate request data for calling contract
Parameters:
string
: Contract's ABI definitionstring
: Calling method name[]string
: Passed-in parameters. Element is either a string for simple type or JSON string for complex
Returns:
[]byte
Data
Example:
# contract_getContractInfo
Return contract information by specified contract address
Parameters:
string
: Contract address
Returns:
ContractInfo
code
:[]byte
Contract's codegid
:Gid
The id of delegated consensus group that the contract has assigned 3confirmTime
:uint8
: The number of snapshot blocks by which request sent to this contract is confirmed before responding to the transaction 4seedCount
:uint8
: The number of snapshot blocks having random seed by which request sent to this contract is confirmed before responding to the transaction
Example:
# contract_getCallOffChainData
Encode passed-in parameters for getter
method. The returned value can be passed in method contract_callOffChainMethod
as data input
Parameters:
string
Contract's ABI definitionstring
getter
method name[]string
Passed-in parameters
Returns:
[]byte
Example:
# contract_callOffChainMethod
Query contract's state by calling the specified getter
method off-chain. Please note that only one parameter of either offchainCode
or offChainCodeBytes
is needed.
Parameters:
Object
:selfAddr
:Address
Contract addressoffChainCode
:string
Hex code generated asOffChain Binary
when compiling the contract by specifying--bin
argumentoffChainCodeBytes
:[]byte
Base64 byte code generated asOffChain Binary
when compiling the contract by specifying--bin
argumentData
:[]byte
Encoded parameters, returned bycontract_getCallOffChainData
Returns:
[]byte
Return value of thegetter
methodExample: