Skip to content

Commit

Permalink
fix: Swap handle reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 22, 2024
1 parent 83ab10b commit acd8da7
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions examples/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,26 @@ contract Swap is UniversalContract {
params.to = recipient;
}

swapAndWithdraw(zrc20, amount, params.target, params.to);
uint256 out = handleGasAndSwap(zrc20, amount, params.target);
gateway.withdraw(
params.to,
out,
params.target,
RevertOptions({
revertAddress: address(this),
callOnRevert: true,
abortAddress: address(0),
revertMessage: abi.encode(context.sender, zrc20),
onRevertGasLimit: 100000
})
);
}

function swapAndWithdraw(
function handleGasAndSwap(
address inputToken,
uint256 amount,
address targetToken,
bytes memory recipient
) internal {
address targetToken
) internal returns (uint256) {
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
Expand Down Expand Up @@ -100,13 +111,21 @@ contract Swap is UniversalContract {
IZRC20(gasZRC20).approve(address(gateway), gasFee);
IZRC20(targetToken).approve(address(gateway), outputAmount);
}
return outputAmount;
}

Check warning

Code scanning / Slither

Unused return Medium

Check warning

Code scanning / Slither

Unused return Medium

Check warning

Code scanning / Slither

Unused return Medium


function onRevert(RevertContext calldata context) external onlyGateway {
(address sender, address zrc20) = abi.decode(
context.revertMessage,
(address, address)
);
uint256 out = handleGasAndSwap(context.asset, context.amount, zrc20);
gateway.withdraw(
recipient,
outputAmount,
targetToken,
abi.encodePacked(sender),
out,
zrc20,
RevertOptions({
revertAddress: address(0),
revertAddress: sender,
callOnRevert: false,
abortAddress: address(0),
revertMessage: "",
Expand All @@ -115,7 +134,13 @@ contract Swap is UniversalContract {
);
}

function onRevert(
RevertContext calldata revertContext
) external onlyGateway {}
// // Fallback function that reverts
// fallback() external payable {
// revert("Fallback function triggered");
// }

// // Receive function that reverts (for ETH transfers)
// receive() external payable {
// revert("ETH transfers not allowed");
// }
}

0 comments on commit acd8da7

Please sign in to comment.