# 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.

  1. Call RPC method contract_getCreateContractToAddress to generate a new contract address
  2. Encode passed-in parameters according to ABI. This step can be completed by calling either abi.encodeParameters in vite.js, or contract_getCreateContractParams in RPC
  3. Call RPC method contract_getCreateContractData to generate transaction data
  4. Call RPC method tx_sendRawTx to create contract. Here parameter toAddress 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 and tokenId 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.

  1. 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), or contract_getCallContractData in RPC
  2. Call RPC method tx_sendRawTx to send a transaction to contract. Here parameter toAddress is the contract address. data is transaction data created in Step 1. blockType is set to 2 for calling contract. amount and tokenId 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.

  1. Encode getter method name and passed-in parameters according to ABI. This step can be completed by calling either abi.encodeFunctionCall method in vite.js (recommended), or contract_getCallOffChainData in RPC
  2. 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.

  1. Generate transaction data by calling relevant xxx_getxxxData RPC method. For example, call mintage_getMintData when issuing new token.
  2. 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 requester
    • uint64: Current account block height
    • Hash: 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 confirmTime>0confirmTime>0, additional quota will be consumed for response
    • seedCount: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. Having confirmTime>=seedCountconfirmTime>=seedCount
    • quotaRatio: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 contract
    • params:[]byte: Encoded parameters, returned by contract_getCreateContractParams
  • Returns:

    • []byte Data
  • Example:

# contract_getCallContractData

Generate request data for calling contract

  • Parameters:

    • string: Contract's ABI definition
    • string: 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

    1. code: []byte Contract's code
    2. gid: Gid The id of delegated consensus group that the contract has assigned 3 confirmTime:uint8: The number of snapshot blocks by which request sent to this contract is confirmed before responding to the transaction 4 seedCount: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 definition
    • string 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 address
      • offChainCode:string Hex code generated as OffChain Binary when compiling the contract by specifying --bin argument
      • offChainCodeBytes:[]byte Base64 byte code generated as OffChain Binary when compiling the contract by specifying --bin argument
      • Data:[]byte Encoded parameters, returned by contract_getCallOffChainData
  • Returns: []byte Return value of the getter method

  • Example: