SDK
The Connext SDK allows developers to interact with the Connext protocol in standard Node.js or web environments. See here for a reference of all SDK methods.
This example demonstrates how to execute an
xcall
to transfer funds from a wallet on the source domain to the same address on the destination domain.Install Node.js and use Node.js v18. Follow the instructions to install
nvm
, a node version manager, which will make switching versions easier.Create a project folder and initialize the package. Fill out the project information as you please.
mkdir connext-sdk-example && cd connext-sdk-example
npm init
We'll be using TypeScript so install the following and generate the
tsconfig.json
file.npm install --save-dev @types/node @types/chai @types/mocha typescript
npx tsc --init # or `yarn tsc --init`
We want to use top-level await so we'll set the compiler options accordingly in
tsconfig.json
:{
"compilerOptions": {
"outDir": "./dist",
"target": "es2017",
"module": "esnext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
},
"exclude": ["node_modules"]
}
Add
type
and scripts
as root-level entries to package.json
- they may already exist, so just replace them with the following.{
...
"type": "module",
"scripts": {
"xtransfer": "tsc && node dist/xtransfer.js"
}
...
}
Install the latest beta version of Connext SDK and ethers.
npm install @connext/sdk
npm install ethers@^5
First, we'll configure the SDK. Create a
config.ts
file with the following contents.1
import { SdkConfig } from "@connext/sdk";
2
import { ethers } from "ethers";
3
4
// Create a Signer and connect it to a Provider on the sending chain
5
const privateKey = "<PRIVATE_KEY>";
6
7
let signer = new ethers.Wallet(privateKey);
8
9
// Use the RPC url for the origin chain
10
const provider = new ethers.providers.JsonRpcProvider("https://rpc.ankr.com/eth_goerli");
11
signer = signer.connect(provider);
12
const signerAddress = await signer.getAddress();
13
14
const sdkConfig: SdkConfig = {
15
signerAddress: signerAddress,
16
// Use `mainnet` when you're ready...
17
network: "testnet",
18
// Add more chains here! Use mainnet domains if `network: mainnet`.
19
// This information can be found at https://docs.connext.network/resources/supported-chains
20
chains: {
21
1735353714: { // Goerli domain ID
22
providers: ["https://rpc.ankr.com/eth_goerli"],
23
},
24
1735356532: { // Optimism-Goerli domain ID
25
providers: ["https://goerli.optimism.io"],
26
},
27
},
28
};
29
30
export { signer, sdkConfig };
Replace
<PRIVATE_KEY>
with your own private key on line 5.Notice that the config supports Goerli and Optimism-Goerli. We've also hard-coded the origin chain provider on line 10.
Now create a
xtransfer.ts
file with the following:import { create } from "@connext/sdk";
import { BigNumber } from "ethers";
import { signer, sdkConfig } from "./config.js";
const {sdkBase} = await create(sdkConfig);
const signerAddress = await signer.getAddress();
// xcall parameters
const originDomain = "1735353714";
const destinationDomain = "1735356532";
const originAsset = "0x7ea6eA49B0b0Ae9c5db7907d139D9Cd3439862a1";
const amount = "1000000000000000000";
const slippage = "10000";
// Estimate the relayer fee
const relayerFee = (
await sdkBase.estimateRelayerFee({
originDomain,
destinationDomain
})
).toString();
// Prepare the xcall params
const xcallParams = {
origin: originDomain, // send from Goerli
destination: destinationDomain, // to Mumbai
to: signerAddress, // the address that should receive the funds on destination
asset: originAsset, // address of the token contract
delegate: signerAddress, // address allowed to execute transaction on destination side in addition to relayers
amount: amount, // amount of tokens to transfer
slippage: slippage, // the maximum amount of slippage the user will accept in BPS (e.g. 30 = 0.3%)
callData: "0x", // empty calldata for a simple transfer (byte-encoded)
relayerFee: relayerFee, // fee paid to relayers
};
// Approve the asset transfer if the current allowance is lower than the amount.
// Necessary because funds will first be sent to the Connext contract in xcall.
const approveTxReq = await sdkBase.approveIfNeeded(
originDomain,
originAsset,
amount
)
if (approveTxReq) {
const approveTxReceipt = await signer.sendTransaction(approveTxReq);
await approveTxReceipt.wait();
}
// Send the xcall
const xcallTxReq = await sdkBase.xcall(xcallParams);
xcallTxReq.gasLimit = BigNumber.from("20000000");
const xcallTxReceipt = await signer.sendTransaction(xcallTxReq);
console.log(xcallTxReceipt);
await xcallTxReceipt.wait();
Most of the parameters are hardcoded in this example. For a detailed description of each parameter, see the SDK reference for
xcall
.Fire off the cross-chain transfer!
npm run xtransfer
We can now use the transaction
hash
from the logged transaction receipt to track the status of this xcall
.After the transfer is
status: Executed
on the destination side, the transferred tokens should show up in the recipient wallet.Last modified 2mo ago