SDK

A guide on using the Snowbridge TypeScript SDK for integration.

Packages

The following packages are used in the Snowbridge SDK:

  • @snowbridge/api This is the main entry point for developers integrating with Snowbridge. It provides all core interfaces and helper functions to initiate, validate, and send cross-chain transactions. It abstracts over the complexities of constructing and handling XCM messages, Ethereum transactions, and relayer coordination.

    • Use the toPolkadotV2 module for sending packages from Ethereum -> Polkadot.

    • Use the toEthereumV2 module for sending packages from Polkadot -> Ethereum.

  • @snowbridge/registry This package contains the asset and parachain registry used by Snowbridge. It defines the list of supported tokens, parachains, and associated metadata (like contract addresses and decimals). It ensures your transfers use valid combinations of assets and destinations.

  • @snowbridge/contract-types Contains TypeScript typings and contract ABIs for the Ethereum contracts Snowbridge interacts with. Use this package to interact with contracts directly, or to extend SDK functionality.

  • @snowbridge/contracts Provides deployed contract addresses and metadata for Snowbridge smart contracts on supported networks. This is useful when you need to interact with Snowbridge's Ethereum-side contracts directly.

  • @snowbridge/base-types Defines common data types used throughout the SDK, such as asset representations, transfer objects, parachain locations, and more. These types are shared between @snowbridge/api and the registry.

Example Scripts

We have a wide range of scripts using the Snowbridge SDK at https://github.com/Snowfork/snowbridge/tree/main/web/packages/operations/src, as examples of how to use the SDK and the bridge.

Demos

The following examples show how to do an Ether transfer from Ethereum to Polkadot, and back. The full example code can be viewed by following the stated links.

Ethereum to Polkadot

Full example: send_ether_from_eth_to_assethub.ts

Uses the @snowbridge/api toPolkadotV2 package to send the transaction.

Setup

This step prepares all required state and dependencies for the transfer operation. This includes loading the asset registry, initializing the context, which sets up the connections to Ethereum and Substrate-based networks and loading the user wallets for both Ethereum and Substrate chains.

Step 1: Get Delivery Fee

Use getDeliveryFee() to calculate how much the user must pay in order to deliver the message across chains. This includes relayer fees and any protocol-specific gas or weight costs. Displaying this to the user upfront ensures clarity and reduces failed transactions due to underpayment.

Step 2: Create Transfer

The createTransfer() function generates a transfer object, which includes source and destination accounts, the amount to send, the token being transferred and the precomputed delivery fee. This object contains all data necessary to execute the cross-chain transfer and is later used for validation and signing.

Step 3: Validate Transfer

Although optional, validateTransfer() is strongly recommended. It performs local checks and dry-runs the transaction (when possible) to ensure:

  • The sender has enough funds

  • The asset is supported

  • The constructed transaction will succeed on-chain

This step can save users from wasting gas or fees on transactions that would otherwise revert.

Step 4: Send Transaction

Finally, the transaction is signed and submitted to the source chain. Use the Wallet instance to send the transaction.

Polkadot to Ethereum

Full example: send_ether_from_assethub_to_eth.ts

Uses the @snowbridge/api toEthereumV2 module to send the transaction.

Setup

This step prepares all required state and dependencies for the transfer operation. This includes loading the asset registry, initializing the context, which sets up the connections to Ethereum and Substrate-based networks and loading the user wallets for both Ethereum and Substrate chains.

Step 1: Get Delivery Fee

Use getDeliveryFee() to calculate how much the user must pay in order to deliver the message across chains. This includes relayer fees and any protocol-specific gas or weight costs. Displaying this to the user upfront ensures clarity and reduces failed transactions due to underpayment.

Step 2: Create Transfer

The createTransfer() function generates a transfer object, which includes source and destination accounts, the amount to send, the token being transferred and the precomputed delivery fee. This object contains all data necessary to execute the cross-chain transfer and is later used for validation and signing.

Step 3: Validate Transfer

Although optional, validateTransfer() is strongly recommended. It performs local checks and dry-runs the transaction (when possible) to ensure:

  • The sender has enough funds

  • The asset is supported

  • The constructed transaction will succeed on-chain

This step can save users from wasting gas or fees on transactions that would otherwise revert.

Step 4: Send Transaction

Finally, the transaction is signed and submitted to the source chain. Use the SDK helper signAndSend() which manages construction, signing, and submission of the extrinsic.

Last updated