@@ -22,18 +22,23 @@ import { ether } from '@setprotocol/set-protocol-v2/dist/utils/common';
22
22
23
23
import TradeAPI from '@src/api/TradeAPI' ;
24
24
import TradeModuleWrapper from '@src/wrappers/set-protocol-v2/TradeModuleWrapper' ;
25
+ import type SetTokenAPI from '@src/api/SetTokenAPI' ;
26
+ import { TradeQuoter } from '@src/api/utils/tradequote' ;
25
27
import { expect } from '@test/utils/chai' ;
28
+ import { TradeQuote } from '@src/types' ;
26
29
27
30
const provider = new ethers . providers . JsonRpcProvider ( 'http://localhost:8545' ) ;
28
31
29
32
jest . mock ( '@src/wrappers/set-protocol-v2/TradeModuleWrapper' ) ;
33
+ jest . mock ( '@src/api/utils/tradequote' ) ;
30
34
31
35
describe ( 'TradeAPI' , ( ) => {
32
36
let tradeModuleAddress : Address ;
33
37
let setTokenAddress : Address ;
34
38
let owner : Address ;
35
39
36
40
let tradeModuleWrapper : TradeModuleWrapper ;
41
+ let tradeQuoter : TradeQuoter ;
37
42
38
43
let tradeAPI : TradeAPI ;
39
44
@@ -46,10 +51,12 @@ describe('TradeAPI', () => {
46
51
47
52
tradeAPI = new TradeAPI ( provider , tradeModuleAddress ) ;
48
53
tradeModuleWrapper = ( TradeModuleWrapper as any ) . mock . instances [ 0 ] ;
54
+ tradeQuoter = ( TradeQuoter as any ) . mock . instances [ 0 ] ;
49
55
} ) ;
50
56
51
57
afterEach ( async ( ) => {
52
58
( TradeModuleWrapper as any ) . mockClear ( ) ;
59
+ ( TradeQuoter as any ) . mockClear ( ) ;
53
60
} ) ;
54
61
55
62
describe ( '#initializeAsync' , ( ) => {
@@ -175,4 +182,113 @@ describe('TradeAPI', () => {
175
182
} ) ;
176
183
} ) ;
177
184
} ) ;
185
+
186
+ describe ( '#fetchTradeQuote' , ( ) => {
187
+ let subjectFromToken : Address ;
188
+ let subjectToToken : Address ;
189
+ let subjectFromTokenDecimals : number ;
190
+ let subjectToTokenDecimals : number ;
191
+ let subjectRawAmount : string ;
192
+ let subjectFromAddress : Address ;
193
+ let subjectSetToken : SetTokenAPI ;
194
+ let subjectGasPrice : number ;
195
+
196
+ beforeEach ( async ( ) => {
197
+ subjectFromToken = '0xAAAA15AA9B462ed4fC84B5dFc43Fd2a10a54B569' ;
198
+ subjectToToken = '0xBBBB262A92581EC09C2d522b48bCcd9E3C8ACf9C' ;
199
+ subjectFromTokenDecimals = 8 ;
200
+ subjectToTokenDecimals = 6 ;
201
+ subjectRawAmount = '5' ;
202
+ subjectFromAddress = '0xCCCC262A92581EC09C2d522b48bCcd9E3C8ACf9C' ;
203
+ subjectSetToken = < unknown > { val : 'settoken' } as SetTokenAPI ;
204
+ subjectGasPrice = 20 ;
205
+ } ) ;
206
+
207
+ async function subject ( ) : Promise < TradeQuote > {
208
+ return await tradeAPI . fetchTradeQuoteAsync (
209
+ subjectFromToken ,
210
+ subjectToToken ,
211
+ subjectFromTokenDecimals ,
212
+ subjectToTokenDecimals ,
213
+ subjectRawAmount ,
214
+ subjectFromAddress ,
215
+ subjectSetToken ,
216
+ subjectGasPrice
217
+ ) ;
218
+ }
219
+
220
+ it ( 'should call the TradeQuoter with correct params' , async ( ) => {
221
+ const expectedQuoteOptions = {
222
+ fromToken : subjectFromToken ,
223
+ toToken : subjectToToken ,
224
+ fromTokenDecimals : subjectFromTokenDecimals ,
225
+ toTokenDecimals : subjectToTokenDecimals ,
226
+ rawAmount : subjectRawAmount ,
227
+ fromAddress : subjectFromAddress ,
228
+ chainId : ( await provider . getNetwork ( ) ) . chainId ,
229
+ tradeModule : tradeModuleWrapper ,
230
+ provider : provider ,
231
+ setToken : subjectSetToken ,
232
+ gasPrice : subjectGasPrice ,
233
+ slippagePercentage : undefined ,
234
+ isFirmQuote : undefined ,
235
+ feePercentage : undefined ,
236
+ feeRecipient : undefined ,
237
+ excludedSources : undefined ,
238
+ } ;
239
+ await subject ( ) ;
240
+
241
+ expect ( tradeQuoter . generate ) . to . have . beenCalledWith ( expectedQuoteOptions ) ;
242
+ } ) ;
243
+
244
+ describe ( 'when the fromToken address is invalid' , ( ) => {
245
+ beforeEach ( async ( ) => {
246
+ subjectFromToken = '0xInvalidAddress' ;
247
+ } ) ;
248
+
249
+ it ( 'should throw with invalid params' , async ( ) => {
250
+ await expect ( subject ( ) ) . to . be . rejectedWith ( 'Validation error' ) ;
251
+ } ) ;
252
+ } ) ;
253
+
254
+ describe ( 'when the toToken address is invalid' , ( ) => {
255
+ beforeEach ( async ( ) => {
256
+ subjectToToken = '0xInvalidAddress' ;
257
+ } ) ;
258
+
259
+ it ( 'should throw with invalid params' , async ( ) => {
260
+ await expect ( subject ( ) ) . to . be . rejectedWith ( 'Validation error' ) ;
261
+ } ) ;
262
+ } ) ;
263
+
264
+ describe ( 'when the fromTokenDecimals is invalid' , ( ) => {
265
+ beforeEach ( async ( ) => {
266
+ subjectFromTokenDecimals = < unknown > '100' as number ;
267
+ } ) ;
268
+
269
+ it ( 'should throw with invalid params' , async ( ) => {
270
+ await expect ( subject ( ) ) . to . be . rejectedWith ( 'Validation error' ) ;
271
+ } ) ;
272
+ } ) ;
273
+
274
+ describe ( 'when the toTokenDecimals is invalid' , ( ) => {
275
+ beforeEach ( async ( ) => {
276
+ subjectToTokenDecimals = < unknown > '100' as number ;
277
+ } ) ;
278
+
279
+ it ( 'should throw with invalid params' , async ( ) => {
280
+ await expect ( subject ( ) ) . to . be . rejectedWith ( 'Validation error' ) ;
281
+ } ) ;
282
+ } ) ;
283
+
284
+ describe ( 'when the rawAmount quantity is invalid' , ( ) => {
285
+ beforeEach ( async ( ) => {
286
+ subjectRawAmount = < unknown > 5 as string ;
287
+ } ) ;
288
+
289
+ it ( 'should throw with invalid params' , async ( ) => {
290
+ await expect ( subject ( ) ) . to . be . rejectedWith ( 'Validation error' ) ;
291
+ } ) ;
292
+ } ) ;
293
+ } ) ;
178
294
} ) ;
0 commit comments