瀏覽代碼

交易测试成功。

龚成明 3 年之前
父節點
當前提交
add20a7a19
共有 3 個文件被更改,包括 49 次插入3 次删除
  1. 0 0
      abi/UNIV3_ABI.ts
  2. 6 0
      contracts.ts
  3. 43 3
      test/BaseOperationTest.ts

文件差異過大導致無法顯示
+ 0 - 0
abi/UNIV3_ABI.ts


+ 6 - 0
contracts.ts

@@ -0,0 +1,6 @@
+export default {
+  WETH: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
+  HEX: '0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39',
+  UNIV3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
+  ZERO: '0x0000000000000000000000000000000000000000'
+}

+ 43 - 3
test/BaseOperationTest.ts

@@ -1,5 +1,7 @@
 import { web3 } from "hardhat";
 import deployer from '../.secret'
+import contracts from '../contracts'
+import {default as WETH_ABI} from "../abi/WETH_ABI";
 
 describe('Base operation test', () => {
   it('Parse ether test', async () => {
@@ -20,16 +22,16 @@ describe('Base operation test', () => {
       nonce: await web3.eth.getTransactionCount(deployer.address),
       gasPrice: web3.utils.toWei('2', 'gwei'),
       gasLimit: 1_000_000,
-      to: '0x0000000000000000000000000000000000000000',
+      to: contracts.ZERO,
       value: 1e9
     }
 
     await web3.eth.sendTransaction(rawTx).then(console.log)
   })
 
-  it('Weth  test', async () => {
+  it('Weth withdraw test', async () => {
     const WETH_ABI = require('../abi/WETH_ABI').default
-    const WETH_ADDR = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
+    const WETH_ADDR = contracts.WETH
 
     let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)
     // await weth_contract.methods.balanceOf(deployer.address).call().then(console.log)
@@ -42,4 +44,42 @@ describe('Base operation test', () => {
     }
     await weth_contract.methods.withdraw(1e15).send(rawTx).then(console.log)
   })
+
+  it ('Weth approve test', async () => {
+    const WETH_ABI = require('../abi/WETH_ABI').default
+    const WETH_ADDR = contracts.WETH
+
+    let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)
+    // await weth_contract.methods.balanceOf(deployer.address).call().then(console.log)
+
+    let rawTx = {
+      from: deployer.address,
+      nonce: await web3.eth.getTransactionCount(deployer.address),
+      gasPrice: web3.utils.toWei('2', 'gwei'),
+      gasLimit: 1_000_000
+    }
+    await weth_contract.methods.approve(contracts.UNIV3, 1e9).send(rawTx).then(console.log)
+  })
+
+  it('Univ3 swap test', async () => {
+    const UNIV3_ABI = require('../abi/UNIV3_ABI').default
+
+    let univ3_contract = new web3.eth.Contract(UNIV3_ABI, contracts.UNIV3)
+
+    let rawTx = {
+      from: deployer.address,
+      nonce: await web3.eth.getTransactionCount(deployer.address),
+      gasPrice: web3.utils.toWei('2', 'gwei'),
+      gasLimit: 1_000_000
+    }
+
+    let amountIn = 1_000_000
+    let amountOutMin = 1
+    let path = [contracts.WETH, contracts.HEX]
+    let to = deployer.address
+
+    await univ3_contract.methods
+        .swapExactTokensForTokens(amountIn, amountOutMin, path, to)
+        .send(rawTx).then(console.log)
+  })
 })

部分文件因文件數量過多而無法顯示