Skip to content

Commit

Permalink
fix swap
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Feb 14, 2025
1 parent d882d1b commit c824618
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
56 changes: 30 additions & 26 deletions examples/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ contract Swap is
(uint256 out, address gasZRC20, uint256 gasFee) = handleGasAndSwap(
zrc20,
amount,
params.target
params.target,
params.withdraw
);
emit TokenSwap(
context.sender,
Expand Down Expand Up @@ -149,7 +150,8 @@ contract Swap is
(uint256 out, address gasZRC20, uint256 gasFee) = handleGasAndSwap(
inputToken,
amount,
targetToken
targetToken,
withdrawFlag
);
emit TokenSwap(
msg.sender,
Expand Down Expand Up @@ -179,33 +181,34 @@ contract Swap is
function handleGasAndSwap(
address inputToken,
uint256 amount,
address targetToken
address targetToken,
bool withdraw

Check notice

Code scanning / Slither

Local variable shadowing Low

) internal returns (uint256, address, uint256) {
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
uint256 swapAmount;

(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();

uint256 minInput = quoteMinInput(inputToken, targetToken);
if (amount < minInput) {
revert InsufficientAmount(
"The input amount is less than the min amount required to cover the withdraw gas fee"
);
}
uint256 gasFee = 0;
uint256 swapAmount = amount;

if (gasZRC20 == inputToken) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
uniswapRouter,
inputToken,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
if (withdraw) {
(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();
uint256 minInput = quoteMinInput(inputToken, targetToken);
if (amount < minInput) {
revert InsufficientAmount(
"The input amount is less than the min amount required to cover the withdraw gas fee"
);
}
if (gasZRC20 == inputToken) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
uniswapRouter,
inputToken,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
}
}

uint256 out = SwapHelperLib.swapExactTokensForTokens(
Expand Down Expand Up @@ -279,7 +282,8 @@ contract Swap is
(uint256 out, , ) = handleGasAndSwap(
context.asset,
context.amount,
zrc20
zrc20,
true
);

gateway.withdraw(
Expand Down
14 changes: 13 additions & 1 deletion examples/swap/scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
set -x
set -o pipefail

if [ "$1" = "start" ]; then npx hardhat localnet --exit-on-error & sleep 10; fi
if [ "$1" = "start" ]; then npx hardhat localnet --exit-on-error & sleep 15; fi

echo -e "\n🚀 Compiling contracts..."
npx hardhat compile --force --quiet
Expand All @@ -14,6 +14,7 @@ USDC_ETHEREUM=$(jq -r '.addresses[] | select(.type=="ERC-20 USDC" and .chain=="e
ZRC20_USDC=$(jq -r '.addresses[] | select(.type=="ZRC-20 USDC on 97") | .address' localnet.json)
ZRC20_BNB=$(jq -r '.addresses[] | select(.type=="ZRC-20 BNB on 97") | .address' localnet.json)
ZRC20_SOL=$(jq -r '.addresses[] | select(.type=="ZRC-20 SOL on 901") | .address' localnet.json)
WZETA=$(jq -r '.addresses[] | select(.type=="wzeta" and .chain=="zetachain") | .address' localnet.json)
GATEWAY_ETHEREUM=$(jq -r '.addresses[] | select(.type=="gatewayEVM" and .chain=="ethereum") | .address' localnet.json)
GATEWAY_ZETACHAIN=$(jq -r '.addresses[] | select(.type=="gatewayZEVM" and .chain=="zetachain") | .address' localnet.json)
UNISWAP_ROUTER=$(jq -r '.addresses[] | select(.type=="uniswapRouterInstance" and .chain=="zetachain") | .address' localnet.json)
Expand All @@ -23,6 +24,17 @@ DEFAULT_MNEMONIC="grape subway rack mean march bubble carry avoid muffin conside
CONTRACT_SWAP=$(npx hardhat deploy --name Swap --network localhost --gateway "$GATEWAY_ZETACHAIN" --uniswap-router "$UNISWAP_ROUTER" | jq -r '.contractAddress')
COMPANION=$(npx hardhat deploy-companion --gateway "$GATEWAY_ETHEREUM" --network localhost --json | jq -r '.contractAddress')

npx hardhat evm-swap \
--network localhost \
--receiver "$CONTRACT_SWAP" \
--amount 0.1 \
--target "$WZETA" \
--skip-checks \
--withdraw false \
--recipient "$SENDER"

npx hardhat localnet-check

npx hardhat evm-swap \
--network localhost \
--receiver "$CONTRACT_SWAP" \
Expand Down

0 comments on commit c824618

Please sign in to comment.