-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge Swap and SwapToAnyToken into a single example (#223)
- Loading branch information
Showing
12 changed files
with
558 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.26; | ||
|
||
import "@zetachain/protocol-contracts/contracts/evm/GatewayEVM.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
|
||
contract SwapCompanion { | ||
using SafeERC20 for IERC20; | ||
|
||
error ApprovalFailed(); | ||
|
||
GatewayEVM public immutable gateway; | ||
|
||
constructor(address payable gatewayAddress) { | ||
gateway = GatewayEVM(gatewayAddress); | ||
} | ||
|
||
function swapNativeGas( | ||
address universalSwapContract, | ||
address targetToken, | ||
bytes memory recipient, | ||
bool withdraw | ||
) public payable { | ||
gateway.depositAndCall{value: msg.value}( | ||
universalSwapContract, | ||
abi.encode(targetToken, recipient, withdraw), | ||
RevertOptions(msg.sender, false, address(0), "", 0) | ||
); | ||
} | ||
|
||
function swapERC20( | ||
address universalSwapContract, | ||
address targetToken, | ||
bytes memory recipient, | ||
uint256 amount, | ||
address asset, | ||
bool withdraw | ||
) public { | ||
IERC20(asset).safeTransferFrom(msg.sender, address(this), amount); | ||
if (!IERC20(asset).approve(address(gateway), amount)) { | ||
revert ApprovalFailed(); | ||
} | ||
gateway.depositAndCall( | ||
universalSwapContract, | ||
amount, | ||
asset, | ||
abi.encode(targetToken, recipient, withdraw), | ||
RevertOptions(msg.sender, false, address(0), "", 0) | ||
); | ||
} | ||
|
||
receive() external payable {} | ||
|
||
fallback() external payable {} | ||
} |
Oops, something went wrong.