-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generic Message passing (#285)
* basic GMP class and helpers * refactor * add dynamic fee handler * fix example * fix tests * fix lint * fix encoding * remove private key from example * resolve comments * move basic transfer params to config * update param names * fix test and lint
- Loading branch information
Showing
19 changed files
with
529 additions
and
91 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 @@ | ||
PRIVATE_KEY="YOUR_PRIVATE_KEY_HERE" |
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,61 @@ | ||
## Sygma SDK Generic Message Example | ||
|
||
This is an example script that demonstrates the functionality of the SDK using the Sygma ecosystem. The script showcases generic message passing (execution of a function on the destination chain) between two networks using the Sygma SDK. | ||
|
||
## Prerequisites | ||
|
||
Before running the script, ensure that you have the following: | ||
|
||
- Node.js installed on your machine | ||
- [Yarn](https://yarnpkg.com/) (version 3.4.1 or higher) | ||
- Access to an Ethereum provider | ||
|
||
## Getting started | ||
|
||
### 1. Clone the repository | ||
|
||
To get started, clone this repository to your local machine with: | ||
|
||
```bash | ||
git clone [email protected]: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 | ||
|
||
To call a function on a destination chain contract: | ||
|
||
```bash | ||
yarn run transfer | ||
``` | ||
|
||
The example will use `ethers` in conjuction with the sygma-sdk to | ||
call a function on a smart contract on `Goerli` by calling the Deposit method on `Sepolia` and passing the details of the function to be called. | ||
|
||
Replace the placeholder values in the `.env` file with your own Ethereum wallet private key and provider 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 Generic Message Passing resource from the list of supported resources registered. | ||
- Searches for the Goerli and Sepolia domains in the list of supported domains based on their chain IDs. | ||
- Constructs a transfer object that defines the details of the destination chain smart contract, function and call-data | ||
- 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,37 @@ | ||
{ | ||
"name": "@buildwithsygma/sygma-sdk-evm-to-evm-generic-message-example", | ||
"version": "0.1.0", | ||
"description": "Sygma sdk examples", | ||
"type": "module", | ||
"sideEffects": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sygmaprotocol/sygma-sdk" | ||
}, | ||
"keywords": [ | ||
"sygma", | ||
"sygmaprotocol", | ||
"buildwithsygma", | ||
"web3", | ||
"bridge", | ||
"ethereum" | ||
], | ||
"scripts": { | ||
"transfer": "ts-node src/transfer.ts" | ||
}, | ||
"author": "Sygmaprotocol Product Team", | ||
"license": "LGPL-3.0-or-later", | ||
"devDependencies": { | ||
"dotenv": "16.3.1", | ||
"eslint": "8", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-only-warn": "1.0.3", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"ts-node": "10.9.1", | ||
"typescript": "5.0.4" | ||
}, | ||
"dependencies": { | ||
"@buildwithsygma/sygma-sdk-core": "2.1.0", | ||
"ethers": "5.6.2" | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
examples/evm-to-evm-generic-mesage-passing/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,55 @@ | ||
import dotenv from "dotenv"; | ||
import { | ||
EVMGenericMessageTransfer, | ||
Environment, | ||
} from "@buildwithsygma/sygma-sdk-core"; | ||
import { Wallet, providers, utils } from "ethers"; | ||
|
||
dotenv.config(); | ||
|
||
const privateKey = process.env.PRIVATE_KEY; | ||
|
||
if (!privateKey) { | ||
throw new Error("Missing environment variable: PRIVATE_KEY"); | ||
} | ||
|
||
const DESTINATION_CHAIN_ID = 5; // Goerli | ||
const RESOURCE_ID = | ||
"0x0000000000000000000000000000000000000000000000000000000000000500"; // Generic Message Handler | ||
const EXECUTE_CONTRACT_ADDRESS = "0xdFA5621F95675D37248bAc9e536Aab4D86766663"; | ||
const EXECUTE_FUNCTION_SIGNATURE = "0xa271ced2"; | ||
const MAX_FEE = "3000000"; | ||
|
||
export async function genericMessage(): Promise<void> { | ||
const provider = new providers.JsonRpcProvider( | ||
"https://gateway.tenderly.co/public/sepolia" | ||
); | ||
const wallet = new Wallet(privateKey as string, provider); | ||
const messageTransfer = new EVMGenericMessageTransfer(); | ||
await messageTransfer.init(provider, Environment.DEVNET); | ||
|
||
const EXECUTION_DATA = utils.defaultAbiCoder.encode(["uint"], [Date.now()]); | ||
|
||
const transfer = messageTransfer.createGenericMessageTransfer( | ||
await wallet.getAddress(), | ||
DESTINATION_CHAIN_ID, | ||
RESOURCE_ID, | ||
EXECUTE_CONTRACT_ADDRESS, | ||
EXECUTE_FUNCTION_SIGNATURE, | ||
EXECUTION_DATA, | ||
MAX_FEE | ||
); | ||
|
||
const fee = await messageTransfer.getFee(transfer); | ||
const transferTx = await messageTransfer.buildTransferTransaction( | ||
transfer, | ||
fee | ||
); | ||
|
||
const response = await wallet.sendTransaction( | ||
transferTx as providers.TransactionRequest | ||
); | ||
console.log("Sent transfer with hash: ", response.hash); | ||
} | ||
|
||
genericMessage().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,25 @@ | ||
{ | ||
"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", | ||
}, | ||
"ts-node": { | ||
"esm": true, | ||
"experimentalSpecifierResolution": "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
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
Oops, something went wrong.