Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions conf/global_conf.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/assets/erc20_tokens_kovan.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"symbol": "DAI",
"address": "0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa",
"address": "0x1528F3FCc26d13F7079325Fb78D9442607781c8C",
"decimals": 18
},
{
Expand Down
11 changes: 4 additions & 7 deletions src/routes/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) => {
/*
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions src/routes/uniswap_v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 9 additions & 16 deletions src/services/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand All @@ -262,19 +262,12 @@ export class EthereumService {
async getTransactionReceipt(txHash: string): Promise<EthTransactionReceipt> {
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
};
}
}
6 changes: 5 additions & 1 deletion src/services/ethereum_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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')
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/fees.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/services/uniswap_v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -178,7 +178,7 @@ export default class UniswapV3 {
request[twap].value.tickCumulatives[0].toNumber()
)
)
.toSignificant(8)
.toFixed()
);
}
}
Expand Down Expand Up @@ -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
Expand Down