10.4.10 • Published 5 months ago

@paraspell/xcm-router v10.4.10

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Introduction

XCM Router (Codenamed SpellRouter) is ParaSpell's latest innovation that allows for seamless XCM Exchanges. Send one token type and receive a different one you choose on the destination chain cross-chain. All within one call and only two signatures. This seamless operation allows for a better user experience, limiting the possibility of user errors. The router currently implements the 8 largest Parachain DEXes and is easy to extend as the number of DEXes with public SDKs increases. Together, there are 556 asset pools to choose from, making XCM Router the largest liquidity bridging tool in the ecosystem.

Exchanges implemented:

  • Acala / 36 Pools available
  • Basilisk / 15 Pools available
  • BifrostKusama / 66 Pools available / Requires native token for swaps
  • BifrostPolkadot / 45 Pools available / Requires native token for swaps
  • HydraDX / 210 Pools available
  • Karura / 136 Pools available
  • AssetHubPolkadot / 32 Pools available / Requires specific native tokens for swaps
  • AssetHubKusama / 16 Pools available / Requires specific native tokens for swaps

NOTE: Some exchanges require native tokens in order to proceed with swaps.

Installation

Install dependencies

⚠️ NOTE
Enabling Wasm is required by Hydration SDK in order for XCM-Router to work in your dAPP. You can either enable it in web app config or by plugin.
Hydration also requires augment package - https://github.com/galacticcouncil/sdk/issues/114

⚠️⚠️ NOTE
XCM Router is now migrated towards PAPI library! To migrate you just need to replace old PJS injector with PAPI signer and install new peer dependency. Explore docs to find out more.
yarn add || pnpm | npm install polkadot-api

Install XCM Router

yarn add || pnpm | npm install @paraspell/xcm-router

Importing package to your project

Builder:

import { RouterBuilder } from '@paraspell/xcm-router'

Other exposed functions/ways:

// ESM
import * as xcmRouter from '@paraspell/xcm-router'

//Multiple import options
import { transfer, 
         TransactionType, 
         TTransferOptions, 
         TTxProgressInfo } from '@paraspell/xcm-router'

//As Polkadot moves to ESM only, our Router also moves to ESM only. CJS is not supported anymore.

Implementation

Automatic exchange selection (Based on best price)

If you wish to have an exchange chain selection based on the best price outcome, you can opt for an automatic exchange selection method. This method can be selected by not using the .exchange() parameter in the call. The router will then automatically select the best exchange chain for you based on the best price outcome.

await RouterBuilder
       .from('Polkadot')   //Origin Parachain/Relay chain - OPTIONAL PARAMETER
       .to('Astar')    //Destination Parachain/Relay chain - OPTIONAL PARAMETER
       .currencyFrom({symbol: 'DOT'})    // Currency to send {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount}
       .currencyTo({symbol: 'ASTR'})    // Currency to receive {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount}
       .amount('1000000')  // Amount to send
       .slippagePct('1')   // Max slipppage percentage
       .senderAddress(injectorAddress)   //Injector address
       .recipientAddress(recipientAddress) //Recipient address
       .signer(signer)    //PAPI Signer
       //.evmSenderAddress(evmInjector address)   //Optional parameters when origin node is EVM based (Required with evmSigner)
       //.evmSigner(EVM signer)                     //Optional parameters when origin node is EVM based (Required with evmInjectorAddress)

       .onStatusChange((status: TRouterEvent) => {  //This is how we subscribe to calls that need signing
         console.log(status.type);   // Current transaction type
         console.log(status.routerPlan);   // Array of all transactions to execute
         console.log(status.node);   // Current transaction origin node
         console.log(status.destinationNode);    // Current transaction destination node
         console.log(status.currentStep);    // 0-based step index of current transaction
       })
       .buildAndSend()

Whitelist exchange selection

If you wish to have specific exchanges selection and select the best one among them based on the best price outcome, you can opt for the whitelist automatic exchange selection method. This method can be selected by using .exchange() parameter in the call and feeding it with array of exchanges. The router will then automatically select the best exchange chain for you based on the best price outcome.

await RouterBuilder
        .from('Polkadot')   //Origin Parachain/Relay chain - OPTIONAL PARAMETER
        .exchange(['HydrationDex','AcalaDex','AssetHubPolkadotDex'])    //Exchange Parachains
        .to('Astar')    //Destination Parachain/Relay chain - OPTIONAL PARAMETER
        .currencyFrom({symbol: 'DOT'})    // Currency to send - {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount} 
        .currencyTo({symbol: 'ASTR'})    // Currency to receive - {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount}
        .amount('1000000')  // Amount to send
        .slippagePct('1')   // Max slipppage percentage
        .senderAddress(selectedAccount.address)   //Injector address
        .recipientAddress(recipientAddress) //Recipient address
        .signer(signer)    //PAPI Signer
        //.evmSenderAddress(evmInjector address)   //Optional parameters when origin node is EVM based (Required with evmSigner)
        //.evmSigner(EVM signer)                     //Optional parameters when origin node is EVM based (Required with evmInjectorAddress)

        .onStatusChange((status: TRouterEvent) => {  //This is how we subscribe to calls that need signing
          console.log(status.type);   // Current transaction type
          console.log(status.routerPlan);   // Array of all transactions to execute
          console.log(status.node);   // Current transaction origin node
          console.log(status.destinationNode);    // Current transaction destination node
          console.log(status.currentStep);    // 0-based step index of current transaction
        })
        .buildAndSend()

Manual exchange selection

If you wish to select your exchange chain manually, you can provide the additional .exchange() parameter to the call. The router will then use the exchange chain of your choice.

await RouterBuilder
       .from('Polkadot')   //Origin Parachain/Relay chain - OPTIONAL PARAMETER
       .exchange('HydraDDex')    //Exchange Parachain
       .to('Astar')    //Destination Parachain/Relay chain - OPTIONAL PARAMETER
       .currencyFrom({symbol: 'DOT'})    // Currency to send {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount}
       .currencyTo({symbol: 'ASTR'})    // Currency to receive {id: currencyID, amount: amount} | {symbol: currencySymbol, amount: amount} | {symbol: Native('currencySymbol'), amount: amount} | {symbol: Foreign('currencySymbol'), amount: amount} | {symbol: ForeignAbstract('currencySymbol'), amount: amount} | {multilocation: AssetMultilocationString, amount: amount | AssetMultilocationJson, amount: amount}
       .amount('1000000')  // Amount to send
       .slippagePct('1')   // Max slipppage percentage
       .senderAddress(selectedAccount.address)   //Injector address
       .recipientAddress(recipientAddress) //Recipient address
       .signer(signer)    //PAPI Signer
       //.evmSignerAddress(evmInjector address)   //Optional parameters when origin node is EVM based (Required with evmSigner)
       //.evmSigner(EVM signer)                     //Optional parameters when origin node is EVM based (Required with evmInjectorAddress)

       .onStatusChange((status: TRouterEvent) => {  //This is how we subscribe to calls that need signing
         console.log(status.type);   // Current transaction type
         console.log(status.routerPlan);   // Array of all transactions to execute
         console.log(status.node);   // Current transaction origin node
         console.log(status.destinationNode);    // Current transaction destination node
         console.log(status.currentStep);    // 0-based step index of current transaction
       })
       .buildAndSend()

Get amount out for your currency pair

To retrieve exchange amount, that you receive for your desired asset pair you can use following function. This function returns 2 parameters. Name of best fitting DEX (Automatic selection - can be further used for manual selection) and Amount out

const result = await RouterBuilder()
      .from('Astar') //Optional parameter based on scenario
      .to('Acala') //Optional parameter based on scenario
      .exchange('Hydration') //Optional parameter based on scenario
      .currencyFrom({ symbol: 'ASTR' }) 
      .currencyTo({ symbol: 'DOT' })
      .amount(10000000000n)
      .getBestAmountOut();

console.log(result.amountOut)
console.log(result.exchange)

Get Router fees

You can retrieve fees for all operations XCM Router performs. Keep in mind, that they are not as accurate for transfer from exchange to destination as the currency that is planned to be routed after the swap is not yet available on that account (Thus it uses payment info method instead of dryrun in that scenario).

const fees = await RouterBuilder()
      .from(from) //Optional parameter based on scenario
      .exchange(exchange) //Optional parameter based on scenario
      .to(to) //Optional parameter based on scenario
      .currencyFrom(currencyFrom)
      .currencyTo(currencyTo)
      .amount(amount)
      .senderAddress(senderAddress)
      .recipientAddress(recipientAddress)
      .slippagePct(slippagePct)
      .getXcmFees();

Helpful functions

Below, you can find helpful functions that are exported from XCM Router to help you enhance front end usability of XCM Router.

import {getExchangeAssets, getExchangePairs} from @paraspell/xcm-router

//Returns all assets that DEX supports
const assets = getExchangeAssets('AssetHubPolkadotDex')

//Returns asset pairs supported by selected exchanges
const pairs = getExchangePairs(exchange) // exchange can be also array of exchanges such as [“HydrationDex”, “AcalaDex”] or undefined which will return all available pairs for all dexes

List of DEX chains, assets, and Parachains supported by XCM Router

DEXCan send to/receive fromSupported assetsNotes
Acala DEXPolkadot Relay, Astar, HydraDX, Interlay, Moonbeam, Parallel, AssetHubPolkadot, Unique networkACA, DOT, aSEED, USDCet, UNQ, IBTC, INTR, lcDOT, LDOTFees are paid by either ACA or DOT
Karura DEXKusama Relay, Altair, Basilisk, BifrostKusama, Calamari, Crab, Parallel Heiko, Kintsugi, Moonriver, Quartz, Crust Shadow, Shiden, AssetHubKusamaBNC, USDCet, RMRK, ARIS, AIR, QTZ, CSM, USDT, KAR, KBTC, KINT, KSM, aSEED, LKSM, PHA, tKSM, TAIFees are paid by either KAR or KSM
Hydration DEXPolkadot Relay, Acala, Interlay, AssetHubPolkadot, Zeitgeist, Astar, Centrifuge, BifrostPolkadot, MythosUSDT, MYTH, HDX, WETH, GLMR, IBTC, BNC, WBTC, vDOT, DAI, CFG, DOT, DAI, ZTG, WBTC, INTR, ASTR, LRNA, USDCChain automatically gives you native asset to pay for fees.
Basilisk DEXKusama Relay, Karura, AssetHubKusama, Tinkernet, RobonomicsBSX, USDT, aSEED, XRT, KSM, TNKRChain automatically gives you native asset to pay for fees.
Bifrost Kusama DEXKusama Relay, AssetHubKusama, Karura, Moonriver, KintsugiBNC, vBNC, vsKSM, vKSM, USDT, aSEED, KAR, ZLK, RMRK, KBTC, MOVR, vMOVRChain requires native BNC asset for fees.
Bifrost Polkadot DEXPolkadot Relay, AssetHubPolkadot, Moonbeam, Astar, InterlayBNC, vDOT, vsDOT, USDT, FIL, vFIL, ASTR, vASTR, GLMR, vGLMR, MANTA, vMANTAChain requires native BNC asset for fees.
AssetHubPolkadotPolkadot Relay, Any Parachain it has HRMP channel withDOT, WETH.e, USDC, USDT, LAOS, MYTH, WBBTC.e, ASX, BILL, DEMO, TATE, PINK, MODE, MVPW, PIGS, DED, wstETH.e, TTT, KSM, tBTC.e, PEPE.e, SHIB.e, TON.e, NAT, NT2, DOTA, STINK, MTC, AJUN, GGI, GLMR, NINRequires specific native tokens for swaps
AssetHubKusamaKusama Relay, Any Parachain it has HRMP channel withKSM, DOT, USDC, USDT, BILLCOIN, WOOD, dUSD, TACP, TSM, MA42, USDT, DMO, JAMRequires specific native tokens for swaps

💻 Testing

  • Run compilation using yarn compile

  • Run linting test using yarn lint

  • Run unit tests using yarn test

  • Run integration tests using yarn test:integration

XCM Router can be tested in Playground.

License

Made with 💛 by ParaSpell✨

Published under MIT License.

Support

10.4.1

5 months ago

10.4.2

5 months ago

10.4.3

5 months ago

10.4.4

5 months ago

9.2.5

5 months ago

10.4.5

5 months ago

9.2.4

6 months ago

10.4.6

5 months ago

10.4.7

5 months ago

9.2.2

6 months ago

10.4.8

5 months ago

9.2.1

6 months ago

8.11.1

6 months ago

8.11.0

7 months ago

8.13.1

6 months ago

10.0.0

6 months ago

8.13.0

6 months ago

10.0.1

6 months ago

8.15.1

6 months ago

10.2.0

5 months ago

10.0.2

6 months ago

8.15.0

6 months ago

8.13.2

6 months ago

10.4.0

5 months ago

8.15.2

6 months ago

2.2.1

9 months ago

8.8.0

8 months ago

8.6.2

8 months ago

2.2.0

9 months ago

2.2.3

9 months ago

2.2.2

9 months ago

8.6.1

8 months ago

10.4.9

5 months ago

2.0.0

10 months ago

8.9.10

7 months ago

9.1.1

6 months ago

9.1.0

6 months ago

10.1.4

6 months ago

10.1.5

5 months ago

10.1.6

5 months ago

10.1.7

5 months ago

8.10.0

7 months ago

9.1.2

6 months ago

8.12.0

6 months ago

8.14.0

6 months ago

8.12.1

6 months ago

10.1.0

6 months ago

8.16.0

6 months ago

10.1.1

6 months ago

1.5.0

11 months ago

10.3.0

5 months ago

10.1.2

6 months ago

10.3.1

5 months ago

10.1.3

6 months ago

8.9.0

8 months ago

2.3.0

8 months ago

8.7.1

8 months ago

8.9.2

8 months ago

8.9.1

8 months ago

8.7.0

8 months ago

8.9.8

7 months ago

8.9.7

7 months ago

8.9.9

7 months ago

8.9.4

7 months ago

8.9.3

7 months ago

8.9.6

7 months ago

2.1.0

10 months ago

8.9.5

7 months ago

10.4.10

5 months ago

9.2.0

6 months ago

9.0.0

6 months ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago