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

Update tutorials for Gateway on testnet #499

Merged
merged 13 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Docs/components/ContractAddresses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type ContractAddressData = {
type ContractAddressesByChain = Record<string, ContractAddressData[]>;

const addressesUrl: Record<NetworkType, string> = {
testnet: "https://raw.githubusercontent.com/zeta-chain/protocol-contracts/main/v1/data/addresses.testnet.json",
mainnet: "https://raw.githubusercontent.com/zeta-chain/protocol-contracts/main/v1/data/addresses.mainnet.json",
testnet: "https://raw.githubusercontent.com/zeta-chain/protocol-contracts/main/v2/data/addresses.testnet.json",
mainnet: "https://raw.githubusercontent.com/zeta-chain/protocol-contracts/main/v2/data/addresses.mainnet.json",
fadeev marked this conversation as resolved.
Show resolved Hide resolved
};

const groupDataByChain = (data: ContractAddressData[]) =>
Expand Down
22 changes: 11 additions & 11 deletions src/pages/developers/tutorials/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
"description": "Learn how to set up a smart contract template, create an account, and use faucet"
},
"hello": {
"title": "First Universal App on Localnet",
"title": "Message Passing",
"readTime": "30 min",
"description": "Learn how to create, deploy and interact with a universal app"
},
"swap-tss": {
"title": "Swap on Testnet",
"readTime": "30 min",
"description": "Implement an omnichain swap app compatible with chains like Ethereum, BNB and Bitcoin"
"description": "Learn the fundamentals of message passing and cross-chain contract calls"
},
"swap": {
"title": "Swap on Localnet",
"title": "Swap",
"readTime": "30 min",
"description": "Implement an omnichain swap app compatible with chains like Ethereum, BNB and Bitcoin"
"description": "Implement a universal swap app compatible with chains like Ethereum, Solana and Bitcoin"
},
"swap-any": {
"title": "Swap Any Token on Localnet",
"title": "Swap Any Token",
"readTime": "60 min",
"description": "Enhance the universal swap app with the ability to swap to any token"
},
"nft": {
"title": "NFT",
"readTime": "60 min",
"description": "Enhance the omnichain swap app with the ability to swap to any token"
"description": "Mint a universal NFT, which can be transferred between connected chains"
},
"localnet": {
"title": "Localnet",
Expand Down
203 changes: 82 additions & 121 deletions src/pages/developers/tutorials/hello.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Your First Universal App
title: Message Passing
---

import { Alert } from "~/components/shared";
Expand All @@ -17,11 +17,7 @@ By the end of this tutorial, you will have learned how to:
blockchain in localnet.
- Gracefully handle reverts by implementing revert logic.

<Alert>
{" "}
This tutorial relies on the gateway, which is currently available only on localnet. It will support testnet once the gateway
is deployed there. Therefore, deploying this tutorial on testnet is not possible at this time.
</Alert>
<Alert>This tutorial relies on the Gateway, which is currently available only on localnet and testnet.</Alert>

## Prerequisites

Expand Down Expand Up @@ -69,8 +65,8 @@ contract Hello is UniversalContract {
gateway = GatewayZEVM(gatewayAddress);
}

function onCrossChainCall(
zContext calldata context,
function onCall(
MessageContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
Expand All @@ -91,9 +87,8 @@ contract Hello is UniversalContract {
RevertOptions memory revertOptions
) external {
(, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit(gasLimit);
if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee)) {
if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee))
revert TransferFailed();
}
IZRC20(zrc20).approve(address(gateway), gasFee);
gateway.call(receiver, zrc20, message, gasLimit, revertOptions);
}
Expand Down Expand Up @@ -257,7 +252,74 @@ contract Echo {
}
```

## Start Localnet
## Option 1: Deploy on Testnet

```
npx hardhat compile --force
npx hardhat deploy --gateway 0x6c533f7fe93fae114d0954697069df33c9b74fd7 --network zeta_testnet
npx hardhat deploy --gateway 0x0c487a766110c85d301d96e33579c5b317fa4995 --network base_testnet --name Echo
```

```
🔑 Using account: 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32

🚀 Successfully deployed "Hello" contract on zeta_testnet.
📜 Contract address: 0x496CD66950a1F1c5B02513809A2d37fFB942be1B

🔑 Using account: 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32

🚀 Successfully deployed "Echo" contract on base_sepolia.
📜 Contract address: 0x775329c70A8d09AEb5e5ca92C369FF3155C5f1Ed
```

## Call from Base to ZetaChain

```
npx hardhat echo-call \
--contract 0x775329c70A8d09AEb5e5ca92C369FF3155C5f1Ed \
--receiver 0x496CD66950a1F1c5B02513809A2d37fFB942be1B \
--network base_sepolia \
--tx-options-gas-price 20000 --types '["string"]' alice
```
fadeev marked this conversation as resolved.
Show resolved Hide resolved

https://sepolia.basescan.org/tx/0x133cdf3a06195de0a6bb89dd4761ca98d1301534b3c4987f0ff93c95c3fff78c

https://zetachain-athens.blockpi.network/lcd/v1/public/zeta-chain/crosschain/inboundHashToCctxData/0x133cdf3a06195de0a6bb89dd4761ca98d1301534b3c4987f0ff93c95c3fff78c

## Call from ZetaChain to Base

```
npx hardhat hello-call \
--contract 0x496CD66950a1F1c5B02513809A2d37fFB942be1B \
--receiver 0x775329c70A8d09AEb5e5ca92C369FF3155C5f1Ed \
--zrc20 0x236b0DE675cC8F46AE186897fCCeFe3370C9eDeD \
--function "hello(string)" \
--network zeta_testnet \
--tx-options-gas-price 200000 --types '["string"]' alice
```

https://zetachain-testnet.blockscout.com/tx/0x19d476fa2c3d29ba41467ae7f2742541fd11e0b67d6548fe7655a3d40274323e

https://zetachain-athens.blockpi.network/lcd/v1/public/zeta-chain/crosschain/inboundHashToCctxData/0x19d476fa2c3d29ba41467ae7f2742541fd11e0b67d6548fe7655a3d40274323e

## Withdraw And Call from ZetaChain to Base

```
npx hardhat hello-withdraw-and-call \
--contract 0x496CD66950a1F1c5B02513809A2d37fFB942be1B \
--receiver 0x775329c70A8d09AEb5e5ca92C369FF3155C5f1Ed \
--zrc20 0x236b0DE675cC8F46AE186897fCCeFe3370C9eDeD \
--function "hello(string)" \
--amount 0.005 \
--network zeta_testnet \
--types '["string"]' hello
```

https://zetachain-testnet.blockscout.com/tx/0x67099389ab6cb44ee03602d164320c615720b57762c5ddab042d65bdbe30c7a2

https://zetachain-athens.blockpi.network/lcd/v1/public/zeta-chain/crosschain/inboundHashToCctxData/0x67099389ab6cb44ee03602d164320c615720b57762c5ddab042d65bdbe30c7a2

## Option 2: Start Localnet

[Localnet](/developers/tutorials/localnet) provides a simulated environment for
developing and testing ZetaChain contracts locally.
Expand All @@ -277,7 +339,7 @@ With localnet running, open a new terminal window to compile and deploy the
`Hello` and `Echo` contracts:

```bash
yarn deploy
yarn deploy:localnet
```

You should see output indicating the successful deployment of the contracts:
Expand All @@ -286,12 +348,12 @@ You should see output indicating the successful deployment of the contracts:
🔑 Using account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

🚀 Successfully deployed "Hello" contract on localhost.
📜 Contract address: 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E
📜 Contract address: 0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB

🔑 Using account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

🚀 Successfully deployed "Echo" contract on localhost.
📜 Contract address: 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690
📜 Contract address: 0x9E545E3C0baAB3E08CdfD552C960A1050f373042
```

**Note**: The deployed contract addresses may differ in your environment.
Expand All @@ -304,8 +366,8 @@ command, replacing the contract addresses with those from your deployment:

```bash
npx hardhat echo-call \
--contract 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--receiver 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--contract 0x9E545E3C0baAB3E08CdfD552C960A1050f373042 \
--receiver 0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB \
--network localhost \
--types '["string"]' alice
```
Expand All @@ -328,55 +390,6 @@ npx hardhat echo-call \
- **ZetaChain**: The `Hello` contract decodes the message and emits a
`HelloEvent`.

## Simulating a Revert

To demonstrate how reverts are handled, let's intentionally send incorrect data.
Instead of a `string`, we'll send a `uint256`:

```bash
npx hardhat echo-call \
--contract 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--receiver 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--network localhost \
--types '["uint256"]' 42
```

**What Happens:**

- The `Hello` contract's `onCrossChainCall` attempts to decode a `uint256` as a
`string`, causing `abi.decode` to fail and revert the transaction.
- The EVM chain detects the revert, and the transaction does not execute the
intended logic.

## Handling a Revert

To handle reverts gracefully, configure the gateway to call the `Echo` contract
on the source chain upon a revert:

```bash
npx hardhat echo-call \
--contract 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--receiver 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--network localhost \
--revert-address 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--revert-message 0x \
--call-on-revert \
--types '["uint256"]' 42
```

**Parameters:**

- `--revert-address`: Address of the `Echo` contract on the source EVM chain.
- `--revert-message`: Data to pass to the `Echo` contract's `onRevert` function.
- `--call-on-revert`: Flag indicating that the gateway should invoke a contract
upon revert.

**What Happens:**

- When the revert occurs, the gateway invokes the `Echo` contract's `onRevert`
function on the EVM chain.
- This allows you to handle the error gracefully within your application logic.

## Calling an EVM Contract from a Universal App

Now, let's perform the reverse operation: calling a contract on a connected EVM
Expand All @@ -386,8 +399,8 @@ from your deployment:

```bash
npx hardhat hello-call \
--contract 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--receiver 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--contract 0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB \
--receiver 0x9E545E3C0baAB3E08CdfD552C960A1050f373042 \
--zrc20 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
--function "hello(string)" \
--network localhost \
Expand All @@ -406,67 +419,15 @@ npx hardhat hello-call \
- `--types`: ABI types of the message parameters.
- `alice`: The message to send.

## Simulating a Revert

To simulate a revert when calling an EVM contract from ZetaChain, send incorrect
data types:

```bash
npx hardhat hello-call \
--contract 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--receiver 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--zrc20 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
--function "hello(string)" \
--network localhost \
--types '["uint256"]' 42
```

**What Happens:**

- The `Echo` contract expects a `string` but receives a `uint256`, causing the
function to fail and revert the transaction.

## Handling a Revert

To handle reverts gracefully when calling an EVM contract from ZetaChain,
include revert parameters:

```bash
npx hardhat hello-call \
--contract 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--receiver 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--zrc20 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
--function "hello(string)" \
--network localhost \
--revert-address 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--revert-message 0x \
--call-on-revert \
--types '["uint256"]' 42
```

**Parameters:**

- `--revert-address`: Address of the `Hello` contract on ZetaChain.
- `--revert-message`: Data to pass to the `Hello` contract's `onRevert`
function.
- `--call-on-revert`: Flag indicating that the gateway should invoke a contract
upon revert.

**What Happens:**

- Upon revert, the gateway calls the `onRevert` function of the `Hello` contract
on ZetaChain.
- This allows you to handle the error within your ZetaChain application.

## Withdrawing and Calling an EVM Contract from a Universal App

To withdraw tokens and call a contract on a connected chain from a universal
app, run the following command:

```bash
npx hardhat hello-withdraw-and-call \
--contract 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--receiver 0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690 \
--contract 0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB \
--receiver 0x9E545E3C0baAB3E08CdfD552C960A1050f373042 \
--zrc20 0x9fd96203f7b22bCF72d9DCb40ff98302376cE09c \
--function "hello(string)" \
--amount 1 \
Expand Down
Loading
Loading