Analog发送第一条跨链消息. 在本教程中,我们将使用 analog-gmp-examples GitHub… | by moncici | May, 2024


moncici

测试网链接

在本教程中,我们将使用 analog-gmp-examples GitHub 仓库中的一个简单的跨链计数器合约。导航到 examples/simple目录,找到我们将在此引用的 Simple Counter 示例。

Counter.sol

这是一个合约的示例,该合约将在目标链上接收一个跨链消息。接收合约需要在 IGmpReceiver 接口中实现 onGmpReceived 函数。在这个示例中,合约在接收到跨链消息后仅增加一个计数器。这个示例没有使用 calldata 变量,但如果源链的发送合约发送了一些数据,它可以在那里接收到。

function onGmpReceived(bytes32, uint128, bytes32, bytes calldata) 
external payable returns (bytes32) {
require(msg.sender == _gateway, "unauthorized");
number++;
return bytes32(number);
}

Counter.t.sol

这个合约展示了如何从源链发送跨链消息。DApp 源合约需要使用 IGateway 接口在源链上的 Analog Gateway 合约上调用 submitMessage() 函数。这个示例合约使用了一个测试工具 GmpTestTools,它使得测试 GMP 变得简单,但在您实际部署的合约中,您只需要使用下面这样的代码行来通过 GMP 在另一个支持的链上发送消息。

请使用下面的代码片段开发您自己的测试合约,并在源链上部署:

// Destination network ID and source gateway
uint16 private constant sepoliaId = GmpTestTools.SEPOLIA_NETWORK_ID;
IGateway private constant shibuyaGateway = GmpTestTools.SHIBUYA_GATEWAY;

// Submit a new GMP from Shibuya to Sepolia
shibuyaGateway.submitMessage(address(counter), sepoliaId, 100_000, "");

  1. 开发环境

2. 获取Gateway Addresses

Analog GMP 支持的每条链都有其独特的网络 ID。在使用 Analog GMP 进行跨链调用时,您需要为目的地跨链消息指定网络 ID。

3. 获取测试代币

要进行 GMP 调用,您必须在相关链上拥有足够的测试网代币。以下是一些获取水龙头的链接:

Sepolia:

Shibuya:

在这个示例中,我们将从 Astar Shibuya 发送一个跨链测试消息到以太坊 Sepolia 测试网。

在目的地 Sepolia 测试网上部署 Counter.sol。我们将使用这个简单的合约,在用户从源头 Shibuya 网络发送消息时增加数字。按照以下步骤在 Sepolia 测试网上部署 Counter 合约:

  1. 使用Remix部署Counter.sol。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IGmpReceiver} from "@analog-gmp/interfaces/IGmpReceiver.sol";
contract Counter is IGmpReceiver {
address private immutable _gateway;
uint256 public number;
constructor(address gateway) {
_gateway = gateway;
}
function onGmpReceived(bytes32, uint128, bytes32, bytes calldata) external payable returns (bytes32) {
require(msg.sender == _gateway, "unauthorized");
number++;
return bytes32(number);
}
}

  1. 编译合约。
  2. 按照如下的步骤部署合约:

打开你的 MetaMask 钱包,并切换到 “Sepolia” 网络。

在 Remix IDE 中,找到 “Deploy & Run Transactions”(部署和运行交易)标签页,并在环境列表中选择 “Injected Provider — MetaMask”(注入提供者 — MetaMask)。在 “Gas Limit”(燃气限制)文本框中填写你愿意支付的 ETH 代币作为燃气费,在 “At Address”(在地址)文本框中填写网关地址。

现在点击 “Deploy”(部署)按钮来部署你的合约。MetaMask 将提示你确认交易。检查以确保你正在 Sepolia 测试网上部署合约,并确认交易。

如果对教程感兴趣,请收藏订阅

更多信息,请关注 Twitter

想获得更详细的指导,请加入 Discord

@OneAnalog #Airdrops #空投 #零撸





Source link

Register at Binance

Scroll to Top