Select Page



Joerg Hiller
Aug 16, 2024 04:42

Circle’s Smart Contract Platform simplifies ETH to USDC swaps via a smart contract with an SDK for easy deployment and interaction.





Circle’s Smart Contract Platform now offers a streamlined method for swapping Ethereum (ETH) to USD Coin (USDC) via a smart contract. This development simplifies the process of deploying and interacting with smart contracts, leveraging an SDK that includes wallet and gas abstraction infrastructure, according to Circle.com.

Prerequisites

To use Circle’s platform for ETH to USDC swaps, ensure you have:

  1. Remix IDE for writing and compiling smart contracts.
  2. Circle Web3 Services Account for managing smart contract interactions.
  3. Developer Controlled Wallet for deploying and executing contract functions.

Writing the Smart Contract

The smart contract interacts with Uniswap to perform token swaps. When ETH is deposited, it is converted to Wrapped ETH (WETH), which can be swapped for USDC using Uniswap’s protocol. Below is the contract code:


// 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 

Compiling the Smart Contract

Use Remix IDE to compile the contract to obtain the ABI (Application Binary Interface) and bytecode:

  • Create a new file in Remix IDE and paste the smart contract code.
  • Select the appropriate Solidity compiler version (0.8.20) and compile the contract.
  • Copy the ABI JSON and bytecode from the “Compilation Details” section.

Deploying the Smart Contract

Deploy the compiled contract using Circle’s 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...'
});

Upon successful deployment, you will receive a contractId and transactionId for reference.

Interacting with the Deployed Contract

To perform token swaps using the deployed contract:


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

Conclusion

Circle’s Smart Contract Platform provides an efficient solution for deploying and managing smart contracts to swap ETH to USDC. By leveraging Circle’s SDK, developers can easily execute transactions on the blockchain.

Source

Image source: Shutterstock


Share it on social networks