Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions src/pages/developers/tutorials/call.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ import "@zetachain/protocol-contracts/contracts/evm/GatewayEVM.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract Connected {
using SafeERC20 for IERC20; // Use SafeERC20 for IERC20 operations
using SafeERC20 for IERC20;

GatewayEVM public immutable gateway;

Expand Down Expand Up @@ -423,7 +423,7 @@ With localnet running, open a new terminal window to compile and deploy the
```bash
npx hardhat compile --force
npx hardhat deploy --name Universal --network localhost --gateway 0x5FC8d32690cc91D4c39d9d3abcBD16989F875707
npx hardhat deploy --name Connected --network localhost --gateway 0x610178dA211FEF7D417bC0e6FeD39F05609AD788
npx hardhat deploy --name Connected --network localhost --gateway 0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0
```

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

🚀 Successfully deployed "Universal" contract on localhost.
📜 Contract address: 0xc351628EB244ec633d5f21fBD6621e1a683B1181
📜 Contract address: 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7

🔑 Using account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

🚀 Successfully deployed "Connected" contract on localhost.
📜 Contract address: 0xFD471836031dc5108809D173A067e8486B9047A3
📜 Contract address: 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726
```

**Note**: The deployed contract addresses may differ in your environment.
Expand All @@ -451,8 +451,8 @@ deployment:

```bash
npx hardhat connected-call \
--contract 0xFD471836031dc5108809D173A067e8486B9047A3 \
--receiver 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--contract 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--receiver 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7 \
--network localhost \
--types '["string"]' alice
```
Expand All @@ -466,8 +466,8 @@ from your deployment:

```bash
npx hardhat universal-call \
--contract 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--receiver 0xFD471836031dc5108809D173A067e8486B9047A3 \
--contract 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7 \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--zrc20 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
--function "hello(string)" \
--network localhost \
Expand All @@ -481,8 +481,8 @@ app, run the following command:

```bash
npx hardhat universal-withdraw-and-call \
--contract 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--receiver 0xFD471836031dc5108809D173A067e8486B9047A3 \
--contract 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7 \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--zrc20 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
--function "hello(string)" \
--amount 1 \
Expand Down
6 changes: 3 additions & 3 deletions src/pages/developers/tutorials/hello.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ npx hardhat deploy --network localhost --gateway 0x5FC8d32690cc91D4c39d9d3abcBD1

```
🚀 Successfully deployed "Universal" contract on localhost.
📜 Contract address: 0xc351628EB244ec633d5f21fBD6621e1a683B1181
📜 Contract address: 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7
```

## Make a Call to the Universal App
Expand All @@ -124,8 +124,8 @@ call to the Gateway contract on a connected EVM chain using the `evm-call` task:
```
npx hardhat evm-call \
--network localhost \
--gateway-evm 0x610178dA211FEF7D417bC0e6FeD39F05609AD788 \
--receiver 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--gateway-evm 0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0 \
--receiver 0x8198f5d8F8CfFE8f9C413d98a0A55aEB8ab9FbB7 \
--types '["string"]' alice
```

Expand Down
170 changes: 134 additions & 36 deletions src/pages/developers/tutorials/swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ contract Swap is
(uint256 out, address gasZRC20, uint256 gasFee) = handleGasAndSwap(
zrc20,
amount,
params.target
params.target,
params.withdraw
);
emit TokenSwap(
context.sender,
Expand Down Expand Up @@ -213,7 +214,8 @@ contract Swap is
(uint256 out, address gasZRC20, uint256 gasFee) = handleGasAndSwap(
inputToken,
amount,
targetToken
targetToken,
withdrawFlag
);
emit TokenSwap(
msg.sender,
Expand Down Expand Up @@ -243,33 +245,34 @@ contract Swap is
function handleGasAndSwap(
address inputToken,
uint256 amount,
address targetToken
address targetToken,
bool withdraw
) internal returns (uint256, address, uint256) {
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
uint256 swapAmount;

(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();

uint256 minInput = quoteMinInput(inputToken, targetToken);
if (amount < minInput) {
revert InsufficientAmount(
"The input amount is less than the min amount required to cover the withdraw gas fee"
);
}

if (gasZRC20 == inputToken) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
uniswapRouter,
inputToken,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
uint256 gasFee = 0;
uint256 swapAmount = amount;

if (withdraw) {
(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();
uint256 minInput = quoteMinInput(inputToken, targetToken);
if (amount < minInput) {
revert InsufficientAmount(
"The input amount is less than the min amount required to cover the withdraw gas fee"
);
}
if (gasZRC20 == inputToken) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
uniswapRouter,
inputToken,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
}
}

uint256 out = SwapHelperLib.swapExactTokensForTokens(
Expand Down Expand Up @@ -343,7 +346,8 @@ contract Swap is
(uint256 out, , ) = handleGasAndSwap(
context.asset,
context.amount,
zrc20
zrc20,
true
);

gateway.withdraw(
Expand Down Expand Up @@ -495,7 +499,7 @@ The Swap contract can be called in two ways:

```
npx hardhat compile --force
 

npx hardhat deploy \
--gateway 0x6c533f7fe93fae114d0954697069df33c9b74fd7 \
--uniswap-router 0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe \
Expand All @@ -521,6 +525,47 @@ npx hardhat evm-deposit-and-call \
--types '["address", "bytes"]' 0x777915D031d1e8144c90D025C594b3b8Bf07a08d 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32
```

Transaction on Base:

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

Incoming transaction from Base to ZetaChain:

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

Outgoing transaction from ZetaChain to Polygon:

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

Transaction on Polygon:

https://amoy.polygonscan.com/tx/0x5a83fe0321c13f386d0953052a0335f06095455fcf1470b8e8fe9c3119adbb40

## Swap from Solana SOL to Base Sepolia ETH

```
npx hardhat solana-deposit-and-call \
--amount 0.1 \
--recipient 0x162CefCe314726698ac1Ee5895a6c392ba8e20d3 \
--types '["address", "bytes", "bool"]' 0x236b0DE675cC8F46AE186897fCCeFe3370C9eDeD 0x4955a3F38ff86ae92A914445099caa8eA2B9bA32 true
```

Transaction on Solana:

https://solana.fm/tx/4TyLdAt2jcxPwnddbwvHSUzrUTZrLoJ8idTGgNXySFw5vDsW3uo4qwGVxBFD3ziLzbWrVrmfx9boYM9pWooftM5x?cluster=devnet-solana

Incoming transaction from Solana to ZetaChain:

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

Outgoing transaction from ZetaChain to Base:

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

Transaction on Base:

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

## Option 2: Deploy on Localnet

Start the local development environment to simulate ZetaChain's behavior by
Expand All @@ -546,7 +591,7 @@ You should see output similar to:
🔑 Using account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

🚀 Successfully deployed contract on localhost.
📜 Contract address: 0xc351628EB244ec633d5f21fBD6621e1a683B1181
📜 Contract address: =0x0355B7B8cb128fA5692729Ab3AAa199C1753f726
```

## Swapping Gas Tokens for ERC-20 Tokens
Expand All @@ -556,9 +601,11 @@ To swap gas tokens for ERC-20 tokens, run the following command:
```
npx hardhat evm-swap \
--network localhost \
--receiver 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--amount 1 \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--amount 0.1 \
--gateway-evm 0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0 \
--target 0x05BA149A7bd6dC1F937fA9046A9e05C05f3b18b0 \
--skip-checks \
--recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
```

Expand Down Expand Up @@ -591,11 +638,13 @@ ERC-20 token you're swapping from using the `--erc20` parameter:
```
npx hardhat evm-swap \
--network localhost \
--receiver 0xc351628EB244ec633d5f21fBD6621e1a683B1181 \
--amount 1 \
--target 0x05BA149A7bd6dC1F937fA9046A9e05C05f3b18b0 \
--recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
--erc20 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--amount 0.1 \
--target 0x65a45c57636f9BcCeD4fe193A602008578BcA90b \
--gateway-evm 0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0 \
--skip-checks \
--erc20 0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E \
--recipient 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
```

Here, the `--erc20` option specifies the ERC-20 token address you're swapping
Expand All @@ -615,6 +664,55 @@ recipient, and initiates the swap logic.
The EVM chain then receives the withdrawal request, and the swapped gas tokens
are transferred to the recipient's address.

## Swapping SUI for Solana SOL

```
npx hardhat localnet:sui-deposit-and-call \
--mnemonic "grape subway rack mean march bubble carry avoid muffin consider thing street" \
--gateway 0x36f26de84772c7dc8d4b9e291c92c2b067c448e14936aa7bad546ed9a5f348d3 \
--module 0x7418f0a63ff1d1fdc0b4dd2a1b7fc1760c62e7bc7609e1fb71ec30f0fbfb0a00 \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--amount 1000000 \
--types '["address", "bytes", "bool"]' 0x777915D031d1e8144c90D025C594b3b8Bf07a08d DrexsvCMH9WWjgnjVbx1iFf3YZcKadupFmxnZLfSyotd true
```

### Swapping Solana SOL for SUI

```
npx hardhat localnet:solana-deposit-and-call \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--amount 0.0001 \
--types '["address", "bytes", "bool"]' 0xe573a6e11f8506620F123DBF930222163D46BCB6 0x2fec3fafe08d2928a6b8d9a6a77590856c458d984ae090ccbd4177ac13729e65 true
```

Before swapping to Sui, make sure you have either deposited tokens from Sui or
deposited and made a call from Sui first. Otherwise, there will be no tokens
available in custody on Sui.

### Swapping Solana SPL for SUI

```
npx hardhat localnet:solana-deposit-and-call \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--mint 2rJE9EiWx7hrCKiBsAgtmTZbcuDRESkEvw9ZaAwX1YHN \
--to 7nHtQCVaUMRkwiUhDmoJUJ2p1WMatoz3k22C3xGvid64 \
--from DdTS9SENotAj5z96TQX5pRKd11TkvHKaSneK1GnCCrKD \
--amount 0.1 \
--types '["address", "bytes", "bool"]' 0xe573a6e11f8506620F123DBF930222163D46BCB6 0x2fec3fafe08d2928a6b8d9a6a77590856c458d984ae090ccbd4177ac13729e65 true
```

### Swapping SUI for Solana SPL

```
npx hardhat localnet:sui-deposit-and-call \
--mnemonic "grape subway rack mean march bubble carry avoid muffin consider thing street" \
--gateway 0xb58507c84f8247d2866b393fbd11467211c89e4e4935d01714a868b2dde493ae \
--module 0xb98bb74211a4f8463d87b61ace7d36f703370dae989baf8c620131d28525a899 \
--receiver 0x0355B7B8cb128fA5692729Ab3AAa199C1753f726 \
--amount 100000 \
--types '["address", "bytes", "bool"]' 0xfC9201f4116aE6b054722E10b98D904829b469c3 DrexsvCMH9WWjgnjVbx1iFf3YZcKadupFmxnZLfSyotd true
```

## Conclusion

In this tutorial, you learned how to define a universal app contract that
Expand Down