Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0f9af9a

Browse files
chore: Examples doc updates (#555)
Co-authored-by: Saad Ahmed Siddiqui <saadahm.siddiqui@gmail.com>
1 parent 6ba1c2b commit 0f9af9a

10 files changed

Lines changed: 366 additions & 105 deletions

File tree

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18
1+
v18.20.4
Lines changed: 136 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
1-
## Sygma SDK ERC20 Example
1+
# EVM-to-EVM (Sepolia to Base Sepolia) token transfer example
22

3-
This is an example script that demonstrates the functionality of the SDK using the Sygma ecosystem. The script showcases an ERC20 token transfer between the same account on two different testnets using the Sygma SDK.
3+
In the following example, we will use the `TESTNET` environment to perform a cross-chain ERC-20 transfer with 1 testnet `USDC` tokens. The transfer will be initiated on the EVM-side via the Ethereum Sepolia testnet and received on Base Sepolia testnet.
44

5-
## Prerequisites
5+
This is an example script that demonstrates the functionality of the Sygma SDK and the wider Sygma ecosystem of relayers and bridge and handler contracts. The complete example can be found in this [repo](https://github.com/sygmaprotocol/sygma-sdk/tree/main/examples/evm-to-evm-fungible-transfer).
6+
7+
### Prerequisites
68

79
Before running the script, ensure that you have the following:
810

9-
- Node.js
11+
- Node.js v18
1012
- Yarn (version 3.4.1 or higher)
11-
- A development wallet funded with `ERC20LRTest` tokens from the [Sygma faucet](https://faucet-ui-stage.buildwithsygma.com/)
1213
- The [exported private key](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key) of your development wallet
13-
- [Sepolia ETH](https://www.alchemy.com/faucets/ethereum-sepolia) for gas
14+
- Testnet [ETH](https://cloud.google.com/application/web3/faucet/ethereum/sepolia) for gas
1415
- An Ethereum [provider](https://www.infura.io/) (in case the hardcoded RPC within the script does not work)
16+
- A development wallet funded with `USDC` tokens from the [USDC Faucet](https://faucet.circle.com)
17+
18+
:::danger
19+
We make use of the dotenv module to manage exported private keys with environment variables. Please note that accidentally committing a .env file containing private keys to a wallet with real funds, onto GitHub, could result in the complete loss of your funds. **Never expose your private keys.**
20+
:::
1521

16-
## Getting started
22+
### Getting started
1723

18-
### 1. Clone the repository
24+
1. Clone the repository
1925

20-
To get started, clone this repository to your local machine with:
26+
Clone the sygma-sdk repository into a directory of your choice, and then `cd` into the folder:
2127

2228
```bash
23-
git clone git@github.com:sygmaprotocol/sygma-sdk.git
29+
git clone https://github.com/sygmaprotocol/sygma-sdk.git
2430
cd sygma-sdk/
2531
```
2632

27-
### 2. Install dependencies
33+
2. Install dependencies
2834

2935
Install the project dependencies by running:
3036

3137
```bash
3238
yarn install
3339
```
3440

35-
### 3. Build the sdk
41+
3. Build the SDK
3642

37-
To start the example you need to build the sdk first with:
43+
Build the SDK by running the following command:
3844

3945
```bash
40-
yarn build:all
46+
yarn build
4147
```
4248

43-
## Usage
49+
4. Usage
4450

4551
This example uses the `dotenv` module to manage private keys. To run the example, you will need to configure your environment variable to include your test development account's [exported private key](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key). A `.env.sample` is provided as a template.
4652

@@ -55,34 +61,131 @@ touch .env
5561

5662
Replace between the quotation marks your exported private key:
5763

58-
`PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE"`
64+
```dotenv
65+
PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE"
66+
```
5967

60-
To send an ERC20 example transfer run:
68+
To send an ERC-20 example transfer from EVM to EVM, run:
6169

6270
```bash
71+
cd examples/evm-to-evm-fungible-transfer
6372
yarn run transfer
6473
```
6574

66-
The example will use `ethers` in conjuction with the sygma-sdk to
67-
create a transfer from `Sepolia` to `Holesky` with a test ERC20 token.
75+
The example will use `ethers` in conjunction with the sygma-sdk to create a transfer from Sepolia to Base Sepolia with the `USDC` token. It will be received on Sepolia as the `USDC` token.
76+
77+
## Script functionality
78+
79+
This example script performs a cross-chain ERC-20 token transfer using the Sygma SDK. The transfer starts on one EVM chain (e.g., Sepolia) and is received on another EVM chain (e.g., BASE). Here’s how the script works:
80+
81+
### 1. Imports the Required Packages:
82+
83+
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).
84+
85+
```ts
86+
import { getSygmaScanLink, type Eip1193Provider } from "@buildwithsygma/core";
87+
import {
88+
createFungibleAssetTransfer,
89+
FungibleTransferParams,
90+
} from "@buildwithsygma/evm";
91+
import dotenv from "dotenv";
92+
import { Wallet, providers } from "ethers";
93+
import Web3HttpProvider from "web3-providers-http";
94+
```
95+
96+
Constants like `SEPOLIA_CHAIN_ID`, `RESOURCE_ID`, and `BASE_SEPOLIA_CHAIN_ID` are defined based on the specific environment you are working in.
97+
98+
```ts
99+
const SEPOLIA_CHAIN_ID = 11155111;
100+
const BASE_SEPOLIA_CHAIN_ID = 84532;
101+
const RESOURCE_ID =
102+
"0x0000000000000000000000000000000000000000000000000000000000001200";
103+
const SEPOLIA_RPC_URL =
104+
process.env.SEPOLIA_RPC_URL || "https://ethereum-sepolia-rpc.publicnode.com";
105+
```
106+
107+
### 2. Configures dotenv Module:
108+
109+
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.
68110

69-
Replace the placeholder values in the `.env` file with your own Ethereum wallet private key.
111+
```ts
112+
import dotenv from "dotenv";
70113

71-
**Note**
114+
dotenv.config();
72115

73-
To replace default rpc Cronos and Sepolia urls use env variables:
116+
const privateKey = process.env.PRIVATE_KEY;
74117

75-
- `SEPOLIA_RPC_URL="SEPOLIA_RPC_URL_HERE"`
118+
if (!privateKey) {
119+
throw new Error("Missing environment variable: PRIVATE_KEY");
120+
}
121+
```
122+
123+
The PRIVATE_KEY environment variable is critical for signing transactions with your Ethereum wallet.
124+
125+
### 3. Defines the Transfer Function:
76126

77-
## Script Functionality
127+
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.
128+
129+
```ts
130+
export async function erc20Transfer(): Promise<void> {}
131+
```
78132

79-
This example script performs the following steps:
133+
### 4. Sets Up the Provider and Wallet:
80134

81-
- initializes the SDK and establishes a connection to the Ethereum provider.
82-
- retrieves the list of supported domains and resources from the SDK configuration.
83-
- Searches for the ERC20 token resource with the specified symbol
84-
- Searches for the Cronos and Sepolia domains in the list of supported domains based on their chain IDs
85-
- Constructs a transfer object that defines the details of the ERC20 token transfer
86-
- Retrieves the fee required for the transfer from the SDK.
87-
- 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 ERC20 tokens.
88-
- Builds the final transfer transaction and sends it using the Ethereum wallet.
135+
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.
136+
137+
```ts
138+
const web3Provider = new Web3HttpProvider(SEPOLIA_RPC_URL);
139+
const ethersWeb3Provider = new providers.Web3Provider(web3Provider);
140+
const wallet = new Wallet(privateKey ?? "", ethersWeb3Provider);
141+
const sourceAddress = await wallet.getAddress();
142+
const destinationAddress = await wallet.getAddress();
143+
```
144+
145+
### 5. Initializes the Asset Transfer Object:
146+
147+
The Sygma SDK’s EVM Asset Transfer object is initialized using the TESTNET environment. This object is used to build and manage the cross-chain ERC-20 transfer. 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 (1 token, represented with 6 decimal places).
148+
149+
```ts
150+
const params: FungibleTransferParams = {
151+
source: SEPOLIA_CHAIN_ID,
152+
destination: BASE_SEPOLIA_CHAIN_ID,
153+
sourceNetworkProvider: web3Provider as unknown as Eip1193Provider,
154+
resource: RESOURCE_ID,
155+
amount: BigInt(1) * BigInt(1e6),
156+
recipientAddress: destinationAddress,
157+
sourceAddress: sourceAddress,
158+
};
159+
160+
const transfer = await createFungibleAssetTransfer(params);
161+
```
162+
163+
### 6. Builds and Sends Approval Transactions:
164+
165+
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.
166+
167+
```ts
168+
const approvals = await transfer.getApprovalTransactions();
169+
for (const approval of approvals) {
170+
const response = await wallet.sendTransaction(approval);
171+
await response.wait();
172+
}
173+
```
174+
175+
### 7. Builds and Sends the Final Transfer Transaction:
176+
177+
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.
178+
179+
```ts
180+
const transferTx = await transfer.getTransferTransaction();
181+
const response = await wallet.sendTransaction(transferTx);
182+
await response.wait();
183+
```
184+
185+
### 9. Call the method
186+
187+
Call the described method above
188+
189+
```ts
190+
erc20Transfer().finally(() => {});
191+
```

examples/evm-to-evm-fungible-transfer/src/transfer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getSygmaScanLink, type Eip1193Provider } from "@buildwithsygma/core";
1+
import {
2+
Environment,
3+
getSygmaScanLink,
4+
type Eip1193Provider,
5+
} from "@buildwithsygma/core";
26
import {
37
createFungibleAssetTransfer,
48
FungibleTransferParams,
@@ -20,8 +24,7 @@ const BASE_SEPOLIA_CHAIN_ID = 84532;
2024
const RESOURCE_ID =
2125
"0x0000000000000000000000000000000000000000000000000000000000001200";
2226
const SEPOLIA_RPC_URL =
23-
process.env.SEPOLIA_RPC_URL ||
24-
"https://eth-sepolia.g.alchemy.com/v2/MeCKDrpxLkGOn4LMlBa3cKy1EzzOzwzG";
27+
process.env.SEPOLIA_RPC_URL || "https://ethereum-sepolia-rpc.publicnode.com";
2528

2629
const explorerUrls: Record<number, string> = {
2730
[SEPOLIA_CHAIN_ID]: "https://sepolia.etherscan.io",
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PRIVATE_KEY=""
22
SOURCE_EVM_RPC_URL="SEPOLIA_RPC_URL_HERE"
3-
SYGMA_ENV="testnet"
3+
SYGMA_ENV="testnet"
4+
RECIPIENT_ADDRESS=""

0 commit comments

Comments
 (0)