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

Authenticated call updates #504

Merged
merged 24 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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/pages/developers/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"description": "Begin your journey on ZetaChain, the decentralized blockchain and smart contract platform designed for omnichain interoperability."
},
"apps": {
"title": "Building Universal Apps",
"title": "Universal Apps",
fadeev marked this conversation as resolved.
Show resolved Hide resolved
"description": "Explore the basics of developing on ZetaChain."
},
"evm": {
"title": "Universal EVM",
"description": "EVM enhanced with omnichain interoperability features, enabling the development of robust universal apps."
},
"chains": {
"title": "Connected Blockchains",
"title": "Connected Chains",
"description": "Use Gateway to make calls to and from universal apps, deposit and withdraw tokens."
},
"tutorials": {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/apps/_meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"intro": {
"title": "Universal App Basics",
"title": "Basics",
"description": "Build universal apps that can be called from any blockchain"
},
"advantages": {
Expand Down
10 changes: 2 additions & 8 deletions src/pages/developers/apps/intro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,14 @@ connected chain. Each connected chain has a single Gateway contract that exposes
methods to deposit tokens to and call universal apps. Users can pass both data
and tokens when calling universal apps.

<Alert>
{" "}
Gateway is an upcoming unified interface that will replace the TSS address and ERC-20 custody contract. Gateway is scheduled
for release towards the end of Q3 2024.{" "}
</Alert>

In this example an Ethereum user sends 1 ETH and a message "hello" to a
universal app:

<iframe
style={{ border: "1px solid rgba(0,0,0,.1)", marginTop: "2rem", borderRadius: "0.5rem" }}
width="100%"
height="450"
src="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Fboard%2FgPX2Rjuxb44SYr1FTwZCXv%2FIntro-1%3Fnode-id%3D0-1%26t%3DowucPIvKfVlZrHJR-1"
src="https://embed.figma.com/board/Tz0eeQMlUDAigpSdWYPi0F/Intro-1?node-id=0-1&embed-host=share"
fadeev marked this conversation as resolved.
Show resolved Hide resolved
allowfullscreen
></iframe>

Expand Down Expand Up @@ -102,7 +96,7 @@ different chains.
style={{ border: "1px solid rgba(0,0,0,.1)", marginTop: "2rem", borderRadius: "0.5rem" }}
width="100%"
height="450"
src="https://www.figma.com/embed?embed_host=share&url=https%3A%2F%2Fwww.figma.com%2Fboard%2FKtVyu73smn4q9P4Gq5Bczn%2FIntro-2%3Fnode-id%3D0-1%26t%3DB7lT6avDGo620CZb-1"
src="https://embed.figma.com/board/3eA0Lo6hqPrh40WFaRKATf/Intro-2?node-id=0-1&embed-host=share"
allowfullscreen
></iframe>

Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/chains/_meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"list": {
"title": "List of Connected Chains",
"title": "List of Chains",
"description": ""
},
"zetachain": {
Expand Down
69 changes: 44 additions & 25 deletions src/pages/developers/chains/zetachain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ withdraw tokens, use the ZetaChain gateway.
ZetaChain gateway supports:

- withdrawing ZRC-20 tokens as native gas or ERC-20 tokens to connected chains
- withdrawing ZETA tokens to connected chains
withdrawing tokens to and making a contract call on connected chains
- withdrawing ZETA tokens to connected chains withdrawing tokens to and making a
contract call on connected chains
- calling contracts on connected chains

## Withdraw ZRC-20 Tokens

To withdraw ZRC-20 tokens to an EOA or a contract on a connected chain, call
the `withdraw` function of the gateway contract.
To withdraw ZRC-20 tokens to an EOA or a contract on a connected chain, call the
`withdraw` function of the gateway contract.

```solidity
withdraw(bytes memory receiver, uint256 amount, address zrc20, RevertOptions calldata revertOptions) external;
function withdraw(bytes memory receiver, uint256 amount, address zrc20, RevertOptions calldata revertOptions) external;
```

The `receiver` is either an externally-owned account (EOA) or a contract on a
Expand All @@ -37,51 +37,70 @@ only to the chain from which it was originally deposited. This means that if you
want to withdraw ZRC-20 USDC.ETH to the BNB chain, you first have to swap it to
ZRC-20 USDC.BNB.

## Withdraw ZETA Tokens

The `withdraw` function can also be used to withdraw ZETA tokens to a connected
chain.

```
withdraw(bytes memory receiver, uint256 amount, uint256 chainId, RevertOptions calldata revertOptions) external;
```

## Withdraw ZRC-20 Tokens and Call a Contract on Connected Chain

To withdraw ZRC-20 tokens and make a call from a universal app to a contract on
a connected chain use the `withdrawAndCall` function of the gateway contract.

```solidity
function withdrawAndCall(bytes memory receiver, uint256 amount, address zrc20, bytes calldata message, uint256 gasLimit, RevertOptions calldata revertOptions) external;
function withdrawAndCall(bytes memory receiver, uint256 amount, address zrc20, bytes calldata message, CallOptions calldata callOptions, RevertOptions calldata revertOptions)
```

The tokens are withdrawn and a call is made to a contract on the connected chain
identified by the `zrc20` address. For example, if ZRC-20 ETH is being
withdrawn, then the call is made to a contract on Ethereum.

## Withdraw ZETA Tokens and Call a Contract on Connected Chain

The `withdrawAndCall` function can also be used to withdraw ZETA tokens and make
a call from a universal app to a contract on a connected chain.

```solidity
withdrawAndCall(bytes memory receiver, uint256 amount, uint256 chainId, bytes calldata message, RevertOptions calldata revertOptions) external;
```

## Call a Contract on a Connected Chain

To call a contract on a connected chain (without withdrawing tokens), use the
`call` function.

```solidity
function call(bytes memory receiver, address zrc20, bytes calldata message, uint256 gasLimit, RevertOptions calldata revertOptions) external;
function call(bytes memory receiver, address zrc20, bytes calldata message, CallOptions calldata callOptions, RevertOptions calldata revertOptions)
```

`zrc20` represents the ZRC-20 token address of the gas token of the destination
chain. In the context of this function `zrc20` address acts as an identifier for
the chain to which the call is made. For example, to make a call to Ethereum,
use ZRC-20 ETH token address.

## Call Options

The `CallOptions` parameter provides details for making calls to contracts on
connected chains. `CallOptions` is applied in both `call` and `withdrawAndCall`
functions.

```solidity
struct CallOptions {
uint256 gasLimit;
bool isArbitraryCall;
}
```

`gasLimit` specifies the maximum amount of gas that a cross-chain contract call
can consume. If the gas usage exceeds this limit, the cross-chain transaction
will revert.

`isArbitraryCall` determines whether the call is "arbitrary" (`true`) or
"authenticated" (`false`).

An arbitrary call can invoke any function of any contract on a connected chain.
For instance, a universal app might call a `hello(string)` function on an
Ethereum contract. However, an arbitrary call does not retain the original
caller's identity—`msg.sender` within the called contract will point to the
Gateway address rather than the universal contract that initiated the call. This
lack of caller identification is often acceptable for operations like a swap in
a `withdrawAndCall`, where the swap function only needs to perform the token
swap and send it to the designated address, without needing to verify the
caller.
fadeev marked this conversation as resolved.
Show resolved Hide resolved

An authenticated call targets the `onCall` function on a connected chain’s
contract. Here, "authentication" is achieved because one of the `onCall`
parameters, `context.sender`, directly references the originating universal
contract. This enables the called contract to verify and establish a trusted
connection with the initiating universal contract, rejecting calls from any
unauthorized third-party contracts.

## Format of the `message` when Calling Contracts

The `withdrawAndCall` and `call` functions have a `bytes calldata message`
Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/evm/_meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"overview": {
"title": "Architecture Overview",
"title": "Architecture",
"description": "Architecture overview of ZetaChain"
},
"gateway": {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/evm/gas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ When withdrawing ZRC-20 tokens back to a connected connected chain, the
"withdraw gas fee" is applicable (listed as "Total fee" in the table below).

To find out the fee amount, call the `withdrawGasFee` function on the [ZRC-20
contract](https://github.com/zeta-chain/protocol-contracts/blob/main/contracts/zevm/ZRC20.sol)
contract](https://github.com/zeta-chain/protocol-contracts/blob/3ede272fba5b0c28a25778477b02f0639e5877f3/v2/contracts/zevm/ZRC20.sol)
fadeev marked this conversation as resolved.
Show resolved Hide resolved
for the token you wish to withdraw. This function will return the fee in the
native gas token of the connected chain.

Expand Down
142 changes: 0 additions & 142 deletions src/pages/developers/tokens/zrc20.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ New assets can be added or removed by broadcasting a transaction with a
corresponding message of the [`fungible`
module](/developers/architecture/modules/fungible/messages/) on ZetaChain.

## Introduction

At a high-level, ZRC-20 tokens are an extension of the standard
[ERC-20](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/)
tokens found in the Ethereum ecosystem, ZRC-20 tokens have the added ability to
Expand All @@ -57,146 +55,6 @@ Bitcoin, ETH, other gas assets and ERC-20-equivalents on other chains, may be
represented on ZetaChain as a ZRC-20 and orchestrated as if it were any other
fungible token (like an ERC-20).

## Interface

ZRC-20 is based on ERC-20, with three additional functions and some associated
events for integration with Cross-Chain Transactions (CCTXs) in ZetaChain (see
the [`IZRC20`
interface](https://github.com/zeta-chain/protocol-contracts/blob/main/contracts/zevm/interfaces/IZRC20.sol)).

Comparing ZRC-20 to ERC-20, there are additional external functions to deposit
and withdraw, and additional events for each of them. This makes ZRC-20
completely compatible with any applications built for ERC-20s, but with an
extremely simple interface to also function in an omnichain way.

## Depositing Native Gas Tokens as ZRC-20

```mermaid
flowchart LR
subgraph Ethereum ["Ethereum (Polygon or BSC)"]
direction LR
subgraph send ["Transaction"]
direction LR
Data -- contains --- Message("Message")
Data((Data)) -- contains --- address("Omnichain contract address")
Value((Value)) -- contains --- eth("1 ETH")
end
account("Account") -- sends --- send
send --> TSS("TSS Address")
end
subgraph ZetaChain
SystemContract("System Contract")
subgraph contract ["Omnichain contract"]
addr("Contract address")
subgraph onCall
msg("bytes calldata message")
zrc20("address zrc20")
amount("uint256 amount")
context("MessageContext calldata context")
end
end
end
TSS --> SystemContract
SystemContract -- calls --> contract
address -.- addr
eth -. ZRC-20 contract address of ETH .- zrc20
eth -. deposited amount .- amount
Ethereum -. chainID .- context
Message -. arbitrary data .- msg
account -. "origin" .- context
```

To deposit a native gas token (like sETH, tMATIC, tBNB, or tBTC) to ZetaChain,
send it to the [TSS address](/reference/network/contracts) on a connected chain.

If the input data field of the transaction is empty, the token will be deposited
to the sender's address on ZetaChain.

If the input data field is not empty, the protocol looks up the first 20 bytes
of the input data field. If the first 20 bytes correspond to an EOA address on
ZetaChain, the token will be deposited to that address. If the first 20 bytes
correspond to a contract address on ZetaChain, the token will be deposited to
that contract and the `onCall` function of that contract will be called with the
remaining input data as the `message`.

When depositing native gas tokens from EVM-based connected chains, there is no
additional cross-chain fee. If you send 1 token to a TSS address, you will
receive 1 ZRC-20 version of the same token on ZetaChain.

For Bitcoin deposits, which utilize the UTXO (Unspent Transaction Output) model,
the process [incurs additional fees](/developers/chains/bitcoin#deposit-fee).

## Depositing Supported ERC-20 Tokens as ZRC-20

```mermaid
flowchart LR
subgraph Ethereum ["Ethereum (Polygon or BSC)"]
direction LR
subgraph send ["Deposit arguments"]
direction LR
recipient("bytes recipient") -- contains --- address("Omnichain contract address")
message("bytes message") -- contains --- Message("Message")
asset("IERC20 asset") -- contains --- erc20address("ERC-20 address")
erc20amount("uint256 amount") -- contains --- eth("1")
end
account("Account") -- "call deposit method" --- send
send --> TSS("ERC-20 custody")
end
subgraph ZetaChain
SystemContract("System Contract")
subgraph contract ["Omnichain contract"]
addr("Contract address")
subgraph onCall
msg("bytes calldata message")
zrc20("address zrc20")
amount("uint256 amount")
context("MessageContext calldata context")
end
end
end
TSS --> SystemContract
SystemContract -- calls --> contract
address -.- addr
erc20address -. ZRC-20 contract address of ERC-20 .- zrc20
eth -. deposited amount .- amount
Ethereum -. chainID .- context
Message -. arbitrary data .- msg
account -. "origin" .- context
```

To deposit a supported ERC-20 token to ZetaChain, use the `deposit` method of
the [ERC-20 custody contract](/reference/network/contracts) on a connected
chain.

The `deposit` method accepts the following parameters:

- `recipient`: the address on ZetaChain to deposit the tokens to. If the
recipient is an EOA, the tokens will be deposited to the recipient's address.
If the recipient is a contract, the tokens will be deposited to the contract
and the `onCall` function of that contract will be called with the `message`
as an argument.
- `asset`: the address of the ERC-20 token to deposit.
- `amount`: the amount of tokens to deposit.
- `message`: an arbitrary message to be passed to the `onCall` function of the
recipient contract. If the recipient is an EOA, the message should be empty.

## Withdrawing ZRC-20 Tokens from ZetaChain

ZRC-20 tokens, which include those representing native gas tokens as well as
ERC-20 tokens, can be withdrawn from ZetaChain by calling the `withdraw` method
on the ZRC-20 contract.

This method burns the tokens and emits a `Withdrawal` event. This event will
trigger a CCTX from ZetaChain to the connected chain from which the token was
deposited. After the CCTX is processed the token amount will be transferred to
the recipient address on the connected chain either from a TSS address (for
native gas tokens) or from the ERC-20 custody contract (for ERC-20 tokens).

Check out the [Swap tutorial](/developers/tutorials/swap) to learn how to build
omnichain contracts that accept token deposits form connected chains, swap
between ZRC-20 tokens using the internal liquidity pools on ZetaChain, and
withdraw them to connected chains.

## Block Confirmations

When depositing to or withdrawing from ZetaChain, the protocol requires a
Expand Down
16 changes: 13 additions & 3 deletions src/pages/developers/tutorials/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
"intro": {
"title": "Getting Started",
"readTime": "10 min",
"description": "Learn how to set up a smart contract template, create an account, and use faucet"
"description": "Learn more about universal apps"
},
"hello": {
"title": "Message Passing",
"title": "First Universal App",
"readTime": "10 min",
"description": "Build your first universal app"
},
"call": {
"title": "Call & Deposit",
"readTime": "30 min",
"description": "Learn the fundamentals of message passing and cross-chain contract calls"
},
Expand All @@ -25,8 +30,13 @@
"description": "Mint a universal NFT, which can be transferred between connected chains"
},
"localnet": {
"title": "Localnet",
"title": "Localnet Setup",
"readTime": "30 min",
"description": "Build and interact with your universal app in a local dev environment"
},
"testnet": {
"title": "Testnet Setup",
"readTime": "10 min",
"description": "Setup your account, request test tokens from the faucet"
}
}
Loading
Loading