-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Example (Swap to ZETA) and update to toolkit v8 #176
Merged
Conversation
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
Tested
|
Updated to Toolkit v8-rc1, which includes swap helper lib changes (accept system contract as arg) and changes to the deploy script, which allows choosing which contract to deploy. |
@andresaiello right now this example sends WZETA to the recipient. I wanted to try to unwrap and send ZETA, instead, but this logic fails: IWETH9(wzeta).approve(address(this), outputAmount);
IWETH9(wzeta).withdraw(outputAmount);
payable(address(uint160(bytes20(to)))).transfer(outputAmount); Any idea why? Or how to do it properly? The whole source: // SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";
import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IWZETA.sol";
contract SwapToZeta is zContract {
SystemContract public systemContract;
uint256 constant BITCOIN = 18332;
constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
}
modifier onlySystem() {
require(
msg.sender == address(systemContract),
"Only system contract can call this function"
);
_;
}
function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override onlySystem {
address target;
bytes memory to;
if (context.chainID == BITCOIN) {
target = BytesHelperLib.bytesToAddress(message, 0);
to = abi.encodePacked(BytesHelperLib.bytesToAddress(message, 20));
} else {
(address targetToken, bytes memory recipient) = abi.decode(
message,
(address, bytes)
);
target = targetToken;
to = recipient;
}
address wzeta = systemContract.wZetaContractAddress();
bool isTargetZeta = target == wzeta;
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
if (!isTargetZeta) {
(gasZRC20, gasFee) = IZRC20(target).withdrawGasFee();
inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);
}
uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
isTargetZeta ? amount : amount - inputForGas,
target,
0
);
if (isTargetZeta) {
IWETH9(wzeta).approve(address(this), outputAmount);
IWETH9(wzeta).withdraw(outputAmount);
payable(address(uint160(bytes20(to)))).transfer(outputAmount);
} else {
IZRC20(gasZRC20).approve(target, gasFee);
IZRC20(target).withdraw(to, outputAmount);
}
}
} |
andresaiello
reviewed
May 9, 2024
andresaiello
approved these changes
May 9, 2024
fadeev
changed the title
Omnichain swap: add ability to swap to ZETA
New Example (Swap to ZETA) and update to toolkit v8
May 13, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
CompilerError: Stack too deep, try removing local variables
errorrequire
withrevert
in the NFT example.Swap to ZETA
If the destination token address is WZETA, swap it to WZETA and don't withdraw.
Keeping both
Swap
andSwapExtended
in the contracts directory. The thinking is, we keep the swap tutorial as is and introduce part 2 where we extend the swap to add "swap to ZETA" capability. Advantages: we still have a simple tutorial, and we offer more content for users who want to go further. Depends on zeta-chain/toolkit#130Can be simplified further: zeta-chain/toolkit#129
Depends on zeta-chain/template#55