多链应用的跨链自动化

  • gelato
  • 发布于 2023-04-29 18:54
  • 阅读 17

本文介绍了Gelato和LayerZero如何协同工作,为多链应用提供跨链自动化。

博客 — 用例

Layer Zero

Layer Zero

·

针对多链应用的跨链自动化

随着多链格局的普及,开发者们正面临着多个应用各自的状态存在于独立的区块链上的问题。

为了克服这个问题,像 LayerZero 这样的跨链消息传递协议使用户能够将交易从一条链发送到另一条链,同时保持高水平的数据完整性和安全性。

然而,由于多个 dApp 存在于孤立的区块链上,每个区块链都有自己的独立状态,因此很难管理不同智能合约之间的自动化流程。

项目通常必须构建、运行和维护自定义基础设施,以并行处理每个链上的交易自动化。

而这正是 Gelato 的用武之地。作为 web3 的去中心化后端,Gelato 使项目能够自动执行所有主要区块链上的智能合约功能。

前提条件

如果你不熟悉 Gelato 和智能合约自动化,请参阅我们的文档

有关 LayerZero 和全链互操作性的更多信息,请参阅他们的文档

什么是跨链自动化?

由 Gelato 提供支持的跨链自动化允许开发人员自动执行任意函数,这些函数定期通过像 LayerZero 这样的协议向另一条链进行跨链消息传递调用。

跨链自动化的一个很好的用例是定期同步多个链上多个孤立的 dApp 之间的状态,以便整合整个应用程序的余额核算。

它是如何工作的?

跨链舔冰淇淋的例子 用户可以尝试在 Avalanche 上创建一个 Automate 任务,以舔 Fantom 上的冰淇淋。

步骤
  • 点击创建任务

  • 输入 0xC8aE2A55bC4Bf3280a38C76077A43D4D4a086272 作为合约地址

  • 选择 initateCCLick 作为要自动化的函数

  • 输入 0x126E75609cb5B20D07e9250E38e5fC14dFb7851b 作为 _dstLicker

  • 输入用户在 fantom 上的 iceCreamNFT token id 作为 _tokenId,或者直接使用 1 作为 _tokenId

  • 选择时间间隔并使用你的 Gelato 余额支付

当任务执行时,用户可以检查 GelatoLickerDst 上的事件,以查看他们的冰淇淋是否被舔了。

GelatoLickerSrc - https://snowtrace.io/address/0xC8aE2A55bC4Bf3280a38C76077A43D4D4a086272#code

GelatoLickerDst - https://ftmscan.com/address/0x126E75609cb5B20D07e9250E38e5fC14dFb7851b#code

GelatoLickerSrc

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface ILayerZeroEndpoint {
   // @notice send a LayerZero message to the specified address at a LayerZero endpoint.
   // @param _dstChainId - the destination chain identifier
   // @param _destination - the address on destination chain (in bytes). address length/format may vary by chains
   // @param _payload - a custom bytes payload to send to the destination contract
   // @param _refundAddress - if the source transaction is cheaper than the amount of value passed, refund the additional amount to this address
   // @param _zroPaymentAddress - the address of the ZRO token holder who would pay for the transaction
   // @param _adapterParams - parameters for custom functionality. e.g. receive airdropped native gas from the relayer on destination
   function send(
       uint16 _dstChainId,
       bytes calldata _destination,
       bytes calldata _payload,
       address payable _refundAddress,
       address _zroPaymentAddress,
       bytes calldata _adapterParams
   ) external payable;
}

contract GelatoLickerSrc {
   uint16 public constant dstChainId = 112; // fantom lz chainId
   ILayerZeroEndpoint public constant lzEndpoint =
       ILayerZeroEndpoint(0x3c2269811836af69497E5F486A85D7316753cf62); // avalanche lz endpoint address

   mapping(uint256 => uint256) public lastLicked;

   receive() external payable {}

   //@dev called by Gelato whenever `checker` returns true
   // @dev 当 `checker` 返回 true 时,Gelato 会调用此函数
   function initiateCCLick(address _dstLicker, uint256 _tokenId)
       external
       payable
   {
       bytes memory lickPayload = abi.encode(_tokenId);

       lastLicked[_tokenId] = block.timestamp;

       lzEndpoint.send{value: address(this).balance}(
           dstChainId,
           abi.encodePacked(_dstLicker, address(this)),
           lickPayload,
           payable(this),
           address(0),
           bytes("")
       );
   }

   //@dev called by Gelato check if it is time to call `initiateCCLick`
   // @dev Gelato 检查是否应该调用 `initiateCCLick`
   function checker(address _dstLicker, uint256 _tokenId)
       external
       view
       returns (bool canExec, bytes memory execPayload)
   {
       if (block.timestamp < lastLicked[_tokenId] + 600) {
           canExec = false;
           execPayload = bytes(
               "CrossChainGelatoLicker: Not time to cross chain lick"
           );
           return (canExec, execPayload);
       }

       canExec = true;
       execPayload = abi.encodeWithSelector(
           this.initiateCCLick.selector,
           _dstLicker,
           _tokenId
       );

       return (canExec, execPayload);
   }
}

GelatoLickerDst

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface ILayerZeroReceiver {
   // @notice LayerZero endpoint will invoke this function to deliver the message on the destination
   // @param _srcChainId - the source endpoint identifier
   // @param _srcAddress - the source sending contract address from the source chain
   // @param _nonce - the ordered message nonce
   // @param _payload - the signed payload is the UA bytes has encoded to be sent
   function lzReceive(
       uint16 _srcChainId,
       bytes calldata _srcAddress,
       uint64 _nonce,
       bytes calldata _payload
   ) external;
}

interface IIceCreamNFT {
   function lick(uint256 _tokenId) external;
}

contract GelatoLickerDst is ILayerZeroReceiver {
   address public constant lzEndpoint =
       address(0xb6319cC6c8c27A8F5dAF0dD3DF91EA35C4720dd7); // fantom lz endpoint
   IIceCreamNFT public constant iceCreamNFT =
       IIceCreamNFT(0x255F82563b5973264e89526345EcEa766DB3baB2); // fantom IceCreamNFT address
   address public constant srcLicker =
       address(0xC8aE2A55bC4Bf3280a38C76077A43D4D4a086272); // avalanche gelato licker address

   event CCLicked(
       uint16 srcChainId,
       address srcAddress,
       uint256 indexed tokenId,
       uint256 timestamp
   );

   function lzReceive(
       uint16 _srcChainId,
       bytes memory _srcAddress,
       uint64,
       bytes memory lickPayload
   ) external override {
       require(
           msg.sender == address(lzEndpoint),
           "CrossChainGelatoLicker: Only endpoint"
       );
       address srcAddress;
       assembly {
           srcAddress := mload(add(_srcAddress, 20))
       }
       require(
           srcAddress == srcLicker,
           "CrossChainGelatoLicker: Only srcLicker"
       );

       uint256 tokenId = abi.decode(lickPayload, (uint256));

       iceCreamNFT.lick(tokenId);

       emit CCLicked(_srcChainId, srcAddress, tokenId, block.timestamp);
   }
}

全链 Staking 奖励支付

Abracadabra 已经在使用 LayerZero 来优化其协议的功能。

该团队已经实施了 Gelato 和 LayerZero,以自动化和同步跨多个 EVM 链的 staking 奖励支付。

Abracadabra 允许用户跨多个链 staking 治理代币。这对用户来说很棒,但最终可能导致治理和协议更新出现问题。 借助 Gelato 和 LayerZero,通过实施全链治理和 staking 功能,可以快速缓解此问题。

例如,每天将向所有链上的所有 SPELL staker 支付 100 个 MIM 的奖励。但是,这些 MIM 奖励是在 Ethereum 上铸造的,并且必须根据每个链上 staking 的 SPELL 总量自动将正确数量的 MIM 转移到每个受支持的链。

教程

Abracadabra 使用 Gelato 设置了两个自动化任务,并已使用 LayerZero 作为其首选的跨链消息传递协议。

任务 #1: 第一个任务是每日快照多个 EVM 链(例如 Polygon、Arbitrum)上 staking 的 $SPELL,并将此信息发送到 Ethereum 进行聚合。 Ethereum 现在知道每个链上 staking 了多少 $SPELL,以及每个链应该收到多少 $MIM 奖励。 更多信息可以在相应的 Gelato tasksmart contract 中找到。

任务 #2: 第二个任务然后根据先前拍摄的快照将 $MIM 奖励从 Ethereum(中心链)分配到所有 spoke 链。 更多信息可以在相应的 Gelato tasksmart contract 中找到。

什么时候应该使用它?

希望解决跨链通信的智能合约开发人员可以通过以下方式使用 Gelato 和 LayerZero:

Gelato & LayerZero:安全概况

通过 LayerZero 的全链通用消息传递技术,Gelato Automate 比现有解决方案更加安全。

如今,大多数跨链解决方案都包装资产,通过中心化实体传递信息,或依赖可能被破坏的许可验证器集。近年来,我们已经看到近 10 亿美元的损失累积到基于这些技术的桥梁上。

LayerZero 只是在多个链上的端点之间传递消息,并且从不依赖于包装的资产或中心化的验证器集。LayerZero 使用独特的超轻节点模型来实现这一点,该模型使用独立的 Relayer 和 Oracle 来传递和验证链之间的消息。

此外,LayerZero 允许应用程序选择自己的 Relayer、Oracle 和安全参数,以根据其需求优化安全要求。

安全消息传递和用户应用程序可配置安全配置文件的结合使 Gelato Automate 成为安全可靠的链上自动化的理想解决方案。

关于 Gelato

Gelato 是一个 Web3 云平台,使开发人员能够创建自动化的、gasless 和链下感知的 Layer 2 链和智能合约。400 多个 web3 项目多年来一直依赖 Gelato 来促进 DeFi、NFT 和游戏中的数百万笔交易。

  • Gelato RaaS: 单击一下即可部署你自己的量身定制的 ZK 或 OP L2 链,其中内置了原生账户抽象和所有 Gelato 中间件。

  • Web3 Functions: 通过运行去中心化的云函数,将你的智能合约连接到链下数据和计算。

  • Automate: 以可靠、对开发人员友好且去中心化的方式自动执行交易,从而自动执行你的智能合约。

  • Relay: 通过易于使用的 API,让你的用户可以访问可靠、强大且可扩展的 gasless 交易。

  • 账户抽象 SDK: Gelato 已与 Safe 合作构建一个功能完善的账户抽象 SDK,将 Gelato 行业最佳的 gasless 交易功能与行业最安全的智能合约钱包相结合。

订阅我们的新闻通讯并打开你的 Twitter 通知,以获取有关 Gelato 生态系统的最新更新! 如果你有兴趣成为 Gelato 团队的一员并构建互联网的未来,请浏览空缺职位并在此处申请 here

资源

用例分析 - Abracadabra

LayerZero 入门

Gelato 入门

Layer Zero

Layer Zero

· Layer Zero

·

  • 原文链接: gelato.cloud/blog/cross-...
  • 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
gelato
gelato
The Web3 Developer Cloud. Launch your own chain via our #1 Rollup-As-A-Service platform.