BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page

Was this helpful?

  1. DeFi 筆記
  2. Uniswap

Swap 互動

以下使用 hardhat test case 撰寫,並在模擬環境下配置 fork 主網特定區塊

當 swap 時有提供 ETH 都會被自動轉為 WETH

const { ethers } = require("hardhat");
const { expect } = require("chai");

describe("USDC Balance Check", function () {
  it("Should return the USDC balance of the address", async function () {
    const usdcAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // USDC Contract Address
    const addressToCheck = "0x7713974908Be4BEd47172370115e8b1219F4A5f0"; // Address whose balance you want to check

    // USDC Contract ABI (Simplified; use the full ABI from Etherscan)
    const usdcAbi = [
      "function balanceOf(address owner) view returns (uint256)",
    ];

    // Connect to the USDC contract
    const usdcContract = new ethers.Contract(
      usdcAddress,
      usdcAbi,
      ethers.provider
    );

    // Fetch the balance
    const balance = await usdcContract.balanceOf(addressToCheck);

    // Log the balance (optional)
    console.log("USDC Balance:", balance.toString());

    const mainnetAccount = addressToCheck; // Replace with a mainnet account address
    await hre.network.provider.request({
      method: "hardhat_impersonateAccount",
      params: [mainnetAccount],
    });

    // Set the balance of the impersonated account
    await hre.network.provider.send("hardhat_setBalance", [
      mainnetAccount,
      "0x8AC7230489E80000000", // 40960 ETH
    ]);

    const ethBalance = await ethers.provider.getBalance(addressToCheck);

    // Log the balance (optional)
    console.log("ETH Balance:", ethers.formatEther(ethBalance), "ETH");

    const swapRouterAddress = "0xe592427a0aece92de3edee1f18e0157c05861564"; // Uniswap V3 SwapRouter
    const WETH = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"; // Wrapped ETH address on Ethereum Mainnet
    const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // USDC address

    // SwapRouter ABI (Simplified, only including the required function)
    const swapRouterAbi = [
      "function exactInputSingle((address tokenIn, address tokenOut, uint24 fee, address recipient, uint256 deadline, uint256 amountIn, uint256 amountOutMinimum, uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountOut)",
    ];

    // Set up the SwapRouter contract interface
    const swapRouterContract = new ethers.Contract(
      swapRouterAddress,
      swapRouterAbi,
      await ethers.provider.getSigner()
    );

    const swapParams = {
      tokenIn: WETH,
      tokenOut: USDC,
      fee: 3000, // Pool fee, 0.3% for USDC-ETH
      recipient: addressToCheck, // Your account address
      deadline: Math.floor(Date.now() / 1000) + 60 * 20, // Transaction deadline (20 minutes from now)
      amountIn: ethers.parseEther("1"), // Amount of ETH to swap
      amountOutMinimum: 0, // Set to 0 or a minimum amount of USDC you would like to receive
      sqrtPriceLimitX96: 0, // No price limit
    };

    // Perform the swap - sending ETH along with the transaction
    const tx = await swapRouterContract.exactInputSingle(swapParams, {
      value: swapParams.amountIn,
    });
    await tx.wait();

    console.log(`Swap executed, transaction hash: ${tx.hash}`);

    const USDCBalanceNew = await usdcContract.balanceOf(addressToCheck);
    console.log("USDC Balance:", USDCBalanceNew.toString());
  });
});
PreviousUniswapNextAAVE、Compound

Last updated 1 year ago

Was this helpful?