Circle 的智能合约平台可实现 ETH 与 USDC 的无缝兑换


约尔格·希勒
2024 年 8 月 16 日 04:42

Circle 的智能合约平台通过带有 SDK 的智能合约简化了 ETH 到 USDC 的交换,以便于部署和交互。




Circle 的智能合约平台现在提供了一种通过智能合约将以太坊 (ETH) 兑换为美元币 (USDC) 的简化方法。据 Circle.com 称,这一发展简化了部署和与智能合约交互的过程,利用了包含钱包和 gas 抽象基础设施的 SDK。

先决条件

要使用 Circle 平台进行 ETH 到 USDC 的兑换,请确保您已具备:

  1. Remix IDE 用于编写和编译智能合约。
  2. Circle Web3 服务账户用于管理智能合约交互。
  3. 开发人员控制的钱包,用于部署和执行合约功能。

编写智能合约

智能合约与 Uniswap 交互以执行代币交换。存入 ETH 后,它将转换为 Wrapped ETH (WETH),可以使用 Uniswap 的协议将其兑换为 USDC。以下是合约代码:


// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.20;
import '@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol';
import '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol';

contract SwapExamples {
    ISwapRouter public immutable swapRouter;
    address public constant WETH9 = 0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619;
    address public constant USDC = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359;
    uint24 public constant poolFee = 3000;
    
    constructor(ISwapRouter _swapRouter) {
        swapRouter = _swapRouter;
    }

    function swapExactInputSingle(uint256 amountIn) external returns (uint256 amountOut) {
        TransferHelper.safeTransferFrom(WETH9, msg.sender, address(this), amountIn);
        TransferHelper.safeApprove(WETH9, address(swapRouter), amountIn);
        ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
            tokenIn: WETH9,
            tokenOut: USDC,
            fee: poolFee,
            recipient: msg.sender,
            deadline: block.timestamp,
            amountIn: amountIn,
            amountOutMinimum: 0,
            sqrtPriceLimitX96: 0
        });
        amountOut = swapRouter.exactInputSingle(params);
    }

    function swapExactOutputSingle(uint256 amountOut, uint256 amountInMaximum) external returns (uint256 amountIn) {
        TransferHelper.safeTransferFrom(WETH9, msg.sender, address(this), amountInMaximum);
        TransferHelper.safeApprove(WETH9, address(swapRouter), amountInMaximum);
        ISwapRouter.ExactOutputSingleParams memory params = ISwapRouter.ExactOutputSingleParams({
            tokenIn: WETH9,
            tokenOut: USDC,
            fee: poolFee,
            recipient: msg.sender,
            deadline: block.timestamp,
            amountOut: amountOut,
            amountInMaximum: amountInMaximum,
            sqrtPriceLimitX96: 0
        });
        amountIn = swapRouter.exactOutputSingle(params);
        if (amountIn 

编译智能合约

使用Remix IDE编译合约获取ABI(应用程序二进制接口)和字节码:

  • 在 Remix IDE 中创建一个新文件并粘贴智能合约代码。
  • 选择合适的Solidity编译器版本(0.8.20)并编译合约。
  • 从“编译细节”部分复制 ABI JSON 和字节码。

部署智能合约

使用Circle的SDK部署编译后的合约:


npm install @circle-fin/smart-contract-platform @circle-fin/developer-controlled-wallets --save
const circleContractSdk = require('@circle-fin/smart-contract-platform');
const developerControlledWallets = require('@circle-fin/developer-controlled-wallets');

const response = await circleContractSdk.deployContract({
    name: 'Swap Contract',
    description: 'Contract for swapping WETH9 to USDC using Uniswap',
    walletId: '046b6c7f-0b8a-43b9-b35d-6489e6daee91',
    blockchain: 'MATIC-AMOY',
    fee: { type: 'level', config: { feeLevel: 'MEDIUM' } },
    constructorParameters: ('0xYourWalletAddress'),
    entitySecretCiphertext: '0NtD3d3+nmgb4GqYQXzAjKF8h5Zq6sHM2k/...',
    abiJSON: '(...)',
    bytecode: '0x...'
});

部署成功后,您将收到一个 contractId 和 transactionId 以供参考。

与已部署合约交互

要使用已部署的合约执行代币交换:


const response = await circleDeveloperSdk.createContractExecutionTransaction({
    walletId: 'ce714f5b-0d8e-4062-9454-61aa1154869b',
    contractAddress: '0x2f3A40A3db8a7e3D09B0adfEfbCe4f6F81927557',
    abiFunctionSignature: 'swapExactInputSingle(uint256)',
    abiParameters: (1000),
    fee: { type: 'level', config: { feeLevel: 'MEDIUM' } }
});

结论

Circle 的智能合约平台为部署和管理将 ETH 兑换为 USDC 的智能合约提供了高效的解决方案。通过利用 Circle 的 SDK,开发人员可以轻松地在区块链上执行交易。

来源

图片来源:Shutterstock


(标签翻译)人工智能(t)加密(t)区块链(t)新闻



关键词:AI,crypto,blockchain,news

AIblockchainCircleCryptoETHnewsUSDC的无缝兑换的智能合约平台可实现
Comments (0)
Add Comment