@cetusprotocol/aggregator-sdk 0.0.2 → 0.0.4
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/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +23 -11
- package/dist/index.mjs +23 -11
- package/dist/src/client.d.ts +2 -1
- package/dist/src/utils/coin.d.ts +0 -2
- package/package.json +2 -2
- package/src/client.ts +17 -4
- package/src/math.ts +14 -8
- package/src/transaction/common.ts +9 -9
- package/src/transaction/router.ts +0 -1
- package/src/transaction/swap.ts +1 -0
- package/src/utils/coin.ts +0 -21
- package/tests/math.test.ts +13 -0
- package/tests/router.test.ts +18 -28
- package/src/utils/coin.spec.ts +0 -10
- /package/dist/{src/utils/coin.spec.d.ts → tests/math.test.d.ts} +0 -0
package/dist/index.d.mts
CHANGED
|
@@ -114,6 +114,7 @@ type RouterData = {
|
|
|
114
114
|
amountIn: BN;
|
|
115
115
|
amountOut: BN;
|
|
116
116
|
routes: Router[];
|
|
117
|
+
insufficientLiquidity: boolean;
|
|
117
118
|
};
|
|
118
119
|
type AggregatorResponse = {
|
|
119
120
|
code: number;
|
|
@@ -165,7 +166,7 @@ declare class AggregatorClient {
|
|
|
165
166
|
swapInPools(params: SwapInPoolsParams): Promise<SwapInPoolsResult | null>;
|
|
166
167
|
routerSwap(params: BuildRouterSwapParams): Promise<Transaction>;
|
|
167
168
|
signAndExecuteTransaction(txb: Transaction, signer: Signer): Promise<_mysten_sui_client.SuiTransactionBlockResponse>;
|
|
168
|
-
devInspectTransactionBlock(txb: Transaction
|
|
169
|
+
devInspectTransactionBlock(txb: Transaction): Promise<_mysten_sui_client.DevInspectResults>;
|
|
169
170
|
sendTransaction(txb: Transaction, signer: Signer): Promise<_mysten_sui_client.SuiTransactionBlockResponse>;
|
|
170
171
|
}
|
|
171
172
|
|
package/dist/index.d.ts
CHANGED
|
@@ -114,6 +114,7 @@ type RouterData = {
|
|
|
114
114
|
amountIn: BN;
|
|
115
115
|
amountOut: BN;
|
|
116
116
|
routes: Router[];
|
|
117
|
+
insufficientLiquidity: boolean;
|
|
117
118
|
};
|
|
118
119
|
type AggregatorResponse = {
|
|
119
120
|
code: number;
|
|
@@ -165,7 +166,7 @@ declare class AggregatorClient {
|
|
|
165
166
|
swapInPools(params: SwapInPoolsParams): Promise<SwapInPoolsResult | null>;
|
|
166
167
|
routerSwap(params: BuildRouterSwapParams): Promise<Transaction>;
|
|
167
168
|
signAndExecuteTransaction(txb: Transaction, signer: Signer): Promise<_mysten_sui_client.SuiTransactionBlockResponse>;
|
|
168
|
-
devInspectTransactionBlock(txb: Transaction
|
|
169
|
+
devInspectTransactionBlock(txb: Transaction): Promise<_mysten_sui_client.DevInspectResults>;
|
|
169
170
|
sendTransaction(txb: Transaction, signer: Signer): Promise<_mysten_sui_client.SuiTransactionBlockResponse>;
|
|
170
171
|
}
|
|
171
172
|
|
package/dist/index.js
CHANGED
|
@@ -5347,7 +5347,14 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
|
|
|
5347
5347
|
targetCoinAmount: 0
|
|
5348
5348
|
};
|
|
5349
5349
|
}
|
|
5350
|
-
|
|
5350
|
+
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
|
|
5351
|
+
if (totalCoinBalance < amount) {
|
|
5352
|
+
throw new AggregateError(
|
|
5353
|
+
"Insufficient balance when build merge coin",
|
|
5354
|
+
"InsufficientBalance" /* InsufficientBalance */
|
|
5355
|
+
);
|
|
5356
|
+
}
|
|
5357
|
+
if (CoinUtils.isSuiCoin(coinType) && amount <= BigInt(95e10)) {
|
|
5351
5358
|
const resultCoin = txb.splitCoins(txb.gas, [
|
|
5352
5359
|
txb.pure.u64(amount.toString())
|
|
5353
5360
|
]);
|
|
@@ -5357,13 +5364,6 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
|
|
|
5357
5364
|
targetCoinAmount: Number(amount.toString())
|
|
5358
5365
|
};
|
|
5359
5366
|
}
|
|
5360
|
-
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
|
|
5361
|
-
if (totalCoinBalance < amount) {
|
|
5362
|
-
throw new AggregateError(
|
|
5363
|
-
"Insufficient balance when build merge coin",
|
|
5364
|
-
"InsufficientBalance" /* InsufficientBalance */
|
|
5365
|
-
);
|
|
5366
|
-
}
|
|
5367
5367
|
let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
|
|
5368
5368
|
let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
|
|
5369
5369
|
if (totalThreeCoinBalance < BigInt(amount)) {
|
|
@@ -6459,7 +6459,8 @@ function swapInPools(client, params, config2) {
|
|
|
6459
6459
|
amountOut: new import_bn3.BN((_e = event.amount_out) != null ? _e : 0),
|
|
6460
6460
|
initialPrice
|
|
6461
6461
|
}
|
|
6462
|
-
]
|
|
6462
|
+
],
|
|
6463
|
+
insufficientLiquidity: false
|
|
6463
6464
|
};
|
|
6464
6465
|
const result = {
|
|
6465
6466
|
isExceed: event.is_exceed,
|
|
@@ -6540,6 +6541,14 @@ var AggregatorClient = class {
|
|
|
6540
6541
|
const res = parseRouterResponse(data.data);
|
|
6541
6542
|
return res;
|
|
6542
6543
|
}
|
|
6544
|
+
if (data.msg === "liquidity is not enough") {
|
|
6545
|
+
return {
|
|
6546
|
+
amountIn: ZERO,
|
|
6547
|
+
amountOut: ZERO,
|
|
6548
|
+
routes: [],
|
|
6549
|
+
insufficientLiquidity: true
|
|
6550
|
+
};
|
|
6551
|
+
}
|
|
6543
6552
|
return null;
|
|
6544
6553
|
});
|
|
6545
6554
|
}
|
|
@@ -6549,6 +6558,7 @@ var AggregatorClient = class {
|
|
|
6549
6558
|
try {
|
|
6550
6559
|
result = yield swapInPools(this.client, params, this.config);
|
|
6551
6560
|
} catch (e) {
|
|
6561
|
+
console.error("swapInPools error:", e);
|
|
6552
6562
|
return null;
|
|
6553
6563
|
}
|
|
6554
6564
|
return result;
|
|
@@ -6664,11 +6674,12 @@ var AggregatorClient = class {
|
|
|
6664
6674
|
return res;
|
|
6665
6675
|
});
|
|
6666
6676
|
}
|
|
6667
|
-
devInspectTransactionBlock(txb
|
|
6677
|
+
devInspectTransactionBlock(txb) {
|
|
6668
6678
|
return __async(this, null, function* () {
|
|
6679
|
+
console.log(this.wallet);
|
|
6669
6680
|
const res = yield this.client.devInspectTransactionBlock({
|
|
6670
6681
|
transactionBlock: txb,
|
|
6671
|
-
sender:
|
|
6682
|
+
sender: this.wallet
|
|
6672
6683
|
});
|
|
6673
6684
|
return res;
|
|
6674
6685
|
});
|
|
@@ -6687,6 +6698,7 @@ function parseRouterResponse(data) {
|
|
|
6687
6698
|
return {
|
|
6688
6699
|
amountIn: new import_bn4.default(data.amount_in.toString()),
|
|
6689
6700
|
amountOut: new import_bn4.default(data.amount_out.toString()),
|
|
6701
|
+
insufficientLiquidity: false,
|
|
6690
6702
|
routes: data.routes.map((route) => {
|
|
6691
6703
|
return {
|
|
6692
6704
|
path: route.path.map((path) => {
|
package/dist/index.mjs
CHANGED
|
@@ -5345,7 +5345,14 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
|
|
|
5345
5345
|
targetCoinAmount: 0
|
|
5346
5346
|
};
|
|
5347
5347
|
}
|
|
5348
|
-
|
|
5348
|
+
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
|
|
5349
|
+
if (totalCoinBalance < amount) {
|
|
5350
|
+
throw new AggregateError(
|
|
5351
|
+
"Insufficient balance when build merge coin",
|
|
5352
|
+
"InsufficientBalance" /* InsufficientBalance */
|
|
5353
|
+
);
|
|
5354
|
+
}
|
|
5355
|
+
if (CoinUtils.isSuiCoin(coinType) && amount <= BigInt(95e10)) {
|
|
5349
5356
|
const resultCoin = txb.splitCoins(txb.gas, [
|
|
5350
5357
|
txb.pure.u64(amount.toString())
|
|
5351
5358
|
]);
|
|
@@ -5355,13 +5362,6 @@ function buildInputCoin(txb, allCoins, amount, coinType) {
|
|
|
5355
5362
|
targetCoinAmount: Number(amount.toString())
|
|
5356
5363
|
};
|
|
5357
5364
|
}
|
|
5358
|
-
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests);
|
|
5359
|
-
if (totalCoinBalance < amount) {
|
|
5360
|
-
throw new AggregateError(
|
|
5361
|
-
"Insufficient balance when build merge coin",
|
|
5362
|
-
"InsufficientBalance" /* InsufficientBalance */
|
|
5363
|
-
);
|
|
5364
|
-
}
|
|
5365
5365
|
let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests);
|
|
5366
5366
|
let totalThreeCoinBalance = sortCoinAssets.slice(0, 3).reduce((acc, coin) => acc + coin.balance, BigInt(0));
|
|
5367
5367
|
if (totalThreeCoinBalance < BigInt(amount)) {
|
|
@@ -6457,7 +6457,8 @@ function swapInPools(client, params, config2) {
|
|
|
6457
6457
|
amountOut: new import_bn3.BN((_e = event.amount_out) != null ? _e : 0),
|
|
6458
6458
|
initialPrice
|
|
6459
6459
|
}
|
|
6460
|
-
]
|
|
6460
|
+
],
|
|
6461
|
+
insufficientLiquidity: false
|
|
6461
6462
|
};
|
|
6462
6463
|
const result = {
|
|
6463
6464
|
isExceed: event.is_exceed,
|
|
@@ -6538,6 +6539,14 @@ var AggregatorClient = class {
|
|
|
6538
6539
|
const res = parseRouterResponse(data.data);
|
|
6539
6540
|
return res;
|
|
6540
6541
|
}
|
|
6542
|
+
if (data.msg === "liquidity is not enough") {
|
|
6543
|
+
return {
|
|
6544
|
+
amountIn: ZERO,
|
|
6545
|
+
amountOut: ZERO,
|
|
6546
|
+
routes: [],
|
|
6547
|
+
insufficientLiquidity: true
|
|
6548
|
+
};
|
|
6549
|
+
}
|
|
6541
6550
|
return null;
|
|
6542
6551
|
});
|
|
6543
6552
|
}
|
|
@@ -6547,6 +6556,7 @@ var AggregatorClient = class {
|
|
|
6547
6556
|
try {
|
|
6548
6557
|
result = yield swapInPools(this.client, params, this.config);
|
|
6549
6558
|
} catch (e) {
|
|
6559
|
+
console.error("swapInPools error:", e);
|
|
6550
6560
|
return null;
|
|
6551
6561
|
}
|
|
6552
6562
|
return result;
|
|
@@ -6662,11 +6672,12 @@ var AggregatorClient = class {
|
|
|
6662
6672
|
return res;
|
|
6663
6673
|
});
|
|
6664
6674
|
}
|
|
6665
|
-
devInspectTransactionBlock(txb
|
|
6675
|
+
devInspectTransactionBlock(txb) {
|
|
6666
6676
|
return __async(this, null, function* () {
|
|
6677
|
+
console.log(this.wallet);
|
|
6667
6678
|
const res = yield this.client.devInspectTransactionBlock({
|
|
6668
6679
|
transactionBlock: txb,
|
|
6669
|
-
sender:
|
|
6680
|
+
sender: this.wallet
|
|
6670
6681
|
});
|
|
6671
6682
|
return res;
|
|
6672
6683
|
});
|
|
@@ -6685,6 +6696,7 @@ function parseRouterResponse(data) {
|
|
|
6685
6696
|
return {
|
|
6686
6697
|
amountIn: new import_bn4.default(data.amount_in.toString()),
|
|
6687
6698
|
amountOut: new import_bn4.default(data.amount_out.toString()),
|
|
6699
|
+
insufficientLiquidity: false,
|
|
6688
6700
|
routes: data.routes.map((route) => {
|
|
6689
6701
|
return {
|
|
6690
6702
|
path: route.path.map((path) => {
|
package/dist/src/client.d.ts
CHANGED
|
@@ -31,6 +31,7 @@ export type RouterData = {
|
|
|
31
31
|
amountIn: BN;
|
|
32
32
|
amountOut: BN;
|
|
33
33
|
routes: Router[];
|
|
34
|
+
insufficientLiquidity: boolean;
|
|
34
35
|
};
|
|
35
36
|
export type AggregatorResponse = {
|
|
36
37
|
code: number;
|
|
@@ -82,6 +83,6 @@ export declare class AggregatorClient {
|
|
|
82
83
|
swapInPools(params: SwapInPoolsParams): Promise<SwapInPoolsResult | null>;
|
|
83
84
|
routerSwap(params: BuildRouterSwapParams): Promise<Transaction>;
|
|
84
85
|
signAndExecuteTransaction(txb: Transaction, signer: Signer): Promise<import("@mysten/sui/client").SuiTransactionBlockResponse>;
|
|
85
|
-
devInspectTransactionBlock(txb: Transaction
|
|
86
|
+
devInspectTransactionBlock(txb: Transaction): Promise<import("@mysten/sui/client").DevInspectResults>;
|
|
86
87
|
sendTransaction(txb: Transaction, signer: Signer): Promise<import("@mysten/sui/client").SuiTransactionBlockResponse>;
|
|
87
88
|
}
|
package/dist/src/utils/coin.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
export declare function completionCoin(s: string): string;
|
|
2
2
|
export declare function compareCoins(coinA: string, coinB: string): boolean;
|
|
3
|
-
export declare function parseTurbosPoolFeeType(typeData: string): string | null;
|
|
4
|
-
export declare function parseAftermathFeeType(typeData: string): string | undefined;
|
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AggregatorConfig } from "./config"
|
|
2
2
|
import Decimal from "decimal.js"
|
|
3
3
|
import { SuiClient } from "@mysten/sui/client"
|
|
4
|
-
import { CoinAsset } from "./types/sui"
|
|
4
|
+
import { CoinAsset, SuiAddress } from "./types/sui"
|
|
5
5
|
import { createTarget, extractStructTagFromType } from "./utils"
|
|
6
6
|
import { Transaction } from "@mysten/sui/transactions"
|
|
7
7
|
import {
|
|
@@ -16,7 +16,7 @@ import BN from "bn.js"
|
|
|
16
16
|
import { swapInPools } from "./transaction/swap"
|
|
17
17
|
import { completionCoin } from "./utils/coin"
|
|
18
18
|
import { SUI_FRAMEWORK_ADDRESS } from "@mysten/sui/utils"
|
|
19
|
-
import { AFTERMATH_AMM, JOIN_FUNC, PAY_MODULE, TURBOS_DEX } from "./const"
|
|
19
|
+
import { AFTERMATH_AMM, JOIN_FUNC, PAY_MODULE, TURBOS_DEX, ZERO } from "./const"
|
|
20
20
|
|
|
21
21
|
export type ExtendedDetails = {
|
|
22
22
|
aftermathPoolFlatness?: number
|
|
@@ -48,6 +48,7 @@ export type RouterData = {
|
|
|
48
48
|
amountIn: BN
|
|
49
49
|
amountOut: BN
|
|
50
50
|
routes: Router[]
|
|
51
|
+
insufficientLiquidity: boolean
|
|
51
52
|
}
|
|
52
53
|
|
|
53
54
|
export type AggregatorResponse = {
|
|
@@ -190,6 +191,14 @@ export class AggregatorClient {
|
|
|
190
191
|
return res
|
|
191
192
|
}
|
|
192
193
|
|
|
194
|
+
if (data.msg === "liquidity is not enough") {
|
|
195
|
+
return {
|
|
196
|
+
amountIn: ZERO,
|
|
197
|
+
amountOut: ZERO,
|
|
198
|
+
routes: [],
|
|
199
|
+
insufficientLiquidity: true,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
193
202
|
return null
|
|
194
203
|
}
|
|
195
204
|
|
|
@@ -200,6 +209,7 @@ export class AggregatorClient {
|
|
|
200
209
|
try {
|
|
201
210
|
result = await swapInPools(this.client, params, this.config)
|
|
202
211
|
} catch (e) {
|
|
212
|
+
console.error("swapInPools error:", e)
|
|
203
213
|
return null
|
|
204
214
|
}
|
|
205
215
|
|
|
@@ -226,6 +236,7 @@ export class AggregatorClient {
|
|
|
226
236
|
)
|
|
227
237
|
|
|
228
238
|
const txb = new Transaction()
|
|
239
|
+
// txb.setGasBudget(42392686100000000)
|
|
229
240
|
|
|
230
241
|
if (refreshAllCoins || this.allCoins.length === 0) {
|
|
231
242
|
this.allCoins = await this.getAllCoins()
|
|
@@ -320,10 +331,11 @@ export class AggregatorClient {
|
|
|
320
331
|
return res
|
|
321
332
|
}
|
|
322
333
|
|
|
323
|
-
async devInspectTransactionBlock(txb: Transaction
|
|
334
|
+
async devInspectTransactionBlock(txb: Transaction) {
|
|
335
|
+
console.log(this.wallet)
|
|
324
336
|
const res = await this.client.devInspectTransactionBlock({
|
|
325
337
|
transactionBlock: txb,
|
|
326
|
-
sender:
|
|
338
|
+
sender: this.wallet,
|
|
327
339
|
})
|
|
328
340
|
|
|
329
341
|
return res
|
|
@@ -343,6 +355,7 @@ function parseRouterResponse(data: any): RouterData {
|
|
|
343
355
|
return {
|
|
344
356
|
amountIn: new BN(data.amount_in.toString()),
|
|
345
357
|
amountOut: new BN(data.amount_out.toString()),
|
|
358
|
+
insufficientLiquidity: false,
|
|
346
359
|
routes: data.routes.map((route: any) => {
|
|
347
360
|
return {
|
|
348
361
|
path: route.path.map((path: any) => {
|
package/src/math.ts
CHANGED
|
@@ -7,7 +7,11 @@ import { ZERO } from "./const"
|
|
|
7
7
|
import Decimal from "decimal.js"
|
|
8
8
|
|
|
9
9
|
// `slippage` is a percentage, for example, 0.01 means 1%.
|
|
10
|
-
export function CalculateAmountLimit(
|
|
10
|
+
export function CalculateAmountLimit(
|
|
11
|
+
expectAmount: BN,
|
|
12
|
+
byAmountIn: boolean,
|
|
13
|
+
slippage: number
|
|
14
|
+
): number {
|
|
11
15
|
let amountLimit = ZERO
|
|
12
16
|
if (byAmountIn) {
|
|
13
17
|
amountLimit = expectAmount.muln(1 - slippage)
|
|
@@ -18,20 +22,22 @@ export function CalculateAmountLimit(expectAmount: BN, byAmountIn: boolean, slip
|
|
|
18
22
|
return Number(amountLimit.toString())
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
const MAX_SQER_PRICE_X64 =
|
|
22
|
-
const MIN_SQER_PRICE_X64 =
|
|
25
|
+
const MAX_SQER_PRICE_X64 = "79226673515401279992447579055"
|
|
26
|
+
const MIN_SQER_PRICE_X64 = "4295048016"
|
|
23
27
|
|
|
24
28
|
export function GetDefaultSqrtPriceLimit(a2b: boolean): BN {
|
|
25
29
|
if (a2b) {
|
|
26
30
|
return new BN(MIN_SQER_PRICE_X64)
|
|
27
31
|
} else {
|
|
28
|
-
|
|
32
|
+
return new BN(MAX_SQER_PRICE_X64)
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
export function sqrtPriceX64ToPrice(
|
|
36
|
+
export function sqrtPriceX64ToPrice(
|
|
37
|
+
sqrtPriceStr: string,
|
|
38
|
+
decimalsA: number,
|
|
39
|
+
decimalsB: number
|
|
40
|
+
): Decimal {
|
|
33
41
|
const sqrtPriceX64 = new Decimal(sqrtPriceStr).mul(Decimal.pow(2, -64))
|
|
34
|
-
return sqrtPriceX64
|
|
35
|
-
.pow(2)
|
|
36
|
-
.mul(Decimal.pow(10, decimalsA - decimalsB))
|
|
42
|
+
return sqrtPriceX64.pow(2).mul(Decimal.pow(10, decimalsA - decimalsB))
|
|
37
43
|
}
|
|
@@ -51,7 +51,15 @@ export function buildInputCoin(
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests)
|
|
55
|
+
if (totalCoinBalance < amount) {
|
|
56
|
+
throw new AggregateError(
|
|
57
|
+
"Insufficient balance when build merge coin",
|
|
58
|
+
TransactionErrorCode.InsufficientBalance
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (CoinUtils.isSuiCoin(coinType) && amount <= BigInt(950000000000)) {
|
|
55
63
|
const resultCoin = txb.splitCoins(txb.gas, [
|
|
56
64
|
txb.pure.u64(amount.toString()),
|
|
57
65
|
])
|
|
@@ -62,14 +70,6 @@ export function buildInputCoin(
|
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
|
|
65
|
-
let totalCoinBalance = CoinUtils.calculateTotalBalance(usedCoinAsests)
|
|
66
|
-
if (totalCoinBalance < amount) {
|
|
67
|
-
throw new AggregateError(
|
|
68
|
-
"Insufficient balance when build merge coin",
|
|
69
|
-
TransactionErrorCode.InsufficientBalance
|
|
70
|
-
)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
73
|
// sort used coin by amount, asc
|
|
74
74
|
let sortCoinAssets = CoinUtils.sortByBalance(usedCoinAsests)
|
|
75
75
|
|
|
@@ -26,7 +26,6 @@ import { transferOrDestoryCoin } from "./common"
|
|
|
26
26
|
import { GetDefaultSqrtPriceLimit } from "../math"
|
|
27
27
|
import { flowxAmmSwapMovecall } from "./flowx"
|
|
28
28
|
import { turbosSwapMovecall } from "./turbos"
|
|
29
|
-
import { parseAftermathFeeType, parseTurbosPoolFeeType } from "~/utils/coin"
|
|
30
29
|
import { AftermathAmmSwapMovecall } from "./aftermath"
|
|
31
30
|
import { TransactionErrorCode } from "~/errors"
|
|
32
31
|
|
package/src/transaction/swap.ts
CHANGED
package/src/utils/coin.ts
CHANGED
|
@@ -38,24 +38,3 @@ export function compareCoins(coinA: string, coinB: string): boolean {
|
|
|
38
38
|
// If both strings are the same length and all characters are equal
|
|
39
39
|
return true // or coinB, they are equal
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
export function parseTurbosPoolFeeType(typeData: string) {
|
|
43
|
-
// "0x91bfbc386a41afcfd9b2533058d7e915a1d3829089cc268ff4333d54d6339ca1::pool::Pool<0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07::icorgi::ICORGI, 0x2::sui::SUI, 0x91bfbc386a41afcfd9b2533058d7e915a1d3829089cc268ff4333d54d6339ca1::fee10000bps::FEE10000BPS>"
|
|
44
|
-
const regex = /,([^,>]*>)/g
|
|
45
|
-
const matches = [...typeData.matchAll(regex)]
|
|
46
|
-
if (matches.length > 0) {
|
|
47
|
-
const lastMatch = matches[matches.length - 1][1]
|
|
48
|
-
return lastMatch.substring(0, lastMatch.length - 1).trim()
|
|
49
|
-
}
|
|
50
|
-
return null
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function parseAftermathFeeType(typeData: string) {
|
|
54
|
-
// 0xefe170ec0be4d762196bedecd7a065816576198a6527c99282a2551aaa7da38c::pool::Pool<0xf66c5ba62888cd0694677bbfbd2332d08ead3b8a4332c40006c474e83b1a6786::af_lp::AF_LP>
|
|
55
|
-
// get 0xf66c5ba62888cd0694677bbfbd2332d08ead3b8a4332c40006c474e83b1a6786::af_lp::AF_LP
|
|
56
|
-
const regex = /<([^>]*)>/
|
|
57
|
-
const matches = typeData.match(regex)
|
|
58
|
-
if (matches) {
|
|
59
|
-
return matches[1]
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
|
|
2
|
+
import { sqrtPriceX64ToPrice } from "~/math"
|
|
3
|
+
|
|
4
|
+
describe("test math functions", () => {
|
|
5
|
+
test("test sqrt price x64 to price", () => {
|
|
6
|
+
const sqrtPriceStr = "1312674575678912631"
|
|
7
|
+
const decimalsA = 9
|
|
8
|
+
const decimalsB = 6
|
|
9
|
+
|
|
10
|
+
const price = sqrtPriceX64ToPrice(sqrtPriceStr, decimalsA, decimalsB)
|
|
11
|
+
console.log("price", price.toFixed(9))
|
|
12
|
+
})
|
|
13
|
+
})
|
package/tests/router.test.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
|
|
|
15
15
|
import { printTransaction } from "~/utils/transaction"
|
|
16
16
|
import BN from "bn.js"
|
|
17
17
|
import { fromHEX } from "@mysten/bcs"
|
|
18
|
+
import { fromB64 } from "@mysten/sui/utils"
|
|
18
19
|
|
|
19
20
|
dotenv.config()
|
|
20
21
|
|
|
@@ -35,16 +36,17 @@ describe("router module", () => {
|
|
|
35
36
|
const secret = process.env.SUI_WALLET_SECRET!
|
|
36
37
|
|
|
37
38
|
// const byte = Buffer.from(secret, "hex")
|
|
38
|
-
const byte = Buffer.from(secret, "base64")
|
|
39
|
-
const u8Array = new Uint8Array(byte)
|
|
40
39
|
// keypair = secret
|
|
41
40
|
// ? Ed25519Keypair.fromSecretKey(u8Array.slice(1, 33))
|
|
42
41
|
// : buildTestAccount()
|
|
42
|
+
const byte = Buffer.from(secret, "base64")
|
|
43
|
+
const u8Array = new Uint8Array(byte)
|
|
43
44
|
|
|
44
|
-
keypair = Ed25519Keypair.fromSecretKey(
|
|
45
|
+
keypair = Ed25519Keypair.fromSecretKey(fromB64(secret).slice(1, 33))
|
|
45
46
|
|
|
46
47
|
const wallet = keypair.getPublicKey().toSuiAddress()
|
|
47
48
|
console.log("wallet", wallet)
|
|
49
|
+
|
|
48
50
|
// const wallet =
|
|
49
51
|
// "0x1d30e55c730f92a02a33dbdf6b052cd178e5d924aa58c5e2350a24852250ae58"
|
|
50
52
|
// const wallet = "0xaabf2fedcb36146db164bec930b74a47969c4df98216e049342a3c49b6d11580"
|
|
@@ -93,12 +95,12 @@ describe("router module", () => {
|
|
|
93
95
|
const amount = 1000000
|
|
94
96
|
|
|
95
97
|
const res = await client.swapInPools({
|
|
96
|
-
from:
|
|
98
|
+
from: M_USDC,
|
|
97
99
|
target: M_SUI,
|
|
98
100
|
amount: new BN(amount),
|
|
99
|
-
byAmountIn:
|
|
101
|
+
byAmountIn: true,
|
|
100
102
|
pools: [
|
|
101
|
-
"
|
|
103
|
+
"0xcf994611fd4c48e277ce3ffd4d4364c914af2c3cbb05f7bf6facd371de688630",
|
|
102
104
|
],
|
|
103
105
|
})
|
|
104
106
|
|
|
@@ -108,26 +110,18 @@ describe("router module", () => {
|
|
|
108
110
|
})
|
|
109
111
|
|
|
110
112
|
test("Find router", async () => {
|
|
111
|
-
const amount = "
|
|
113
|
+
const amount = "42392676100000000"
|
|
112
114
|
|
|
113
115
|
const res = await client.findRouter({
|
|
114
116
|
from: M_SUI,
|
|
115
117
|
target: M_USDC,
|
|
116
118
|
amount: new BN(amount),
|
|
117
119
|
byAmountIn: true,
|
|
118
|
-
depth:
|
|
120
|
+
depth: 3,
|
|
119
121
|
splitAlgorithm: null,
|
|
120
122
|
splitFactor: null,
|
|
121
123
|
splitCount: 1,
|
|
122
|
-
providers: [
|
|
123
|
-
"AFTERMATH",
|
|
124
|
-
"CETUS",
|
|
125
|
-
"DEEPBOOK",
|
|
126
|
-
"KRIYA",
|
|
127
|
-
"FLOWX",
|
|
128
|
-
"AFTERMATH",
|
|
129
|
-
"TRUBOS",
|
|
130
|
-
],
|
|
124
|
+
providers: ["CETUS"],
|
|
131
125
|
})
|
|
132
126
|
|
|
133
127
|
if (res != null) {
|
|
@@ -140,10 +134,10 @@ describe("router module", () => {
|
|
|
140
134
|
|
|
141
135
|
test("Build router tx", async () => {
|
|
142
136
|
const byAmountIn = true
|
|
143
|
-
const amount =
|
|
137
|
+
const amount = "42392676100000000"
|
|
144
138
|
|
|
145
139
|
const from = M_SUI
|
|
146
|
-
const target =
|
|
140
|
+
const target = M_USDC
|
|
147
141
|
|
|
148
142
|
const res = await client.findRouter({
|
|
149
143
|
from,
|
|
@@ -154,7 +148,7 @@ describe("router module", () => {
|
|
|
154
148
|
splitAlgorithm: null,
|
|
155
149
|
splitFactor: null,
|
|
156
150
|
splitCount: null,
|
|
157
|
-
providers: ["CETUS"
|
|
151
|
+
providers: ["CETUS"],
|
|
158
152
|
})
|
|
159
153
|
|
|
160
154
|
if (res != null) {
|
|
@@ -175,12 +169,11 @@ describe("router module", () => {
|
|
|
175
169
|
fromCoinType: from,
|
|
176
170
|
targetCoinType: target,
|
|
177
171
|
partner: undefined,
|
|
178
|
-
isMergeTragetCoin:
|
|
172
|
+
isMergeTragetCoin: false,
|
|
173
|
+
refreshAllCoins: true,
|
|
179
174
|
})
|
|
180
175
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
let result = await client.devInspectTransactionBlock(routerTx, keypair)
|
|
176
|
+
let result = await client.devInspectTransactionBlock(routerTx)
|
|
184
177
|
|
|
185
178
|
if (result.effects.status.status === "success") {
|
|
186
179
|
console.log("Sim exec transaction success")
|
|
@@ -233,10 +226,7 @@ describe("router module", () => {
|
|
|
233
226
|
isMergeTragetCoin: false,
|
|
234
227
|
})
|
|
235
228
|
|
|
236
|
-
let result = await client.devInspectTransactionBlock(
|
|
237
|
-
routerTx,
|
|
238
|
-
keypair
|
|
239
|
-
)
|
|
229
|
+
let result = await client.devInspectTransactionBlock(routerTx)
|
|
240
230
|
// console.log('result', result)
|
|
241
231
|
|
|
242
232
|
if (result.effects.status.status === "success") {
|
package/src/utils/coin.spec.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { parseTurbosPoolFeeType } from "./coin"
|
|
2
|
-
|
|
3
|
-
describe("Coin Utils", () => {
|
|
4
|
-
it("should fetch token infos by URL and return data", async () => {
|
|
5
|
-
const typeDate =
|
|
6
|
-
"0x91bfbc386a41afcfd9b2533058d7e915a1d3829089cc268ff4333d54d6339ca1::pool::Pool<0xc91acfb75009c5ff2fd57c54f3caaee12ad1fbe997681334adc0b574fc277a07::icorgi::ICORGI, 0x2::sui::SUI, 0x91bfbc386a41afcfd9b2533058d7e915a1d3829089cc268ff4333d54d6339ca1::fee10000bps::FEE10000BPS>"
|
|
7
|
-
const result = parseTurbosPoolFeeType(typeDate)
|
|
8
|
-
console.log("parse turbos pool type", result)
|
|
9
|
-
})
|
|
10
|
-
})
|
|
File without changes
|