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

chore: update examples #175

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
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
50 changes: 25 additions & 25 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ jobs:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: yarn
node-version: 18
- run: yarn install --frozen-lockfile
- run: yarn typecheck
- run: yarn spellcheck
- name: Check for uppercase section links
run: |
found="$(grep -rE '\(#.*[A-Z].\)' ./docs || echo '')"
if [ "$found" = "" ]; then
exit 0
fi
echo -e "Found problematic uppercase section links:\n$found"
exit 1
- run: yarn build
- name: Publish to Cloudflare Pages
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: 2238a825c5aca59233eab1f221f7aefb
projectName: sygma-docs
directory: ./build
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
- run: corepack enable
- run: yarn install --immutable
- run: yarn typecheck
- run: yarn spellcheck
- name: Check for uppercase section links
run: |
found="$(grep -rE '\(#.*[A-Z].\)' ./docs || echo '')"
if [ "$found" = "" ]; then
exit 0
fi
echo -e "Found problematic uppercase section links:\n$found"
exit 1
- run: yarn build
- name: Publish to Cloudflare Pages
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: 2238a825c5aca59233eab1f221f7aefb
projectName: sygma-docs
directory: ./build
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
compressionLevel: mixed

enableGlobalCache: false

nodeLinker: node-modules
8 changes: 8 additions & 0 deletions dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,11 @@ IPFS
Paseo
DefaultMessageReceiver
SprinterNameService
EVMAssetTransfer
SEPOLIA_CHAIN_ID
CRONOS_RPC_URL
erc20Transfer
EVMAssetTransfer
broadcasted
SDK's
evm-to-substrate-fungible-transfer
2 changes: 1 addition & 1 deletion docs/03-sygma-sdk/03-Advanced/01-local-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ yarn install
And build the SDK by running:

```
yarn sdk:build
yarn build
```

#### 4) Run the EVM-to-Substrate token transfer example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
slug: /sdk/examples/erc20/evm-example
id: examples-erc20-evm-example
title: EVM To EVM Token Transfer
title: EVM-to-EVM (Sepolia to Base Sepolia) token transfer example
description: Section that describes how to perform an EVM to EVM token transfer.
sidebar_position: 1
draft: false
---

### EVM-to-EVM token transfer example
### EVM-to-EVM (Sepolia to Base Sepolia) token transfer example

In the following example, we will use the `TESTNET` environment to perform a cross-chain ERC-20 transfer with 5 `ERC20LRTST` tokens. The transfer will be initiated on the EVM-side via the Cronos testnet and received on the EVM-side via the Sepolia Ethereum testnet.

Expand Down Expand Up @@ -41,7 +41,7 @@ We make use of the dotenv module to manage exported private keys with environmen
Clone the sygma-sdk repository into a directory of your choice, and then `cd` into the folder:

```bash
git clone git@github.com:sygmaprotocol/sygma-sdk.git
git clone https://github.com/sygmaprotocol/sygma-sdk.git
cd sygma-sdk/
```

Expand All @@ -58,7 +58,7 @@ yarn install
Build the SDK by running the following command:

```bash
yarn sdk:build
yarn build
```

4. Usage
Expand All @@ -76,7 +76,9 @@ touch .env

Replace between the quotation marks your exported private key:

`PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE"`
```dotenv
PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE"
```

To send an ERC-20 example transfer from EVM to EVM, run:

Expand All @@ -87,29 +89,35 @@ yarn run transfer

The example will use `ethers` in conjunction with the sygma-sdk to create a transfer from Cronos to Sepolia with the `ERC20LRTST` token. It will be received on Sepolia as the `ERC20LRTST` token.

### Script functionality

This example script performs the following steps:
## Script functionality

- Initializes the SDK by importing the required packages and defining the constants for the script.
This example script performs a cross-chain ERC-20 token transfer using the Sygma SDK. The transfer starts on one EVM chain (e.g., Cronos or Sepolia) and is received on another EVM chain (e.g., Sepolia or BASE). Here’s how the script works:

### 1. Imports the Required Packages:
The script first imports all the necessary modules, including those from the Sygma SDK (for asset transfer) and ethers.js (for interacting with Ethereum wallets and providers).
```ts
import { EVMAssetTransfer, Environment, getTransferStatusData, TransferStatusResponse } from "@buildwithsygma/sygma-sdk-core";
import { getSygmaScanLink, type Eip1193Provider } from "@buildwithsygma/core";
import {
createFungibleAssetTransfer,
FungibleTransferParams,
} from "@buildwithsygma/evm";
import dotenv from "dotenv";
import { Wallet, providers } from "ethers";

import Web3HttpProvider from "web3-providers-http";
```
Constants like SEPOLIA_CHAIN_ID, RESOURCE_ID, and CRONOS_RPC_URL are defined based on the specific environment you are working in.
```ts
const SEPOLIA_CHAIN_ID = 11155111;
const BASE_SEPOLIA_CHAIN_ID = 84532;
const RESOURCE_ID =
"0x0000000000000000000000000000000000000000000000000000000000000300"; // This is the resource ID for the ERC20LRTEST token according to Sygma's testnet environment
const CRONOS_RPC_URL = process.env.CRONOS_RPC_URL || "https://evm-t3.cronos.org" // use your own provider in case this RPC URL does not work
const getStatus = async (
txHash: string
): Promise<TransferStatusResponse[]> => {
const data = await getTransferStatusData(Environment.TESTNET, txHash);
return data as TransferStatusResponse[];
};
"0x0000000000000000000000000000000000000000000000000000000000001200";
const SEPOLIA_RPC_URL =
process.env.SEPOLIA_RPC_URL ||
"https://eth-sepolia.g.alchemy.com/v2/MeCKDrpxLkGOn4LMlBa3cKy1EzzOzwzG";
```

- Configures the dotenv module and sets the `privateKey` as a value to be pulled from the `.env` file.
### 2. Configures dotenv Module:
The script loads environment variables using the dotenv module. This includes sensitive information like your private key, which should be stored in a .env file for security purposes.

```ts
import dotenv from "dotenv";
Expand All @@ -123,23 +131,32 @@ if (!privateKey) {
}
```

- Defines the ERC-20 transfer function.
The PRIVATE_KEY environment variable is critical for signing transactions with your Ethereum wallet.

### 3. Defines the Transfer Function:
The erc20Transfer function is the main function that handles the token transfer. It initializes the provider and wallet, sets up the asset transfer, and constructs the transfer object.
```ts
export async function erc20Transfer(): Promise<void> {
export async function erc20Transfer(): Promise<void> {}
```

- Set up the provider, wallet, and asset transfer objects using the TESTNET environment.
### 4. Sets Up the Provider and Wallet:

The script sets up a Web3 provider and creates a wallet using the provided private key. In this case, the script is interacting with the Sepolia network.

```ts
const provider = new providers.JsonRpcProvider(CRONOS_RPC_URL);
const wallet = new Wallet(privateKey ?? "", provider);
const assetTransfer = new EVMAssetTransfer();
await assetTransfer.init(provider, Environment.TESTNET);
const provider = new providers.JsonRpcProvider(CRONOS_RPC_URL);
const wallet = new Wallet(privateKey ?? "", provider);
```

- Constructs a `transfer` object that defines the details of the ERC-20 token transfer using the previously declared constants, as well as the amount to be transferred. As we specify the `getAddress` method from the ethers.js `wallet` module for our recipient, the recipient will be the same address but on a different test network.

### 5. Initializes the Asset Transfer Object:
The Sygma SDK’s EVMAssetTransfer object is initialized using the TESTNET environment. This object is used to build and manage the cross-chain ERC-20 transfer.
```ts
const assetTransfer = new EVMAssetTransfer();
await assetTransfer.init(provider, Environment.TESTNET);
```
### 6. Constructs the Transfer Object:
The script constructs a transfer object using the sender’s address, recipient’s address (same in this case but on a different chain), and the amount to be transferred (5 tokens, represented with 18 decimal places).

```ts
const transfer = assetTransfer.createFungibleTransfer(
await wallet.getAddress(),
Expand All @@ -150,52 +167,34 @@ export async function erc20Transfer(): Promise<void> {
);
```

- Retrieves the fee required to complete the transfer from the SDK.
- Builds the necessary approval transactions for the transfer and sends them using the Ethereum wallet. The approval transactions are required to authorize the transfer of ERC-20 tokens.

### 7. Builds and Sends Approval Transactions:
Before the actual transfer, approval transactions must be sent to authorize the transfer of ERC-20 tokens. The script iterates over the approval transactions, sends them, and logs their transaction hashes.
```ts
const fee = await assetTransfer.getFee(transfer);
const approvals = await assetTransfer.buildApprovals(transfer, fee);
for (const approval of approvals) {
const response = await wallet.sendTransaction(
approval as providers.TransactionRequest
const approvals = await transfer.getApprovalTransactions();
console.log(`Approving Tokens (${approvals.length})...`);
for (const approval of approvals) {
const response = await wallet.sendTransaction(approval);
await response.wait();
console.log(
`Approved, transaction: ${getTxExplorerUrl({txHash: response.hash, chainId: SEPOLIA_CHAIN_ID})}`
);
console.log("Sent approval with hash: ", response.hash);
}
```

- Invokes the `getTransferStatusData` and `getStatus` functions by taking the transaction hash as an input to periodically check the status of the cross-chain transaction.

### 8. Builds and Sends the Final Transfer Transaction:
After approval, the script builds the transfer transaction and sends it to the Ethereum network. Once the transaction is sent, it logs the transaction hash.
```ts
const id = setInterval(() => {
getStatus(response.hash)
.then((data) => {
if (data[0]) {
console.log("Status of the transfer", data[0].status);
if(data[0].status == "executed") {
clearInterval(id);
process.exit(0);
}
} else {
console.log("Waiting for the TX to be indexed");
}
})
.catch((e) => {
console.log("error:", e);
});
}, 5000);
}
const transferTx = await transfer.getTransferTransaction();
const response = await wallet.sendTransaction(transferTx);
await response.wait();
console.log(
`Depositted, transaction: ${getSygmaScanLink(response.hash, process.env.SYGMA_ENV)}`
);
```

- Builds the final `transfer` transaction and sends it using the Ethereum wallet.
### 9. Call the method
Call the described method above
```ts
const transferTx = await assetTransfer.buildTransferTransaction(
transfer,
fee
);
const response = await wallet.sendTransaction(
transferTx as providers.TransactionRequest
);
console.log("Sent transfer with hash: ", response.hash);
}
```
erc20Transfer().finally(() => {});
```
Loading
Loading