-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update substrate example (#506)
Closes #503
- Loading branch information
Showing
10 changed files
with
256 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PRIVATE_KEY="" | ||
SOURCE_EVM_RPC_URL="SEPOLIA_RPC_URL_HERE" | ||
SYGMA_ENV="testnet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
## Sygma SDK Substrate Asset Transfer Example | ||
|
||
This is an example script that demonstrates the functionality of the SDK using the Sygma ecosystem. The script showcases a EVM Asset transfer between a EVM network and an Substrate network using the Sygma SDK. | ||
|
||
## Prerequisites | ||
|
||
Before running the script, ensure that you have the following: | ||
|
||
- Node.js installed on your machine | ||
- Yarn (version 3.4.1 or higher) | ||
- A EVM development wallet funded with `SepoliaETH` tokens | ||
- The Private key for your EVM development wallet | ||
- An Substrate wallet to receive tokens into (the example presets an existing wallet address already) | ||
|
||
## Getting started | ||
|
||
### 1. Clone the repository | ||
|
||
To get started, clone this repository to your local machine with: | ||
|
||
```bash | ||
git clone git@github.com:sygmaprotocol/sygma-sdk.git | ||
cd sygma-sdk/ | ||
``` | ||
|
||
### 2. Install dependencies | ||
|
||
Install the project dependencies by running: | ||
|
||
```bash | ||
yarn install | ||
``` | ||
|
||
### 3. Build the sdk | ||
|
||
To start the example you need to build the sdk first with: | ||
|
||
```bash | ||
yarn sdk:build | ||
``` | ||
|
||
## Usage | ||
|
||
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. | ||
|
||
**DO NOT COMMIT PRIVATE KEYS WITH REAL FUNDS TO GITHUB. DOING SO COULD RESULT IN COMPLETE LOSS OF YOUR FUNDS.** | ||
|
||
Create a `.env` file in the evm-to-evm example folder: | ||
|
||
```bash | ||
cd examples/evm-to-substrate-fungible-transfer | ||
touch .env | ||
``` | ||
|
||
Replace between the quotation marks your exported private key: | ||
|
||
`PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE"` | ||
|
||
To send an ERC20 example transfer run: | ||
|
||
```bash | ||
yarn run transfer | ||
``` | ||
|
||
The example will use `ethers` in conjuction with the sygma-sdk to | ||
create a transfer from `Sepolia` to `Tangle` with a test ERC20 token. | ||
|
||
Replace the placeholder values in the `.env` file with your own Ethereum wallet private key. | ||
|
||
|
||
**Note** | ||
|
||
To replace default Sepolia url use env variables: | ||
- `SOURCE_EVM_RPC_URL="EVM_RPC_URL"` | ||
## Script Functionality | ||
|
||
This example script performs the following steps: | ||
- initializes the SDK and establishes a connection to the Ethereum provider. | ||
- retrieves the list of supported domains and resources from the SDK configuration. | ||
- Searches for the ERC20 token resource with the specified symbol | ||
- Searches for the Sepolia and Tangle domains in the list of supported domains based on their chain IDs | ||
- Constructs a transfer object that defines the details of the ERC20 token transfer | ||
- Retrieves the fee required for 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 ERC20 tokens. | ||
- Builds the final transfer transaction and sends it using the Ethereum wallet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@buildwithsygma/evm-to-substrate-fungible-transfer-example", | ||
"version": "0.1.0", | ||
"license": "LGPL-3.0-or-later", | ||
"description": "Sygma SDK EVM to Substrate Example", | ||
"type": "module", | ||
"sideEffects": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sygmaprotocol/sygma-sdk" | ||
}, | ||
"keywords": [ | ||
"sygma", | ||
"sygmaprotocol", | ||
"buildwithsygma", | ||
"web3", | ||
"bridge", | ||
"ethereum" | ||
], | ||
"scripts": { | ||
"transfer": "tsx src/transfer.ts" | ||
}, | ||
"author": "Sygmaprotocol Product Team", | ||
"devDependencies": { | ||
"dotenv": "^16.3.1", | ||
"eslint": "8", | ||
"ts-node": "10.9.1", | ||
"typescript": "5.0.4" | ||
}, | ||
"dependencies": { | ||
"@buildwithsygma/core": "workspace:*", | ||
"@buildwithsygma/substrate": "workspace:*", | ||
"@polkadot/api": "^12.3.1", | ||
"@polkadot/keyring": "^12.6.2", | ||
"@polkadot/util-crypto": "^12.6.2", | ||
"tsx": "^4.15.4" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/evm-to-substrate-fungible-transfer/src/environment.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Environment } from "@buildwithsygma/core"; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
SYGMA_ENV: Environment; | ||
PRIVATE_KEY: string; | ||
SOURCE_EVM_RPC_URL: string; | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
examples/evm-to-substrate-fungible-transfer/src/transfer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { Eip1193Provider } from "@buildwithsygma/core"; | ||
import { createEvmFungibleAssetTransfer } from "@buildwithsygma/evm"; | ||
import dotenv from "dotenv"; | ||
import { Wallet, providers } from "ethers"; | ||
import Web3HttpProvider from "web3-providers-http"; | ||
|
||
dotenv.config(); | ||
|
||
const privateKey = process.env.PRIVATE_KEY; | ||
|
||
if (!privateKey) { | ||
throw new Error("Missing environment variable: PRIVATE_KEY"); | ||
} | ||
|
||
const SEPOLIA_CHAIN_ID = 11155111; | ||
const TANGLE_CHAIN_ID = 3799; | ||
const RESOURCE_ID = | ||
"0x0000000000000000000000000000000000000000000000000000000000002000"; | ||
const SEPOLIA_RPC_URL = | ||
process.env.SOURCE_EVM_RPC_URL ?? | ||
"https://eth-sepolia.g.alchemy.com/v2/MeCKDrpxLkGOn4LMlBa3cKy1EzzOzwzG"; | ||
|
||
const explorerUrls: Record<number, string> = { | ||
[SEPOLIA_CHAIN_ID]: "https://sepolia.etherscan.io", | ||
}; | ||
const getTxExplorerUrl = (params: { | ||
txHash: string; | ||
chainId: number; | ||
}): string => `${explorerUrls[params.chainId]}/tx/${params.txHash}`; | ||
|
||
export async function erc20Transfer(): Promise<void> { | ||
const web3Provider = new Web3HttpProvider(SEPOLIA_RPC_URL); | ||
const ethersWeb3Provider = new providers.Web3Provider(web3Provider); | ||
const wallet = new Wallet(privateKey ?? "", ethersWeb3Provider); | ||
const sourceAddress = await wallet.getAddress(); | ||
const destinationAddress = "5GjowPEaFNnwbrmpPuDmBVdF2e7n3cHwk2LnUwHXsaW5KtEL"; | ||
|
||
const params = { | ||
source: SEPOLIA_CHAIN_ID, | ||
destination: TANGLE_CHAIN_ID, | ||
sourceNetworkProvider: web3Provider as unknown as Eip1193Provider, | ||
resource: RESOURCE_ID, | ||
amount: BigInt(1) * BigInt(1e18), | ||
destinationAddress: destinationAddress, | ||
sourceAddress: sourceAddress, | ||
}; | ||
|
||
const transfer = await createEvmFungibleAssetTransfer(params); | ||
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 })}`, | ||
); | ||
} | ||
|
||
const transferTx = await transfer.getTransferTransaction(); | ||
const response = await wallet.sendTransaction(transferTx); | ||
await response.wait(); | ||
console.log( | ||
`Deposited, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`, | ||
); | ||
} | ||
|
||
erc20Transfer().finally(() => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"module": "ES2022", | ||
"allowJs": true, | ||
"declaration": true, | ||
"sourceMap": true, | ||
"declarationMap": true, | ||
"resolveJsonModule": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"downlevelIteration": true, | ||
"allowSyntheticDefaultImports": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"moduleResolution": "node" | ||
}, | ||
"include": [ | ||
"src" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
PRIVATE_MNEMONIC="YOUR TWELVE WORD MNEMONIC HERE WITH SPACES" | ||
RHALA_RPC_URL="RHALA_RPC_URL_HERE" | ||
SOURCE_SUBSTRATE_RPC_URL="TANGLE_RPC_URL_HERE" | ||
SYGMA_ENV="testnet" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters