3.13.0 • Published 3 days ago

@allbridge/bridge-core-sdk v3.13.0

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

Allbridge Core SDK

Provides an easy integration with the Allbridge Core Bridge for DApps in the browser or Node.js

Table of Contents

Installing

$ npm install @allbridge/bridge-core-sdk

How to use

1. Initialize SDK instance

const { AllbridgeCoreSdk } = require("@allbridge/bridge-core-sdk");
const sdk = new AllbridgeCoreSdk();

2. Get the list of supported tokens

const supportedChains = await sdk.chainDetailsMap();
// extract information about ETH chain
const {bridgeAddress, tokens, chainId, name} = supportedChains[ChainSymbol.ETH];
// Choose one of the tokens supported on ETH
const usdtOnEthTokenInfo = tokens.find(tokenInfo => tokenInfo.symbol === 'USDT');

3.1 Approve the transfer of tokens

Before sending tokens, the bridge has to be authorized to use the tokens of the owner. This is done by calling the approve method on SDK instance. For Ethereum USDT - due to specificity of the USDT contract: If the current allowance is not 0, this function will perform an additional transaction to set allowance to 0 before setting the new allowance value.

const response = await sdk.approve(web3, {
  token: sourceTokenInfo,
  owner: accountAddress,
  spender: sourceTokenInfo.poolAddress,
});

TIP: To interact with the Tron blockchain: use tronWeb instead of web3

3.2 Send Tokens

Initiate the transfer of tokens with send method on SDK instance.

await sdk.send(web3, {
  amount: '1.01',
  fromAccountAddress: senderAddress,
  toAccountAddress: recipientAddress,
  sourceChainToken: usdtOnEthTokenInfo,
  destinationChainToken: usdtOnTrxTokenInfo,
  messenger: Messenger.ALLBRIDGE,
});

TIP: To interact with the Tron blockchain: use tronWeb instead of web3

Full example

Swap BUSD on BSC chain to USDT on TRX chain

const {
  AllbridgeCoreSdk,
  ChainSymbol,
  Messenger,
} = require("@allbridge/bridge-core-sdk");
const Web3 = require("web3");
require("dotenv").config();

async function runExample() {
  // sender address
  const fromAddress = '0x01234567890abcdef01234567890abcdef012345';
  // recipient address
  const toAddress = 'AbcDefGHIJklmNoPQRStuvwXyz1aBcDefG';

  // configure web3
  const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
  const account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY);
  web3.eth.accounts.wallet.add(account);

  const sdk = new AllbridgeCoreSdk();

  // fetch information about supported chains
  const chains = await sdk.chainDetailsMap();

  const bscChain = chains[ChainSymbol.BSC];
  const busdTokenInfo = bscChain.tokens.find(tokenInfo => tokenInfo.symbol === 'BUSD');

  const trxChain = chains[ChainSymbol.TRX];
  const usdtTokenInfo = trxChain.tokens.find(tokenInfo => tokenInfo.symbol === 'USDT');

  // authorize a transfer of tokens from sender's address
  await sdk.approve(web3, {
    tokenAddress: busdTokenInfo.tokenAddress,
    owner: fromAddress,
    spender: busdTokenInfo.poolAddress,
  });

  // initiate transfer
  const response = await sdk.send(web3, {
    amount: "1.01",
    fromAccountAddress: fromAddress,
    toAccountAddress: toAddress,
    sourceChainToken: busdTokenInfo,
    destinationChainToken: usdtTokenInfo,
    messenger: Messenger.ALLBRIDGE,
  });
  console.log("Tokens sent:", response.txId);
}

runExample();

TIP: For more details, see Examples

Other operations

Liquidity pools operations

SDK supports operation with Liquidity Pools For more details, see Docs For more details, see Examples

Transaction builder

Approve Transaction

SDK method rawTransactionBuilder.approve can be used to create approve Transaction.

const rawTransactionApprove = await sdk.rawTransactionBuilder.approve(web3, approveData);

TIP: To interact with the Tron blockchain: use tronWeb instead of web3

Send Transaction

SDK method rawTransactionBuilder.send can be used to create send Transaction.

const rawTransactionSend = await sdk.rawTransactionBuilder.send(sendParams, web3);

TIP: To interact with the Tron blockchain: use tronWeb instead of web3

Solana Blockchain

To create send transaction on Solana blockchain:

const { transaction, signer } = await sdk.rawTransactionBuilder.send(sendParams);

TIP: For more details, see Example

Get information about sent transaction

SDK method getTransferStatus can be used to get information about tokens transfer.

const transferStatus = await sdk.getTransferStatus(chainSymbol, txId);

Calculating amount of tokens to be received after fee

SDK method getAmountToBeReceived can be used to calculate the amount of tokens the receiving party will get after applying the bridging fee.

const amountToBeReceived = await sdk.getAmountToBeReceived(
  amountToSend,
  sourceTokenInfo,
  destinationTokenInfo
);

Calculating amount of tokens to send

SDK method getAmountToSend can be used to calculate the amount of tokens to send based on the required amount of tokens the receiving party should get.

const amountToSend = await sdk.getAmountToSend(
  amountToBeReceived,
  sourceTokenInfo,
  destinationTokenInfo
);

Getting the amount of gas fee

The SDK method getGasFeeOptions allows to retrieve information about the available methods to pay the gas fee, as well as the amount of gas fee needed to complete a transfer on the destination chain. Gas fee is paid during the send operation and can be paid either in the source chain's currency or in source tokens.

The method returns an object with two properties:

  • native: The amount of gas fee, denominated in the smallest unit of the source chain currency (e.g. wei for Ethereum).
  • stablecoin: (optional) The amount of gas fee, denominated in the smallest unit of the source token. If this property is not present, it indicates that the stablecoin payment method is not available.
const { native, stablecoin } = await sdk.getGasFeeOptions(
  usdtOnEthTokenInfo, // from ETH
  usdtOnTrxTokenInfo, // to TRX
  Messenger.ALLBRIDGE
);
console.log(native); // Output: "10000000000000000" (0.01 ETH)
console.log(stablecoin); // Output: "10010000" (10.01 USDT)

Getting the average transfer time

SDK method getAverageTransferTime can be used to get the average time in ms it takes to complete a transfer for a given combination of tokens and messenger.

const transferTimeMs = sdk.getAverageTransferTime(
  sourceTokenInfo,
  destinationTokenInfo,
  Messenger.ALLBRIDGE
);

Semver

Until bridge-core-sdk reaches a 1.0.0 release, breaking changes will be released with a new minor version. For example 0.3.1, and 0.3.4 will have the same API, but 0.4.0 will have breaking changes.

3.13.1-beta.1

3 days ago

3.13.0

6 days ago

3.13.0-beta.2

11 days ago

3.13.0-beta.1

12 days ago

3.12.3-beta.2

14 days ago

3.12.3-beta.1

24 days ago

3.12.2-beta.1

1 month ago

3.12.2

1 month ago

3.12.1

1 month ago

3.12.0

1 month ago

3.12.0-beta.5

2 months ago

3.12.0-beta.6

2 months ago

3.12.0-beta.4

2 months ago

4.0.0-beta.2

2 months ago

4.0.0-beta.1

2 months ago

3.12.0-beta.3

2 months ago

3.12.0-beta.1

2 months ago

3.12.0-beta.2

2 months ago

3.11.0

3 months ago

3.11.0-beta.2

3 months ago

3.11.0-alpha.1

3 months ago

3.11.0-beta.1

3 months ago

3.10.2-beta.1

4 months ago

3.10.1-beta.1

4 months ago

3.10.1-alpha.1

4 months ago

3.10.2-alpha.2

4 months ago

3.10.2-alpha.1

4 months ago

3.10.1

4 months ago

3.10.0

4 months ago

3.10.0-beta.2

4 months ago

3.10.0-beta.1

4 months ago

3.9.1-beta.1

4 months ago

3.9.1

4 months ago

3.10.0-alpha.1

5 months ago

3.10.0-alpha.2

5 months ago

3.9.0

5 months ago

3.8.0

5 months ago

3.8.0-beta.10

5 months ago

3.8.0-beta.8

5 months ago

3.8.0-beta.9

5 months ago

3.8.0-beta.7

5 months ago

3.8.0-beta.6

5 months ago

3.8.0-beta.5

5 months ago

3.8.0-beta.4

5 months ago

3.8.0-alpha.2

6 months ago

3.8.0-alpha.3

6 months ago

3.8.0-alpha.4

5 months ago

3.8.0-alpha.5

5 months ago

3.8.0-alpha.1

6 months ago

3.8.0-alpha.6

5 months ago

3.8.0-beta.2

5 months ago

3.8.0-beta.3

5 months ago

3.8.0-beta.1

5 months ago

3.6.0

6 months ago

3.7.0-alpha.1

6 months ago

3.7.0-alpha.2

6 months ago

3.7.0

6 months ago

3.6.0-alpha.1

6 months ago

3.5.0

6 months ago

3.5.1-alpha.1

6 months ago

3.5.1-alpha.2

6 months ago

3.5.0-alpha.11

7 months ago

1.2.0

10 months ago

3.3.0-beta.2

8 months ago

3.3.0-beta.1

8 months ago

1.2.1

10 months ago

3.1.1-beta.1

8 months ago

2.0.0-alpha.1

10 months ago

3.1.1-beta.2

8 months ago

2.0.0

9 months ago

3.2.0

8 months ago

1.1.1

10 months ago

2.0.0-beta.9

9 months ago

1.1.0

10 months ago

2.0.0-beta.8

10 months ago

2.0.0-beta.7

10 months ago

3.0.0-beta.1

9 months ago

3.0.0-beta.3

9 months ago

3.0.0-beta.2

9 months ago

3.0.0-beta.5

9 months ago

3.0.0-beta.4

9 months ago

3.0.0-beta.7

9 months ago

3.0.0-beta.6

9 months ago

3.0.0-beta.9

9 months ago

3.0.0-beta.8

9 months ago

3.4.1-alpha.1

7 months ago

2.0.0-beta.1

10 months ago

2.0.0-beta.6

10 months ago

2.0.0-beta.5

10 months ago

2.0.0-beta.4

10 months ago

2.0.0-beta.3

10 months ago

3.1.2

8 months ago

3.1.1

8 months ago

3.1.0

8 months ago

3.5.0-alpha.3

7 months ago

3.5.0-alpha.4

7 months ago

3.5.0-alpha.1

7 months ago

3.5.0-alpha.2

7 months ago

3.5.0-alpha.7

7 months ago

3.5.0-alpha.8

7 months ago

3.5.0-alpha.5

7 months ago

3.5.0-alpha.6

7 months ago

1.0.0

10 months ago

3.4.0-alpha.1

8 months ago

3.4.0-alpha.8

7 months ago

3.4.0-alpha.9

7 months ago

3.1.3-beta.1

8 months ago

3.4.0-beta.5

7 months ago

3.4.0-alpha.6

7 months ago

3.1.3-beta.2

8 months ago

3.4.0-beta.4

7 months ago

3.4.0-alpha.7

7 months ago

3.4.0-alpha.4

7 months ago

3.4.0-alpha.5

7 months ago

3.4.0-alpha.2

8 months ago

3.4.0-alpha.3

8 months ago

3.4.0-beta.3

7 months ago

3.4.0-beta.2

7 months ago

3.4.0-beta.1

7 months ago

3.4.0

7 months ago

3.0.0

8 months ago

2.0.0-beta.10

9 months ago

3.0.0-beta.10

9 months ago

3.0.0-beta.11

9 months ago

3.0.0-beta.12

8 months ago

1.0.0-alpha.23

10 months ago

1.0.0-alpha.25

10 months ago

1.0.0-alpha.24

10 months ago

3.5.0-alpha.10

7 months ago

3.1.0-beta.1

8 months ago

3.1.0-beta.2

8 months ago

3.1.2-beta.2

8 months ago

3.1.0-beta.3

8 months ago

3.1.2-beta.1

8 months ago

3.1.0-beta.4

8 months ago

3.1.0-beta.5

8 months ago

3.1.0-beta.6

8 months ago

3.1.2-beta.4

8 months ago

3.1.2-beta.3

8 months ago

1.2.0-alpha.1

10 months ago

2.1.0

9 months ago

3.3.0

8 months ago

3.2.0-beta.6

8 months ago

3.2.0-beta.4

8 months ago

3.2.0-beta.5

8 months ago

3.2.0-beta.2

8 months ago

3.2.0-beta.3

8 months ago

3.2.0-beta.1

8 months ago

1.0.0-alpha.19

10 months ago

1.0.0-alpha.18

10 months ago

1.0.0-alpha.17

10 months ago

1.0.0-alpha.21

10 months ago

1.0.0-alpha.20

10 months ago

1.0.0-alpha.22

10 months ago

1.0.0-alpha.9

11 months ago

1.0.0-alpha.8

11 months ago

1.0.0-alpha.7

11 months ago

1.0.0-alpha.6

11 months ago

1.0.0-alpha.10

11 months ago

1.0.0-alpha.5

11 months ago

1.0.0-alpha.4

11 months ago

1.0.0-alpha.3

11 months ago

1.0.0-alpha.16

11 months ago

1.0.0-alpha.15

11 months ago

1.0.0-alpha.12

11 months ago

1.0.0-alpha.11

11 months ago

1.0.0-alpha.14

11 months ago

1.0.0-alpha.13

11 months ago

1.0.0-alpha.2

1 year ago

1.0.0-alpha.1

1 year ago

0.11.0

1 year ago

0.11.1

1 year ago

0.10.0

1 year ago

0.10.1

1 year ago

0.9.0

1 year ago

0.10.2

1 year ago

0.8.0

1 year ago

0.5.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.7.0

1 year ago

0.6.0

2 years ago

0.4.2

2 years ago

0.3.0

2 years ago