You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wormhole Solidity SDK - CCTPSender编译错误:跨链USDC转账合约修改咨询

Wormhole Solidity SDK跨链USDC转账合约修改方案

针对SDK结构变更导致的编译错误,以下是核心修改点及完整修正后的合约代码:

核心修改说明

  • 更新SDK导入路径:新版官方SDK包名变更为@wormhole-foundation/wormhole-solidity-sdk,需调整导入语句到正确的合约路径。
  • 修正继承与构造逻辑:
    • 原CCTPBase基类已拆分,需分别初始化CCTPSender和CCTPReceiver;
    • 构造函数新增当前链的Circle域名参数,目标链调用时也需传入对应Circle域名;
    • 需自行定义并初始化USDC变量,新版基类不再自动继承该变量。
  • 调整函数调用:
    • 报价函数从quoteCrossChainDeposit改为quoteSendTokensWithPayload,参数需传入目标链ID和gas限制;
    • 发送函数从sendUSDCWithPayloadToEvm改为sendUSDCWithPayload,调整参数顺序并新增目标Circle域名;
    • 新增USDC授权步骤:需将合约自身授权为USDC的spender才能完成跨链转移。
  • 补全强制接口实现:继承CCTPReceiver必须实现onMessageReceived函数,用于处理跨链到账的USDC和payload,原代码缺失此函数会触发编译错误。
  • 修复基础语法错误:补全原代码构造函数末尾缺失的分号与闭合大括号。

修正后的完整合约代码

pragma solidity ^0.8.19;

// 新版SDK导入路径
import "@wormhole-foundation/wormhole-solidity-sdk/contracts/cctp/CCTPSender.sol";
import "@wormhole-foundation/wormhole-solidity-sdk/contracts/cctp/CCTPReceiver.sol";
import "@wormhole-foundation/wormhole-solidity-sdk/contracts/interfaces/IERC20.sol";

contract HelloUSDC is CCTPSender, CCTPReceiver {
    uint256 constant GAS_LIMIT = 250_000;
    address public immutable USDC;

    constructor(
        address _wormholeRelayer,
        address _wormhole,
        address _circleMessageTransmitter,
        address _circleTokenMessenger,
        address _USDC,
        uint16 _circleDomain
    ) 
        CCTPSender(_wormholeRelayer, _wormhole, _circleMessageTransmitter, _circleTokenMessenger)
        CCTPReceiver(_wormholeRelayer, _wormhole, _circleMessageTransmitter)
    {
        USDC = _USDC;
    }

    function sendCrossChainDeposit(
        uint16 targetChain,
        uint16 targetCircleDomain,
        address targetHelloUSDC,
        address recipient,
        uint256 amount
    ) public payable {
        uint256 cost = quoteSendTokensWithPayload(targetChain, GAS_LIMIT);
        require(msg.value == cost, "ETH金额与中继费不匹配");

        IERC20(USDC).transferFrom(msg.sender, address(this), amount);
        IERC20(USDC).approve(address(this), amount);

        bytes memory payload = abi.encode(recipient);
        sendUSDCWithPayload(
            targetChain,
            targetCircleDomain,
            targetHelloUSDC,
            payload,
            amount
        );
    }

    // 必须实现的CCTPReceiver接口函数
    function onMessageReceived(
        uint16,
        bytes32,
        bytes memory payload,
        bytes memory,
        uint256 amount
    ) public payable override onlyCircleMessageTransmitter {
        address recipient = abi.decode(payload, (address));
        IERC20(USDC).transfer(recipient, amount);
    }
}

内容的提问来源于stack exchange,提问作者Fernando Garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:36:06