44 TokenERC20Info ,
55} from '../services/ethereum' ;
66import { 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+
810import { logger } from '../services/logger' ;
911import { Router , Request , Response } from 'express' ;
1012import { ethers } from 'ethers' ;
@@ -17,7 +19,8 @@ const latency = (startTime: number, endTime: number): number => {
1719
1820const config = new EthereumConfigService ( ) ;
1921const ethereumService = new EthereumService ( config ) ;
20- const ethereumGasService = new EthereumGasService ( config ) ;
22+ // const ethereumGasService = new EthereumGasService(config);
23+ const fees = new Fees ( ) ;
2124
2225router . 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+
216198export default router ;
0 commit comments