Aeternity 中的跨合约调用

问题描述

我正在查看 Sophia 文档 (https://aeternity-sophia.readthedocs.io/en/latest/contracts/#calling-other-contracts),但我不明白如何执行从一个 Sophia 合约到另一个 Sophia 合约的跨合约调用(从一个 Sophia 合约调用一个合约函数)。我是否必须创建一个界面或类似的东西?

解决方法

要从另一个合约进行合约调用,您需要:

  1. 使用您感兴趣的被调用合约(即 MyRemoteContract)的入口点在调用者合约上创建一个接口:
contract MyRemoteContract =
  entrypoint my_remote_entrypoint : (int) => bool
  entrypoint another_remote_entrypoint : (address,int) => bool
  1. 管理调用者合约中的另一个合约(即:保持它在你的状态)
contract MyCallerContract = 
  record state = {
    some_field : address,remote_contract_instance : MyRemoteContract }
  1. 最后,当您进行 init 时,您可以通过参数发送您的远程合约地址以实例化您的调用者合约
stateful entrypoint init( remote_contract_instance_param : MyRemoteContract) : state = {
  remote_contract_instance = remote_contract_instance_param }
  1. 调用此合约(调用方)时,您需要告诉他已部署合约实例的地址(ct_....)

  2. 从现在起,您的合约将能够调用您在界面上获得的任何远程入口点。

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

一些旁注,当你的远程合约Call.caller使用时要小心,因为它不会是实际地址,而是作为调用者的合约地址。