Skip to content

Commit

Permalink
chore: update substrate example (#506)
Browse files Browse the repository at this point in the history
Closes #503
  • Loading branch information
Lykhoyda authored Sep 10, 2024
1 parent e9769ce commit 47acbdb
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/evm-to-evm-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ export async function erc20Transfer(): Promise<void> {
const response = await wallet.sendTransaction(approval);
await response.wait();
console.log(
`Approved, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`
`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(
`Depositted, transaction: ${getSygmaScanLink(response.hash, process.env.SYGMA_ENV)}`
`Depositted, transaction: ${getSygmaScanLink(response.hash, process.env.SYGMA_ENV)}`,
);
}

Expand Down
3 changes: 3 additions & 0 deletions examples/evm-to-substrate-fungible-transfer/.env.sample
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"
85 changes: 85 additions & 0 deletions examples/evm-to-substrate-fungible-transfer/README.md
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.
38 changes: 38 additions & 0 deletions examples/evm-to-substrate-fungible-transfer/package.json
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 examples/evm-to-substrate-fungible-transfer/src/environment.d.ts
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 examples/evm-to-substrate-fungible-transfer/src/transfer.ts
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(() => {});
21 changes: 21 additions & 0 deletions examples/evm-to-substrate-fungible-transfer/tsconfig.json
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"
]
}
2 changes: 1 addition & 1 deletion examples/substrate-to-evm-fungible-transfer/.env.sample
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"
22 changes: 11 additions & 11 deletions examples/substrate-to-evm-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ if (!MNEMONIC) {
}

const SEPOLIA_CHAIN_ID = 11155111;
const RHALA_CHAIN_ID = 5231;
const TANGLE_CHAIN_ID = 3799;

const RESOURCE_ID_SYGMA_USD =
"0x0000000000000000000000000000000000000000000000000000000000001100";
const recipient = "0x98729c03c4D5e820F5e8c45558ae07aE63F97461";
const RHALA_RPC_URL =
process.env.RHALA_RPC_URL ?? "wss://rhala-node.phala.network/ws";
"0x0000000000000000000000000000000000000000000000000000000000002000";
const recipient = "0xE39bb23F17a2cf7C9a8C4918376A32036A8867db";
const TANGLE_RPC_URL =
process.env.SOURCE_SUBSTRATE_RPC_URL ?? "wss://rpc.tangle.tools";

const SYGMA_EXPLORER_URL = "https://scan.test.buildwithsygma.com";
const getSygmaExplorerTransferUrl = (params: {
Expand All @@ -33,16 +33,16 @@ const substrateTransfer = async (): Promise<void> => {
const keyring = new Keyring({ type: "sr25519" });
await cryptoWaitReady();
const account = keyring.addFromUri(MNEMONIC);
const wsProvider = new WsProvider(RHALA_RPC_URL);
const wsProvider = new WsProvider(TANGLE_RPC_URL);
const api = await ApiPromise.create({ provider: wsProvider });

const transferParams: SubstrateAssetTransferRequest = {
source: RHALA_CHAIN_ID,
source: TANGLE_CHAIN_ID,
destination: SEPOLIA_CHAIN_ID,
sourceNetworkProvider: api,
sourceAddress: account.address,
resource: RESOURCE_ID_SYGMA_USD,
amount: BigInt("1"),
amount: BigInt(1) * BigInt(1e18),
destinationAddress: recipient,
};

Expand All @@ -55,18 +55,18 @@ const substrateTransfer = async (): Promise<void> => {

if (status.isInBlock) {
console.log(
`Transaction included at blockHash ${status.asInBlock.toString()}`
`Transaction included at blockHash ${status.asInBlock.toString()}`,
);
} else if (status.isFinalized) {
const blockNumber = results.blockNumber?.toNumber();
const extrinsicIndex = results.txIndex;

if (blockNumber && extrinsicIndex) {
console.log(
`Transaction finalized at blockHash ${status.asFinalized.toString()}`
`Transaction finalized at blockHash ${status.asFinalized.toString()}`,
);
console.log(
`Explorer URL: ${getSygmaExplorerTransferUrl({ blockNumber, extrinsicIndex })}`
`Explorer URL: ${getSygmaExplorerTransferUrl({ blockNumber, extrinsicIndex })}`,
);
}
unsub();
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,23 @@ __metadata:
languageName: unknown
linkType: soft

"@buildwithsygma/evm-to-substrate-fungible-transfer-example@workspace:examples/evm-to-substrate-fungible-transfer":
version: 0.0.0-use.local
resolution: "@buildwithsygma/evm-to-substrate-fungible-transfer-example@workspace:examples/evm-to-substrate-fungible-transfer"
dependencies:
"@buildwithsygma/core": "workspace:*"
"@buildwithsygma/substrate": "workspace:*"
"@polkadot/api": "npm:^12.3.1"
"@polkadot/keyring": "npm:^12.6.2"
"@polkadot/util-crypto": "npm:^12.6.2"
dotenv: "npm:^16.3.1"
eslint: "npm:8"
ts-node: "npm:10.9.1"
tsx: "npm:^4.15.4"
typescript: "npm:5.0.4"
languageName: unknown
linkType: soft

"@buildwithsygma/evm@workspace:^, @buildwithsygma/evm@workspace:packages/evm":
version: 0.0.0-use.local
resolution: "@buildwithsygma/evm@workspace:packages/evm"
Expand Down

0 comments on commit 47acbdb

Please sign in to comment.