zeta-chain / docs

Documentation for ZetaChain
https://www.zetachain.com/docs/
MIT License
74 stars 59 forks source link

Enhance cross-chain messaging tutorials #208

Open fadeev opened 11 months ago

fadeev commented 11 months ago

Right now we have two introductory CCM tutorials:

The problem with this setup is that a more complex tutorial (First Cross-Chain Message) is presented first, and a simpler one is presented next. And we're asking users to manually delete code, which is not cool.

Proposed solution

Replace these two tutorials with these two:

Value Transfer

npx hardhat messaging Value --fees zeta

Generates:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/tools/ZetaInteractor.sol";
import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.sol";
import "@zetachain/protocol-contracts/contracts/evm/Zeta.eth.sol";

contract MyContract is ZetaInteractor {
    IERC20 internal immutable _zetaToken;

    constructor(
        address connectorAddress,
        address zetaTokenAddress
    ) ZetaInteractor(connectorAddress) {
        _zetaToken = IERC20(zetaTokenAddress);
    }

    function sendMessage(
        uint256 destinationChainId,
        uint256 zetaValueAndGas
    ) external payable {
        if (!_isValidChainId(destinationChainId))
            revert InvalidDestinationChainId();

        bool success1 = _zetaToken.approve(address(connector), zetaValueAndGas);
        bool success2 = _zetaToken.transferFrom(
            msg.sender,
            address(this),
            zetaValueAndGas
        );
        if (!(success1 && success2)) revert ErrorTransferringZeta();

        connector.send(
            ZetaInterfaces.SendInput({
                destinationChainId: destinationChainId,
                destinationAddress: interactorsByChainId[destinationChainId],
                destinationGasLimit: 300000,
                message: abi.encode(),
                zetaValueAndGas: zetaValueAndGas,
                zetaParams: abi.encode("")
            })
        );
    }
}

Super simple, user doesn't need to delete anything. Requires changes to the toolkit: when now arguments are supplied, assume they just want value transfer.

Value and Data Transfer

npx hardhat messaging CrossChainMessage message:string

The same as the current tutorial.

Summary

I propose to:

fadeev commented 11 months ago

@andresaiello wdyt?

fadeev commented 11 months ago

Also, to simplify the template I'm thinking about removing message type from the default template and associated type checks:

    bytes32 public constant CROSS_CHAIN_MESSAGE_MESSAGE_TYPE =
        keccak256("CROSS_CHAIN_CROSS_CHAIN_MESSAGE");

I think having a message type doesn't make sense when the template generates a contract that supports only one message type.