Cross-contract calls in Aeternity

Viewed 57
2 Answers

For making a contract call from another contract you need to:

  1. Create an interface on the caller contract with the entry points of your interest of the to-be-called contract (ie MyRemoteContract):
contract MyRemoteContract =
  entrypoint my_remote_entrypoint : (int) => bool
  entrypoint another_remote_entrypoint : (address, int) => bool
  1. Manage the other contract inside your caller contract (ie: keeping it in your state)
contract MyCallerContract = 
  record state = {
    some_field : address,
    remote_contract_instance : MyRemoteContract }
  1. Finally when you are doing your init you can send by parameter your remote contract address to instantiate on your caller contract
stateful entrypoint init( remote_contract_instance_param : MyRemoteContract) : state = {
  remote_contract_instance = remote_contract_instance_param }
  1. When calling to this contract (caller) you will need to tell him the address (ct_....) of the instance of the deployed contract

  2. From now own your contract will be able to call any remote entrypoint you got on the interface.

stateful entrypoint try_remote_call(value: int) =
  state.token.my_remote_entrypoint(value)

Some side note, be careful when your remote contract does Call.caller uses because it won't be the actual address but the contract address as caller.

To call another contract, you can deploy the first contract and then pass the same parameter into the method for the call

Related