Browse Source

计算合约

skyfffire 3 years ago
parent
commit
515784d517
6 changed files with 85 additions and 63 deletions
  1. 1 0
      config/contracts.ts
  2. 16 0
      contracts/CalcTest.sol
  3. 4 0
      package.json
  4. 5 10
      scripts/deploy.ts
  5. 0 53
      test/BaseOperationTest.ts
  6. 59 0
      test/Univ3Test.ts

+ 1 - 0
config/contracts.ts

@@ -3,5 +3,6 @@ export default {
   HEX: '0x2b591e99afE9f32eAA6214f7B7629768c40Eeb39',
   USDT: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
   UNIV3: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
+  UNIV3_FACTORY: '0x1F98431c8aD98523631AE4a59f267346ea31F984',
   ZERO: '0x0000000000000000000000000000000000000000'
 }

+ 16 - 0
contracts/CalcTest.sol

@@ -0,0 +1,16 @@
+pragma solidity ^0.7.6;
+
+import '@uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol';
+
+import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol';
+
+import 'hardhat/console.sol';
+
+contract CalcTest {
+    IUniswapV3Factory factory = IUniswapV3Factory(0x1F98431c8aD98523631AE4a59f267346ea31F984);
+
+    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);
+    }
+}

+ 4 - 0
package.json

@@ -10,5 +10,9 @@
     "typechain": "^8.1.0",
     "typescript": "^4.8.4",
     "web3": "^1.8.0"
+  },
+  "dependencies": {
+    "@uniswap/v3-core": "^1.0.1",
+    "@uniswap/v3-periphery": "^1.4.2"
   }
 }

+ 5 - 10
scripts/deploy.ts

@@ -1,18 +1,13 @@
 import { ethers } from "hardhat";
 
 async function main() {
-  const currentTimestampInSeconds = Math.round(Date.now() / 1000);
-  const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
-  const unlockTime = currentTimestampInSeconds + ONE_YEAR_IN_SECS;
+  const CalcTest = await ethers.getContractFactory("CalcTest");
+  const calc = await CalcTest.deploy();
 
-  const lockedAmount = ethers.utils.parseEther("1");
+  await calc.deployed();
 
-  const Lock = await ethers.getContractFactory("Lock");
-  const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
-
-  await lock.deployed();
-
-  console.log(`Lock with 1 ETH and unlock timestamp ${unlockTime} deployed to ${lock.address}`);
+  console.log(`Lock deployed to ${calc.address}`);
+  //0xA56768f700434321765a373B040c17BBB25eB803
 }
 
 // We recommend this pattern to be able to use async/await everywhere

+ 0 - 53
test/BaseOperationTest.ts

@@ -62,57 +62,4 @@ describe('Base operation test', () => {
 
     await weth_contract.methods.approve(contracts.UNIV3, 1e18.toString()).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)
-  })
-
-  it('Univ3 `exactInput` 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: 1e6
-    }
-
-    let amountIn = 1e9
-    let amountOutMin = 1
-    let deadline = parseInt(String(new Date().getTime() / 1e3))
-    let path = [contracts.WETH, fee._5_per_10000, contracts.USDT]
-    let to = deployer.address
-
-    let params = {
-      path: ethers.utils.solidityPack(['address', 'uint24', 'address'], path),
-      recipient: to,
-      deadline: deadline,
-      amountIn: amountIn,
-      amountOutMinimum: amountOutMin
-    }
-
-    await univ3_contract.methods
-        .exactInput(params)
-        .send(rawTx).then(console.log)
-  })
 })

+ 59 - 0
test/Univ3Test.ts

@@ -0,0 +1,59 @@
+import {ethers, web3} from "hardhat";
+import contracts from "../config/contracts";
+import deployer from "../.secret";
+import fee from "../config/fee";
+
+describe('Uniswap v3 test', () => {
+    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)
+    })
+
+    it('Univ3 `exactInput` 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: 1e6
+        }
+
+        let amountIn = 1e9
+        let amountOutMin = 1
+        let deadline = parseInt(String(new Date().getTime() / 1e3)) + 60
+        let path = [contracts.WETH, fee._5_per_10000, contracts.USDT]
+        let to = deployer.address
+
+        let params = {
+            path: ethers.utils.solidityPack(['address', 'uint24', 'address'], path),
+            recipient: to,
+            deadline: deadline,
+            amountIn: amountIn,
+            amountOutMinimum: amountOutMin
+        }
+
+        await univ3_contract.methods
+            .exactInput(params)
+            .send(rawTx).then(console.log)
+    })
+})