Browse Source

计算,还有点bug

龚成明 3 years ago
parent
commit
28563f2cb4
4 changed files with 69 additions and 4 deletions
  1. 48 0
      abi/Calc_ABI.ts
  2. 1 1
      config/contracts.ts
  3. 9 1
      contracts/Calc.sol
  4. 11 2
      test/CalcTest.ts

+ 48 - 0
abi/Calc_ABI.ts

@@ -1,4 +1,52 @@
 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": [
       {

+ 1 - 1
config/contracts.ts

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

+ 9 - 1
contracts/Calc.sol

@@ -11,6 +11,14 @@ 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);
+        return OracleLibrary.getQuoteAtTick(tick, uint128(amountA), tokenA, tokenB) * (100000 - fee) / 100000;
+    }
+
+    function calc1(uint num1, uint24 fee) public view returns (uint256 rst1) {
+        return num1 * fee;
+    }
+
+    function calc2(uint num2, uint24 fee) public view returns (uint256 rst2) {
+        return num2 * fee / 100000;
     }
 }

+ 11 - 2
test/CalcTest.ts

@@ -8,7 +8,16 @@ describe('Calc test', () => {
 
     let calc = new web3.eth.Contract(Calc_ABI, contracts.CALC)
 
-    // 1450752979
-    console.log(await calc.methods.getQuote(contracts.WETH, 1e15, contracts.HEX, fee._30_per_10000).call())
+    // 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
+
+    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)
   })
 })