Skip to content

Commit

Permalink
handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 22, 2024
1 parent 5dfba30 commit a279ac1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
20 changes: 17 additions & 3 deletions examples/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract Swap is UniversalContract {

error InvalidAddress();
error Unauthorized();
error ApprovalFailed();

modifier onlyGateway() {
if (msg.sender != address(gateway)) revert Unauthorized();
Expand Down Expand Up @@ -115,10 +116,23 @@ contract Swap is UniversalContract {
address inputToken
) public {
if (gasZRC20 == params.target) {
IZRC20(gasZRC20).approve(address(gateway), outputAmount + gasFee);
if (
!IZRC20(gasZRC20).approve(
address(gateway),
outputAmount + gasFee
)
) {
revert ApprovalFailed();
}
} else {
IZRC20(gasZRC20).approve(address(gateway), gasFee);
IZRC20(params.target).approve(address(gateway), outputAmount);
if (!IZRC20(gasZRC20).approve(address(gateway), gasFee)) {
revert ApprovalFailed();
}
if (
!IZRC20(params.target).approve(address(gateway), outputAmount)
) {
revert ApprovalFailed();
}
}
gateway.withdraw(
params.to,
Expand Down
19 changes: 15 additions & 4 deletions examples/swap/contracts/SwapToAnyToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ contract SwapToAnyToken is UniversalContract {

error InvalidAddress();
error Unauthorized();
error ApprovalFailed();
error TransferFailed();

modifier onlyGateway() {
if (msg.sender != address(gateway)) revert Unauthorized();
Expand Down Expand Up @@ -155,10 +157,16 @@ contract SwapToAnyToken is UniversalContract {
) public {
if (params.withdraw) {
if (gasZRC20 == params.target) {
IZRC20(gasZRC20).approve(address(gateway), out + gasFee);
if (!IZRC20(gasZRC20).approve(address(gateway), out + gasFee)) {
revert ApprovalFailed();
}
} else {
IZRC20(gasZRC20).approve(address(gateway), gasFee);
IZRC20(params.target).approve(address(gateway), out);
if (!IZRC20(gasZRC20).approve(address(gateway), gasFee)) {
revert ApprovalFailed();
}
if (!IZRC20(params.target).approve(address(gateway), out)) {
revert ApprovalFailed();
}
}
gateway.withdraw(
abi.encodePacked(params.to),
Expand All @@ -173,10 +181,13 @@ contract SwapToAnyToken is UniversalContract {
})
);
} else {
IWETH9(params.target).transfer(
bool success = IWETH9(params.target).transfer(
address(uint160(bytes20(params.to))),
out
);
if (!success) {
revert TransferFailed();
}
}
}

Expand Down

0 comments on commit a279ac1

Please sign in to comment.