Circle智能合约平台实现无缝的ETH到USDC交换 - Blockchain.News

Circle智能合约平台实现无缝的ETH到USDC交换

realtime news Aug 16, 2024 05:11

Circle的智能合约平台通过智能合约简化了ETH到USDC的交换,提供容易部署和交互的SDK。

Circle智能合约平台实现无缝的ETH到USDC交换

Circle的智能合约平台现在提供了一种简化的方法,通过智能合约将以太坊(ETH)转换为USD Coin(USDC)。根据Circle.com,此开发简化了智能合约的部署和交互过程,利用包含钱包和燃料抽象基础设施的SDK。

先决条件

要使用Circle的平台进行ETH到USDC的交换,请确保您具备以下条件:

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

编写智能合约

智能合约与Uniswap互动,进行代币交换。当存入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 < amountInMaximum) {
            TransferHelper.safeApprove(WETH9, address(swapRouter), 0);
            TransferHelper.safeTransfer(WETH9, msg.sender, amountInMaximum - 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...'
});

成功部署后,您将收到合约ID和交易ID供参考。

与部署合同的交互

要使用部署的合同进行代币交换:


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,开发人员可以轻松地在区块链上执行交易。

来源

Image source: Shutterstock