Skip to content
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
merged 14 commits into from
May 14, 2024

Conversation

fadeev
Copy link
Member

@fadeev fadeev commented May 8, 2024

Swap to ZETA

If the destination token address is WZETA, swap it to WZETA and don't withdraw.

Keeping both Swap and SwapExtended 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#130

Can be simplified further: zeta-chain/toolkit#129

Depends on zeta-chain/template#55

@fadeev
Copy link
Member Author

fadeev commented May 9, 2024

Tested SwapExtended:

npx hardhat deploy --network zeta_testnet                    
🔑 Using account: 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32

🚀 Successfully deployed contract on zeta_testnet.
📜 Contract address: 0x68DFB7aF2037f5a3B05bcc490FCf8df10b4c6175
🌍 ZetaScan: https://athens.explorer.zetachain.com/address/0x68DFB7aF2037f5a3B05bcc490FCf8df10b4c6175
🌍 Blockcsout: https://zetachain-athens-3.blockscout.com/address/0x68DFB7aF2037f5a3B05bcc490FCf8df10b4c6175
npx hardhat interact --contract 0x68DFB7aF2037f5a3B05bcc490FCf8df10b4c6175 --network bsc_testnet --amount 0.01 --target-token 0x05BA149A7bd6dC1F937fA9046A9e05C05f3b18b0 --recipient 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32
npx hardhat cctx 0xf10a3df6611f7b4d114f282491d7c1588ddb3d308ed3bea0fad4ac8a78a65ed6
^C✓ CCTXs on ZetaChain foun

✓ 0x39c6bf8727735e4a467dae5e98941539b549e411bb0b665a88a78dc8553a34bf: 97 → 7001: OutboundMined (Remote omni
chain contract call completed)
⠸ 0x1cf3d161e96f4e90cfacb7426fa6c7b31ea2fe70494adbd6747377b0f572d81a: 7001 → 11155111 (1 in queue): Pending
Outbound

@fadeev
Copy link
Member Author

fadeev commented May 9, 2024

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.

Copy link

socket-security bot commented May 9, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/[email protected] None 0 77.2 kB types
npm/@types/[email protected] None 0 96.3 kB types
npm/@types/[email protected] None 0 3.83 MB types
npm/@zetachain/[email protected] Transitive: environment, filesystem, network, shell +117 86.8 MB
npm/[email protected] environment 0 11.4 kB pfmooney
npm/[email protected] network Transitive: environment, filesystem +3 1.9 MB jasonsaayman
npm/[email protected] None +1 39.9 kB ljharb
npm/[email protected] None +5 860 kB chai
npm/[email protected] None 0 32 kB sindresorhus
npm/[email protected] None +1 21.8 kB ljharb
npm/[email protected] None 0 38.4 kB bevryme
npm/[email protected] None +39 3.81 MB ljharb
npm/[email protected] Transitive: filesystem, unsafe +4 395 kB jounqin
npm/[email protected] filesystem, unsafe Transitive: environment +18 1.9 MB ljharb
npm/[email protected] environment, filesystem Transitive: shell, unsafe +40 6.78 MB eslintbot
npm/[email protected] None 0 22.8 kB dap
npm/[email protected] filesystem +1 153 kB mrmlnc
npm/[email protected] eval +3 74.4 kB ljharb
npm/[email protected] environment, filesystem, network, shell Transitive: eval, unsafe +147 59 MB fvictorio
npm/[email protected] None 0 51.2 kB kael
npm/[email protected] None +2 57.7 kB ljharb
npm/[email protected] None +1 1.58 MB fanatid
npm/[email protected] filesystem 0 7.53 kB isaacs
npm/[email protected] environment, filesystem 0 13.1 kB mafintosh
npm/[email protected] None 0 94.8 kB ljharb
npm/[email protected] None 0 32.9 kB google-wombot
npm/[email protected] None 0 126 kB ljharb
npm/[email protected] environment, filesystem +1 154 kB ljharb
npm/[email protected] filesystem Transitive: environment, eval, network, shell, unsafe +105 20 MB cgewecke
npm/[email protected] network +1 548 kB jstash
npm/[email protected] environment, filesystem, unsafe +15 1.49 MB cspotcode
npm/[email protected] filesystem Transitive: environment +27 1.5 MB ethereum-ts-bot
npm/[email protected] None 0 40.1 MB typescript-bot
npm/[email protected] None +4 79.5 kB ljharb
npm/[email protected] environment +2 67.5 kB oss-bot
npm/[email protected] environment, filesystem +13 354 kB oss-bot

🚮 Removed packages: npm/@inquirer/[email protected], npm/@inquirer/[email protected], npm/@noble/[email protected], npm/@nomicfoundation/[email protected], npm/@nomiclabs/[email protected], npm/@types/[email protected], npm/@types/[email protected], npm/@types/[email protected], npm/@zetachain/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

@fadeev
Copy link
Member Author

fadeev commented May 9, 2024

@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);
        }
    }
}

@fadeev fadeev marked this pull request as ready for review May 9, 2024 12:08
@fadeev 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
@fadeev fadeev merged commit ebcd0c1 into main May 14, 2024
5 checks passed
@fadeev fadeev deleted the omnichain-swap-to-zeta branch May 14, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants