Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Aug 7, 2023
1 parent 25e93bb commit e8834bb
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 127 deletions.
14 changes: 9 additions & 5 deletions contracts/DePayRouterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]);
Expand Down
29 changes: 18 additions & 11 deletions test/_approve-exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Expand Down
25 changes: 16 additions & 9 deletions test/_deadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
)
Expand Down
8 changes: 3 additions & 5 deletions test/_pay-with-exchange-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
Expand Down Expand Up @@ -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
Expand Down
106 changes: 67 additions & 39 deletions test/_pay-with-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Expand All @@ -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 }
)
Expand All @@ -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 }
)
Expand All @@ -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!'
)
})
})
Expand Down
106 changes: 67 additions & 39 deletions test/_pay-with-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)

Expand All @@ -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
)

Expand All @@ -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!'
)
})
})
Expand Down
Loading

0 comments on commit e8834bb

Please sign in to comment.