|
|
@@ -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;
|
|
|
- }
|
|
|
-}
|