@@ -11,17 +11,28 @@ import { getErc20Balance } from '../../src/payment/erc20';
11
11
import Utils from '@requestnetwork/utils' ;
12
12
import { revokeErc20Approval } from '@requestnetwork/payment-processor/src/payment/utils' ;
13
13
import { EnrichedRequest , IConversionPaymentSettings } from '../../src/index' ;
14
- import { currencyManager } from './shared' ;
14
+ // import { currencyManager } from './shared';
15
15
import {
16
16
approveErc20BatchConversionIfNeeded ,
17
17
getBatchConversionProxyAddress ,
18
18
payBatchConversionProxyRequest ,
19
19
prepareBatchConversionPaymentTransaction ,
20
20
} from '../../src/payment/batch-conversion-proxy' ;
21
21
import { batchConversionPaymentsArtifact } from '@requestnetwork/smart-contracts' ;
22
- import { UnsupportedCurrencyError } from '@requestnetwork/currency' ;
22
+ import { CurrencyManager , UnsupportedCurrencyError } from '@requestnetwork/currency' ;
23
23
import { BATCH_PAYMENT_NETWORK_ID } from '@requestnetwork/types/dist/payment-types' ;
24
24
25
+ const currencyManager = new CurrencyManager ( [
26
+ ...CurrencyManager . getDefaultList ( ) ,
27
+ {
28
+ address : '0x38cf23c52bb4b13f051aec09580a2de845a7fa35' ,
29
+ decimals : 18 ,
30
+ network : 'private' ,
31
+ symbol : 'DAI' ,
32
+ type : RequestLogicTypes . CURRENCY . ERC20 ,
33
+ } ,
34
+ ] ) ;
35
+
25
36
/* eslint-disable no-magic-numbers */
26
37
/* eslint-disable @typescript-eslint/no-unused-expressions */
27
38
@@ -52,7 +63,7 @@ const alphaPaymentSettings: IConversionPaymentSettings = {
52
63
53
64
// requests setting
54
65
55
- const EURExpectedAmount = 100 ;
66
+ const EURExpectedAmount = 55000 ; // 55 000 €
56
67
const EURFeeAmount = 2 ;
57
68
// amounts used for DAI and FAU requests
58
69
const expectedAmount = 100000 ;
@@ -182,6 +193,19 @@ describe('erc20-batch-conversion-proxy', () => {
182
193
FAUTokenAddress ,
183
194
wallet ,
184
195
) ;
196
+
197
+ // Approve the contract to spent DAI with a conversion request
198
+ const approvalTx = await approveErc20BatchConversionIfNeeded (
199
+ EURValidRequest ,
200
+ wallet . address ,
201
+ batchConvVersion ,
202
+ wallet . provider ,
203
+ alphaPaymentSettings ,
204
+ ) ;
205
+ expect ( approvalTx ) . toBeDefined ( ) ;
206
+ if ( approvalTx ) {
207
+ await approvalTx . wait ( 1 ) ;
208
+ }
185
209
} ) ;
186
210
187
211
describe ( `Conversion:` , ( ) => {
@@ -341,78 +365,70 @@ describe('erc20-batch-conversion-proxy', () => {
341
365
{ gasPrice : '20000000000' } ,
342
366
) ;
343
367
expect ( spy ) . toHaveBeenCalledWith ( {
344
- data : '0x92cddb91000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000c5fdf4076b8f3a5357c5e395ab970b5b54098fef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f17f52151ebef6c7334fad080c5704d77216b7320000000000000000000000000000000000000000000000000000000005f5e10000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000001e84800000000000000000000000000000000000000000204fce5e3e250261100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000017b4158805772ced11225e77339f90beb5aae968000000000000000000000000775eb53d00dd0acd3ec1696472105d579b9b386b00000000000000000000000038cf23c52bb4b13f051aec09580a2de845a7fa35000000000000000000000000000000000000000000000000000000000000000886dfbccad783599a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ,
368
+ data : '0x92cddb91000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000c5fdf4076b8f3a5357c5e395ab970b5b54098fef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f17f52151ebef6c7334fad080c5704d77216b7320000000000000000000000000000000000000000000000000000000cce41660000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000001e84800000000000000000000000000000000000000000204fce5e3e250261100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000017b4158805772ced11225e77339f90beb5aae968000000000000000000000000775eb53d00dd0acd3ec1696472105d579b9b386b00000000000000000000000038cf23c52bb4b13f051aec09580a2de845a7fa35000000000000000000000000000000000000000000000000000000000000000886dfbccad783599a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ,
345
369
gasPrice : '20000000000' ,
346
370
to : getBatchConversionProxyAddress ( EURValidRequest , '0.1.0' ) ,
347
371
value : 0 ,
348
372
} ) ;
349
373
wallet . sendTransaction = originalSendTransaction ;
350
374
} ) ;
351
- it ( 'should convert and pay a request in EUR with ERC20' , async ( ) => {
352
- // Approve the contract
353
- const approvalTx = await approveErc20BatchConversionIfNeeded (
354
- EURValidRequest ,
355
- wallet . address ,
356
- batchConvVersion ,
357
- wallet . provider ,
358
- alphaPaymentSettings ,
359
- ) ;
360
- expect ( approvalTx ) . toBeDefined ( ) ;
361
- if ( approvalTx ) {
362
- await approvalTx . wait ( 1 ) ;
363
- }
364
-
365
- // Get the balances to compare after payment
366
- const initialETHFromBalance = await wallet . getBalance ( ) ;
367
- const initialDAIFromBalance = await getErc20Balance (
368
- DAIValidRequest ,
369
- wallet . address ,
370
- provider ,
371
- ) ;
372
-
373
- // Convert and pay
374
- const tx = await payBatchConversionProxyRequest (
375
- [
376
- {
377
- paymentNetworkId : BATCH_PAYMENT_NETWORK_ID . BATCH_MULTI_ERC20_CONVERSION_PAYMENTS ,
378
- request : EURValidRequest ,
379
- paymentSettings : alphaPaymentSettings ,
380
- } ,
381
- ] ,
382
- batchConvVersion ,
383
- wallet ,
384
- true ,
385
- ) ;
386
- const confirmedTx = await tx . wait ( 1 ) ;
387
- expect ( confirmedTx . status ) . toEqual ( 1 ) ;
388
- expect ( tx . hash ) . toBeDefined ( ) ;
389
-
390
- // Get the new balances
391
- const ETHFromBalance = await wallet . getBalance ( ) ;
392
- const DAIFromBalance = await getErc20Balance ( DAIValidRequest , wallet . address , provider ) ;
393
-
394
- // Check each balance
395
- const amountToPay = expectedConversionAmount ( EURExpectedAmount ) ;
396
- const feeToPay = expectedConversionAmount ( EURFeeAmount ) ;
397
- const expectedAmountToPay = amountToPay
398
- . add ( feeToPay )
399
- . mul ( BATCH_DENOMINATOR + BATCH_CONV_FEE )
400
- . div ( BATCH_DENOMINATOR ) ;
401
- expect (
402
- BigNumber . from ( initialETHFromBalance ) . sub ( ETHFromBalance ) . toNumber ( ) ,
403
- ) . toBeGreaterThan ( 0 ) ;
404
- expect (
405
- BigNumber . from ( initialDAIFromBalance ) . sub ( BigNumber . from ( DAIFromBalance ) ) ,
406
- // Calculation of expectedAmountToPay
407
- // expectedAmount: 1.00
408
- // feeAmount: + .02
409
- // = 1.02
410
- // AggEurUsd.sol x 1.20
411
- // AggDaiUsd.sol / 1.01
412
- // BATCH_CONV_FEE x 1.003
413
- // (exact result) = 1.215516831683168316 (over 18 decimals for this ERC20)
414
- ) . toEqual ( expectedAmountToPay ) ;
415
- } ) ;
375
+ for ( const skipFeeUSDLimit in [ 'true' , 'false' ] ) {
376
+ it ( `should convert and pay a request in EUR with ERC20, ${
377
+ skipFeeUSDLimit === 'true' ? 'skipFeeUSDLimit' : 'no skipFeeUSDLimit'
378
+ } `, async ( ) => {
379
+ // Get the balances to compare after payment
380
+ const initialETHFromBalance = await wallet . getBalance ( ) ;
381
+ const initialDAIFromBalance = await getErc20Balance (
382
+ DAIValidRequest ,
383
+ wallet . address ,
384
+ provider ,
385
+ ) ;
386
+
387
+ // Convert and pay
388
+ const tx = await payBatchConversionProxyRequest (
389
+ [
390
+ {
391
+ paymentNetworkId : BATCH_PAYMENT_NETWORK_ID . BATCH_MULTI_ERC20_CONVERSION_PAYMENTS ,
392
+ request : EURValidRequest ,
393
+ paymentSettings : alphaPaymentSettings ,
394
+ } ,
395
+ ] ,
396
+ batchConvVersion ,
397
+ wallet ,
398
+ skipFeeUSDLimit === 'true' ,
399
+ ) ;
400
+ const confirmedTx = await tx . wait ( 1 ) ;
401
+ expect ( confirmedTx . status ) . toEqual ( 1 ) ;
402
+ expect ( tx . hash ) . toBeDefined ( ) ;
403
+
404
+ // Get the new balances
405
+ const ETHFromBalance = await wallet . getBalance ( ) ;
406
+ const DAIFromBalance = await getErc20Balance ( DAIValidRequest , wallet . address , provider ) ;
407
+
408
+ // Check each balance
409
+ const amountToPay = expectedConversionAmount ( EURExpectedAmount ) ;
410
+ const feeToPay = expectedConversionAmount ( EURFeeAmount ) ;
411
+ const totalFeeToPay =
412
+ skipFeeUSDLimit === 'true'
413
+ ? amountToPay . add ( feeToPay ) . mul ( BATCH_CONV_FEE ) . div ( BATCH_DENOMINATOR )
414
+ : BigNumber . from ( '1984229702970297029' ) ; // eq to 150$ batch fee (USD limit) + 2$
415
+ const expectedAmountToPay = amountToPay . add ( totalFeeToPay ) ;
416
+ expect (
417
+ BigNumber . from ( initialETHFromBalance ) . sub ( ETHFromBalance ) . toNumber ( ) ,
418
+ ) . toBeGreaterThan ( 0 ) ;
419
+ expect (
420
+ BigNumber . from ( initialDAIFromBalance ) . sub ( BigNumber . from ( DAIFromBalance ) ) ,
421
+ // Calculation of expectedAmountToPay
422
+ // expectedAmount: 1.00
423
+ // feeAmount: + .02
424
+ // = 1.02
425
+ // AggEurUsd.sol x 1.20
426
+ // AggDaiUsd.sol / 1.01
427
+ // BATCH_CONV_FEE x 1.003
428
+ // (exact result) = 1.215516831683168316 (over 18 decimals for this ERC20)
429
+ ) . toEqual ( expectedAmountToPay ) ;
430
+ } ) ;
431
+ }
416
432
it ( 'should convert and pay two requests in EUR with ERC20' , async ( ) => {
417
433
// Get initial balances
418
434
const initialETHFromBalance = await wallet . getBalance ( ) ;
@@ -542,9 +558,7 @@ describe('erc20-batch-conversion-proxy', () => {
542
558
FAURequest . currencyInfo . type = RequestLogicTypes . CURRENCY . ETH ;
543
559
await expect (
544
560
payBatchConversionProxyRequest ( enrichedRequests , batchConvVersion , wallet , true ) ,
545
- ) . rejects . toThrowError (
546
- 'request cannot be processed, or is not an pn-erc20-fee-proxy-contract request' ,
547
- ) ;
561
+ ) . rejects . toThrowError ( 'wrong request currencyInfo type' ) ;
548
562
} ) ;
549
563
550
564
it ( "should throw an error if one request's currencyInfo has no value" , async ( ) => {
0 commit comments