From e8834bb40c0ce79fd1e8ce13e18d9d12c5a544de Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Mon, 7 Aug 2023 16:45:35 +0200 Subject: [PATCH] fix tests --- contracts/DePayRouterV2.sol | 14 ++-- test/_approve-exchange.ts | 29 ++++--- test/_deadline.ts | 25 +++--- test/_pay-with-exchange-conversion.ts | 8 +- test/_pay-with-native.ts | 106 ++++++++++++++++---------- test/_pay-with-token.ts | 106 ++++++++++++++++---------- test/_pay-with-wrapped-conversion.ts | 56 +++++++++----- 7 files changed, 217 insertions(+), 127 deletions(-) diff --git a/contracts/DePayRouterV2.sol b/contracts/DePayRouterV2.sol index c7b4fcd..c917690 100644 --- a/contracts/DePayRouterV2.sol +++ b/contracts/DePayRouterV2.sol @@ -55,10 +55,14 @@ contract DePayRouterV2 is Ownable { // pull requires approve, push is pushing the token prior calling // [ - // 0: exchangePullToken, - // 1: receiverPullToken + // 0: exchangeType, + // 1: receiverType // ] - bool[] calldata pull, + // type + // 0: do nothing + // 1: pull + // 2: push + uint8[] calldata types, // [ // 0: exchangeCallData, @@ -104,9 +108,9 @@ contract DePayRouterV2 is Ownable { if(addresses[0] == NATIVE) { (success,) = addresses[1].call{value: msg.value}(calls[0]); } else { - if(pull[0]) { + if(types[0] == 1) { // pull IERC20(addresses[0]).safeApprove(addresses[1], amounts[0]); - } else { // push + } else if(types[0] == 2) { // push IERC20(addresses[0]).safeTransfer(addresses[1], amounts[0]); } (success,) = addresses[1].call(calls[0]); diff --git a/test/_approve-exchange.ts b/test/_approve-exchange.ts index fdf5ef2..22c644d 100644 --- a/test/_approve-exchange.ts +++ b/test/_approve-exchange.ts @@ -48,17 +48,24 @@ export default ({ blockchain })=>{ it('fails if trying to convert through a not-approved exchange', async ()=> { await expect( router.connect(wallets[0]).pay( - 1000000000, // amountIn - NATIVE, // tokenIn - "0x00000000000080C886232E9b7EBBFb942B5987AA", // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - 1000000000, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver - deadline, - { value: 1000000000 } // deadline + [ // amounts + 1000000000, // amountIn + 1000000000, // paymentAmount + 1 // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + "0x00000000000080C886232E9b7EBBFb942B5987AA", // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], + deadline, // deadline + { value: 1000000000 } ) ).to.be.revertedWith( 'DePay: Exchange has not been approved!' diff --git a/test/_deadline.ts b/test/_deadline.ts index a889db6..93416f6 100644 --- a/test/_deadline.ts +++ b/test/_deadline.ts @@ -31,15 +31,22 @@ export default ({ blockchain })=>{ it('fails if payment deadline has passed', async ()=> { await expect( router.connect(wallets[0]).pay( - 1000000000, // amountIn - NATIVE, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - 1000000000, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver + [ // amounts + 1000000000, // amountIn + 1000000000, // paymentAmount + 1 // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + ZERO, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], 0, // deadline { value: 1000000000 } ) diff --git a/test/_pay-with-exchange-conversion.ts b/test/_pay-with-exchange-conversion.ts index 1327b8a..10644c3 100644 --- a/test/_pay-with-exchange-conversion.ts +++ b/test/_pay-with-exchange-conversion.ts @@ -61,9 +61,7 @@ export default ({ blockchain, fromToken, fromAccount, exchanges })=>{ wallets[1].address, // paymentReceiver wallets[2].address, // feeReceiver ], - [ // pull - false - ], + [0], // types [ // calls Web3Blockchains[blockchain].zero, // exchangeCall ], @@ -129,8 +127,8 @@ export default ({ blockchain, fromToken, fromAccount, exchanges })=>{ wallets[1].address, // paymentReceiver wallets[2].address, // feeReceiver ], - [ // pull - exchange.type === 'pull' + [ // types + exchange.type === 'pull' ? 1 : 2 ], [ // calls callData, // exchangeCall diff --git a/test/_pay-with-native.ts b/test/_pay-with-native.ts index ecb5fcd..8f4d52d 100644 --- a/test/_pay-with-native.ts +++ b/test/_pay-with-native.ts @@ -31,16 +31,23 @@ export default ({ blockchain })=>{ it('fails if native amount was not paid in', async ()=> { await expect( router.connect(wallets[0]).pay( - 1000000000, // amountIn - NATIVE, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - 1000000000, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver - deadline // deadline + [ // amounts + 1000000000, // amountIn + 1000000000, // paymentAmount + 0 // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + ZERO, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], + deadline, // deadline ) ).to.be.revertedWith( 'DePay: Insufficient amount paid in!' @@ -55,15 +62,22 @@ export default ({ blockchain })=>{ await expect( router.connect(wallets[0]).pay( - amountIn, // amountIn - NATIVE, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + 0 // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + ZERO, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], deadline, // deadline { value: 1000000000 } ) @@ -84,15 +98,22 @@ export default ({ blockchain })=>{ await expect( router.connect(wallets[0]).pay( - amountIn, // amountIn - NATIVE, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - feeAmount, // feeAmount - wallets[2].address, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + feeAmount // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + ZERO, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + wallets[2].address, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], deadline, // deadline { value: 1000000000 } ) @@ -111,19 +132,26 @@ export default ({ blockchain })=>{ await wallets[0].sendTransaction({ to: router.address, value: 1000000000 }); await expect( router.connect(wallets[0]).pay( - 0, // amountIn - NATIVE, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - NATIVE, // tokenOut - 1000000000, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver - deadline // deadline + [ // amounts + 0, // amountIn + 1000000000, // paymentAmount + 0 // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + ZERO, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], + deadline, // deadline ) ).to.be.revertedWith( - 'DePay: Insufficient balance after payment!' + 'DePay: Insufficient balanceIn after payment!' ) }) }) diff --git a/test/_pay-with-token.ts b/test/_pay-with-token.ts index c2b1bd7..d5f8be3 100644 --- a/test/_pay-with-token.ts +++ b/test/_pay-with-token.ts @@ -36,17 +36,24 @@ export default ({ blockchain, token, fromAccount, reversalReason })=>{ it('fails if approval was not granted and amount was not paid in', async ()=> { await expect( - router.connect(fromAccount).pay( - 1000000000, // amountIn - TOKEN, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - TOKEN, // tokenOut - 1000000000, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver - deadline // deadline + router.connect(wallets[0]).pay( + [ // amounts + 1000000000, // amountIn + 1000000000, // paymentAmount + 0 // feeAmount + ], + [ // addresses + TOKEN, // tokenIn + ZERO, // exchangeAddress + TOKEN, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], + deadline, // deadline ) ).to.be.revertedWith( reversalReason @@ -62,15 +69,22 @@ export default ({ blockchain, token, fromAccount, reversalReason })=>{ await tokenContract.connect(fromAccount).approve(router.address, amountIn) await router.connect(fromAccount).pay( - amountIn, // amountIn - TOKEN, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - TOKEN, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - 0, // feeAmount - ZERO, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + 0 // feeAmount + ], + [ // addresses + TOKEN, // tokenIn + ZERO, // exchangeAddress + TOKEN, // tokenOut + wallets[1].address, // paymentReceiver + ZERO, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], deadline, // deadline ) @@ -89,15 +103,22 @@ export default ({ blockchain, token, fromAccount, reversalReason })=>{ await tokenContract.connect(fromAccount).approve(router.address, amountIn) await router.connect(fromAccount).pay( - amountIn, // amountIn - TOKEN, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - TOKEN, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - feeAmount, // feeAmount - wallets[2].address, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + feeAmount // feeAmount + ], + [ // addresses + TOKEN, // tokenIn + ZERO, // exchangeAddress + TOKEN, // tokenOut + wallets[1].address, // paymentReceiver + wallets[2].address, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], deadline, // deadline ) @@ -122,19 +143,26 @@ export default ({ blockchain, token, fromAccount, reversalReason })=>{ await expect( router.connect(fromAccount).pay( - amountIn, // amountIn - TOKEN, // tokenIn - ZERO, // exchangeAddress - ZERO, // exchangeCall - TOKEN, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - feeAmount, // feeAmount - wallets[2].address, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + feeAmount // feeAmount + ], + [ // addresses + TOKEN, // tokenIn + ZERO, // exchangeAddress + TOKEN, // tokenOut + wallets[1].address, // paymentReceiver + wallets[2].address, // feeReceiver + ], + [], // types + [ // calls + ZERO, // exchangeCall + ], deadline, // deadline ) ).to.be.revertedWith( - 'DePay: Insufficient balance after payment!' + 'DePay: Insufficient balanceIn after payment!' ) }) }) diff --git a/test/_pay-with-wrapped-conversion.ts b/test/_pay-with-wrapped-conversion.ts index 4a52cb6..778b1f6 100644 --- a/test/_pay-with-wrapped-conversion.ts +++ b/test/_pay-with-wrapped-conversion.ts @@ -46,15 +46,24 @@ export default ({ blockchain })=>{ const feeReceiverBalanceBefore = await wrapperContract.balanceOf(wallets[2].address) await router.connect(wallets[0]).pay( - amountIn, // amountIn - NATIVE, // tokenIn - WRAPPED, // exchangeAddress - callData, // exchangeCall - WRAPPED, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - feeAmount, // feeAmount - wallets[2].address, // feeReceiver + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + feeAmount // feeAmount + ], + [ // addresses + NATIVE, // tokenIn + WRAPPED, // exchangeAddress + WRAPPED, // tokenOut + wallets[1].address, // paymentReceiver + wallets[2].address, // feeReceiver + ], + [ // types + 0 + ], + [ // calls + callData, // exchangeCall + ], deadline, // deadline { value: 1000000000 } ) @@ -81,16 +90,25 @@ export default ({ blockchain })=>{ await wrapperContract.connect(wallets[0]).approve(router.address, amountIn) await router.connect(wallets[0]).pay( - amountIn, // amountIn - WRAPPED, // tokenIn - WRAPPED, // exchangeAddress - callData, // exchangeCall - NATIVE, // tokenOut - paymentAmount, // paymentAmount - wallets[1].address, // paymentReceiver - feeAmount, // feeAmount - wallets[2].address, // feeReceiver - deadline // deadline + [ // amounts + amountIn, // amountIn + paymentAmount, // paymentAmount + feeAmount // feeAmount + ], + [ // addresses + WRAPPED, // tokenIn + WRAPPED, // exchangeAddress + NATIVE, // tokenOut + wallets[1].address, // paymentReceiver + wallets[2].address, // feeReceiver + ], + [ // types + 0 + ], + [ // calls + callData, // exchangeCall + ], + deadline, // deadline ) const paymentReceiverBalanceAfter = await await provider.getBalance(wallets[1].address)