0.4.0-beta-2 • Published 25 days ago

@routerprotocol/asset-transfer-sdk-ts v0.4.0-beta-2

Weekly downloads
-
License
MIT
Repository
github
Last release
25 days ago

asset transfer sdk

Provides functions to use pathfinder api, through which one can use Router's asset transfer solution.

PathFinder SDK

To simplify the process of carrying out cross-chain swaps using Router Nitro, we have abstracted the process of interacting with Nitro's bridge contracts using our Asset Transfer SDK. Follow this documentation to integrate and use Nitro's Asset Transfer SDK.

Instalation

Install Nitro's Asset Transfer SDK into your JavaScript development environment by running the following command in your terminal:

npm install @routerprotocol/asset-transfer-sdk-ts

Initializing

After installing the module, import and initialize it into your code:

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";

const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

You can use Network.Testnet for mandara and Network.Mainet for mainnet.

Example:

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";

const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

Performing a Cross Chain Transaction

1️⃣ Request a Quote

To initiate a cross-chain transaction, you first need to obtain a quote from the Pathfinder API. The quote requires the following information:

  • sourceChainId: The ID of the source blockchain.
  • sourceTokenAddress: The address of the token on the source blockchain.
  • destinationChainId: The ID of the destination blockchain.
  • destinationTokenAddress: The address of the token on the destination blockchain.
  • expandedInputAmount: The amount of the token you want to transfer, in its smallest unit (like wei for ETH).

Use the getQuote method of the Pathfinder instance to request a quote:

const quote = await pathfinder.getQuote({
            sourceChainId,
            sourceTokenAddress,
            destinationChainId,
            destinationTokenAddress,
            expandedInputAmount,
        });

The getQuote method returns an object containing:

  • Fee details
  • Estimated amount to be received
  • Estimated time of completion

and more fields. Here's a example:

Getting a quote for transferring 10 USDT from Fuji to Mumbai:

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";

// initialize a Pathfinder instance
const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

const main = async() => {


// getting a quote for 10 USDT from Fuji to Mumbai
const quote = await pathfinder.getQuote({
            sourceChainId: "43113",
            sourceTokenAddress: "0xb452b513552aa0B57c4b1C9372eFEa78024e5936",
            destinationChainId: "80001",
            destinationTokenAddress: "0x22bAA8b6cdd31a0C5D1035d6e72043f4Ce6aF054",
            expandedInputAmount: "10000000",
        });

console.log(quote)
}

main()

The Response

2️⃣ Executing a Quote

After getting quote for executing the quote you need have EvmSigner for evm chains, NearAccount for near and tronweb for Tron. You can create them using private keys rpc using the following functions or you can pass your own.

        import { evmSignerFromPrivateKeyAndRpc } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/EvmChainClient';
        import { getTronWeb } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/TronChainClient';
        import { nearSignerFromPrivateKey } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/NearChainClient';

        const evmSigner = evmSignerFromPrivateKeyAndRpc(evmPrivateKey, EVM_RPC);
        const tronSigner = getTronWeb(tronPrivateKey, TRON_RPC);
        const nearSigner = await nearSignerFromPrivateKey(nearAccountId, nearPrivateKey, nearAccount);

Complete implemenation of executing the quote.

TRON

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";
import { evmSignerFromPrivateKeyAndRpc } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/EvmChainClient';
        import { getTronWeb } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/TronChainClient';

// initialize a Pathfinder instance
const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);
const TRON_RPC="https://api.shasta.trongrid.io"
const main = async() => {

    // building evmsigner
            const evmSigner = evmSignerFromPrivateKeyAndRpc(evmPrivateKey, TRON_RPC);
    // building tron signer
                    const tronSigner = getTronWeb(tronPrivateKey, TRON_RPC);

// getting a quote for 10 USDT from Tron to Mumbai
const quote = await pathfinder.getQuote({
            sourceChainId: "2494104990", // tron chainId
            sourceTokenAddress: "0xF2340B8D37A198B2D66795C8B5B7C467CF92C4EC", // tron token address
            destinationChainId: "80001",
            destinationTokenAddress: "0x22bAA8b6cdd31a0C5D1035d6e72043f4Ce6aF054",
            expandedInputAmount: "1000000",
        });
        const transaction = await pathfinder.executeQuote({
            quote,
            slippageTolerance: "1",
            senderAddress: evmSigner.address, // tron address
            receiverAddress: evmSigner.address,
        },
            {
                evmSigner,
                tronweb: tronSigner
            }
        );

main()

EVM

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";
import { evmSignerFromPrivateKeyAndRpc } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/EvmChainClient';

// initialize a Pathfinder instance
const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

const main = async() => {

    // building evmsigner
            const evmSigner = evmSignerFromPrivateKeyAndRpc(evmPrivateKey, "https://rpc.ankr.com/avalanche_fuji");


// getting a quote for 10 USDT from Fuji to Mumbai
const quote = await pathfinder.getQuote({
            sourceChainId: "43113",
            sourceTokenAddress: "0xb452b513552aa0B57c4b1C9372eFEa78024e5936",
            destinationChainId: "80001",
            destinationTokenAddress: "0x22bAA8b6cdd31a0C5D1035d6e72043f4Ce6aF054",
            expandedInputAmount: "10000000",
        });

        // execute quote handles approval as well
         const transaction = await pathfinder.executeQuote({
            quote,
            slippageTolerance: "1",
            senderAddress: evmSigner.address,
            receiverAddress: evmSigner.address,
        },
            {
                evmSigner
            }
        );

}

main()

NEAR

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";
import { nearSignerFromPrivateKey } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/NearChainClient';

// initialize a Pathfinder instance
const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

const main = async() => {

    // building near account
        const nearRpc = "https://rpc.testnet.near.org"
        const nearSigner = await nearSignerFromPrivateKey(nearAccountId, nearPrivateKey, "testnet", nearRpc);


// getting a quote for 10 USDT from Fuji to Mumbai
 const quote = await pathfinder.getQuote({
            sourceChainId: "near-testnet", // cross check 
            sourceTokenAddress: "usdt_router.router_protocol.testnet", // near usdt address
            destinationChainId: "80001",
            destinationTokenAddress: "0x22bAA8b6cdd31a0C5D1035d6e72043f4Ce6aF054",
            expandedInputAmount: "1000000000000000000",
        });
        const transaction = await pathfinder.executeQuote({
            quote,
            slippageTolerance: "1",
            senderAddress: "joydeeeep.testnet",
            receiverAddress: "0x40d5250D1ce81fdD1F0E0FB4F471E57AA0c1FaD3",
        },
            {
                nearAccount: nearSigner
            }
        );

}

main()

3️⃣ Getting transaction status

To get transaction status you need to pass the source transaction hash to the following function.

const status = await pathfinder.getTransactionStatus(SRC_TXN_HASH);

Example:

import { Pathfinder, Network } from "@routerprotocol/asset-transfer-sdk-ts";
import { evmSignerFromPrivateKeyAndRpc } from '@routerprotocol/asset-transfer-sdk-ts/pathfinder/ChainClient/EvmChainClient';

// initialize a Pathfinder instance
const YOUR_WIDGET_ID = 24 // get your unique sdk id by contacting us on Telegram
const pathfinder = new Pathfinder(Network.Testnet, YOUR_WIDGET_ID);

const main = async() => {

    // building evmsigner
            const evmSigner = evmSignerFromPrivateKeyAndRpc(evmPrivateKey, "https://rpc.ankr.com/avalanche_fuji");


// getting a quote for 10 USDT from Fuji to Mumbai
const quote = await pathfinder.getQuote({
            sourceChainId: "43113",
            sourceTokenAddress: "0xb452b513552aa0B57c4b1C9372eFEa78024e5936",
            destinationChainId: "80001",
            destinationTokenAddress: "0x22bAA8b6cdd31a0C5D1035d6e72043f4Ce6aF054",
            expandedInputAmount: "10000000",
        });

        // execute quote handles approval as well
         const transaction = await pathfinder.executeQuote({
            quote,
            slippageTolerance: "1",
            senderAddress: evmSigner.address,
            receiverAddress: evmSigner.address,
        },
            {
                evmSigner
            }
        );

        const status = await pathfinder.getTransactionStatus(transaction);

}

main()

AssetGuard SDK (E2E automation test suite)

Exposes a class called AssetGuard though which you can tests muliple transactions in bulk.

0.4.0-beta-2

25 days ago

0.3.0

25 days ago

0.4.0-beta-0

25 days ago

0.4.0-beta-1

25 days ago

0.3.0-beta-3

1 month ago

0.3.0-beta-2

1 month ago

0.3.0-beta-1

1 month ago

0.3.0-beta-0

1 month ago

0.2.0

3 months ago

0.2.0-beta-0

4 months ago

0.2.0-dev-0

4 months ago

0.1.0-dev-10

6 months ago

0.1.0-dev-9

6 months ago

0.1.0-dev-8

6 months ago

0.1.0-dev-7

6 months ago

0.1.0-dev-6

6 months ago

0.1.0-dev-5

6 months ago

0.1.0-dev-4

6 months ago

0.1.0-dev-3

6 months ago

0.1.0-dev-2

6 months ago

0.1.0-dev-1

6 months ago

0.1.0-dev-0

6 months ago