From 0843d650ce78f148c171009c73ebf3741ba7496c Mon Sep 17 00:00:00 2001 From: Michael Feng Date: Sat, 17 Jul 2021 18:02:10 -0700 Subject: [PATCH 1/4] fixed constants --- conf/global_conf.yml.example | 4 ++-- src/services/ethereum.ts | 2 +- src/services/ethereum_config.ts | 4 ---- src/services/uniswap_v3.js | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/conf/global_conf.yml.example b/conf/global_conf.yml.example index 8cf1925..70219aa 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) -ENABLE_ETH_GAS_STATION: true +ETH_GAS_STATION_ENABLE: true ETH_GAS_STATION_API_KEY: ETH_GAS_STATION_GAS_LEVEL: fast ETH_GAS_STATION_REFRESH_TIME: 60 -MANUAL_GAS_PRICE: 100 +ETH_MANUAL_GAS_PRICE: 100 # Balancer Config BALANCER_MAX_SWAPS: 4 diff --git a/src/services/ethereum.ts b/src/services/ethereum.ts index 0e8713c..15762c9 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: this.config.approvalGasLimit, + gasLimit: 60000, }); } catch (err) { throw new Error(err.reason || 'error approval'); diff --git a/src/services/ethereum_config.ts b/src/services/ethereum_config.ts index db15df8..4026dd0 100644 --- a/src/services/ethereum_config.ts +++ b/src/services/ethereum_config.ts @@ -17,10 +17,6 @@ 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'); } diff --git a/src/services/uniswap_v3.js b/src/services/uniswap_v3.js index 450c408..624d4e7 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') || 5506880; +const GAS_LIMIT = globalConfig.getConfig('UNISWAP_GAS_LIMIT') || 550688; 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); From a132705d3781b03457c4e8586bfc876fa576f1b9 Mon Sep 17 00:00:00 2001 From: Michael Feng Date: Sat, 17 Jul 2021 18:45:42 -0700 Subject: [PATCH 2/4] use gas from fees --- src/routes/ethereum.ts | 11 +++++++---- src/services/ethereum_config.ts | 2 +- src/services/fees.js | 4 ++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/routes/ethereum.ts b/src/routes/ethereum.ts index d41f081..f1a2fd6 100644 --- a/src/routes/ethereum.ts +++ b/src/routes/ethereum.ts @@ -4,7 +4,9 @@ import { TokenERC20Info, } from '../services/ethereum'; import { EthereumConfigService } from '../services/ethereum_config'; -import { EthereumGasService } from '../services/ethereum_gas'; +// import { EthereumGasService } from '../services/ethereum_gas'; +import Fees from '../services/fees'; + import { logger } from '../services/logger'; import { Router, Request, Response } from 'express'; import { ethers } from 'ethers'; @@ -17,7 +19,8 @@ const latency = (startTime: number, endTime: number): number => { const config = new EthereumConfigService(); const ethereumService = new EthereumService(config); -const ethereumGasService = new EthereumGasService(config); +// const ethereumGasService = new EthereumGasService(config); +const fees = new Fees(); router.post('/', async (_req: Request, res: Response) => { /* @@ -158,8 +161,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 || ethereumGasService.getGasPrice(); + const gasPrice = req.body.gasPrice || fees.ethGasPrice; let amount = ethers.constants.MaxUint256; if (req.body.amount) { diff --git a/src/services/ethereum_config.ts b/src/services/ethereum_config.ts index 4026dd0..5fc3570 100644 --- a/src/services/ethereum_config.ts +++ b/src/services/ethereum_config.ts @@ -27,7 +27,7 @@ export class EthereumConfigService { get gasServiceUrl(): string { return ( - this.config.getConfig('ETH_GAS_STATION_URL') + + 'https://ethgasstation.info/api/ethgasAPI.json?api-key=' + this.config.getConfig('ETH_GAS_STATION_API_KEY') ); } diff --git a/src/services/fees.js b/src/services/fees.js index 1a6776d..af9c417 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('ENABLE_ETH_GAS_STATION') || false; + globalConfig.getConfig('ETH_GAS_STATION_ENABLE') || false; const ethGasStationApiKey = globalConfig.getConfig('ETH_GAS_STATION_API_KEY'); -const ethManualGasPrice = parseInt(globalConfig.getConfig('MANUAL_GAS_PRICE')); +const ethManualGasPrice = parseInt(globalConfig.getConfig('ETH_MANUAL_GAS_PRICE')); const ethGasStationURL = ethGasStationHost + '/api/ethgasAPI.json?api-key=' + ethGasStationApiKey; const defaultRefreshInterval = 120; From 7fb7c4670509229bd71c43c6701267a032ce10b7 Mon Sep 17 00:00:00 2001 From: Michael Feng Date: Sat, 17 Jul 2021 19:53:35 -0700 Subject: [PATCH 3/4] change DAI kovan address --- src/assets/erc20_tokens_kovan.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets/erc20_tokens_kovan.json b/src/assets/erc20_tokens_kovan.json index ea84a4b..44bd5d7 100644 --- a/src/assets/erc20_tokens_kovan.json +++ b/src/assets/erc20_tokens_kovan.json @@ -13,7 +13,7 @@ }, { "symbol": "DAI", - "address": "0x1528F3FCc26d13F7079325Fb78D9442607781c8C", + "address": "0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa", "decimals": 18 }, { From e9aad3f73053b8b3a8c540b3722ebc7d05f03632 Mon Sep 17 00:00:00 2001 From: Michael Feng Date: Sat, 17 Jul 2021 20:55:18 -0700 Subject: [PATCH 4/4] increase price precision and fix linting --- src/routes/uniswap_v3.ts | 14 +++++++------- src/services/ethereum.ts | 23 +++++++++++++++-------- src/services/uniswap_v3.js | 10 +++++----- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/routes/uniswap_v3.ts b/src/routes/uniswap_v3.ts index 0f07b18..15a1664 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().toFixed(); + const price = trade.executionPrice.invert().toSignificant(8); 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.toFixed(), + expectedIn: expectedAmount.toSignificant(8), price: price, gasPrice: gasPrice, gasLimit, @@ -286,7 +286,7 @@ router.post('/trade', async (req: Request, res: Response) => { } } else { // sell - const price = trade.executionPrice.toFixed(); + const price = trade.executionPrice.toSignificant(8); 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.toFixed(), + expectedOut: expectedAmount.toSignificant(8), 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().toFixed() - : trade.executionPrice.toFixed(); + ? trade.executionPrice.invert().toSignificant(8) + : trade.executionPrice.toSignificant(8); priceResult = { price: parseFloat(price), amount: parseFloat(amount), - expectedAmount: parseFloat(expectedAmount.toFixed()), + expectedAmount: parseFloat(expectedAmount.toSignificant(8)), }; } } else { diff --git a/src/services/ethereum.ts b/src/services/ethereum.ts index 15762c9..f1cb72a 100644 --- a/src/services/ethereum.ts +++ b/src/services/ethereum.ts @@ -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 obj.address.toUpperCase() === tokenAddress.toUpperCase(); + }); + return tokenContract[0]; + } + return; } - return; -} /** * Return wallet of a private string @@ -262,12 +262,19 @@ 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: transaction.gasUsed.toNumber(), + gasUsed: gasUsed, blockNumber: transaction.blockNumber, confirmations: transaction.confirmations, status: transaction.status || 0, - logs: transaction.logs + logs: transaction.logs, }; } } diff --git a/src/services/uniswap_v3.js b/src/services/uniswap_v3.js index 624d4e7..38dfe02 100644 --- a/src/services/uniswap_v3.js +++ b/src/services/uniswap_v3.js @@ -178,7 +178,7 @@ export default class UniswapV3 { request[twap].value.tickCumulatives[0].toNumber() ) ) - .toFixed() + .toSignificant(8) ); } } @@ -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.toFixed(8), - upperPrice: positionInst.token0PriceUpper.toFixed(8), - amount0: positionInst.amount0.toFixed(), - amount1: positionInst.amount1.toFixed(), + lowerPrice: positionInst.token0PriceLower.toSignificant(8), + upperPrice: positionInst.token0PriceUpper.toSignificant(8), + amount0: positionInst.amount0.toSignificant(8), + amount1: positionInst.amount1.toSignificant(8), unclaimedToken0: ethers.utils.formatUnits( feeInfo.amount0.toString(), token0.decimals