skyfffire пре 2 година
родитељ
комит
819749dcda
4 измењених фајлова са 34 додато и 80 уклоњено
  1. 0 73
      contracts/PriceQueryTool.sol
  2. 0 6
      contracts/base/UniswapV2Router.sol
  3. 1 1
      scripts/calculate_top_n_tokens.ts
  4. 33 0
      utils/http.ts

+ 0 - 73
contracts/PriceQueryTool.sol

@@ -1,73 +0,0 @@
-// 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;
-    }
-}

+ 0 - 6
contracts/base/UniswapV2Router.sol

@@ -1,6 +0,0 @@
-// 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/calculate_top_n_tokens.ts

@@ -42,7 +42,7 @@ async function calculateTopNTokens(filePath: string, N: number): Promise<void> {
   // Get the top N tokens
   const topNTokens = tokenCountArray.slice(0, N);
 
-  console.log(topNTokens);
+  logger.info(topNTokens);
 }
 
 function sleep(ms: number) {

+ 33 - 0
utils/http.ts

@@ -0,0 +1,33 @@
+import {Md5} from 'ts-md5'
+import {replaceAll} from "hardhat/internal/util/strings";
+import axios from 'axios'
+import qs from 'qs'
+
+axios.defaults.baseURL = 'web.410eth.com:8088'
+
+axios.interceptors.request.use(
+  function (config) {
+    // auth
+    let timestamp = parseInt((Date.parse(new Date().toString()) / 1000) + '')
+    let baseStr = '410410' + timestamp
+    let splitStrList = baseStr.split("")
+    let replacedStr = replaceAll(baseStr, splitStrList[splitStrList.length - 1], "")
+    let authStr = Md5.hashStr(replacedStr)
+
+    config.data.timestamp = timestamp
+    config.data.auth = authStr
+
+    // headers处理
+    // @ts-ignore
+    config.headers['content-type'] = 'application/x-www-form-urlencoded'
+    config.data = qs.stringify(config.data)
+
+    return config;
+  },
+  function (error) {
+    // Do something with request error
+    return Promise.reject(error);
+  }
+)
+
+export default axios