Skip to content

与XCM Utilities预编译交互

概览

XCM Utilities预编译合约为开发者提供了直接在EVM中与XCM相关的实用性函数。这允许开发者能够更轻松地与其他XCM相关的预编译进行交易和交互。

与其他预编译合约类似,XCM Utilities预编译位于以下地址:

0x000000000000000000000000000000000000080C
0x000000000000000000000000000000000000080C
0x000000000000000000000000000000000000080C

注意事项

在Moonbeam使用预编译合约时,可能会出现一些意想不到的后果。 请参阅安全注意事项 页面了解更多信息。

XCM Utilities Solidity接口

XcmUtils.sol是一个与预编译交互的接口:

注意事项

预编译将在之后更新以包含更多其他功能。欢迎您在Discord给出其他实用性函数的建议。

此接口包含以下函数:

  • multilocationToAddress(Multilocation memory multilocation) — 只读函数,为给定multilocation返回multilocation的衍生账户
  • weightMessage(bytes memory message) — 只读函数,返回XCM消息将在链上消耗的权重。消息参数必须为SCALE编码的XCM版本化的XCM消息
  • getUnitsPerSecond(Multilocation memory multilocation) — 只读函数,为以Multilocation形式给定的资产获取每秒单位数。multilocation必须描述一个可以支持作为费用支付的资产,例如外部XC-20,否则此函数将回退当前调用(revert)

在XCM Utilities预编译中的Multilocation结构构建与XCM Transactor预编译的Multilocation相同。

使用XCM Utilities预编译

XCM Utilities预编译允许用户无需前往波卡库即可通过Ethereum JSON-RPC读取数据。此功能更多的是带来了便利性,而不是为了智能合约用例。

对于multilocationToAddress,一个示例用例是能够通过将其multilocation派生的地址加入白名单来允许来自其他平行链的交易。一个用户可以通过计算和存储地址来将multilocation加入白名单。EVM交易可以通过远程EVM调用从其他平行链进行发起。

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

import "https://github.com/moonbeam-foundation/moonbeam/blob/master/precompiles/xcm-utils/XcmUtils.sol";

contract MultilocationWhitelistExample {
    XcmUtils xcmutils = XcmUtils(0x000000000000000000000000000000000000080C);
    mapping(address => bool) public whitelistedAddresses;

    modifier onlyWhitelisted(address addr) {
        _;
        require(whitelistedAddresses[addr], "Address not whitelisted!");
        _;
    }

    function addWhitelistedMultilocation(
        XcmUtils.Multilocation calldata externalMultilocation
    ) external onlyWhitelisted(msg.sender) {
        address derivedAddress = xcmutils.multilocationToAddress(
            externalMultilocation
        );
        whitelistedAddresses[derivedAddress] = true;
    }

    ...
}

查看如何使用xcmExecute函数在本地执行自定义XCM消息的示例,请参阅创建并执行自定义XCM消息 指南。

Last update: January 25, 2024
| Created: December 29, 2022