@@ -27,17 +27,12 @@ import SetTokenAPI from './SetTokenAPI';
27
27
import Assertions from '../assertions' ;
28
28
29
29
import {
30
- TradeQuoter ,
31
- CoinGeckoDataService ,
32
- GasOracleService
30
+ TradeQuoter
33
31
} from './utils' ;
34
32
35
33
import {
36
34
TradeQuote ,
37
- CoinGeckoTokenData ,
38
- CoinGeckoTokenMap ,
39
- GasOracleSpeed ,
40
- CoinGeckoCoinPrices
35
+ ZeroExApiUrls
41
36
} from '../types' ;
42
37
43
38
/**
@@ -53,18 +48,17 @@ export default class TradeAPI {
53
48
private assert : Assertions ;
54
49
private provider : Provider ;
55
50
private tradeQuoter : TradeQuoter ;
56
- private coinGecko : CoinGeckoDataService ;
57
- private chainId : number ;
58
51
59
52
public constructor (
60
53
provider : Provider ,
61
54
tradeModuleAddress : Address ,
62
55
zeroExApiKey ?: string ,
56
+ zeroExApiUrls ?: ZeroExApiUrls
63
57
) {
64
58
this . provider = provider ;
65
59
this . tradeModuleWrapper = new TradeModuleWrapper ( provider , tradeModuleAddress ) ;
66
60
this . assert = new Assertions ( ) ;
67
- this . tradeQuoter = new TradeQuoter ( zeroExApiKey ) ;
61
+ this . tradeQuoter = new TradeQuoter ( zeroExApiKey , zeroExApiUrls ) ;
68
62
}
69
63
70
64
/**
@@ -150,7 +144,8 @@ export default class TradeAPI {
150
144
* @param isFirmQuote (Optional) Whether quote request is indicative or firm
151
145
* @param feePercentage (Optional) Default: 0
152
146
* @param feeRecipient (Optional) Default: 0xD3D555Bb655AcBA9452bfC6D7cEa8cC7b3628C55
153
- * @param excludedSources (Optional) Exchanges to exclude (Default: ['Kyber', 'Eth2Dai', 'Uniswap', 'Mesh'])
147
+ * @param excludedSources (Optional) Exchanges to exclude (Default: ['Kyber', 'Eth2Dai', 'Mesh'])
148
+ * @param simulatedChainId (Optional) ChainId of target network (useful when using a forked development client)
154
149
*
155
150
* @return {Promise<TradeQuote> }
156
151
*/
@@ -168,6 +163,7 @@ export default class TradeAPI {
168
163
feePercentage ?: number ,
169
164
feeRecipient ?: Address ,
170
165
excludedSources ?: string [ ] ,
166
+ simulatedChainId ?: number ,
171
167
) : Promise < TradeQuote > {
172
168
this . assert . schema . isValidAddress ( 'fromToken' , fromToken ) ;
173
169
this . assert . schema . isValidAddress ( 'toToken' , toToken ) ;
@@ -176,9 +172,12 @@ export default class TradeAPI {
176
172
this . assert . schema . isValidJsNumber ( 'toTokenDecimals' , toTokenDecimals ) ;
177
173
this . assert . schema . isValidString ( 'rawAmount' , rawAmount ) ;
178
174
179
- const chainId = ( await this . provider . getNetwork ( ) ) . chainId ;
175
+ // The forked Hardhat network has a chainId of 31337 so we can't rely on autofetching this value
176
+ const chainId = ( simulatedChainId !== undefined )
177
+ ? simulatedChainId
178
+ : ( await this . provider . getNetwork ( ) ) . chainId ;
180
179
181
- return this . tradeQuoter . generate ( {
180
+ return this . tradeQuoter . generateQuoteForTrade ( {
182
181
fromToken,
183
182
toToken,
184
183
fromTokenDecimals,
@@ -197,67 +196,4 @@ export default class TradeAPI {
197
196
excludedSources,
198
197
} ) ;
199
198
}
200
-
201
- /**
202
- * Fetches a list of tokens and their metadata from CoinGecko. Each entry includes
203
- * the token's address, proper name, decimals, exchange symbol and a logo URI if available.
204
- * For Ethereum, this is a list of tokens tradeable on Uniswap, for Polygon it's a list of
205
- * tokens tradeable on Sushiswap's Polygon exchange. Method is useful for acquiring token decimals
206
- * necessary to generate a trade quote and images for representing available tokens in a UI.
207
- *
208
- * @return List of tradeable tokens for chain platform
209
- */
210
- public async fetchTokenListAsync ( ) : Promise < CoinGeckoTokenData [ ] > {
211
- await this . initializeForChain ( ) ;
212
- return this . coinGecko . fetchTokenList ( ) ;
213
- }
214
-
215
- /**
216
- * Fetches the same info as `fetchTokenList` in the form of a map indexed by address. Method is
217
- * useful if you're cacheing the token list and want quick lookups for a variety of trades.
218
- *
219
- * @return Map of token addresses to token metadata
220
- */
221
- public async fetchTokenMapAsync ( ) : Promise < CoinGeckoTokenMap > {
222
- await this . initializeForChain ( ) ;
223
- return this . coinGecko . fetchTokenMap ( ) ;
224
- }
225
-
226
- /**
227
- * Fetches a list of prices vs currencies for the specified inputs from CoinGecko
228
- *
229
- * @param contractAddresses String array of contract addresses
230
- * @param vsCurrencies String array of currency codes (see CoinGecko api for a complete list)
231
- *
232
- * @return List of prices vs currencies
233
- */
234
- public async fetchCoinPricesAsync (
235
- contractAddresses : string [ ] ,
236
- vsCurrencies : string [ ]
237
- ) : Promise < CoinGeckoCoinPrices > {
238
- await this . initializeForChain ( ) ;
239
- return this . coinGecko . fetchCoinPrices ( { contractAddresses, vsCurrencies} ) ;
240
- }
241
-
242
- /**
243
- * Fetches the recommended gas price for a specified execution speed.
244
- *
245
- * @param speed (Optional) string value: "average" | "fast" | "fastest" (Default: fast)
246
- *
247
- * @return Number: gas price
248
- */
249
- public async fetchGasPriceAsync ( speed ?: GasOracleSpeed ) : Promise < number > {
250
- await this . initializeForChain ( ) ;
251
- const oracle = new GasOracleService ( this . chainId ) ;
252
- return oracle . fetchGasPrice ( speed ) ;
253
- }
254
-
255
-
256
- private async initializeForChain ( ) {
257
- if ( this . coinGecko === undefined ) {
258
- const network = await this . provider . getNetwork ( ) ;
259
- this . chainId = network . chainId ;
260
- this . coinGecko = new CoinGeckoDataService ( network . chainId ) ;
261
- }
262
- }
263
199
}
0 commit comments