Skip to content

Commit

Permalink
update dependencies contracts and add worldchain tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Oct 10, 2024
1 parent cd6d932 commit 8735795
Show file tree
Hide file tree
Showing 28 changed files with 267 additions and 52 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/test-on-worldchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is a basic workflow to help you get started with Actions

name: Test on Worldchain

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches-ignore:
- master

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
test-on-polygon:
# The type of runner that the job will run on
runs-on: ubuntu-latest

env:
MNEMONIC: ${{secrets.MNEMONIC}}
POLYGON_RPC_URL: ${{secrets.POLYGON_RPC_URL}}

# Steps represent a sequence of tasks that will be executed as part of the job
steps:

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- uses: actions/setup-node@v2
with:
node-version: '18'

# Runs a single command using the runners shell
- name: Install packages
run: yarn install

- name: Compile
run: yarn compile

- name: Run test
run: yarn test:polygon
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ event Payment(
uint256 paymentAmount,
uint256 feeAmount,
uint256 protocolAmount,
uint256 slippageAmount,
address tokenInAddress,
address tokenOutAddress,
address feeReceiverAddress
Expand Down Expand Up @@ -426,7 +427,7 @@ yarn test
Test single files:

```
npx hardhat test test/ethereum/pay_with_native.spec.ts --config hardhat.config.ethereum.ts
npx hardhat test test/ethereum/pay_with_exchange_conversion.spec.ts --config hardhat.config.ethereum.ts
```

### Deploy
Expand Down
11 changes: 7 additions & 4 deletions contracts/DePayRouterV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ contract DePayRouterV3 is Ownable2Step {
uint256 paymentAmount,
uint256 feeAmount,
uint256 protocolAmount,
uint256 slippageAmount,
address tokenInAddress,
address tokenOutAddress,
address feeReceiverAddress
Expand All @@ -79,7 +80,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -108,7 +109,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment, permitTransferFromAndSignature);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -297,7 +298,8 @@ contract DePayRouterV3 is Ownable2Step {

/// @dev Emits payment event.
/// @param payment The payment data.
function _emit(IDePayRouterV3.Payment calldata payment) internal {
function _emit(IDePayRouterV3.Payment calldata payment, uint256 balanceOutBefore) internal {
uint256 balanceOutNow = payment.tokenOutAddress == NATIVE ? address(this).balance : IERC20(payment.tokenOutAddress).balanceOf(address(this));
emit Payment(
msg.sender,
payment.paymentReceiverAddress,
Expand All @@ -306,6 +308,7 @@ contract DePayRouterV3 is Ownable2Step {
payment.paymentAmount,
payment.feeAmount,
payment.protocolAmount,
balanceOutNow - balanceOutBefore - payment.protocolAmount,
payment.tokenInAddress,
payment.tokenOutAddress,
payment.feeReceiverAddress
Expand Down
35 changes: 19 additions & 16 deletions flatten/DePayRouterV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -847,18 +847,18 @@ contract DePayRouterV3 is Ownable2Step {
using SafeERC20 for IERC20;

// Custom errors
error PaymentDeadlineReached();
error WrongAmountPaidIn();
error ExchangeNotApproved();
error ExchangeCallMissing();
error ExchangeCallFailed();
error ForwardingPaymentFailed();
error NativePaymentFailed();
error NativeFeePaymentFailed();
error PaymentToZeroAddressNotAllowed();
error InsufficientBalanceInAfterPayment();
error InsufficientBalanceOutAfterPayment();
error InsufficientProtocolAmount();
error PaymentDeadlineReached(); // 0x17e0bcd9
error WrongAmountPaidIn(); // 0xed0842e3
error ExchangeNotApproved(); // 0xc35a3932
error ExchangeCallMissing(); // 0x6b8072c9
error ExchangeCallFailed(); // 0x6d8040c3
error ForwardingPaymentFailed(); // 0xc797a224
error NativePaymentFailed(); // 0xc7abb1a2
error NativeFeePaymentFailed(); // 0x9f06170c
error PaymentToZeroAddressNotAllowed(); // 0xec3a80da
error InsufficientBalanceInAfterPayment(); // 0x84257541
error InsufficientBalanceOutAfterPayment(); // 0x808b9612
error InsufficientProtocolAmount(); // 0x8e1ebd3a

/// @notice Address representing the NATIVE token (e.g. ETH, BNB, MATIC, etc.)
address constant NATIVE = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
Expand Down Expand Up @@ -892,6 +892,7 @@ contract DePayRouterV3 is Ownable2Step {
uint256 paymentAmount,
uint256 feeAmount,
uint256 protocolAmount,
uint256 slippageAmount,
address tokenInAddress,
address tokenOutAddress,
address feeReceiverAddress
Expand All @@ -910,7 +911,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -939,7 +940,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment, permitTransferFromAndSignature);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -973,7 +974,7 @@ contract DePayRouterV3 is Ownable2Step {
_payIn(payment);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);
_emit(payment);
_emit(payment, balanceOutBefore);

return true;
}
Expand Down Expand Up @@ -1128,7 +1129,8 @@ contract DePayRouterV3 is Ownable2Step {

/// @dev Emits payment event.
/// @param payment The payment data.
function _emit(IDePayRouterV3.Payment calldata payment) internal {
function _emit(IDePayRouterV3.Payment calldata payment, uint256 balanceOutBefore) internal {
uint256 balanceOutNow = payment.tokenOutAddress == NATIVE ? address(this).balance : IERC20(payment.tokenOutAddress).balanceOf(address(this));
emit Payment(
msg.sender,
payment.paymentReceiverAddress,
Expand All @@ -1137,6 +1139,7 @@ contract DePayRouterV3 is Ownable2Step {
payment.paymentAmount,
payment.feeAmount,
payment.protocolAmount,
balanceOutNow - balanceOutBefore - payment.protocolAmount,
payment.tokenInAddress,
payment.tokenOutAddress,
payment.feeReceiverAddress
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var {
OPTIMISM_RPC_URL,
AVALANCHE_RPC_URL,
POLYGON_ZKEVM_RPC_URL,
ZKSYNC_ERA_RPC_URL,
WORLDCHAIN_RPC_URL,
} = process.env

const sharedConfig = {
Expand Down Expand Up @@ -44,5 +44,5 @@ export {
OPTIMISM_RPC_URL,
AVALANCHE_RPC_URL,
POLYGON_ZKEVM_RPC_URL,
ZKSYNC_ERA_RPC_URL,
WORLDCHAIN_RPC_URL,
}
21 changes: 21 additions & 0 deletions hardhat.config.worldchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import '@nomiclabs/hardhat-waffle'
import '@nomiclabs/hardhat-ethers'
import { sharedConfig, MNEMONIC, WORLDCHAIN_RPC_URL } from './hardhat.config.shared'

const hardhatConfig = {
...sharedConfig,
defaultNetwork: 'hardhat',
networks: {
hardhat: {
accounts: {
mnemonic: MNEMONIC!
},
forking: {
url: WORLDCHAIN_RPC_URL!,
enabled: true
}
}
}
}

export default hardhatConfig
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"node": ">=10"
},
"devDependencies": {
"@depay/web3-blockchains": "^9.5.1",
"@depay/web3-client-evm": "^10.16.3",
"@depay/web3-exchanges-evm": "^13.2.3",
"@depay/web3-tokens-evm": "^10.1.0",
"@depay/web3-blockchains": "^9.6.7",
"@depay/web3-client-evm": "^10.19.1",
"@depay/web3-exchanges-evm": "^13.11.0",
"@depay/web3-tokens-evm": "^10.4.3",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomiclabs/hardhat-waffle": "^2.0.6",
"@typechain/ethers-v5": "^4.0.0",
Expand Down Expand Up @@ -67,7 +67,7 @@
"test:gnosis": "npx hardhat test test/gnosis/* --config hardhat.config.gnosis.ts",
"test:optimism": "npx hardhat test test/optimism/* --config hardhat.config.optimism.ts",
"test:polygon": "npx hardhat test test/polygon/* --config hardhat.config.polygon.ts",
"test": "yarn test:arbitrum && yarn test:avalanche && yarn test:bsc && yarn test:ethereum && yarn test:fantom && yarn test:gnosis && yarn test:optimism && yarn test:polygon",
"test:worldchain": "npx hardhat test test/worldchain/* --config hardhat.config.worldchain.ts",
"prepublishOnly": "yarn test",
"build": "npx hardhat compile",
"flatten": "rimraf flatten && waffle flatten"
Expand Down
2 changes: 2 additions & 0 deletions test/_pay-to-contract-receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export default ({ blockchain, fromToken, fromAccount, toToken, exchange })=>{
paymentAmount,
feeAmount,
0,
0,
NATIVE,
NATIVE,
wallets[2].address
Expand Down Expand Up @@ -208,6 +209,7 @@ export default ({ blockchain, fromToken, fromAccount, toToken, exchange })=>{
paymentAmount,
feeAmount,
0,
0,
fromToken,
fromToken,
wallets[2].address
Expand Down
85 changes: 85 additions & 0 deletions test/_pay-with-exchange-conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ export default ({ blockchain, fromToken, fromAccount, toToken, exchanges })=>{
tokenOut: Blockchains[blockchain].currency.address,
amountOutMin: totalAmount
})
console.log({
blockchain,
tokenIn: fromToken,
tokenOut: Blockchains[blockchain].currency.address,
amountOutMin: totalAmount
})

const transaction = await route.getTransaction({ account: router.address, inputTokenPushed: exchange.type === 'push' })
const callData = getCallData({
Expand Down Expand Up @@ -236,6 +242,12 @@ export default ({ blockchain, fromToken, fromAccount, toToken, exchanges })=>{
paymentAmountBN,
feeAmountBN,
0,
(amount) => {
expect(amount).to.be.closeTo(
paymentAmountBN.add(feeAmountBN).mul(5).div(1000), // slippage
paymentAmountBN.add(feeAmountBN).mul(1).div(10000) // tollerance
)
},
route.tokenIn,
route.tokenOut,
wallets[2].address
Expand Down Expand Up @@ -441,6 +453,79 @@ export default ({ blockchain, fromToken, fromAccount, toToken, exchanges })=>{
paymentAmountBN,
feeAmountBN,
0,
(amount) => {
expect(amount).to.be.closeTo(
paymentAmountBN.add(feeAmountBN).mul(5).div(1000), // slippage
paymentAmountBN.add(feeAmountBN).mul(1).div(10000) // tollerance
)
},
route.tokenIn,
route.tokenOut,
wallets[2].address
)
})

it('keeps protocol amount and calculates slippageAmount accordingly', async ()=>{

const paymentAmount = 9
const paymentAmountBN = ethers.utils.parseUnits(paymentAmount.toString(), toDecimals)
const feeAmount = 1
const feeAmountBN = ethers.utils.parseUnits(feeAmount.toString(), toDecimals)
const protocolAmount = 1
const protocolAmountBN = ethers.utils.parseUnits(feeAmount.toString(), toDecimals)
const totalAmount = paymentAmount + feeAmount + protocolAmount

const route = await Exchanges[exchange.name].route({
blockchain,
tokenIn: Blockchains[blockchain].currency.address,
tokenOut: toToken,
amountOutMin: totalAmount
})

const transaction = await route.getTransaction({ account: router.address, inputTokenPushed: exchange.type === 'push' })
const callData = getCallData({
address: transaction.to,
api: transaction.api,
provider: wallets[0],
method: transaction.method,
params: transaction.params,
})

const paymentReceiverBalanceBefore = await toTokenContract.balanceOf(wallets[1].address)
const feeReceiverBalanceBefore = await toTokenContract.balanceOf(wallets[2].address)

await expect(
await router.connect(fromAccount)[PAY]({
amountIn: route.amountIn,
paymentAmount: paymentAmountBN,
feeAmount: feeAmountBN,
protocolAmount: protocolAmountBN,
tokenInAddress: route.tokenIn,
exchangeAddress: transaction.to,
tokenOutAddress: route.tokenOut,
paymentReceiverAddress: wallets[1].address,
feeReceiverAddress: wallets[2].address,
exchangeType: exchange.type === 'pull' ? 1 : 2,
receiverType: 0,
exchangeCallData: callData,
receiverCallData: ZERO,
deadline,
}, { value: route.amountIn })
)
.to.emit(router, 'Payment').withArgs(
fromAccount._address, // from
wallets[1].address, // to
deadline, // deadline
route.amountIn,
paymentAmountBN,
feeAmountBN,
protocolAmountBN,
(amount) => {
expect(amount).to.be.closeTo(
paymentAmountBN.add(feeAmountBN).sub(protocolAmountBN).mul(5).div(1000), // slippage
paymentAmountBN.add(feeAmountBN).sub(protocolAmountBN).mul(1).div(10000) // tollerance
)
},
route.tokenIn,
route.tokenOut,
wallets[2].address
Expand Down
Loading

0 comments on commit 8735795

Please sign in to comment.