Browse Source

询价工具完成

skyfffire 2 years ago
parent
commit
9bfba29915
3 changed files with 80 additions and 1 deletions
  1. 73 0
      contracts/PriceQueryTool.sol
  2. 6 0
      contracts/base/UniswapV2Router.sol
  3. 1 1
      scripts/deploy.ts

+ 73 - 0
contracts/PriceQueryTool.sol

@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: UNLICENSED
+pragma solidity ^0.7.6;
+pragma abicoder v2;
+
+import './base/UniswapV2Router.sol';
+
+contract PriceQueryTool {
+    function queryPriceByRouter(uint amountIn, address[] calldata path, address router) public view returns (uint outputAmount) {
+        // Define a default output amount as 0
+        uint defaultOutputAmount = 0;
+        
+        // Use try-catch to handle exceptions
+        try IUniswapV2Router(router).getAmountsOut(amountIn, path) returns (uint[] memory result) {
+            // Check the result length to avoid out-of-bounds access
+            if (result.length > 1) {
+                outputAmount = result[1];
+            } else {
+                outputAmount = defaultOutputAmount;
+            }
+        } catch {
+            outputAmount = defaultOutputAmount;
+        }
+        
+        return outputAmount;
+    }
+
+    // outputAmounts[routerIndex] = outputAmount的形式
+    function queryPriceByRouters(uint amountIn, address[] calldata path, address[] calldata routers) public view returns (uint[] memory outputAmounts) {
+        outputAmounts = new uint[](routers.length);
+        for (uint routerIndex = 0; routerIndex < routers.length; routerIndex++) {
+            // Define a default output amount as 0
+            uint defaultOutputAmount = 0;
+            
+            // Use try-catch to handle exceptions
+            try IUniswapV2Router(routers[routerIndex]).getAmountsOut(amountIn, path) returns (uint[] memory result) {
+                // Check the result length to avoid out-of-bounds access
+                if (result.length > 1) {
+                    outputAmounts[routerIndex] = result[1];
+                } else {
+                    outputAmounts[routerIndex] = defaultOutputAmount;
+                }
+            } catch {
+                outputAmounts[routerIndex] = defaultOutputAmount;
+            }
+        }
+        return outputAmounts;
+    }
+
+    // pathsRoutersOutputAmounts[pathIndex][routerIndex] = outputAmount的形式
+    function queryPriceByPathsAndRouters(uint amountIn, address[][] calldata paths, address[] calldata routers) public view returns (uint[][] memory pathsRoutersOutputAmounts) {
+        pathsRoutersOutputAmounts = new uint[][](paths.length);
+        for (uint pathIndex = 0; pathIndex < paths.length; pathIndex++) {
+            pathsRoutersOutputAmounts[pathIndex] = new uint[](routers.length);
+            for (uint routerIndex = 0; routerIndex < routers.length; routerIndex++) {
+                // Define a default output amount as 0
+                uint defaultOutputAmount = 0;
+                
+                // Use try-catch to handle exceptions
+                try IUniswapV2Router(routers[routerIndex]).getAmountsOut(amountIn, paths[pathIndex]) returns (uint[] memory result) {
+                    // Check the result length to avoid out-of-bounds access
+                    if (result.length > 1) {
+                        pathsRoutersOutputAmounts[pathIndex][routerIndex] = result[1];
+                    } else {
+                        pathsRoutersOutputAmounts[pathIndex][routerIndex] = defaultOutputAmount;
+                    }
+                } catch {
+                    pathsRoutersOutputAmounts[pathIndex][routerIndex] = defaultOutputAmount;
+                }
+            }
+        }
+        return pathsRoutersOutputAmounts;
+    }
+}

+ 6 - 0
contracts/base/UniswapV2Router.sol

@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: UNLICENSED
+pragma solidity ^0.7.6;
+
+interface IUniswapV2Router {
+    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
+}

+ 1 - 1
scripts/deploy.ts

@@ -1,7 +1,7 @@
 import { ethers } from "hardhat";
 
 async function main() {
-  const contractName = 'Flash'
+  const contractName = 'PriceQueryTool'
 
   const Contract = await ethers.getContractFactory(contractName);
   console.log('deploying...')