@allbridge/bridge-core-sdk 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,194 @@
1
- # allbridge-core-js-sdk
1
+ <h1 align="center">
2
+ <b>
3
+ <a href="https://core.allbridge.io/"><img src="https://core.allbridge.io/assets/icons/logo.svg" /></a><br>
4
+ </b>
5
+ </h1>
6
+
7
+
8
+ <p align="center">
9
+ <a href="https://core.allbridge.io/"><b>Website</b></a> •
10
+ <a href="https://docs-core.allbridge.io"><b>Documentation</b></a>
11
+ </p>
12
+
13
+ # Allbridge Core SDK
14
+
15
+ Provides an easy integration with the Allbridge Core Bridge for DApps in the browser or Node.js
16
+
17
+ ## Table of Contents
18
+
19
+ - [Installing](#installing)
20
+ - [How to use](#how-to-use)
21
+ - [1. Initialize SDK instance](#1-initialize-sdk-instance)
22
+ - [2. Get the list of supported tokens](#2-get-the-list-of-supported-tokens)
23
+ - [3.1 Approve the transfer of tokens](#31-approve-the-transfer-of-tokens)
24
+ - [3.2 Send Tokens](#32-send-tokens)
25
+ - [Full example](#full-example)
26
+ - [Other operations](#other-operations)
27
+ - [Calculating amount of tokens to be received after fee](#calculating-amount-of-tokens-to-be-received-after-fee)
28
+ - [Calculating amount of tokens to send](#calculating-amount-of-tokens-to-send)
29
+ - [Getting the amount of gas fee](#getting-the-amount-of-gas-fee)
30
+ - [Getting the average transfer time](#getting-the-average-transfer-time)
31
+ - [Semver](#semver)
32
+
33
+ ## Installing
34
+
35
+ ```bash
36
+ $ npm install @allbridge/bridge-core-sdk
37
+ ```
38
+ ## How to use
39
+
40
+ ### 1. Initialize SDK instance
41
+
42
+ ```js
43
+ const AllbridgeCoreSdk = require('@allbridge/allbridge-core-sdk');
44
+ const sdk = new AllbridgeCoreSdk();
45
+ ```
46
+
47
+ ### 2. Get the list of supported tokens
48
+
49
+ ```js
50
+ const tokensInfo = await sdk.getTokensInfo();
51
+ const supportedChains = tokensInfo.chainDetailsMap();
52
+ // extract information about ETH chain
53
+ const {bridgeAddress, tokens, chainId, name} = supportedChains[ChainSymbol.ETH];
54
+ // Choose one of the tokens supported on ETH
55
+ const usdtOnEthTokenInfo = tokens.find(tokensInfo => tokensInfo.symbol === 'USDT');
56
+ ```
57
+
58
+ ### 3.1 Approve the transfer of tokens
59
+
60
+ Before sending tokens the bridge has to be authorized to use user's tokens. This is done by calling the `evmApprove` method on SDK instance.
61
+
62
+ ```js
63
+ const response = await sdk.evmApprove(web3, {
64
+ tokenAddress: tokenAddress,
65
+ owner: senderAddress,
66
+ spender: bridgeAddress,
67
+ });
68
+ ```
69
+
70
+ ### 3.2 Send Tokens
71
+
72
+ Initiate the transfer of tokens with `send` method on SDK instance.
73
+
74
+ ```js
75
+ await sdk.send(web3, {
76
+ amount: '1.01',
77
+ fromAccountAddress: senderAddress,
78
+ sourceChainToken: usdtOnEthTokenInfo,
79
+ toAccountAddress: recipientAddress,
80
+ destinationChainToken: usdtOnTrxTokenInfo,
81
+ messenger: Messenger.ALLBRIDGE,
82
+ });
83
+ ```
84
+
85
+ ### Full example
86
+ Swap BUSD on BSC chain to USDT on TRX chain
87
+
88
+ ```js
89
+ const {
90
+ AllbridgeCoreSdk,
91
+ ChainSymbol,
92
+ Messenger,
93
+ } = require("@allbridge/bridge-core-sdk");
94
+ const Web3 = require("web3");
95
+ require("dotenv").config();
96
+
97
+ async function runExample() {
98
+ // sender address
99
+ const fromAddress = '0x01234567890abcdef01234567890abcdef012345';
100
+ // recipient address
101
+ const toAddress = 'AbcDefGHIJklmNoPQRStuvwXyz1aBcDefG';
102
+
103
+ // configure web3
104
+ const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
105
+ const account = web3.eth.accounts.privateKeyToAccount(process.env.PRIVATE_KEY);
106
+ web3.eth.accounts.wallet.add(account);
107
+
108
+ const sdk = new AllbridgeCoreSdk();
109
+
110
+ // fetch information about supported chains
111
+ const tokensInfo = await sdk.getTokensInfo();
112
+ const chains = tokensInfo.chainDetailsMap();
113
+
114
+ const bscChain = chains[ChainSymbol.BSC];
115
+ const busdTokenInfo = bscChain.tokens.find(tokenInfo => tokenInfo.symbol === 'BUSD');
116
+
117
+ const trxChain = chains[ChainSymbol.TRX];
118
+ const usdtTokenInfo = trxChain.tokens.find(tokenInfo => tokenInfo.symbol === 'USDT');
119
+
120
+ // authorize the bridge to transfer tokens from sender's address
121
+ await sdk.evmApprove(web3, {
122
+ tokenAddress: busdTokenInfo.tokenAddress,
123
+ owner: fromAddress,
124
+ spender: bscChain.bridgeAddress,
125
+ });
126
+
127
+ // initiate transfer
128
+ const response = await sdk.send(web3, {
129
+ amount: "1.01",
130
+ fromAccountAddress: fromAddress,
131
+ toAccountAddress: toAddress,
132
+ sourceChainToken: busdTokenInfo,
133
+ destinationChainToken: usdtTokenInfo,
134
+ messenger: Messenger.ALLBRIDGE,
135
+ });
136
+ console.log("Tokens sent:", response.txId);
137
+ }
138
+
139
+ runExample();
140
+ ```
141
+
142
+ ## Other operations
143
+
144
+ ### Calculating amount of tokens to be received after fee
145
+
146
+ SDK method `getAmountToBeReceived` can be used to calculate the amount of tokens the receiving party will get after applying the bridging fee.
147
+
148
+ ```js
149
+ const amountToBeReceived = sdk.getAmountToBeReceived(
150
+ amountToSend,
151
+ sourceTokenInfo,
152
+ destinationTokenInfo
153
+ );
154
+ ```
155
+
156
+ ### Calculating amount of tokens to send
157
+
158
+ 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.
159
+
160
+ ```js
161
+ const amountToSend = sdk.getAmountToSend(
162
+ amountToBeReceived,
163
+ sourceTokenInfo,
164
+ destinationTokenInfo
165
+ );
166
+ ```
167
+
168
+ ### Getting the amount of gas fee
169
+
170
+ SDK method `getTxCost` can be used to fetch information about the amount of gas fee required to complete the transfer on the destination chain. Gas fee is paid during the [send](#32-send-tokens) operation in the source chain currency.
171
+
172
+ ```js
173
+ const weiValue = await sdk.getTxCost(
174
+ usdtOnEthTokenInfo, // from ETH
175
+ usdtOnTrxTokenInfo, // to TRX
176
+ Messenger.ALLBRIDGE
177
+ );
178
+ ```
179
+
180
+ ### Getting the average transfer time
181
+
182
+ 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.
183
+
184
+ ```js
185
+ const transferTimeMs = sdk.getAverageTransferTime(
186
+ sourceTokenInfo,
187
+ destinationTokenInfo,
188
+ Messenger.ALLBRIDGE
189
+ );
190
+ ```
191
+
192
+ ## Semver
193
+
194
+ 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.