Omni App

1. 确定合同版本和项目名称

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract OApp is Ownable {

}

2. 导入“@mapprotocol/mos”模块并导入“IMOSV3.sol”接口将如下所示:

Add "@mapprotocol/mos" module and importing the "IMOSV3.sol" interface would look like this:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@mapprotocol/mos/contracts/interface/IMOSV3.sol";

contract OApp is Ownable {

}

合约具体细节请查看github链接 IMOSV3

3. 当然,让我们添加一些代码来确保我们的MOS能够正确运行

在上面的代码中,我们部署时需要正确的MOS合约地址。

4. 现在,我们来完成DAPP所需的具体逻辑

我们的需求是:在源链上发送一个数字,在目标链上执行后,结果会与这个数字累加。

5. 如何发送跨链请求

5.1 首先我们来完成transferOut方法中获取messageData参数的过程; 我们看一下IMOSV3的标准接口,如下面所示

5.2 实现 _getMessageData 方法。

根据上面的接口,我们得知transferOut的messageData字段是通过对struct MessageData进行编码得到的。此外,我们可以根据 MessageType 枚举来选择我们的跨链模式,它为跨链类型提供了两种选择。

6. 使用该transferOut方法发送跨链消息

6.1 设置 feeToken

通过5.1,我们注意到transferOut需要三个参数:

  • toChain 是我们目标的chainId,

  • messageData 5.2就已经可以获取了

  • feeToken 参数就是我们在跨链过程中要用来支付目标链执行费用的代币。我们只需要在源链上进行预付费即可.

在这种情况下,我们将仅使用该链的原生代币进行演示,我们就用address(0)来填充feeToken。每笔跨链交易需要支付多少钱?我们可以参考下面代码中的解释

6.2 完成一个简单而准确的_getTransferOutFee方法

在参阅IMOSV3的方法注释后, 我们了解到每次交易的费用与toChain和gasLimit相关。在 5.2 中,我们已经将每个跨链交易的gasLimit固定为500000。

6.3 实现发送跨链消息的完整且最终的方法

7. 完成 MESSAGE跨链方法用以执行消息跨链的逻辑

发送跨链消息后,在步骤4中,我们完成了CALLDATA跨链方法的执行代码。 使用该MESSAGE方法时,接收合约需要实现一个固定的方法,更多细节可以在IMpoExecutor接口中找到IMapoExecutor interface for more details.

8. 给予远程合约调用权限

看起来我们已经完成了一个跨链DAPP合约,不过先别着急,还有更关键的一步。MOS 有一个巧妙的权限验证过程,要求每个跨链合约自主向源链地址授予权限。让我们完成这一步。

9. 我们已经完成了跨链DAPP合约的开发。现在,我们来看看完整的合约代码。

9.1 我们看一下源链上负责发送跨链请求的合约。

9.2 下面是我们目标链上负责接收和执行跨链消息的合约。

10. 合约交互的完整过程。

  1. Deploy the OAppSourceSender contract on EVM-based Source Chain A and obtain the Source-contract-address

  2. Similarly, deploy OAppTargetReceiver contract on EVM-based Target Chain and obtain the Target-contract-address.

  3. On the Target Chain, call the setTrustFromAddress method to set a security flag for the Source-contract-address.

  4. Verify the completion of the setting by calling getTrustFromAddress on the Target Chain.

  5. Retrieve the current result by calling getCumulativeResult on the Target Chain.

  6. On the Source Chain, call the sendCrossChainAdd method to send a cross-chain message.

  7. After observing the cross-chain completion on the cross-chain browser, call the getTrustFromAddress method on the Target Chain to verify if the result has been successfully accumulated.

Last updated

Was this helpful?