All Products
Search
Document Center

Cross-contract Call

Last Updated: Jan 09, 2020

You can use call or delegatecall method to call contracts on the contract platform of Ant Blockchain.

call

call allows you to call the code of another contract to obtain the call results of a specific method. When you execute the called contract, its memory is changed. If the execution is successful, true is returned. If the execution fails, false is returned. If the contract you want to call is unavailable, execute its fallback function.

Function prototype

  1. id.call(bytes4(keccak256(data), args)) returns (bool result)

Request parameters

Parameter Required Type Description
id Yes identity The ID of a called contract.
data Yes string The signature of the called contract method.
args Yes var The input parameters of the called contract method.

Response parameters

Parameter Required Type Description
result Yes bool The value returned by the method, which can be true or false.

delegatecall

delegatecall allows you to call the methods of other contracts. When you use delegatecall, the memory of the contract caller is changed. This is different from call. You do not need to provide your contract status, such as balance and storage, if you use the code of other contracts. delegatecall cannot transmit values.

Function prototype

  1. id.delegatecall(bytes4(keccak256(data), args)) returns (bool result)

Request parameters

Parameter Required Type Description
id Yes identity The ID of the called contract.
data Yes string The signature of the called contract method.
args Yes var The input parameters of the called contract method.

Response parameters

Parameter Required Type Description
result Yes bool The value returned by the method, which can be true or false.

Comparison between call and delegatecall

Similarities

  • When you use call or delegatecall, all the available gas values in your contract are provided.
  • If your contract fails to be executed, false is returned.

Differences

  • call uses .value to provide the gas value for the called contract.
  • If contract_test contains nameReg.call("somefunction") and nameReg.delegatecall("somefunction"):
    • nameReg.call executes somefunction in nameReg as the nameReg contract.
    • nameReg.delegatecall executes somefunction in nameReg as the contract_test contract.
  • delegatecall allows you to use the code of other contracts without providing your contract status, such as balance and storage.