rainbow-swap-sdk 1.4.4 → 1.4.5

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,73 +1,85 @@
1
1
  # Rainbow Swap 🌈 SDK
2
2
 
3
- This SDK is designed for building applications on top of [Rainbow Swap 🌈](https://github.com/0xblackbot/rainbow-swap) - The Next Gen DEX Aggregator on the TON blockchain 💎.
3
+ This SDK is designed for building applications on top of [Rainbow.ag](https://github.com/0xblackbot/rainbow-swap) - Swap Aggregator on TON blockchain 💎.
4
4
 
5
- ---
5
+ ---
6
6
 
7
- **We have plans to integrate commissions and share them with those who have integrated this SDK into their DApps.**
7
+ **To receive your `partnerId`, set custom fees, and enjoy a 50% revenue share, contact us in our [Community Chat](https://t.me/rainbow_swap_chat).**
8
8
 
9
9
  ---
10
10
 
11
11
  [![npm version](https://badge.fury.io/js/rainbow-swap-sdk.svg)](https://badge.fury.io/js/rainbow-swap-sdk)
12
12
  ![NPM License](https://img.shields.io/npm/l/rainbow-swap-sdk)
13
13
 
14
- ### Installation
14
+ ## Installation
15
+
16
+ You can install the Rainbow Swap SDK using either npm or Yarn:
15
17
 
16
- To install the rainbow-swap-sdk, use the following npm command:
18
+ **Using npm:**
17
19
  ```shell
18
20
  npm install rainbow-swap-sdk
19
21
  ```
20
22
 
21
- ### Integrate your dApp
23
+ **Using Yarn:**
24
+ ```shell
25
+ yarn add rainbow-swap-sdk
26
+ ```
27
+
28
+ ## Integrate Your dApp
29
+
30
+ ### Example: Swapping 1.35 TON to USDT
22
31
 
23
32
  ```typescript
24
- import {
25
- getAssetsRecord,
26
- getBestRoute
27
- } from 'rainbow-swap-sdk';
33
+ import {getAssetsRecord, getBestRoute, toNano} from 'rainbow-swap-sdk';
28
34
 
29
35
  // 1. Load the list of available tokens
30
36
  const assetsRecord = await getAssetsRecord();
31
37
 
32
- ...
38
+ const inputAsset = assetsRecord['ton']; // TON asset
39
+ const outputAsset = assetsRecord['EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs']; // USDT asset
40
+
41
+ // 2. Load the best swap route and swap messages
42
+ const bestRouteResponse = await getBestRoute({
43
+ inputAssetAmount: toNano('1.35', inputAsset.decimals).toString(), // Convert 1.35 TON to nano format
44
+ inputAssetAddress: inputAsset.address,
45
+ outputAssetAddress: outputAsset.address,
46
+ senderAddress: 'UQDGGjjuwhikx8ZPJsrLbKXGq7mx26D8pK_l8GqBejzB52Pa', // Optional user wallet address; if set, swap messages will be returned
47
+ partnerId: 'demo-partner' // Optional unique identifier in our App Developer Partnership program
48
+ });
33
49
 
34
- // 2. Load the best swap route & swap messages
35
- const params = {
36
- inputAssetAmount: '1000000000', // 1 TON in nano
37
- inputAssetAddress: 'ton', // TON
38
- outputAssetAddress: 'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs', // USDT jetton master address
39
- maxDepth: 2, // optional number of maximum route length (should be in range from 1 to 3)
40
- userAddress: 'UQDGGjjuwhikx8ZPJsrLbKXGq7mx26D8pK_l8GqBejzB52Pa', // optional user wallet address, if set - swapMessages will return
41
- slippageTolerance: 5 // optional max slippage value (should be in range )
42
- };
50
+ // 3. Sign and send messages to the blockchain to execute the swap.
51
+ // This example uses the React UI client. For other frameworks, refer to https://docs.ton.org/develop/dapps/ton-connect/overview
52
+ import {useTonConnectUI} from '@tonconnect/ui-react';
43
53
 
44
- const bestRouteResponse = await getBestRoute(params);
54
+ const [tonConnectUI] = useTonConnectUI();
45
55
 
46
- const bestRoute = bestRouteResponse.bestRoute; // Route of the best possible swap route
47
- const swapMessages = bestRouteResponse.swapMessages; // Array of messages that should be sent to @tonconnect
56
+ const result = await tonConnectUI.sendTransaction({
57
+ validUntil: Math.floor(Date.now() / 1000) + 60, // 60 seconds from now
58
+ messages: bestRouteResponse.swapMessages
59
+ });
48
60
  ```
49
61
 
50
- ### Application status check
62
+ ## Application Status Check
51
63
 
52
- Additionally, you might want to check if everything is functioning correctly. For instance, you could temporarily disable swaps if block production on TON is disrupted due to the DOGS listing.
64
+ You may want to check the status of your application to ensure everything is functioning correctly. For example, temporarily disable swaps if block production on TON is disrupted due to an external event like the DOGS listing.
53
65
 
54
66
  ```typescript
55
- import { getAppStatus } from 'rainbow-swap-sdk';
67
+ import {getAppStatus} from 'rainbow-swap-sdk';
56
68
 
57
69
  const {
58
- isSwapsEnabled, // true - if everything works fine
59
- message // explanations why swaps are disabled
70
+ isSwapsEnabled, // true if everything is working fine
71
+ message // Explanation of why swaps are disabled, if applicable
60
72
  } = await getAppStatus();
61
73
  ```
62
74
 
63
- ### Live example
75
+ ## Live Example
64
76
 
65
- For a live example of using the SDK, visit [Rainbow Swap 🌈 repository](https://github.com/0xblackbot/rainbow-swap).
77
+ For a live example of using the SDK, visit the [Rainbow Swap 🌈 Repository](https://github.com/0xblackbot/rainbow-swap).
66
78
 
67
- ### Contact
79
+ ## Contact
68
80
 
69
- For questions and suggestions, contact us at [Blackbot](https://blackbot.technology/).
81
+ For questions and suggestions, visit [Community Chat](https://t.me/rainbow_swap_chat).
70
82
 
71
- ### License
83
+ ## License
72
84
 
73
85
  This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
package/dist/index.d.ts CHANGED
@@ -12,4 +12,5 @@ export type { BestRouteParams } from './types/best-route-params.type';
12
12
  export type { BestRouteResponse } from './types/best-route-response.type';
13
13
  export type { CalculatedSwapRoute } from './types/calculated-swap-route.type';
14
14
  export { getAssetsRecord, getBestRoute, getAppStatus } from './utils/api.utils';
15
+ export { toNano, fromNano } from './utils/big-int.utils';
15
16
  export { getQueryId } from './utils/transfer-params.utils';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getQueryId = exports.getAppStatus = exports.getBestRoute = exports.getAssetsRecord = exports.SwapRouteType = exports.RouteDirectionEnum = exports.DexTypeEnum = exports.mapSwapRouteToRoute = void 0;
3
+ exports.getQueryId = exports.fromNano = exports.toNano = exports.getAppStatus = exports.getBestRoute = exports.getAssetsRecord = exports.SwapRouteType = exports.RouteDirectionEnum = exports.DexTypeEnum = exports.mapSwapRouteToRoute = void 0;
4
4
  var calculated_swap_route_utils_1 = require("./dexes/shared/calculated-swap-route.utils");
5
5
  Object.defineProperty(exports, "mapSwapRouteToRoute", { enumerable: true, get: function () { return calculated_swap_route_utils_1.mapSwapRouteToRoute; } });
6
6
  var dex_type_enum_1 = require("./enums/dex-type.enum");
@@ -13,5 +13,8 @@ var api_utils_1 = require("./utils/api.utils");
13
13
  Object.defineProperty(exports, "getAssetsRecord", { enumerable: true, get: function () { return api_utils_1.getAssetsRecord; } });
14
14
  Object.defineProperty(exports, "getBestRoute", { enumerable: true, get: function () { return api_utils_1.getBestRoute; } });
15
15
  Object.defineProperty(exports, "getAppStatus", { enumerable: true, get: function () { return api_utils_1.getAppStatus; } });
16
+ var big_int_utils_1 = require("./utils/big-int.utils");
17
+ Object.defineProperty(exports, "toNano", { enumerable: true, get: function () { return big_int_utils_1.toNano; } });
18
+ Object.defineProperty(exports, "fromNano", { enumerable: true, get: function () { return big_int_utils_1.fromNano; } });
16
19
  var transfer_params_utils_1 = require("./utils/transfer-params.utils");
17
20
  Object.defineProperty(exports, "getQueryId", { enumerable: true, get: function () { return transfer_params_utils_1.getQueryId; } });
@@ -0,0 +1,11 @@
1
+ export type BestRouteDisplayData = {
2
+ inputAssetAmount: number;
3
+ inputAssetUsdAmount: number;
4
+ outputAssetAmount: number;
5
+ outputAssetUsdAmount: number;
6
+ minOutputAssetAmount: number;
7
+ exchangeRate: number;
8
+ maxSlippage: number;
9
+ routingFeePercent: number;
10
+ priceImprovementPercent: number;
11
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,8 +1,60 @@
1
1
  export type BestRouteParams = {
2
+ /**
3
+ * The amount of the input asset that the user wants to send, specified in nano units.
4
+ * This should be a string representation of a bigint value.
5
+ *
6
+ * **Example:** `'1350000000'` (represents 1.35 TON in nano).
7
+ */
2
8
  inputAssetAmount: string;
9
+ /**
10
+ * The address or identifier of the asset that the user wants to send.
11
+ *
12
+ * **Example:**
13
+ * - `'ton'` for TON.
14
+ * - `'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs'` for USDT.
15
+ */
3
16
  inputAssetAddress: string;
17
+ /**
18
+ * The address or identifier of the asset that the user wants to receive.
19
+ *
20
+ * **Example:**
21
+ * - `'ton'` for TON.
22
+ * - `'EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs'` for USDT.
23
+ */
4
24
  outputAssetAddress: string;
25
+ /**
26
+ * (Optional) The maximum length of the route.
27
+ * Should be a number between **1 and 3** (inclusive).
28
+ *
29
+ * **Default:** `2`
30
+ */
5
31
  maxDepth?: number;
32
+ /**
33
+ * (Optional) The wallet address of the user.
34
+ * If not provided, the `swapMessages` will return an empty array (`[]`).
35
+ */
6
36
  senderAddress?: string;
37
+ /**
38
+ * (Optional) The percentage setting for slippage tolerance.
39
+ * Should be a number between **0 and 100** (inclusive).
40
+ *
41
+ * **Example:** `1.5` for 1.5% slippage tolerance.
42
+ *
43
+ * **Default:** `5`
44
+ */
7
45
  maxSlippage?: number;
46
+ /**
47
+ * (Optional) The referral address of a user.
48
+ * Refer a new user and earn a share of their trading fees.
49
+ *
50
+ * For more details, visit the **Rewards Center** at [rainbow.ag](https://rainbow.ag).
51
+ */
52
+ referralAddress?: string;
53
+ /**
54
+ * (Optional) A unique identifier in our App Developer Partnership program.
55
+ * Integrate our SDK to enable in-app swaps, set custom fees, and enjoy a 50% revenue share.
56
+ *
57
+ * For more details, contact us at **Community Chat**: [https://t.me/rainbow_swap_chat](https://t.me/rainbow_swap_chat).
58
+ */
59
+ partnerId?: string;
8
60
  };
@@ -1,17 +1,22 @@
1
+ import { BestRouteDisplayData } from './best-route-display-data.type';
1
2
  import { CalculatedSwapRoute } from './calculated-swap-route.type';
2
3
  import { Message } from '../interfaces/message.interface';
3
4
  export type BestRouteResponse = {
4
- bestRoute: CalculatedSwapRoute[];
5
- displayData: {
6
- inputAssetAmount: number;
7
- inputAssetUsdAmount: number;
8
- outputAssetAmount: number;
9
- outputAssetUsdAmount: number;
10
- minOutputAssetAmount: number;
11
- exchangeRate: number;
12
- maxSlippage: number;
13
- routingFeePercent: number;
14
- priceImprovementPercent: number;
15
- };
5
+ /**
6
+ * A user-friendly representation of the best route details.
7
+ */
8
+ displayData: BestRouteDisplayData;
9
+ /**
10
+ * An array of messages that should be signed and sent to the blockchain to execute the swap.
11
+ *
12
+ * - For TON dApps, it is recommended to use `@tonconnect/sdk`.
13
+ * - If the `senderAddress` parameter in the request was not set, an empty array (`[]`) will be returned.
14
+ * - An empty array in other cases indicates that no route was found between the input asset and the output asset.
15
+ */
16
16
  swapMessages: Message[];
17
+ /**
18
+ * Detailed information about the most efficient routes for swapping the user's input asset,
19
+ * taking into account swap distribution and gas costs.
20
+ */
21
+ bestRoute: CalculatedSwapRoute[];
17
22
  };
@@ -0,0 +1,2 @@
1
+ export declare const toNano: (src: string, decimals: number) => bigint;
2
+ export declare const fromNano: (value: bigint, decimals: number) => string;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromNano = exports.toNano = void 0;
4
+ const toNano = (src, decimals) => {
5
+ const precision = 10n ** BigInt(decimals);
6
+ // Check sign
7
+ let neg = false;
8
+ while (src.startsWith('-')) {
9
+ neg = !neg;
10
+ src = src.slice(1);
11
+ }
12
+ // Split string
13
+ if (src === '.') {
14
+ throw Error('Invalid number');
15
+ }
16
+ const parts = src.split('.');
17
+ if (parts.length > 2) {
18
+ throw Error('Invalid number');
19
+ }
20
+ // Prepare parts
21
+ let whole = parts[0];
22
+ let frac = parts[1];
23
+ if (!whole) {
24
+ whole = '0';
25
+ }
26
+ if (!frac) {
27
+ frac = '';
28
+ }
29
+ if (frac.length > decimals) {
30
+ throw Error('Invalid number');
31
+ }
32
+ while (frac.length < decimals) {
33
+ frac += '0';
34
+ }
35
+ // Convert
36
+ let r = BigInt(whole) * precision + BigInt(frac);
37
+ if (neg) {
38
+ r = -r;
39
+ }
40
+ return r;
41
+ };
42
+ exports.toNano = toNano;
43
+ const fromNano = (value, decimals) => String(Number(value) / 10 ** decimals);
44
+ exports.fromNano = fromNano;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rainbow-swap-sdk",
3
- "version": "1.4.4",
4
- "description": "SDK for building applications on top of Rainbow Swap - Swap Aggregator on TON 💎.",
3
+ "version": "1.4.5",
4
+ "description": "SDK for building applications on top of Rainbow.ag - Swap Aggregator on TON 💎.",
5
5
  "repository": "https://github.com/0xblackbot/rainbow-swap-sdk.git",
6
6
  "license": "Apache-2.0",
7
7
  "main": "dist/index.js",