瀏覽代碼

新的ABI方式。

之前的计算方式不准确。
龚成明 3 年之前
父節點
當前提交
d5f68616ae
共有 11 個文件被更改,包括 23 次插入109 次删除
  1. 0 84
      abi/Calc_ABI.ts
  2. 0 0
      abi/UNIV3_ABI.json
  3. 0 0
      abi/UNIV3_ABI.ts
  4. 0 0
      abi/WETH_ABI.json
  5. 0 0
      abi/WETH_ABI.ts
  6. 1 1
      config/contracts.ts
  7. 10 5
      contracts/Calc.sol
  8. 1 0
      scripts/deployCalc.ts
  9. 2 2
      test/BaseOperationTest.ts
  10. 5 13
      test/CalcTest.ts
  11. 4 4
      test/Univ3Test.ts

+ 0 - 84
abi/Calc_ABI.ts

@@ -1,84 +0,0 @@
-export default [
-  {
-    "inputs": [
-      {
-        "internalType": "uint256",
-        "name": "num1",
-        "type": "uint256"
-      },
-      {
-        "internalType": "uint24",
-        "name": "fee",
-        "type": "uint24"
-      }
-    ],
-    "name": "calc1",
-    "outputs": [
-      {
-        "internalType": "uint256",
-        "name": "rst1",
-        "type": "uint256"
-      }
-    ],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "uint256",
-        "name": "num2",
-        "type": "uint256"
-      },
-      {
-        "internalType": "uint24",
-        "name": "fee",
-        "type": "uint24"
-      }
-    ],
-    "name": "calc2",
-    "outputs": [
-      {
-        "internalType": "uint256",
-        "name": "rst2",
-        "type": "uint256"
-      }
-    ],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "tokenA",
-        "type": "address"
-      },
-      {
-        "internalType": "uint256",
-        "name": "amountA",
-        "type": "uint256"
-      },
-      {
-        "internalType": "address",
-        "name": "tokenB",
-        "type": "address"
-      },
-      {
-        "internalType": "uint24",
-        "name": "fee",
-        "type": "uint24"
-      }
-    ],
-    "name": "getQuote",
-    "outputs": [
-      {
-        "internalType": "uint256",
-        "name": "amountB",
-        "type": "uint256"
-      }
-    ],
-    "stateMutability": "view",
-    "type": "function"
-  }
-]

File diff suppressed because it is too large
+ 0 - 0
abi/UNIV3_ABI.json


File diff suppressed because it is too large
+ 0 - 0
abi/UNIV3_ABI.ts


File diff suppressed because it is too large
+ 0 - 0
abi/WETH_ABI.json


File diff suppressed because it is too large
+ 0 - 0
abi/WETH_ABI.ts


+ 1 - 1
config/contracts.ts

@@ -5,5 +5,5 @@ export default {
   UNIV3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
   UNIV3_FACTORY: '0x1F98431c8aD98523631AE4a59f267346ea31F984',
   ZERO: '0x0000000000000000000000000000000000000000',
-  CALC: '0x12421be01616930b70CaE8290d1477D3ec61F49D'
+  CALC: '0x30c34b111121aCDb423eF2C0e39235Dc68a793A4'
 }

+ 10 - 5
contracts/Calc.sol

@@ -11,14 +11,19 @@ contract Calc {
 
     function getQuote(address tokenA, uint amountA, address tokenB, uint24 fee) public view returns (uint256 amountB) {
         (int24 tick,) = OracleLibrary.consult(factory.getPool(tokenA, tokenB, fee), 60);
-        return OracleLibrary.getQuoteAtTick(tick, uint128(amountA), tokenA, tokenB) * (100000 - fee) / 100000;
+        return OracleLibrary.getQuoteAtTick(tick, uint128(amountA), tokenA, tokenB);
     }
 
-    function calc1(uint num1, uint24 fee) public view returns (uint256 rst1) {
-        return num1 * fee;
+    function getQuoteHandleFee(address tokenA, uint amountA, address tokenB, uint24 fee) public view returns (uint256 amountB) {
+        (int24 tick,) = OracleLibrary.consult(factory.getPool(tokenA, tokenB, fee), 60);
+        return OracleLibrary.getQuoteAtTick(tick, uint128(amountA) * (1_000_000 - fee) / 1_000_000, tokenA, tokenB);
+    }
+
+    function calc1(uint num1, uint24 fee) public pure returns (uint256 rst1) {
+        return num1 * (1_000_000 - fee);
     }
 
-    function calc2(uint num2, uint24 fee) public view returns (uint256 rst2) {
-        return num2 * fee / 100000;
+    function calc2(uint num2, uint24 fee) public pure returns (uint256 rst2) {
+        return num2 * (1_000_000 - fee) / 1_000_000;
     }
 }

+ 1 - 0
scripts/deployCalc.ts

@@ -4,6 +4,7 @@ async function main() {
   const Calc = await ethers.getContractFactory("Calc");
   const calc = await Calc.deploy();
 
+  console.log('deploying...')
   await calc.deployed();
 
   console.log(`Calc deployed to ${calc.address}`);

+ 2 - 2
test/BaseOperationTest.ts

@@ -31,7 +31,7 @@ describe('Base operation test', () => {
   })
 
   it('Weth withdraw test', async () => {
-    const WETH_ABI = require('../abi/WETH_ABI').default
+    const WETH_ABI = require('../abi/WETH_ABI.json')
     const WETH_ADDR = contracts.WETH
 
     let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)
@@ -47,7 +47,7 @@ describe('Base operation test', () => {
   })
 
   it ('Weth approve test', async () => {
-    const WETH_ABI = require('../abi/WETH_ABI').default
+    const WETH_ABI = require('../abi/WETH_ABI.json')
     const WETH_ADDR = contracts.WETH
 
     let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)

+ 5 - 13
test/CalcTest.ts

@@ -3,21 +3,13 @@ import fee from "../config/fee";
 import contracts from "../config/contracts";
 
 describe('Calc test', () => {
-  it('Univ3 swap test', async () => {
-    const Calc_ABI = require('../abi/Calc_ABI').default
-
-    let calc = new web3.eth.Contract(Calc_ABI, contracts.CALC)
-
-    // 1334059008
-    await calc.methods.getQuote(contracts.WETH, 1e15, contracts.HEX, fee._30_per_10000).call().then(console.log)
-  })
-
   it('Univ3 calc test', async () => {
-    const Calc_ABI = require('../abi/Calc_ABI').default
+    const CALC_ABI = require('../artifacts/contracts/Calc.sol/Calc.json').abi
 
-    let calc = new web3.eth.Contract(Calc_ABI, contracts.CALC)
+    let calc = new web3.eth.Contract(CALC_ABI, contracts.CALC)
 
-    await calc.methods.calc1(1375318565, fee._30_per_10000).call().then(console.log)
-    await calc.methods.calc2(1375318565, fee._30_per_10000).call().then(console.log)
+    let inAmount = 1e17.toString()
+    await calc.methods.getQuote(contracts.WETH, inAmount, contracts.USDT, fee._30_per_10000).call().then(console.log)
+    await calc.methods.getQuoteHandleFee(contracts.WETH, inAmount, contracts.USDT, fee._30_per_10000).call().then(console.log)
   })
 })

+ 4 - 4
test/Univ3Test.ts

@@ -5,7 +5,7 @@ import fee from "../config/fee";
 
 describe('Uniswap v3 test', () => {
     it('Univ3 swap test', async () => {
-        const UNIV3_ABI = require('../abi/UNIV3_ABI').default
+        const UNIV3_ABI = require('../abi/UNIV3_ABI.json')
 
         let univ3_contract = new web3.eth.Contract(UNIV3_ABI, contracts.UNIV3)
 
@@ -27,7 +27,7 @@ describe('Uniswap v3 test', () => {
     })
 
     it('Univ3 `exactInput` test', async () => {
-        const UNIV3_ABI = require('../abi/UNIV3_ABI').default
+        const UNIV3_ABI = require('../abi/UNIV3_ABI.json')
 
         let univ3_contract = new web3.eth.Contract(UNIV3_ABI, contracts.UNIV3)
 
@@ -38,10 +38,10 @@ describe('Uniswap v3 test', () => {
             gasLimit: 1e6
         }
 
-        let amountIn = 1e15
+        let amountIn = 1e17.toString()
         let amountOutMin = 1
         let deadline = parseInt(String(new Date().getTime() / 1e3)) + 60
-        let path = [contracts.WETH, fee._30_per_10000, contracts.HEX]
+        let path = [contracts.WETH, fee._30_per_10000, contracts.USDT]
         let to = deployer.address
 
         let params = {

Some files were not shown because too many files changed in this diff