Demos Network Specifications
  • Introduction
    • What is Demos Network
    • Demos Network Architecture
  • FAQ
  • Cookbook
    • Project setup
      • Run the project (MacOS)
      • Run the project (Windows)
        • WSL 2 Setup on Windows (10 and 11 only)
        • Issue Troubleshooting
      • Run the project (Ubuntu)
  • SDK
    • Getting Started
    • WebSDK
      • Authentication
        • FIDO2 Passkeys
          • Under the Hood: FIDO2 Passkeys
      • NodeCalls
      • Transactions
        • Creating a transaction
        • Signing a transaction
        • Broadcasting a transaction
      • L2PS SDK
        • The l2ps module
        • Interacting with the L2PS
        • L2PS Messaging System
      • Instant Messaging
        • What is the Instant Messaging Protocol?
        • Architecture Overview
        • Encryption
        • Quickstart
        • Message Types
        • API Reference
        • FAQ
    • Cross Chain
      • General layout of the XM SDKs
      • EVM
      • BTC
      • Solana
      • MultiversX (EGLD)
      • NEAR
      • IBC
      • TON
      • XRPL
      • The XMScript
      • Identities
    • Demoswork
    • Cookbook
      • Demoswork
        • Creating work steps
        • Conditional Operation
        • Base Operation
        • Signing and broadcasting
      • Transactions
        • Crosschain Transaction
        • Native Transactions
      • SWAP
        • Crosschain SWAP
    • Web2
      • Quick Start
      • DAHR API Reference
        • Types
      • Making Requests
      • Identities
        • Twitter
        • GitHub
    • API Reference
    • Bridges
      • Rubic Bridge Test
    • Post Quantum Cryptography
  • Backend
    • Internal Mechanisms
      • Network Time Synchronization
      • Cross Context Identities
    • Global Change Registry
      • GCR Structure
      • How is GCR Synced?
    • Consensus Mechanism
      • Unparalleled Scalability
      • Decentralization in PoR-BFT
      • Enhanced Security
      • Comparative Advantage
      • Addressing Potential Criticisms
      • Conclusion
    • Communications Stack
    • L2PS (Subnet) Framework
      • How are L2PS transactions handled?
    • Miscellaneous
      • Browsing the Postgres DB via psql
    • Bridges
      • Rubic Bridge
    • Developers Testbed
      • Setting up the environment
      • Setting up the repository
      • Installing dependencies
      • Node Configuration
      • Running the node
  • Frontend
    • Demos Providers Discovery Mechanism
Powered by GitBook
On this page
  • How to SWAP from one chain to another chain
  • 1. Connect to the network
  • 2. Creating the RubicService instance
  • 3. Creating the payload to get trade data
  • 4. Get trade data
  • 5. Execute trade method
  1. SDK
  2. Cookbook
  3. SWAP

Crosschain SWAP

How to SWAP from one chain to another chain

1. Connect to the network

import { Demos } from "@kynesyslabs/demosdk/websdk"
import RubicService from "@/features/bridges/rubic"
import { WrappedCrossChainTrade } from "rubic-sdk"
import { BridgeTradePayload, SupportedChains } from "@kynesyslabs/demosdk/types"

// Connect to the network
const rpc = "https://demosnode.discus.sh";
const demos = new Demos();
await demos.connect(rpc);
await demos.connectWallet(mnemonic);

2. Creating the RubicService instance

After connecting the wallet you need to create a Rubic Service instance

const address = await demos.getEd25519Address()
const rubicService = new RubicService(address, SupportedChains.POLYGON);

As you can see here used SupportedChains.POLYGON. Need to use that chain from which you want to swap tokens. In this case used Polygon, because we wanted to swap from the Polygon chain to Ethereum.

Here are the supported chains.

export const SupportedChains = {
    ETH: "ETH",
    POLYGON: "POLYGON",
    BSC: "BSC",
    AVALANCHE: "AVALANCHE",
    OPTIMISM: "OPTIMISM",
    ARBITRUM: "ARBITRUM",
    LINEA: "LINEA",
    BASE: "BASE",
    SOLANA: "SOLANA",
}

3. Creating the payload to get trade data

Provided payload for swapping 10 USDT from the Polygon chain to the Ethereum chain

const payload: BridgeTradePayload = {
                fromToken: "USDT",
                toToken: "USDT",
                amount: 10,
                fromChainId: 137,
                toChainId: 1,
            }

4. Get trade data

After creating the payload, need to call the getTrade method

const trade = await rubicService.getTrade(payload);

5. Execute trade method

After successfully getting the trade data, we already can do a swap by calling the executeTrade method

const receipt = await rubicService.executeTrade(trade);

The result of the executeTrade method will be the receipt of that swap transaction

e.g.

"0x2342692074a7484f3ac9713d36cd23fdc1c51810d03639e5cf651bfefb62fdc3"
PreviousSWAPNextWeb2

Last updated 11 days ago

PolygonScan