From 56defc69dfc5b41b9de8be5932260ca37641460e Mon Sep 17 00:00:00 2001 From: Michael Feng <1795030+mifeng@users.noreply.github.com> Date: Sat, 17 Jul 2021 21:32:59 -0700 Subject: [PATCH] Revert "Uniswap V3 fixes" --- conf/global_conf.yml.example | 4 ++-- src/assets/erc20_tokens_kovan.json | 2 +- src/routes/ethereum.ts | 11 ++++------- src/routes/uniswap_v3.ts | 14 +++++++------- src/services/ethereum.ts | 25 +++++++++---------------- src/services/ethereum_config.ts | 6 +++++- src/services/fees.js | 4 ++-- src/services/uniswap_v3.js | 12 ++++++------ 8 files changed, 36 insertions(+), 42 deletions(-) diff --git a/conf/global_conf.yml.example b/conf/global_conf.yml.example index 70219aa..8cf1925 100644 --- a/conf/global_conf.yml.example +++ b/conf/global_conf.yml.example @@ -76,11 +76,11 @@ GMT_OFFSET: +0800 # EthGasStation # API key for defipulse.com gas station API # Gas level you want to use for Ethereum transactions (fast, fastest, safeLow, average) -ETH_GAS_STATION_ENABLE: true +ENABLE_ETH_GAS_STATION: true ETH_GAS_STATION_API_KEY: ETH_GAS_STATION_GAS_LEVEL: fast ETH_GAS_STATION_REFRESH_TIME: 60 -ETH_MANUAL_GAS_PRICE: 100 +MANUAL_GAS_PRICE: 100 # Balancer Config BALANCER_MAX_SWAPS: 4 diff --git a/src/assets/erc20_tokens_kovan.json b/src/assets/erc20_tokens_kovan.json index 44bd5d7..ea84a4b 100644 --- a/src/assets/erc20_tokens_kovan.json +++ b/src/assets/erc20_tokens_kovan.json @@ -13,7 +13,7 @@ }, { "symbol": "DAI", - "address": "0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa", + "address": "0x1528F3FCc26d13F7079325Fb78D9442607781c8C", "decimals": 18 }, { diff --git a/src/routes/ethereum.ts b/src/routes/ethereum.ts index f1a2fd6..d41f081 100644 --- a/src/routes/ethereum.ts +++ b/src/routes/ethereum.ts @@ -4,9 +4,7 @@ import { TokenERC20Info, } from '../services/ethereum'; import { EthereumConfigService } from '../services/ethereum_config'; -// import { EthereumGasService } from '../services/ethereum_gas'; -import Fees from '../services/fees'; - +import { EthereumGasService } from '../services/ethereum_gas'; import { logger } from '../services/logger'; import { Router, Request, Response } from 'express'; import { ethers } from 'ethers'; @@ -19,8 +17,7 @@ const latency = (startTime: number, endTime: number): number => { const config = new EthereumConfigService(); const ethereumService = new EthereumService(config); -// const ethereumGasService = new EthereumGasService(config); -const fees = new Fees(); +const ethereumGasService = new EthereumGasService(config); router.post('/', async (_req: Request, res: Response) => { /* @@ -161,8 +158,8 @@ router.post('/approve', async (req: Request, res: Response) => { res.status(500).send(`Token "${req.body.token}" is not supported`); } else { const tokenAddress = tokenContractInfo.address; - // const gasPrice = req.body.gasPrice || ethereumGasService.getGasPrice(); - const gasPrice = req.body.gasPrice || fees.ethGasPrice; + + const gasPrice = req.body.gasPrice || ethereumGasService.getGasPrice(); let amount = ethers.constants.MaxUint256; if (req.body.amount) { diff --git a/src/routes/uniswap_v3.ts b/src/routes/uniswap_v3.ts index 15a1664..0f07b18 100644 --- a/src/routes/uniswap_v3.ts +++ b/src/routes/uniswap_v3.ts @@ -250,7 +250,7 @@ router.post('/trade', async (req: Request, res: Response) => { ); if (side === 'BUY') { - const price = trade.executionPrice.invert().toSignificant(8); + const price = trade.executionPrice.invert().toFixed(); logger.info(`uniswap.route - Price: ${price.toString()}`); if (!limitPrice || price <= limitPrice) { // pass swaps to exchange-proxy to complete trade @@ -268,7 +268,7 @@ router.post('/trade', async (req: Request, res: Response) => { base: baseTokenAddress, quote: quoteTokenAddress, amount: amount, - expectedIn: expectedAmount.toSignificant(8), + expectedIn: expectedAmount.toFixed(), price: price, gasPrice: gasPrice, gasLimit, @@ -286,7 +286,7 @@ router.post('/trade', async (req: Request, res: Response) => { } } else { // sell - const price = trade.executionPrice.toSignificant(8); + const price = trade.executionPrice.toFixed(); logger.info(`Price: ${price.toString()}`); if (!limitPrice || price >= limitPrice) { // pass swaps to exchange-proxy to complete trade @@ -304,7 +304,7 @@ router.post('/trade', async (req: Request, res: Response) => { base: baseTokenAddress, quote: quoteTokenAddress, amount: parseFloat(req.body.amount), - expectedOut: expectedAmount.toSignificant(8), + expectedOut: expectedAmount.toFixed(), price: parseFloat(price), gasPrice: gasPrice, gasLimit, @@ -389,13 +389,13 @@ router.post('/price', async (req: Request, res: Response) => { if (trade !== null && expectedAmount !== null) { price = side === 'BUY' - ? trade.executionPrice.invert().toSignificant(8) - : trade.executionPrice.toSignificant(8); + ? trade.executionPrice.invert().toFixed() + : trade.executionPrice.toFixed(); priceResult = { price: parseFloat(price), amount: parseFloat(amount), - expectedAmount: parseFloat(expectedAmount.toSignificant(8)), + expectedAmount: parseFloat(expectedAmount.toFixed()), }; } } else { diff --git a/src/services/ethereum.ts b/src/services/ethereum.ts index f1cb72a..0e8713c 100644 --- a/src/services/ethereum.ts +++ b/src/services/ethereum.ts @@ -178,7 +178,7 @@ export class EthereumService { return await contract.approve(spender, amount, { gasPrice: gasPrice * 1e9, // fixate gas limit to prevent overwriting - gasLimit: 60000, + gasLimit: this.config.approvalGasLimit, }); } catch (err) { throw new Error(err.reason || 'error approval'); @@ -235,15 +235,15 @@ export class EthereumService { * @param {string} tokenAddress * @return {TokenERC20Info} | null */ - getERC20TokenByAddress(tokenAddress: string): TokenERC20Info | undefined { + getERC20TokenByAddress(tokenAddress: string): TokenERC20Info | undefined { if (this.erc20TokenList) { const tokenContract = this.erc20TokenList.tokens.filter((obj) => { - return obj.address.toUpperCase() === tokenAddress.toUpperCase(); - }); - return tokenContract[0]; - } - return; + return obj.address.toUpperCase() === tokenAddress.toUpperCase(); + }); + return tokenContract[0]; } + return; +} /** * Return wallet of a private string @@ -262,19 +262,12 @@ export class EthereumService { async getTransactionReceipt(txHash: string): Promise { const transaction = await this.provider.getTransactionReceipt(txHash); - let gasUsed; - if (transaction.gasUsed) { - gasUsed = transaction.gasUsed.toNumber(); - } else { - gasUsed = 0; - } - return { - gasUsed: gasUsed, + gasUsed: transaction.gasUsed.toNumber(), blockNumber: transaction.blockNumber, confirmations: transaction.confirmations, status: transaction.status || 0, - logs: transaction.logs, + logs: transaction.logs }; } } diff --git a/src/services/ethereum_config.ts b/src/services/ethereum_config.ts index 5fc3570..db15df8 100644 --- a/src/services/ethereum_config.ts +++ b/src/services/ethereum_config.ts @@ -17,6 +17,10 @@ export class EthereumConfigService { return this.config.getConfig('ETHEREUM_TOKEN_LIST_URL'); } + get approvalGasLimit(): string { + return this.config.getConfig('APPROVAL_GAS_LIMIT'); + } + get networkName(): string { return this.config.getConfig('ETHEREUM_CHAIN'); } @@ -27,7 +31,7 @@ export class EthereumConfigService { get gasServiceUrl(): string { return ( - 'https://ethgasstation.info/api/ethgasAPI.json?api-key=' + + this.config.getConfig('ETH_GAS_STATION_URL') + this.config.getConfig('ETH_GAS_STATION_API_KEY') ); } diff --git a/src/services/fees.js b/src/services/fees.js index af9c417..1a6776d 100644 --- a/src/services/fees.js +++ b/src/services/fees.js @@ -7,9 +7,9 @@ const ethGasStationHost = 'https://ethgasstation.info'; const globalConfig = require('../services/configuration_manager').configManagerInstance; const ethGasStationEnabled = - globalConfig.getConfig('ETH_GAS_STATION_ENABLE') || false; + globalConfig.getConfig('ENABLE_ETH_GAS_STATION') || false; const ethGasStationApiKey = globalConfig.getConfig('ETH_GAS_STATION_API_KEY'); -const ethManualGasPrice = parseInt(globalConfig.getConfig('ETH_MANUAL_GAS_PRICE')); +const ethManualGasPrice = parseInt(globalConfig.getConfig('MANUAL_GAS_PRICE')); const ethGasStationURL = ethGasStationHost + '/api/ethgasAPI.json?api-key=' + ethGasStationApiKey; const defaultRefreshInterval = 120; diff --git a/src/services/uniswap_v3.js b/src/services/uniswap_v3.js index 38dfe02..450c408 100644 --- a/src/services/uniswap_v3.js +++ b/src/services/uniswap_v3.js @@ -17,7 +17,7 @@ const abiDecoder = require('abi-decoder'); // constants const FeeAmount = uniV3.FeeAmount; -const GAS_LIMIT = globalConfig.getConfig('UNISWAP_GAS_LIMIT') || 550688; +const GAS_LIMIT = globalConfig.getConfig('UNISWAP_GAS_LIMIT') || 5506880; const UPDATE_PERIOD = globalConfig.getConfig('UNISWAP_UPDATE_PERIOD') || 300000; // stop updating pair after 5 minutes from last request const MaxUint128 = ethers.BigNumber.from(2).pow(128).sub(1); @@ -178,7 +178,7 @@ export default class UniswapV3 { request[twap].value.tickCumulatives[0].toNumber() ) ) - .toSignificant(8) + .toFixed() ); } } @@ -418,10 +418,10 @@ Note that extending the uniswap v2 code may be possible in the future if uniswap fee: Object.keys(FeeAmount).find( (key) => FeeAmount[key] === position.fee ), - lowerPrice: positionInst.token0PriceLower.toSignificant(8), - upperPrice: positionInst.token0PriceUpper.toSignificant(8), - amount0: positionInst.amount0.toSignificant(8), - amount1: positionInst.amount1.toSignificant(8), + lowerPrice: positionInst.token0PriceLower.toFixed(8), + upperPrice: positionInst.token0PriceUpper.toFixed(8), + amount0: positionInst.amount0.toFixed(), + amount1: positionInst.amount1.toFixed(), unclaimedToken0: ethers.utils.formatUnits( feeInfo.amount0.toString(), token0.decimals