@7kprotocol/sdk-ts 1.0.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/LICENSE +21 -0
- package/README.md +122 -0
- package/lib/cjs/buildTx.js +93 -0
- package/lib/cjs/constants/explorer.js +6 -0
- package/lib/cjs/constants/tokens.js +5 -0
- package/lib/cjs/estimateGasFee.js +52 -0
- package/lib/cjs/getQuote.js +22 -0
- package/lib/cjs/getSuiPrice.js +31 -0
- package/lib/cjs/index.js +37 -0
- package/lib/cjs/libs/getCoinOjectIdsByAmount.js +74 -0
- package/lib/cjs/libs/getSplitCoinForTx.js +55 -0
- package/lib/cjs/libs/groupSwapRoutes.js +62 -0
- package/lib/cjs/libs/protocols/aftermath/index.js +52 -0
- package/lib/cjs/libs/protocols/base.js +20 -0
- package/lib/cjs/libs/protocols/bluemove/index.js +36 -0
- package/lib/cjs/libs/protocols/cetus/index.js +56 -0
- package/lib/cjs/libs/protocols/constants.js +5 -0
- package/lib/cjs/libs/protocols/deepbook/index.js +96 -0
- package/lib/cjs/libs/protocols/flowx/index.js +31 -0
- package/lib/cjs/libs/protocols/index.js +21 -0
- package/lib/cjs/libs/protocols/kriya/index.js +42 -0
- package/lib/cjs/libs/protocols/suiswap/index.js +45 -0
- package/lib/cjs/libs/protocols/turbos/constants.js +7 -0
- package/lib/cjs/libs/protocols/turbos/index.js +53 -0
- package/lib/cjs/libs/protocols/utils.js +11 -0
- package/lib/cjs/libs/swapWithRoute.js +31 -0
- package/lib/cjs/suiClient.js +14 -0
- package/lib/cjs/types/aggregator.js +2 -0
- package/lib/cjs/types/buildTx.d.ts +4 -0
- package/lib/cjs/types/buildTx.d.ts.map +1 -0
- package/lib/cjs/types/constants/explorer.d.ts +4 -0
- package/lib/cjs/types/constants/explorer.d.ts.map +1 -0
- package/lib/cjs/types/constants/tokens.d.ts +3 -0
- package/lib/cjs/types/constants/tokens.d.ts.map +1 -0
- package/lib/cjs/types/estimateGasFee.d.ts +3 -0
- package/lib/cjs/types/estimateGasFee.d.ts.map +1 -0
- package/lib/cjs/types/getQuote.d.ts +9 -0
- package/lib/cjs/types/getQuote.d.ts.map +1 -0
- package/lib/cjs/types/getSuiPrice.d.ts +2 -0
- package/lib/cjs/types/getSuiPrice.d.ts.map +1 -0
- package/lib/cjs/types/index.d.ts +17 -0
- package/lib/cjs/types/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts +7 -0
- package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts.map +1 -0
- package/lib/cjs/types/libs/getSplitCoinForTx.d.ts +6 -0
- package/lib/cjs/types/libs/getSplitCoinForTx.d.ts.map +1 -0
- package/lib/cjs/types/libs/groupSwapRoutes.d.ts +3 -0
- package/lib/cjs/types/libs/groupSwapRoutes.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/aftermath/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/aftermath/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/base.d.ts +37 -0
- package/lib/cjs/types/libs/protocols/base.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/bluemove/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/bluemove/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/cetus/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/cetus/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/constants.d.ts +3 -0
- package/lib/cjs/types/libs/protocols/constants.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/deepbook/index.d.ts +12 -0
- package/lib/cjs/types/libs/protocols/deepbook/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/flowx/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/flowx/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/index.d.ts +19 -0
- package/lib/cjs/types/libs/protocols/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/kriya/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/kriya/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/suiswap/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/suiswap/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/turbos/constants.d.ts +5 -0
- package/lib/cjs/types/libs/protocols/turbos/constants.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/turbos/index.d.ts +10 -0
- package/lib/cjs/types/libs/protocols/turbos/index.d.ts.map +1 -0
- package/lib/cjs/types/libs/protocols/utils.d.ts +3 -0
- package/lib/cjs/types/libs/protocols/utils.d.ts.map +1 -0
- package/lib/cjs/types/libs/swapWithRoute.d.ts +10 -0
- package/lib/cjs/types/libs/swapWithRoute.d.ts.map +1 -0
- package/lib/cjs/types/sui.js +2 -0
- package/lib/cjs/types/suiClient.d.ts +4 -0
- package/lib/cjs/types/suiClient.d.ts.map +1 -0
- package/lib/cjs/types/token.js +2 -0
- package/lib/cjs/types/tx.js +2 -0
- package/lib/cjs/types/types/aggregator.d.ts +69 -0
- package/lib/cjs/types/types/aggregator.d.ts.map +1 -0
- package/lib/cjs/types/types/sui.d.ts +4 -0
- package/lib/cjs/types/types/sui.d.ts.map +1 -0
- package/lib/cjs/types/types/token.d.ts +30 -0
- package/lib/cjs/types/types/token.d.ts.map +1 -0
- package/lib/cjs/types/types/tx.d.ts +14 -0
- package/lib/cjs/types/types/tx.d.ts.map +1 -0
- package/lib/cjs/types/types/utilities.d.ts +5 -0
- package/lib/cjs/types/types/utilities.d.ts.map +1 -0
- package/lib/cjs/types/utilities.js +2 -0
- package/lib/cjs/types/utils/number.d.ts +4 -0
- package/lib/cjs/types/utils/number.d.ts.map +1 -0
- package/lib/cjs/types/utils/sui.d.ts +39 -0
- package/lib/cjs/types/utils/sui.d.ts.map +1 -0
- package/lib/cjs/types/utils/token.d.ts +6 -0
- package/lib/cjs/types/utils/token.d.ts.map +1 -0
- package/lib/cjs/utils/number.js +15 -0
- package/lib/cjs/utils/sui.js +180 -0
- package/lib/cjs/utils/token.js +36 -0
- package/lib/esm/buildTx.js +77 -0
- package/lib/esm/constants/explorer.js +3 -0
- package/lib/esm/constants/tokens.js +2 -0
- package/lib/esm/estimateGasFee.js +35 -0
- package/lib/esm/getQuote.js +8 -0
- package/lib/esm/getSuiPrice.js +13 -0
- package/lib/esm/index.mjs +15 -0
- package/lib/esm/libs/getCoinOjectIdsByAmount.js +61 -0
- package/lib/esm/libs/getSplitCoinForTx.js +39 -0
- package/lib/esm/libs/groupSwapRoutes.js +58 -0
- package/lib/esm/libs/protocols/aftermath/index.js +37 -0
- package/lib/esm/libs/protocols/base.js +18 -0
- package/lib/esm/libs/protocols/bluemove/index.js +21 -0
- package/lib/esm/libs/protocols/cetus/index.js +41 -0
- package/lib/esm/libs/protocols/constants.js +2 -0
- package/lib/esm/libs/protocols/deepbook/index.js +80 -0
- package/lib/esm/libs/protocols/flowx/index.js +16 -0
- package/lib/esm/libs/protocols/index.js +18 -0
- package/lib/esm/libs/protocols/kriya/index.js +27 -0
- package/lib/esm/libs/protocols/suiswap/index.js +30 -0
- package/lib/esm/libs/protocols/turbos/constants.js +4 -0
- package/lib/esm/libs/protocols/turbos/index.js +38 -0
- package/lib/esm/libs/protocols/utils.js +5 -0
- package/lib/esm/libs/swapWithRoute.js +17 -0
- package/lib/esm/suiClient.js +10 -0
- package/lib/esm/types/aggregator.js +1 -0
- package/lib/esm/types/buildTx.d.ts +4 -0
- package/lib/esm/types/buildTx.d.ts.map +1 -0
- package/lib/esm/types/constants/explorer.d.ts +4 -0
- package/lib/esm/types/constants/explorer.d.ts.map +1 -0
- package/lib/esm/types/constants/tokens.d.ts +3 -0
- package/lib/esm/types/constants/tokens.d.ts.map +1 -0
- package/lib/esm/types/estimateGasFee.d.ts +3 -0
- package/lib/esm/types/estimateGasFee.d.ts.map +1 -0
- package/lib/esm/types/getQuote.d.ts +9 -0
- package/lib/esm/types/getQuote.d.ts.map +1 -0
- package/lib/esm/types/getSuiPrice.d.ts +2 -0
- package/lib/esm/types/getSuiPrice.d.ts.map +1 -0
- package/lib/esm/types/index.d.ts +17 -0
- package/lib/esm/types/index.d.ts.map +1 -0
- package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts +7 -0
- package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts.map +1 -0
- package/lib/esm/types/libs/getSplitCoinForTx.d.ts +6 -0
- package/lib/esm/types/libs/getSplitCoinForTx.d.ts.map +1 -0
- package/lib/esm/types/libs/groupSwapRoutes.d.ts +3 -0
- package/lib/esm/types/libs/groupSwapRoutes.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/aftermath/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/aftermath/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/base.d.ts +37 -0
- package/lib/esm/types/libs/protocols/base.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/bluemove/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/bluemove/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/cetus/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/cetus/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/constants.d.ts +3 -0
- package/lib/esm/types/libs/protocols/constants.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/deepbook/index.d.ts +12 -0
- package/lib/esm/types/libs/protocols/deepbook/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/flowx/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/flowx/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/index.d.ts +19 -0
- package/lib/esm/types/libs/protocols/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/kriya/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/kriya/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/suiswap/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/suiswap/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/turbos/constants.d.ts +5 -0
- package/lib/esm/types/libs/protocols/turbos/constants.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/turbos/index.d.ts +10 -0
- package/lib/esm/types/libs/protocols/turbos/index.d.ts.map +1 -0
- package/lib/esm/types/libs/protocols/utils.d.ts +3 -0
- package/lib/esm/types/libs/protocols/utils.d.ts.map +1 -0
- package/lib/esm/types/libs/swapWithRoute.d.ts +10 -0
- package/lib/esm/types/libs/swapWithRoute.d.ts.map +1 -0
- package/lib/esm/types/sui.js +1 -0
- package/lib/esm/types/suiClient.d.ts +4 -0
- package/lib/esm/types/suiClient.d.ts.map +1 -0
- package/lib/esm/types/token.js +1 -0
- package/lib/esm/types/tx.js +1 -0
- package/lib/esm/types/types/aggregator.d.ts +69 -0
- package/lib/esm/types/types/aggregator.d.ts.map +1 -0
- package/lib/esm/types/types/sui.d.ts +4 -0
- package/lib/esm/types/types/sui.d.ts.map +1 -0
- package/lib/esm/types/types/token.d.ts +30 -0
- package/lib/esm/types/types/token.d.ts.map +1 -0
- package/lib/esm/types/types/tx.d.ts +14 -0
- package/lib/esm/types/types/tx.d.ts.map +1 -0
- package/lib/esm/types/types/utilities.d.ts +5 -0
- package/lib/esm/types/types/utilities.d.ts.map +1 -0
- package/lib/esm/types/utilities.js +1 -0
- package/lib/esm/types/utils/number.d.ts +4 -0
- package/lib/esm/types/utils/number.d.ts.map +1 -0
- package/lib/esm/types/utils/sui.d.ts +39 -0
- package/lib/esm/types/utils/sui.d.ts.map +1 -0
- package/lib/esm/types/utils/token.d.ts +6 -0
- package/lib/esm/types/utils/token.d.ts.map +1 -0
- package/lib/esm/utils/number.js +8 -0
- package/lib/esm/utils/sui.js +167 -0
- package/lib/esm/utils/token.js +19 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 7k Protocol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# 7K TypeScript SDK
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i @7kprotocol/sdk-ts
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### 1. Set Sui Client (Optional)
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
|
|
15
|
+
import { setSuiClient } from "@7kprotocol/sdk-ts";
|
|
16
|
+
|
|
17
|
+
const network = "mainnet";
|
|
18
|
+
const suiClient = new SuiClient({ url: getFullnodeUrl(network) });
|
|
19
|
+
setSuiClient(suiClient);
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Note: this package only supports **mainnet** for now.
|
|
23
|
+
|
|
24
|
+
### 2. Get Quote
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { getQuote } from "@7kprotocol/sdk-ts";
|
|
28
|
+
|
|
29
|
+
const quoteResponse = await getQuote({
|
|
30
|
+
tokenIn: "0x2::sui::SUI",
|
|
31
|
+
tokenOut:
|
|
32
|
+
"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
|
|
33
|
+
amountIn: "1000000000",
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 3. Build Transaction
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { buildTx } from "@7kprotocol/sdk-ts";
|
|
41
|
+
|
|
42
|
+
const tx = await buildTx({
|
|
43
|
+
quoteResponse,
|
|
44
|
+
accountAddress: "0xSenderAddress",
|
|
45
|
+
slippage: 0.01, // 1%
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
or
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { TransactionBlock } from "@mysten/sui.js/transactions";
|
|
53
|
+
import { buildTx } from "@7kprotocol/sdk-ts";
|
|
54
|
+
|
|
55
|
+
const tx = new TransactionBlock();
|
|
56
|
+
|
|
57
|
+
await buildTx({
|
|
58
|
+
tx,
|
|
59
|
+
quoteResponse,
|
|
60
|
+
accountAddress: "0xSenderAddress",
|
|
61
|
+
slippage: 0.01, // 1%
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Full Example
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { setSuiClient, getQuote, buildTx } from "@7kprotocol/sdk-ts";
|
|
69
|
+
|
|
70
|
+
// optional
|
|
71
|
+
const network = "mainnet";
|
|
72
|
+
const suiClient = new SuiClient({ url: getFullnodeUrl(network) });
|
|
73
|
+
setSuiClient(suiClient);
|
|
74
|
+
|
|
75
|
+
const quoteResponse = await getQuote({
|
|
76
|
+
tokenIn: "0x2::sui::SUI",
|
|
77
|
+
tokenOut:
|
|
78
|
+
"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
|
|
79
|
+
amountIn: "1000000000",
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const tx = await buildTx({
|
|
83
|
+
quoteResponse,
|
|
84
|
+
accountAddress: "0xSenderAddress",
|
|
85
|
+
slippage: 0.01, // 1%
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
console.log(tx);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Estimate Gas Fee
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { estimateGasFee } from "@7kprotocol/sdk-ts";
|
|
95
|
+
|
|
96
|
+
const feeInUsd = await estimateGasFee({
|
|
97
|
+
quoteResponse,
|
|
98
|
+
accountAddress: "0xSenderAddress",
|
|
99
|
+
slippage: 0.01, // 1%
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
or
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { getSuiPrice, estimateGasFee } from "@7kprotocol/sdk-ts";
|
|
107
|
+
|
|
108
|
+
// get sui price using sdk or from else where
|
|
109
|
+
const suiPrice = await getSuiPrice();
|
|
110
|
+
|
|
111
|
+
const feeInUsd = await estimateGasFee({
|
|
112
|
+
quoteResponse,
|
|
113
|
+
accountAddress: "0xSenderAddress",
|
|
114
|
+
slippage: 0.01, // 1%
|
|
115
|
+
suiPrice,
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
7K TypeScript SDK released under the MIT license. See the [LICENSE](./LICENSE)
|
|
122
|
+
file for details.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.buildTx = void 0;
|
|
16
|
+
const transactions_1 = require("@mysten/sui.js/transactions");
|
|
17
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
18
|
+
const getSplitCoinForTx_1 = require("./libs/getSplitCoinForTx");
|
|
19
|
+
const groupSwapRoutes_1 = require("./libs/groupSwapRoutes");
|
|
20
|
+
const swapWithRoute_1 = require("./libs/swapWithRoute");
|
|
21
|
+
const token_1 = require("./utils/token");
|
|
22
|
+
const sui_1 = require("./utils/sui");
|
|
23
|
+
const _7K_PACKAGE_ID = "0xd48e7cdc9e92bec69ce3baa75578010458a0c5b2733d661a84971e8cef6806bc";
|
|
24
|
+
const _7K_CONFIG = "0xc31272d90e0c36181b0153776c9d7c1516d4c1eca50fc2eb01f36d3f457ea90d";
|
|
25
|
+
const buildTx = (_a) => __awaiter(void 0, [_a], void 0, function* ({ quoteResponse, accountAddress, slippage, tx: _tx, commission: _commission, }) {
|
|
26
|
+
if (!accountAddress || !quoteResponse.routes)
|
|
27
|
+
return;
|
|
28
|
+
const tx = _tx || new transactions_1.TransactionBlock();
|
|
29
|
+
const routes = (0, groupSwapRoutes_1.groupSwapRoutes)(quoteResponse);
|
|
30
|
+
const splits = routes.map((group) => { var _a, _b; return (_b = (_a = group[0]) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : "0"; });
|
|
31
|
+
const { coinData } = yield (0, getSplitCoinForTx_1.getSplitCoinForTx)(accountAddress, quoteResponse.swapAmountWithDecimal, splits, (0, token_1.denormalizeTokenType)(quoteResponse.tokenIn), tx);
|
|
32
|
+
const coinObjects = [];
|
|
33
|
+
yield Promise.all(routes.map((route, index) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const inputCoinObject = coinData[index];
|
|
35
|
+
const coinRes = yield (0, swapWithRoute_1.swapWithRoute)({
|
|
36
|
+
route,
|
|
37
|
+
inputCoinObject,
|
|
38
|
+
currentAccount: accountAddress,
|
|
39
|
+
tx,
|
|
40
|
+
});
|
|
41
|
+
if (coinRes) {
|
|
42
|
+
coinObjects.push(coinRes);
|
|
43
|
+
}
|
|
44
|
+
})));
|
|
45
|
+
if (coinObjects.length > 0) {
|
|
46
|
+
const mergeCoin = coinObjects.length > 1
|
|
47
|
+
? sui_1.SuiUtils.mergeCoins(coinObjects, tx)
|
|
48
|
+
: coinObjects[0];
|
|
49
|
+
const minReceived = new bignumber_js_1.default(1)
|
|
50
|
+
.minus(slippage)
|
|
51
|
+
.multipliedBy(quoteResponse.returnAmountWithDecimal)
|
|
52
|
+
.toFixed(0);
|
|
53
|
+
const commission = getCommission(tx, _commission);
|
|
54
|
+
tx.moveCall({
|
|
55
|
+
target: `${_7K_PACKAGE_ID}::settle::settle`,
|
|
56
|
+
typeArguments: [quoteResponse.tokenIn, quoteResponse.tokenOut],
|
|
57
|
+
arguments: [
|
|
58
|
+
tx.object(_7K_CONFIG),
|
|
59
|
+
tx.pure.u64(quoteResponse.swapAmountWithDecimal),
|
|
60
|
+
mergeCoin,
|
|
61
|
+
tx.pure.u64(minReceived),
|
|
62
|
+
tx.pure.u64(quoteResponse.returnAmountWithDecimal),
|
|
63
|
+
commission,
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
tx.transferObjects([mergeCoin], tx.pure.address(accountAddress));
|
|
67
|
+
}
|
|
68
|
+
return tx;
|
|
69
|
+
});
|
|
70
|
+
exports.buildTx = buildTx;
|
|
71
|
+
const getCommission = (tx, commission) => {
|
|
72
|
+
if (commission) {
|
|
73
|
+
const [commissionInner] = tx.moveCall({
|
|
74
|
+
target: `${_7K_PACKAGE_ID}::commission::new`,
|
|
75
|
+
typeArguments: [],
|
|
76
|
+
arguments: [
|
|
77
|
+
tx.pure.address(commission.partner),
|
|
78
|
+
tx.pure.u64(commission.commissionBps),
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
const [result] = tx.moveCall({
|
|
82
|
+
target: "0x1::option::some",
|
|
83
|
+
typeArguments: [`${_7K_PACKAGE_ID}::commission::Commission`],
|
|
84
|
+
arguments: [commissionInner],
|
|
85
|
+
});
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
return tx.moveCall({
|
|
89
|
+
target: "0x1::option::none",
|
|
90
|
+
typeArguments: [`${_7K_PACKAGE_ID}::commission::Commission`],
|
|
91
|
+
arguments: [],
|
|
92
|
+
})[0];
|
|
93
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SUI_FULL_TYPE = exports.SUI_TYPE = void 0;
|
|
4
|
+
exports.SUI_TYPE = "0x2::sui::SUI";
|
|
5
|
+
exports.SUI_FULL_TYPE = "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.estimateGasFee = estimateGasFee;
|
|
16
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
|
+
const utils_1 = require("@mysten/sui.js/utils");
|
|
18
|
+
const buildTx_1 = require("./buildTx");
|
|
19
|
+
const number_1 = require("./utils/number");
|
|
20
|
+
const suiClient_1 = require("./suiClient");
|
|
21
|
+
const getSuiPrice_1 = require("./getSuiPrice");
|
|
22
|
+
function estimateGasFee(_a) {
|
|
23
|
+
return __awaiter(this, arguments, void 0, function* ({ quoteResponse, accountAddress, slippage, suiPrice: _suiPrice, tx: _tx, commission, }) {
|
|
24
|
+
if (!accountAddress)
|
|
25
|
+
return 0;
|
|
26
|
+
const tx = yield (0, buildTx_1.buildTx)({
|
|
27
|
+
tx: _tx,
|
|
28
|
+
quoteResponse,
|
|
29
|
+
accountAddress,
|
|
30
|
+
slippage,
|
|
31
|
+
commission,
|
|
32
|
+
}).catch((err) => {
|
|
33
|
+
console.log("build tx error: ", err);
|
|
34
|
+
return undefined;
|
|
35
|
+
});
|
|
36
|
+
if (!tx)
|
|
37
|
+
return 0;
|
|
38
|
+
const suiPrice = _suiPrice || (yield (0, getSuiPrice_1.getSuiPrice)());
|
|
39
|
+
const suiDecimals = utils_1.SUI_DECIMALS;
|
|
40
|
+
const { effects: { gasUsed, status }, } = yield (0, suiClient_1.getSuiClient)().devInspectTransactionBlock({
|
|
41
|
+
sender: accountAddress,
|
|
42
|
+
transactionBlock: tx,
|
|
43
|
+
});
|
|
44
|
+
if (status.status !== "success")
|
|
45
|
+
return 0;
|
|
46
|
+
const fee = new bignumber_js_1.default(gasUsed.computationCost)
|
|
47
|
+
.plus(gasUsed.storageCost)
|
|
48
|
+
.minus(gasUsed.storageRebate);
|
|
49
|
+
const feeUsd = new bignumber_js_1.default(suiPrice).multipliedBy((0, number_1.formatBalance)(fee, suiDecimals));
|
|
50
|
+
return feeUsd.toNumber();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getQuote = getQuote;
|
|
13
|
+
const token_1 = require("./utils/token");
|
|
14
|
+
function getQuote(_a) {
|
|
15
|
+
return __awaiter(this, arguments, void 0, function* ({ tokenIn, tokenOut, amountIn, }) {
|
|
16
|
+
const response = yield fetch(`https://api.7k.ag/quote?amount=${amountIn}&from=${(0, token_1.normalizeTokenType)(tokenIn)}&to=${(0, token_1.normalizeTokenType)(tokenOut)}`);
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new Error("Failed to fetch aggregator sor");
|
|
19
|
+
}
|
|
20
|
+
return response.json();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getSuiPrice = getSuiPrice;
|
|
16
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
|
+
const token_1 = require("./utils/token");
|
|
18
|
+
const tokens_1 = require("./constants/tokens");
|
|
19
|
+
function getSuiPrice() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
var _a;
|
|
22
|
+
try {
|
|
23
|
+
const suiToken = yield (0, token_1.getSuiscanTokenMetadata)(tokens_1.SUI_TYPE);
|
|
24
|
+
const suiPrice = (_a = suiToken === null || suiToken === void 0 ? void 0 : suiToken.tokenPrice) !== null && _a !== void 0 ? _a : "0";
|
|
25
|
+
return new bignumber_js_1.default(suiPrice).toNumber();
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.buildTx = exports.estimateGasFee = exports.getSuiPrice = exports.getQuote = exports.setSuiClient = exports.getSuiClient = void 0;
|
|
18
|
+
__exportStar(require("./types/aggregator"), exports);
|
|
19
|
+
const suiClient_1 = require("./suiClient");
|
|
20
|
+
Object.defineProperty(exports, "getSuiClient", { enumerable: true, get: function () { return suiClient_1.getSuiClient; } });
|
|
21
|
+
Object.defineProperty(exports, "setSuiClient", { enumerable: true, get: function () { return suiClient_1.setSuiClient; } });
|
|
22
|
+
const getQuote_1 = require("./getQuote");
|
|
23
|
+
Object.defineProperty(exports, "getQuote", { enumerable: true, get: function () { return getQuote_1.getQuote; } });
|
|
24
|
+
const getSuiPrice_1 = require("./getSuiPrice");
|
|
25
|
+
Object.defineProperty(exports, "getSuiPrice", { enumerable: true, get: function () { return getSuiPrice_1.getSuiPrice; } });
|
|
26
|
+
const estimateGasFee_1 = require("./estimateGasFee");
|
|
27
|
+
Object.defineProperty(exports, "estimateGasFee", { enumerable: true, get: function () { return estimateGasFee_1.estimateGasFee; } });
|
|
28
|
+
const buildTx_1 = require("./buildTx");
|
|
29
|
+
Object.defineProperty(exports, "buildTx", { enumerable: true, get: function () { return buildTx_1.buildTx; } });
|
|
30
|
+
exports.default = {
|
|
31
|
+
getSuiClient: suiClient_1.getSuiClient,
|
|
32
|
+
setSuiClient: suiClient_1.setSuiClient,
|
|
33
|
+
getQuote: getQuote_1.getQuote,
|
|
34
|
+
getSuiPrice: getSuiPrice_1.getSuiPrice,
|
|
35
|
+
estimateGasFee: estimateGasFee_1.estimateGasFee,
|
|
36
|
+
buildTx: buildTx_1.buildTx,
|
|
37
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getCoinOjectIdsByAmount = void 0;
|
|
16
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
|
+
const suiClient_1 = require("../suiClient");
|
|
18
|
+
const orderByKey = (array, key, sortBy) => {
|
|
19
|
+
if (!(array === null || array === void 0 ? void 0 : array.length)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let swapped;
|
|
23
|
+
const compareFunctionName = sortBy === "desc" ? "isLessThan" : "isGreaterThan";
|
|
24
|
+
do {
|
|
25
|
+
swapped = false;
|
|
26
|
+
for (let i = 0; i < array.length - 1; i++) {
|
|
27
|
+
if (new bignumber_js_1.default(array[i][key])[compareFunctionName](array[i + 1][key])) {
|
|
28
|
+
const temp = array[i];
|
|
29
|
+
array[i] = array[i + 1];
|
|
30
|
+
array[i + 1] = temp;
|
|
31
|
+
swapped = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} while (swapped);
|
|
35
|
+
return array;
|
|
36
|
+
};
|
|
37
|
+
const getCoinOjectIdsByAmount = (address, amount, coinType) => __awaiter(void 0, void 0, void 0, function* () {
|
|
38
|
+
let coinBalances = [];
|
|
39
|
+
let hasNextPage = true;
|
|
40
|
+
let nextCursor = undefined;
|
|
41
|
+
while (hasNextPage) {
|
|
42
|
+
try {
|
|
43
|
+
const coins = yield (0, suiClient_1.getSuiClient)().getCoins({
|
|
44
|
+
owner: address,
|
|
45
|
+
coinType,
|
|
46
|
+
cursor: nextCursor,
|
|
47
|
+
});
|
|
48
|
+
coinBalances = [...coinBalances, ...coins.data];
|
|
49
|
+
hasNextPage = coins.hasNextPage;
|
|
50
|
+
nextCursor = coins.nextCursor;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error("Error fetching data:", error);
|
|
54
|
+
hasNextPage = false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// sort coin balance before get object id
|
|
58
|
+
const coinObj = orderByKey(coinBalances.map((item) => {
|
|
59
|
+
return Object.assign(Object.assign({}, item), { balance: item.balance });
|
|
60
|
+
}), "balance", "desc");
|
|
61
|
+
let balance = "0";
|
|
62
|
+
const objectIds = [];
|
|
63
|
+
const objectCoins = [];
|
|
64
|
+
for (const coin of coinObj !== null && coinObj !== void 0 ? coinObj : []) {
|
|
65
|
+
balance = new bignumber_js_1.default(coin.balance).plus(balance).toFixed();
|
|
66
|
+
objectIds.push(coin.coinObjectId);
|
|
67
|
+
objectCoins.push(coin);
|
|
68
|
+
if (new bignumber_js_1.default(balance).isGreaterThanOrEqualTo(amount)) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { objectIds, balance, objectCoins };
|
|
73
|
+
});
|
|
74
|
+
exports.getCoinOjectIdsByAmount = getCoinOjectIdsByAmount;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getSplitCoinForTx = void 0;
|
|
16
|
+
const transactions_1 = require("@mysten/sui.js/transactions");
|
|
17
|
+
const getCoinOjectIdsByAmount_1 = require("./getCoinOjectIdsByAmount");
|
|
18
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
19
|
+
const tokens_1 = require("../constants/tokens");
|
|
20
|
+
const getSplitCoinForTx = (account, amount, splits, coinType, inheritTx, inspecTransaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
const tx = inheritTx !== null && inheritTx !== void 0 ? inheritTx : new transactions_1.TransactionBlock();
|
|
22
|
+
const { objectIds } = yield (0, getCoinOjectIdsByAmount_1.getCoinOjectIdsByAmount)(account, amount, coinType);
|
|
23
|
+
const coinObjectId = objectIds[0];
|
|
24
|
+
// TODO: find out why the tx.gas is limited at 950 sui
|
|
25
|
+
if (coinType === tokens_1.SUI_TYPE &&
|
|
26
|
+
new bignumber_js_1.default(amount).lt(new bignumber_js_1.default("950e9"))) {
|
|
27
|
+
const pureAmount = [];
|
|
28
|
+
for (const split of splits) {
|
|
29
|
+
pureAmount.push(tx.pure(split));
|
|
30
|
+
}
|
|
31
|
+
let coin;
|
|
32
|
+
if (inspecTransaction) {
|
|
33
|
+
if (objectIds.length > 1) {
|
|
34
|
+
tx.mergeCoins(tx.object(coinObjectId), objectIds.slice(1).map((item) => tx.object(item)));
|
|
35
|
+
}
|
|
36
|
+
coin = tx.splitCoins(tx.object(coinObjectId), pureAmount);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
coin = tx.splitCoins(tx.gas, pureAmount);
|
|
40
|
+
}
|
|
41
|
+
return { tx, coinData: coin };
|
|
42
|
+
}
|
|
43
|
+
if (objectIds.length > 1) {
|
|
44
|
+
tx.mergeCoins(tx.object(coinObjectId), objectIds.slice(1).map((item) => tx.object(item)));
|
|
45
|
+
}
|
|
46
|
+
//handle split coin
|
|
47
|
+
const pureAmount = [];
|
|
48
|
+
for (const split of splits) {
|
|
49
|
+
pureAmount.push(tx.pure(split));
|
|
50
|
+
}
|
|
51
|
+
// split correct amount to swap
|
|
52
|
+
const coinData = tx.splitCoins(tx.object(coinObjectId), pureAmount);
|
|
53
|
+
return { tx, coinData };
|
|
54
|
+
});
|
|
55
|
+
exports.getSplitCoinForTx = getSplitCoinForTx;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.groupSwapRoutes = groupSwapRoutes;
|
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
const token_1 = require("../utils/token");
|
|
9
|
+
function groupSwapRoutes(quoteResponse) {
|
|
10
|
+
if (!quoteResponse.routes || !quoteResponse.swaps) {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
const poolDetails = mapPoolIdsToDetails(quoteResponse.routes);
|
|
14
|
+
const items = getTxSorSwaps(quoteResponse.swaps, poolDetails);
|
|
15
|
+
const groupedItems = [];
|
|
16
|
+
let currentGroup = [];
|
|
17
|
+
for (let i = 0; i < items.length; i++) {
|
|
18
|
+
const item = items[i];
|
|
19
|
+
currentGroup.push(item);
|
|
20
|
+
const nextItem = items[i + 1];
|
|
21
|
+
if (!nextItem || new bignumber_js_1.default(nextItem.amount).gt(0)) {
|
|
22
|
+
groupedItems.push(currentGroup);
|
|
23
|
+
currentGroup = [];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (currentGroup.length > 0) {
|
|
27
|
+
groupedItems.push(currentGroup);
|
|
28
|
+
}
|
|
29
|
+
return groupedItems;
|
|
30
|
+
}
|
|
31
|
+
function mapPoolIdsToDetails(routes) {
|
|
32
|
+
const poolTypes = {};
|
|
33
|
+
routes.forEach((route) => {
|
|
34
|
+
route.hops.forEach((hop) => {
|
|
35
|
+
poolTypes[hop.poolId] = hop.pool;
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
return poolTypes;
|
|
39
|
+
}
|
|
40
|
+
function getTxSorSwaps(swaps, poolDetails) {
|
|
41
|
+
return swaps.map((swap) => {
|
|
42
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
43
|
+
const pool = poolDetails[swap.poolId];
|
|
44
|
+
const assetIn = (0, token_1.denormalizeTokenType)(swap.assetIn);
|
|
45
|
+
const assetOut = (0, token_1.denormalizeTokenType)(swap.assetOut);
|
|
46
|
+
const coinX = {
|
|
47
|
+
type: (0, token_1.denormalizeTokenType)((_b = (_a = pool === null || pool === void 0 ? void 0 : pool.allTokens) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.address),
|
|
48
|
+
decimals: (_d = (_c = pool === null || pool === void 0 ? void 0 : pool.allTokens) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.decimal,
|
|
49
|
+
};
|
|
50
|
+
const coinY = {
|
|
51
|
+
type: (0, token_1.denormalizeTokenType)((_f = (_e = pool === null || pool === void 0 ? void 0 : pool.allTokens) === null || _e === void 0 ? void 0 : _e[1]) === null || _f === void 0 ? void 0 : _f.address),
|
|
52
|
+
decimals: (_h = (_g = pool === null || pool === void 0 ? void 0 : pool.allTokens) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.decimal,
|
|
53
|
+
};
|
|
54
|
+
const swapXtoY = assetIn === coinX.type;
|
|
55
|
+
return Object.assign(Object.assign({}, swap), { pool,
|
|
56
|
+
assetIn,
|
|
57
|
+
assetOut,
|
|
58
|
+
coinX,
|
|
59
|
+
coinY,
|
|
60
|
+
swapXtoY });
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AfterMathContract = void 0;
|
|
13
|
+
const base_1 = require("../base");
|
|
14
|
+
const token_1 = require("../../../utils/token");
|
|
15
|
+
const PACKAGE_ID = "0xc4049b2d1cc0f6e017fda8260e4377cecd236bd7f56a54fee120816e72e2e0dd";
|
|
16
|
+
const MODULE_NAME = "swap";
|
|
17
|
+
const POOL_REGISTRY = "0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae";
|
|
18
|
+
const PROTOCOL_FEE_VAULT = "0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4";
|
|
19
|
+
const TREASURY = "0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce";
|
|
20
|
+
const INSURANCE_FUND = "0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b";
|
|
21
|
+
const REFERRAL_VAULT = "0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278";
|
|
22
|
+
class AfterMathContract extends base_1.BaseContract {
|
|
23
|
+
swap(tx) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const poolId = this.swapInfo.poolId;
|
|
26
|
+
const returnAmount = this.swapInfo.returnAmount;
|
|
27
|
+
const coinInType = (0, token_1.normalizeTokenType)(this.swapInfo.assetIn);
|
|
28
|
+
const coinOutType = (0, token_1.normalizeTokenType)(this.swapInfo.assetOut);
|
|
29
|
+
const inputCoinObject = this.inputCoinObject;
|
|
30
|
+
const [lpCoinType] = this.getTypeParams();
|
|
31
|
+
if (!lpCoinType)
|
|
32
|
+
throw new Error("lpCoinType is not defined");
|
|
33
|
+
const [tokenOut] = tx.moveCall({
|
|
34
|
+
target: `${PACKAGE_ID}::${MODULE_NAME}::swap_exact_in`,
|
|
35
|
+
typeArguments: [lpCoinType, coinInType, coinOutType],
|
|
36
|
+
arguments: [
|
|
37
|
+
tx.object(poolId),
|
|
38
|
+
tx.object(POOL_REGISTRY),
|
|
39
|
+
tx.object(PROTOCOL_FEE_VAULT),
|
|
40
|
+
tx.object(TREASURY),
|
|
41
|
+
tx.object(INSURANCE_FUND),
|
|
42
|
+
tx.object(REFERRAL_VAULT),
|
|
43
|
+
inputCoinObject,
|
|
44
|
+
tx.pure(returnAmount),
|
|
45
|
+
tx.pure.u64("1000000000000000000"), // slippage
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
return tokenOut;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.AfterMathContract = AfterMathContract;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseContract = void 0;
|
|
4
|
+
const utils_1 = require("@mysten/sui.js/utils");
|
|
5
|
+
const sui_1 = require("../../utils/sui");
|
|
6
|
+
class BaseContract {
|
|
7
|
+
constructor({ swapInfo, inputCoinObject, currentAccount, }) {
|
|
8
|
+
this.swapInfo = swapInfo;
|
|
9
|
+
this.inputCoinObject = inputCoinObject;
|
|
10
|
+
this.currentAccount = currentAccount;
|
|
11
|
+
}
|
|
12
|
+
getInputCoinValue(tx) {
|
|
13
|
+
return sui_1.SuiUtils.getCoinValue(this.swapInfo.assetIn, this.inputCoinObject, tx);
|
|
14
|
+
}
|
|
15
|
+
getTypeParams() {
|
|
16
|
+
var _a;
|
|
17
|
+
return (0, utils_1.parseStructTag)(((_a = this.swapInfo.extra) === null || _a === void 0 ? void 0 : _a.poolStructTag) || "").typeParams.map(utils_1.normalizeStructTag);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.BaseContract = BaseContract;
|