rainbow-swap-sdk 1.3.1 → 1.3.2
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.
|
@@ -1,17 +1,4 @@
|
|
|
1
1
|
import { Address, Cell } from '@ton/core';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
minOutputAmount: bigint;
|
|
6
|
-
refundAddress: Address;
|
|
7
|
-
excessesAddress?: Address;
|
|
8
|
-
dexCustomPayload?: Cell;
|
|
9
|
-
dexCustomPayloadForwardGasAmount?: bigint;
|
|
10
|
-
refundPayload?: Cell;
|
|
11
|
-
refundForwardGasAmount?: bigint;
|
|
12
|
-
referralAddress: Address | null;
|
|
13
|
-
referralValue: bigint;
|
|
14
|
-
deadline?: number;
|
|
15
|
-
}
|
|
16
|
-
export declare const packSwapParams: (params: SwapParams) => Cell;
|
|
17
|
-
export {};
|
|
2
|
+
import { RouteStepWithCalculation } from '../../interfaces/route-step-with-calculation.interface';
|
|
3
|
+
export declare const getRouterAddress: (routeStep: RouteStepWithCalculation) => Address;
|
|
4
|
+
export declare const packSwapParams: (remainingRoute: RouteStepWithCalculation[], routeStepGasAmount: bigint, receiverAddress: Address, responseDestination: Address, referralAddress: Address, referralValue: bigint, slippageTolerance: number) => Promise<Cell | undefined>;
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.packSwapParams = void 0;
|
|
3
|
+
exports.packSwapParams = exports.getRouterAddress = void 0;
|
|
4
|
+
const shared_1 = require("@rnw-community/shared");
|
|
4
5
|
const core_1 = require("@ton/core");
|
|
6
|
+
const sdk_1 = require("./sdk");
|
|
7
|
+
const dex_type_enum_1 = require("../../enums/dex-type.enum");
|
|
8
|
+
const globals_1 = require("../../globals");
|
|
9
|
+
const jetton_utils_1 = require("../../utils/jetton.utils");
|
|
10
|
+
const slippage_tolerance_utils_1 = require("../shared/slippage-tolerance.utils");
|
|
5
11
|
const TX_DEADLINE = 15 * 60; // 15 minutes
|
|
6
12
|
const getDefaultDeadline = () => Math.floor(Date.now() / 1000) + TX_DEADLINE;
|
|
7
13
|
var Operation;
|
|
8
14
|
(function (Operation) {
|
|
9
15
|
Operation[Operation["SWAP"] = 1717886506] = "SWAP";
|
|
10
16
|
})(Operation || (Operation = {}));
|
|
11
|
-
const
|
|
17
|
+
const routeStep_packSwapParams = (params) => {
|
|
12
18
|
if (params.referralValue < 0 || params.referralValue > 100) {
|
|
13
19
|
throw Error(`'referralValue' should be in range [0, 100] BPS`);
|
|
14
20
|
}
|
|
@@ -30,4 +36,51 @@ const packSwapParams = (params) => {
|
|
|
30
36
|
.endCell())
|
|
31
37
|
.endCell();
|
|
32
38
|
};
|
|
39
|
+
const getRouterAddress = (routeStep) => {
|
|
40
|
+
if (routeStep.dexPair.dexType !== dex_type_enum_1.DexTypeEnum.Ston_v2) {
|
|
41
|
+
throw new Error(`Unsupported dexType ${routeStep.dexPair.dexType}, ${dex_type_enum_1.DexTypeEnum.Ston_v2} expected`);
|
|
42
|
+
}
|
|
43
|
+
return core_1.Address.parse(routeStep.dexPair.routerAddress);
|
|
44
|
+
};
|
|
45
|
+
exports.getRouterAddress = getRouterAddress;
|
|
46
|
+
const getOutputJettonWalletAddress = (routeStep) => {
|
|
47
|
+
const routerAddress = (0, exports.getRouterAddress)(routeStep);
|
|
48
|
+
const jettonMasterAddress = routeStep.outputAssetAddress === globals_1.TON
|
|
49
|
+
? sdk_1.PROXY_TON_V2_MASTER_ADDRESS
|
|
50
|
+
: routeStep.outputAssetAddress;
|
|
51
|
+
return (0, jetton_utils_1.getJettonWalletAddress)(jettonMasterAddress, routerAddress);
|
|
52
|
+
};
|
|
53
|
+
const getNextReceiverAddress = async (receiverAddress, nextStep) => {
|
|
54
|
+
if ((0, shared_1.isDefined)(nextStep)) {
|
|
55
|
+
const routerAddress = (0, exports.getRouterAddress)(nextStep);
|
|
56
|
+
if (nextStep.inputAssetAddress === globals_1.TON) {
|
|
57
|
+
return (0, jetton_utils_1.getJettonWalletAddress)(sdk_1.PROXY_TON_V2_MASTER_ADDRESS, routerAddress);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return routerAddress;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return receiverAddress;
|
|
64
|
+
};
|
|
65
|
+
const packSwapParams = async (remainingRoute, routeStepGasAmount, receiverAddress, responseDestination, referralAddress, referralValue, slippageTolerance) => {
|
|
66
|
+
if (remainingRoute.length === 0) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
const routeStep = remainingRoute[0];
|
|
70
|
+
const outputJettonWalletAddress = await getOutputJettonWalletAddress(routeStep);
|
|
71
|
+
const nextReceiverAddress = await getNextReceiverAddress(receiverAddress, remainingRoute[1]);
|
|
72
|
+
const minOutputAmount = (0, slippage_tolerance_utils_1.applySlippageTolerance)(routeStep.outputAssetAmount, slippageTolerance);
|
|
73
|
+
const dexCustomPayload = await (0, exports.packSwapParams)(remainingRoute.slice(1), routeStepGasAmount, receiverAddress, responseDestination, referralAddress, referralValue, slippageTolerance);
|
|
74
|
+
const dexCustomPayloadForwardGasAmount = routeStepGasAmount * BigInt(remainingRoute.length - 1);
|
|
75
|
+
return routeStep_packSwapParams({
|
|
76
|
+
outputJettonWalletAddress,
|
|
77
|
+
receiverAddress: nextReceiverAddress,
|
|
78
|
+
minOutputAmount,
|
|
79
|
+
refundAddress: responseDestination,
|
|
80
|
+
dexCustomPayload,
|
|
81
|
+
dexCustomPayloadForwardGasAmount,
|
|
82
|
+
referralAddress,
|
|
83
|
+
referralValue
|
|
84
|
+
});
|
|
85
|
+
};
|
|
33
86
|
exports.packSwapParams = packSwapParams;
|
|
@@ -1,68 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.stonV2_getTransferParams = void 0;
|
|
4
|
+
const shared_1 = require("@rnw-community/shared");
|
|
4
5
|
const core_1 = require("@ton/core");
|
|
5
6
|
const sdk_1 = require("./sdk");
|
|
6
7
|
const transfer_params_pack_utils_1 = require("./transfer-params-pack.utils");
|
|
7
|
-
const dex_type_enum_1 = require("../../enums/dex-type.enum");
|
|
8
8
|
const globals_1 = require("../../globals");
|
|
9
9
|
const jetton_utils_1 = require("../../utils/jetton.utils");
|
|
10
|
-
const
|
|
10
|
+
const ROUTE_STEP_GAS_AMOUNT = (0, core_1.toNano)('0.30');
|
|
11
11
|
const stonV2_getTransferParams = async (route, queryId, gasAmount, senderAddress, receiverAddress, responseDestination, slippageTolerance) => {
|
|
12
|
-
if (route.length
|
|
13
|
-
throw new Error('
|
|
12
|
+
if (route.length === 0) {
|
|
13
|
+
throw new Error('Empty route');
|
|
14
14
|
}
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const firstRouteStep = route[0];
|
|
16
|
+
const routerAddress = (0, transfer_params_pack_utils_1.getRouterAddress)(firstRouteStep);
|
|
17
|
+
const additionalGasAmount = ROUTE_STEP_GAS_AMOUNT * BigInt(route.length - 1);
|
|
18
|
+
const swapPayload = await (0, transfer_params_pack_utils_1.packSwapParams)(route, ROUTE_STEP_GAS_AMOUNT, receiverAddress, responseDestination, globals_1.REFERRAL_ADDRESS, globals_1.REFERRAL_VALUE, slippageTolerance);
|
|
19
|
+
if (!(0, shared_1.isDefined)(swapPayload)) {
|
|
20
|
+
throw new Error('swapPayload not defined');
|
|
18
21
|
}
|
|
19
|
-
|
|
20
|
-
const minOutputAmount = (0, slippage_tolerance_utils_1.applySlippageTolerance)(routeStep.outputAssetAmount, slippageTolerance);
|
|
21
|
-
if (routeStep.inputAssetAddress === globals_1.TON) {
|
|
22
|
+
if (firstRouteStep.inputAssetAddress === globals_1.TON) {
|
|
22
23
|
const stonRouterProxyTonWalletAddress = await (0, jetton_utils_1.getJettonWalletAddress)(sdk_1.PROXY_TON_V2_MASTER_ADDRESS, routerAddress);
|
|
23
|
-
const stonRouterOutputJettonWalletAddress = await (0, jetton_utils_1.getJettonWalletAddress)(routeStep.outputAssetAddress, routerAddress);
|
|
24
|
-
const tonSwapPayload = (0, transfer_params_pack_utils_1.packSwapParams)({
|
|
25
|
-
outputJettonWalletAddress: stonRouterOutputJettonWalletAddress,
|
|
26
|
-
receiverAddress,
|
|
27
|
-
minOutputAmount,
|
|
28
|
-
refundAddress: responseDestination,
|
|
29
|
-
referralAddress: globals_1.REFERRAL_ADDRESS,
|
|
30
|
-
referralValue: globals_1.REFERRAL_VALUE
|
|
31
|
-
});
|
|
32
24
|
return {
|
|
33
25
|
to: stonRouterProxyTonWalletAddress,
|
|
34
|
-
value: gasAmount +
|
|
26
|
+
value: gasAmount +
|
|
27
|
+
additionalGasAmount +
|
|
28
|
+
BigInt(firstRouteStep.inputAssetAmount),
|
|
35
29
|
body: (0, sdk_1.pTonV2_createTonTransferBody)({
|
|
36
30
|
queryId,
|
|
37
|
-
amount: BigInt(
|
|
31
|
+
amount: BigInt(firstRouteStep.inputAssetAmount),
|
|
38
32
|
refundAddress: responseDestination,
|
|
39
|
-
forwardPayload:
|
|
33
|
+
forwardPayload: swapPayload
|
|
40
34
|
})
|
|
41
35
|
};
|
|
42
36
|
}
|
|
43
37
|
else {
|
|
44
|
-
const inputJettonWalletAddress = await (0, jetton_utils_1.getJettonWalletAddress)(
|
|
45
|
-
const outputJettonWalletAddress = await (0, jetton_utils_1.getJettonWalletAddress)(routeStep.outputAssetAddress === globals_1.TON
|
|
46
|
-
? sdk_1.PROXY_TON_V2_MASTER_ADDRESS
|
|
47
|
-
: routeStep.outputAssetAddress, routerAddress);
|
|
48
|
-
const jettonSwapPayload = (0, transfer_params_pack_utils_1.packSwapParams)({
|
|
49
|
-
outputJettonWalletAddress,
|
|
50
|
-
receiverAddress,
|
|
51
|
-
minOutputAmount,
|
|
52
|
-
refundAddress: responseDestination,
|
|
53
|
-
referralAddress: globals_1.REFERRAL_ADDRESS,
|
|
54
|
-
referralValue: globals_1.REFERRAL_VALUE
|
|
55
|
-
});
|
|
38
|
+
const inputJettonWalletAddress = await (0, jetton_utils_1.getJettonWalletAddress)(firstRouteStep.inputAssetAddress, senderAddress);
|
|
56
39
|
return {
|
|
57
40
|
to: inputJettonWalletAddress,
|
|
58
|
-
value: gasAmount + globals_1.JETTON_TRANSFER_GAS_AMOUNT,
|
|
41
|
+
value: gasAmount + globals_1.JETTON_TRANSFER_GAS_AMOUNT + additionalGasAmount,
|
|
59
42
|
body: (0, jetton_utils_1.getJettonTransferBody)({
|
|
60
43
|
queryId,
|
|
61
|
-
amount: BigInt(
|
|
44
|
+
amount: BigInt(firstRouteStep.inputAssetAmount),
|
|
62
45
|
destination: routerAddress,
|
|
63
46
|
responseDestination: responseDestination,
|
|
64
|
-
forwardTonAmount: gasAmount,
|
|
65
|
-
forwardPayload:
|
|
47
|
+
forwardTonAmount: gasAmount + additionalGasAmount,
|
|
48
|
+
forwardPayload: swapPayload
|
|
66
49
|
})
|
|
67
50
|
};
|
|
68
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rainbow-swap-sdk",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "SDK for building applications on top of Rainbow Swap 🌈 - The Next Gen DEX Aggregator on TON 💎.",
|
|
5
5
|
"repository": "https://github.com/0xblackbot/rainbow-swap-sdk.git",
|
|
6
6
|
"license": "Apache-2.0",
|