Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.

Commit 98fbcdc

Browse files
authored
Merge pull request #130 from CoinAlpha/development
release / version 0.8.0 development -> master
2 parents b9c758a + 1175855 commit 98fbcdc

21 files changed

+1669
-1057
lines changed

conf/global_conf.yml.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ GMT_OFFSET: +0800
7676
# EthGasStation
7777
# API key for defipulse.com gas station API
7878
# Gas level you want to use for Ethereum transactions (fast, fastest, safeLow, average)
79-
ENABLE_ETH_GAS_STATION: true
79+
ETH_GAS_STATION_ENABLE: true
8080
ETH_GAS_STATION_API_KEY:
8181
ETH_GAS_STATION_GAS_LEVEL: fast
8282
ETH_GAS_STATION_REFRESH_TIME: 60
83-
MANUAL_GAS_PRICE: 100
83+
ETH_MANUAL_GAS_PRICE: 100
8484

8585
# Balancer Config
8686
BALANCER_MAX_SWAPS: 4

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"license": "Apache 2",
77
"repository": "https://github.com/coinalpha/gateway-api",
8+
"type": "module",
89
"scripts": {
910
"prebuild": "rimraf dist && mkdir dist",
1011
"build": "tsc --project ./",
@@ -25,14 +26,15 @@
2526
"@balancer-labs/sor": "^0.3.3",
2627
"@ethersproject/experimental": "^5.3.0",
2728
"@perp/contract": "^1.0.6",
28-
"@terra-money/terra.js": "^1.7.0",
29+
"@terra-money/terra.js": "^1.8.8",
2930
"@types/node": "^15.12.4",
3031
"@uniswap/sdk": "^3.0.3",
3132
"@uniswap/sdk-core": "^3.0.0",
3233
"@uniswap/v3-core": "^1.0.0",
3334
"@uniswap/v3-periphery": "^1.0.0",
3435
"@uniswap/v3-sdk": "3.0.0",
3536
"abi-decoder": "^2.4.0",
37+
"add": "^2.0.6",
3638
"app-root-path": "^3.0.0",
3739
"axios": "^0.21.1",
3840
"body-parser": "^1.19.0",

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import apiRoutes from './routes/index.route';
1414
import balancerRoutes from './routes/balancer.route';
1515
import ethRoutes from './routes/ethereum';
1616
import perpFiRoutes from './routes/perpetual_finance.route';
17-
import terraRoutes from './routes/terra.route';
17+
import terraRoutes from './routes/terra';
1818
import uniswapRoutes from './routes/uniswap';
1919
import uniswapV3Routes from './routes/uniswap_v3';
2020

src/assets/erc20_tokens_kovan.json

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,80 @@
44
{
55
"symbol": "BAT",
66
"address": "0x1f1f156E0317167c11Aa412E3d1435ea29Dc3cCE",
7-
"decimals": 18
7+
"decimals": 18,
8+
"chainId": 42
89
},
910
{
1011
"symbol": "WETH",
1112
"address": "0xd0A1E359811322d97991E03f863a0C30C2cF029C",
12-
"decimals": 18
13+
"decimals": 18,
14+
"chainId": 42
1315
},
1416
{
1517
"symbol": "DAI",
16-
"address": "0x1528F3FCc26d13F7079325Fb78D9442607781c8C",
17-
"decimals": 18
18+
"address": "0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa",
19+
"decimals": 18,
20+
"chainId": 42
1821
},
1922
{
2023
"symbol": "MKR",
2124
"address": "0xef13C0c8abcaf5767160018d268f9697aE4f5375",
22-
"decimals": 18
25+
"decimals": 18,
26+
"chainId": 42
2327
},
2428
{
2529
"symbol": "USDC",
2630
"address": "0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5",
27-
"decimals": 6
31+
"decimals": 6,
32+
"chainId": 42
2833
},
2934
{
3035
"symbol": "REP",
3136
"address": "0x8c9e6c40d3402480ACE624730524fACC5482798c",
32-
"decimals": 18
37+
"decimals": 18,
38+
"chainId": 42
3339
},
3440
{
3541
"symbol": "WBTC",
3642
"address": "0xe0C9275E44Ea80eF17579d33c55136b7DA269aEb",
37-
"decimals": 18
43+
"decimals": 18,
44+
"chainId": 42
3845
},
3946
{
4047
"symbol": "SNX",
4148
"address": "0x86436BcE20258a6DcfE48C9512d4d49A30C4d8c4",
42-
"decimals": 18
49+
"decimals": 18,
50+
"chainId": 42
4351
},
4452
{
4553
"symbol": "ANT",
4654
"address": "0x37f03a12241E9FD3658ad6777d289c3fb8512Bc9",
47-
"decimals": 18
55+
"decimals": 18,
56+
"chainId": 42
4857
},
4958
{
5059
"symbol": "ZRX",
5160
"address": "0xccb0F4Cf5D3F97f4a55bb5f5cA321C3ED033f244",
52-
"decimals": 18
61+
"decimals": 18,
62+
"chainId": 42
5363
},
5464
{
5565
"symbol": "COIN1",
5666
"address": "0x809F5A762e7b0CC75C42cd76098b85CB7BD2BA64",
57-
"decimals": 18
67+
"decimals": 18,
68+
"chainId": 42
5869
},
5970
{
6071
"symbol": "COIN2",
6172
"address": "0x9866c4043bc6cf47eaf845c56f6ab221c204e0df",
62-
"decimals": 8
73+
"decimals": 8,
74+
"chainId": 42
6375
},
6476
{
6577
"symbol": "COIN3",
6678
"address": "0x3D2097889B97A9eF23B3eA8FC10c626fbda29099",
67-
"decimals": 18
79+
"decimals": 18,
80+
"chainId": 42
6881
}
6982
]
7083
}

src/routes/ethereum.ts

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
TokenERC20Info,
55
} from '../services/ethereum';
66
import { EthereumConfigService } from '../services/ethereum_config';
7-
import { EthereumGasService } from '../services/ethereum_gas';
7+
// import { EthereumGasService } from '../services/ethereum_gas';
8+
import Fees from '../services/fees';
9+
810
import { logger } from '../services/logger';
911
import { Router, Request, Response } from 'express';
1012
import { ethers } from 'ethers';
@@ -17,7 +19,8 @@ const latency = (startTime: number, endTime: number): number => {
1719

1820
const config = new EthereumConfigService();
1921
const ethereumService = new EthereumService(config);
20-
const ethereumGasService = new EthereumGasService(config);
22+
// const ethereumGasService = new EthereumGasService(config);
23+
const fees = new Fees();
2124

2225
router.post('/', async (_req: Request, res: Response) => {
2326
/*
@@ -36,34 +39,24 @@ router.post('/balances', async (req: Request, res: Response) => {
3639

3740
// Trying connect to Wallet
3841
try {
39-
const wallet: ethers.Wallet = ethereumService.getWallet(
40-
req.body.privateKey || ''
41-
);
42+
const wallet = ethereumService.getWallet(req.body.privateKey);
4243

4344
// Populate token contract info using token symbol list
44-
const tokenContractList: Record<string, TokenERC20Info> = {};
45-
45+
const tokenList: Record<string, TokenERC20Info> = {};
4646
for (const symbol of JSON.parse(req.body.tokenList)) {
47-
const tokenContractInfo = ethereumService.getERC20TokenAddress(symbol);
48-
if (!tokenContractInfo) {
49-
continue;
50-
}
51-
52-
tokenContractList[symbol] = tokenContractInfo;
47+
const token = ethereumService.getERC20Token(symbol) as TokenERC20Info;
48+
tokenList[symbol] = token;
5349
}
5450

55-
// Getting user balancers
5651
const balances: Record<string, string> = {};
5752
balances.ETH = await ethereumService.getETHBalance(wallet);
5853
await Promise.all(
59-
Object.keys(tokenContractList).map(async (symbol) => {
60-
if (tokenContractList[symbol] !== undefined) {
61-
const address = tokenContractList[symbol].address;
62-
const decimals = tokenContractList[symbol].decimals;
54+
Object.keys(tokenList).map(async (symbol) => {
55+
if (tokenList[symbol] !== undefined) {
6356
balances[symbol] = await ethereumService.getERC20Balance(
6457
wallet,
65-
address,
66-
decimals
58+
tokenList[symbol].address,
59+
tokenList[symbol].decimals
6760
);
6861
} else {
6962
logger.error(`Token contract info for ${symbol} not found`);
@@ -91,30 +84,26 @@ router.post('/allowances', async (req: Request, res: Response) => {
9184
res.status(500).send('Wrong connector');
9285
}
9386

94-
// Getting Wallet
87+
// Trying connect to Wallet
9588
try {
9689
const wallet = ethereumService.getWallet(req.body.privateKey);
90+
9791
// Populate token contract info using token symbol list
98-
const tokenContractList: Record<string, TokenERC20Info> = {};
92+
const tokenList: Record<string, TokenERC20Info> = {};
9993
for (const symbol of JSON.parse(req.body.tokenList)) {
100-
const tokenContractInfo = ethereumService.getERC20TokenAddress(symbol);
101-
if (!tokenContractInfo) {
102-
continue;
103-
}
104-
105-
tokenContractList[symbol] = tokenContractInfo;
94+
const token = ethereumService.getERC20Token(symbol) as TokenERC20Info;
95+
tokenList[symbol] = token;
10696
}
97+
10798
const approvals: Record<string, string> = {};
10899
await Promise.all(
109-
Object.keys(tokenContractList).map(async (symbol) => {
110-
const address = tokenContractList[symbol].address;
111-
const decimals = tokenContractList[symbol].decimals;
100+
Object.keys(tokenList).map(async (symbol) => {
112101
try {
113102
approvals[symbol] = await ethereumService.getERC20Allowance(
114103
wallet,
115104
spender,
116-
address,
117-
decimals
105+
tokenList[symbol].address,
106+
tokenList[symbol].decimals
118107
);
119108
} catch (err) {
120109
logger.error(err);
@@ -150,33 +139,21 @@ router.post('/approve', async (req: Request, res: Response) => {
150139
const wallet = ethereumService.getWallet(req.body.privateKey);
151140

152141
// Getting token info
153-
const tokenContractInfo = ethereumService.getERC20TokenAddress(
154-
req.body.token
155-
);
142+
const token = ethereumService.getERC20Token(req.body.token);
156143

157-
if (!tokenContractInfo) {
144+
if (!token) {
158145
res.status(500).send(`Token "${req.body.token}" is not supported`);
159146
} else {
160-
const tokenAddress = tokenContractInfo.address;
161-
162-
const gasPrice = req.body.gasPrice || ethereumGasService.getGasPrice();
163-
164-
let amount = ethers.constants.MaxUint256;
165-
if (req.body.amount) {
166-
amount = ethers.utils.parseUnits(
167-
req.body.amount,
168-
tokenContractInfo.decimals
169-
);
170-
}
147+
const amount = ethers.utils.parseUnits(req.body.amount, token.decimals);
171148
// call approve function
172149
let approval;
173150
try {
174151
approval = await ethereumService.approveERC20(
175152
wallet,
176153
spender,
177-
tokenAddress,
154+
token.address,
178155
amount,
179-
gasPrice
156+
fees.ethGasPrice as number
180157
);
181158
} catch (err) {
182159
approval = err;
@@ -186,9 +163,9 @@ router.post('/approve', async (req: Request, res: Response) => {
186163
network: config.networkName,
187164
timestamp: initTime,
188165
latency: latency(initTime, Date.now()),
189-
tokenAddress: tokenAddress,
166+
tokenAddress: token.address,
190167
spender: spender,
191-
amount: bigNumberWithDecimalToStr(amount, tokenContractInfo.decimals),
168+
amount: bigNumberWithDecimalToStr(amount, token.decimals),
192169
approval: approval,
193170
});
194171
}
@@ -213,4 +190,9 @@ router.post('/poll', async (req: Request, res: Response) => {
213190
});
214191
});
215192

193+
router.post('/token', async (req: Request, res: Response) => {
194+
const token = await ethereumService.getERC20Token(req.body.symbol);
195+
res.status(200).json(token);
196+
});
197+
216198
export default router;

0 commit comments

Comments
 (0)