@7kprotocol/sdk-ts 3.4.1 → 3.4.2-beta.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/lib/cjs/features/limitDca/placeLimitOrder.js +8 -3
- package/lib/cjs/features/swap/buildTx.js +101 -66
- package/lib/cjs/features/swap/buildTxV2.js +227 -0
- package/lib/cjs/features/swap/index.js +1 -0
- package/lib/cjs/index.js +6 -2
- package/lib/cjs/types/features/limitDca/placeLimitOrder.d.ts +2 -1
- package/lib/cjs/types/features/limitDca/placeLimitOrder.d.ts.map +1 -1
- package/lib/cjs/types/features/swap/buildTx.d.ts +14 -1
- package/lib/cjs/types/features/swap/buildTx.d.ts.map +1 -1
- package/lib/cjs/types/features/swap/buildTxV2.d.ts +20 -0
- package/lib/cjs/types/features/swap/buildTxV2.d.ts.map +1 -0
- package/lib/cjs/types/features/swap/index.d.ts +1 -0
- package/lib/cjs/types/features/swap/index.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +4 -2
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/libs/protocols/base.d.ts +4 -5
- package/lib/cjs/types/libs/protocols/base.d.ts.map +1 -1
- package/lib/cjs/types/libs/protocols/steamm/index.d.ts +3 -12
- package/lib/cjs/types/libs/protocols/steamm/index.d.ts.map +1 -1
- package/lib/cjs/types/libs/swapWithRoute.d.ts +3 -4
- package/lib/cjs/types/libs/swapWithRoute.d.ts.map +1 -1
- package/lib/cjs/types/types/tx.d.ts +13 -0
- package/lib/cjs/types/types/tx.d.ts.map +1 -1
- package/lib/esm/features/limitDca/placeLimitOrder.js +8 -3
- package/lib/esm/features/swap/buildTx.js +95 -66
- package/lib/esm/features/swap/buildTxV2.js +221 -0
- package/lib/esm/features/swap/index.js +1 -0
- package/lib/esm/index.mjs +12 -10
- package/lib/esm/types/features/limitDca/placeLimitOrder.d.ts +2 -1
- package/lib/esm/types/features/limitDca/placeLimitOrder.d.ts.map +1 -1
- package/lib/esm/types/features/swap/buildTx.d.ts +14 -1
- package/lib/esm/types/features/swap/buildTx.d.ts.map +1 -1
- package/lib/esm/types/features/swap/buildTxV2.d.ts +20 -0
- package/lib/esm/types/features/swap/buildTxV2.d.ts.map +1 -0
- package/lib/esm/types/features/swap/index.d.ts +1 -0
- package/lib/esm/types/features/swap/index.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +4 -2
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/libs/protocols/base.d.ts +4 -5
- package/lib/esm/types/libs/protocols/base.d.ts.map +1 -1
- package/lib/esm/types/libs/protocols/steamm/index.d.ts +3 -12
- package/lib/esm/types/libs/protocols/steamm/index.d.ts.map +1 -1
- package/lib/esm/types/libs/swapWithRoute.d.ts +3 -4
- package/lib/esm/types/libs/swapWithRoute.d.ts.map +1 -1
- package/lib/esm/types/types/tx.d.ts +13 -0
- package/lib/esm/types/types/tx.d.ts.map +1 -1
- package/package.json +3 -3
- package/lib/cjs/libs/getCoinOjectIdsByAmount.js +0 -65
- package/lib/cjs/libs/getSplitCoinForTx.js +0 -33
- package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts +0 -7
- package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts.map +0 -1
- package/lib/cjs/types/libs/getSplitCoinForTx.d.ts +0 -6
- package/lib/cjs/types/libs/getSplitCoinForTx.d.ts.map +0 -1
- package/lib/esm/libs/getCoinOjectIdsByAmount.js +0 -61
- package/lib/esm/libs/getSplitCoinForTx.js +0 -29
- package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts +0 -7
- package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts.map +0 -1
- package/lib/esm/types/libs/getSplitCoinForTx.d.ts +0 -6
- package/lib/esm/types/libs/getSplitCoinForTx.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAEpE,OAAO,EACL,OAAO,EACP,cAAc,EACd,SAAS,EACT,QAAQ,EACR,cAAc,EACd,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAEpE,OAAO,EACL,OAAO,EACP,SAAS,EACT,eAAe,EACf,cAAc,EACd,SAAS,EACT,QAAQ,EACR,cAAc,EACd,SAAS,EACV,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,kBAAkB,EAClB,aAAa,EACb,eAAe,EAChB,MAAM,qBAAqB,CAAC;AAG7B,QAAA,MAAM,YAAY,uDAAsB,CAAC;AACzC,QAAA,MAAM,YAAY,mEAAsB,CAAC;AAEzC,OAAO,EACL,OAAO,EACP,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EAEtB,MAAM,EACN,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,SAAS,EACT,kBAAkB,EAClB,oBAAoB,EACpB,qBAAqB,EACrB,gBAAgB,EAChB,kBAAkB,EAElB,QAAQ,EAER,YAAY,EACZ,WAAW,EACX,cAAc,EAEd,aAAa,EACb,cAAc,EACd,SAAS,EAET,aAAa,EAEb,eAAe,EACf,YAAY,GACb,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEF,wBAqCE"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Transaction } from "@mysten/sui/transactions";
|
|
1
|
+
import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
2
2
|
import { Config, ExtraOracle, TxSorSwap } from "../../types/aggregator";
|
|
3
|
-
import { TransactionResultItem } from "../../types/sui";
|
|
4
3
|
export interface BaseContractParams {
|
|
5
4
|
swapInfo: TxSorSwap;
|
|
6
|
-
inputCoinObject:
|
|
5
|
+
inputCoinObject: TransactionObjectArgument;
|
|
7
6
|
currentAccount: string;
|
|
8
7
|
config: Config;
|
|
9
8
|
/** map price feed id to onchain priceInfoObject id */
|
|
@@ -11,12 +10,12 @@ export interface BaseContractParams {
|
|
|
11
10
|
}
|
|
12
11
|
export declare abstract class BaseContract<T = any> {
|
|
13
12
|
protected swapInfo: TxSorSwap;
|
|
14
|
-
protected inputCoinObject:
|
|
13
|
+
protected inputCoinObject: TransactionObjectArgument;
|
|
15
14
|
protected currentAccount: string;
|
|
16
15
|
protected config: Config;
|
|
17
16
|
protected pythMap: Record<string, string>;
|
|
18
17
|
constructor({ swapInfo, inputCoinObject, currentAccount, config, pythMap, }: BaseContractParams);
|
|
19
|
-
abstract swap(tx: Transaction): Promise<
|
|
18
|
+
abstract swap(tx: Transaction): Promise<TransactionObjectArgument>;
|
|
20
19
|
protected getInputCoinValue(tx: Transaction): import("@mysten/sui/transactions").TransactionArgument;
|
|
21
20
|
protected getTypeParams(): string[];
|
|
22
21
|
protected get extra(): NonNullable<T>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/libs/protocols/base.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../../src/libs/protocols/base.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGxE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,SAAS,CAAC;IACpB,eAAe,EAAE,yBAAyB,CAAC;IAC3C,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED,8BAAsB,YAAY,CAAC,CAAC,GAAG,GAAG;IACxC,SAAS,CAAC,QAAQ,EAAE,SAAS,CAAC;IAC9B,SAAS,CAAC,eAAe,EAAE,yBAAyB,CAAC;IACrD,SAAS,CAAC,cAAc,EAAE,MAAM,CAAC;IACjC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAE9B,EACV,QAAQ,EACR,eAAe,EACf,cAAc,EACd,MAAM,EACN,OAAO,GACR,EAAE,kBAAkB;IAQrB,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,WAAW,GAAG,OAAO,CAAC,yBAAyB,CAAC;IAElE,SAAS,CAAC,iBAAiB,CAAC,EAAE,EAAE,WAAW;IAQ3C,SAAS,CAAC,aAAa;IAMvB,SAAS,KAAK,KAAK,mBAMlB;IAED,SAAS,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,WAAW;CAclD"}
|
|
@@ -14,18 +14,9 @@ export type SteamExtra = {
|
|
|
14
14
|
oracleIndexes?: number[];
|
|
15
15
|
};
|
|
16
16
|
export declare class SteammContract extends BaseContract<SteamExtra> {
|
|
17
|
-
swap(tx: Transaction): Promise<
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}>;
|
|
21
|
-
cpmmSwap(tx: Transaction): {
|
|
22
|
-
$kind: "NestedResult";
|
|
23
|
-
NestedResult: [number, number];
|
|
24
|
-
};
|
|
25
|
-
ommSwap(tx: Transaction, version: "v1" | "v2"): {
|
|
26
|
-
$kind: "NestedResult";
|
|
27
|
-
NestedResult: [number, number];
|
|
28
|
-
};
|
|
17
|
+
swap(tx: Transaction): Promise<import("@mysten/sui/transactions").TransactionObjectArgument>;
|
|
18
|
+
cpmmSwap(tx: Transaction): import("@mysten/sui/transactions").TransactionObjectArgument;
|
|
19
|
+
ommSwap(tx: Transaction, version: "v1" | "v2"): import("@mysten/sui/transactions").TransactionObjectArgument;
|
|
29
20
|
getOraclePriceUpdate(tx: Transaction): readonly [{
|
|
30
21
|
$kind: "NestedResult";
|
|
31
22
|
NestedResult: [number, number];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../src/libs/protocols/steamm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAExD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,MAAM,MAAM,UAAU,GAAG;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,CAAC;AACF,qBAAa,cAAe,SAAQ,YAAY,CAAC,UAAU,CAAC;IACpD,IAAI,CAAC,EAAE,EAAE,WAAW
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../src/libs/protocols/steamm/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAMvD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAExD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,MAAM,MAAM,UAAU,GAAG;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;CAC1B,CAAC;AACF,qBAAa,cAAe,SAAQ,YAAY,CAAC,UAAU,CAAC;IACpD,IAAI,CAAC,EAAE,EAAE,WAAW;IAW1B,QAAQ,CAAC,EAAE,EAAE,WAAW;IAmExB,OAAO,CAAC,EAAE,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,GAAG,IAAI;IAuE7C,oBAAoB,CAAC,EAAE,EAAE,WAAW;;;;;;;CA8BrC"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { Transaction } from "@mysten/sui/transactions";
|
|
1
|
+
import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
2
2
|
import { Config, TxSorSwap } from "../types/aggregator";
|
|
3
|
-
import { TransactionResultItem } from "../types/sui";
|
|
4
3
|
export declare function swapWithRoute({ route, inputCoinObject, currentAccount, config, pythMap, tx, }: {
|
|
5
4
|
route: TxSorSwap[];
|
|
6
|
-
inputCoinObject:
|
|
5
|
+
inputCoinObject: TransactionObjectArgument;
|
|
7
6
|
currentAccount: string;
|
|
8
7
|
config: Config;
|
|
9
8
|
/** map price feed id to onchain priceInfoObject id */
|
|
10
9
|
pythMap: Record<string, string>;
|
|
11
10
|
tx: Transaction;
|
|
12
|
-
}): Promise<
|
|
11
|
+
}): Promise<TransactionObjectArgument | undefined>;
|
|
13
12
|
//# sourceMappingURL=swapWithRoute.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swapWithRoute.d.ts","sourceRoot":"","sources":["../../../../src/libs/swapWithRoute.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"swapWithRoute.d.ts","sourceRoot":"","sources":["../../../../src/libs/swapWithRoute.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAGxD,wBAAsB,aAAa,CAAC,EAClC,KAAK,EACL,eAAe,EACf,cAAc,EACd,MAAM,EACN,OAAO,EACP,EAAE,GACH,EAAE;IACD,KAAK,EAAE,SAAS,EAAE,CAAC;IACnB,eAAe,EAAE,yBAAyB,CAAC;IAC3C,cAAc,EAAE,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,EAAE,EAAE,WAAW,CAAC;CACjB,GAAG,OAAO,CAAC,yBAAyB,GAAG,SAAS,CAAC,CAmBjD"}
|
|
@@ -51,4 +51,17 @@ export interface EstimateGasFeeParams extends CommonParams {
|
|
|
51
51
|
/** Sui price in usd for gas estimation */
|
|
52
52
|
suiPrice?: number;
|
|
53
53
|
}
|
|
54
|
+
export interface SwapInfo {
|
|
55
|
+
quote: QuoteResponse;
|
|
56
|
+
/** @warning this `coinIn` will be consumed completely, no `coinIn` left, user must pass the coinIn object with the balance required for the swap - ie in quote */
|
|
57
|
+
coinIn: TransactionObjectArgument;
|
|
58
|
+
}
|
|
59
|
+
export interface MultiSwapParams {
|
|
60
|
+
swaps: SwapInfo[];
|
|
61
|
+
sender: string;
|
|
62
|
+
slippageBps: number;
|
|
63
|
+
tx: Transaction;
|
|
64
|
+
/** Commission for partner */
|
|
65
|
+
commission: Commission;
|
|
66
|
+
}
|
|
54
67
|
//# sourceMappingURL=tx.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tx.d.ts","sourceRoot":"","sources":["../../../../src/types/tx.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,WAAW,YAAY;IAC3B,iCAAiC;IACjC,aAAa,EAAE,aAAa,CAAC;IAC7B,mBAAmB;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,wCAAwC;IACxC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,6BAA6B;IAC7B,UAAU,EAAE,UAAU,CAAC;IACvB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,QAAQ,CAAC,EAAE;QACT,EAAE,EAAE,WAAW,CAAC;QAChB,MAAM,CAAC,EAAE,yBAAyB,CAAC;KACpC,CAAC;IACF;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,gCAAgC;IAChC,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB"}
|
|
1
|
+
{"version":3,"file":"tx.d.ts","sourceRoot":"","sources":["../../../../src/types/tx.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,yBAAyB,EAC1B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,WAAW,YAAY;IAC3B,iCAAiC;IACjC,aAAa,EAAE,aAAa,CAAC;IAC7B,mBAAmB;IACnB,cAAc,EAAE,MAAM,CAAC;IACvB,wCAAwC;IACxC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,6BAA6B;IAC7B,UAAU,EAAE,UAAU,CAAC;IACvB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,QAAQ,CAAC,EAAE;QACT,EAAE,EAAE,WAAW,CAAC;QAChB,MAAM,CAAC,EAAE,yBAAyB,CAAC;KACpC,CAAC;IACF;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IACjD,gCAAgC;IAChC,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,oBAAqB,SAAQ,YAAY;IACxD,0CAA0C;IAC1C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,aAAa,CAAC;IACrB,kKAAkK;IAClK,MAAM,EAAE,yBAAyB,CAAC;CACnC;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,QAAQ,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,EAAE,EAAE,WAAW,CAAC;IAChB,6BAA6B;IAC7B,UAAU,EAAE,UAAU,CAAC;CACxB"}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { denormalizeTokenType } from "../../utils/token";
|
|
1
|
+
import { coinWithBalance, Transaction } from "@mysten/sui/transactions";
|
|
3
2
|
import { GLOBAL_CONFIG_ID, LIMIT_ORDER_MODULE_ID } from "./constants";
|
|
4
3
|
export async function placeLimitOrder({ accountAddress, payCoinType, targetCoinType, payCoinAmount, rate, slippage, expireTs, devInspect, }) {
|
|
5
|
-
const
|
|
4
|
+
const tx = new Transaction();
|
|
5
|
+
const payCoin = tx.add(coinWithBalance({
|
|
6
|
+
type: payCoinType,
|
|
7
|
+
balance: payCoinAmount,
|
|
8
|
+
useGasCoin: !devInspect,
|
|
9
|
+
}));
|
|
6
10
|
tx.moveCall({
|
|
7
11
|
target: `${LIMIT_ORDER_MODULE_ID}::place_limit_order`,
|
|
8
12
|
arguments: [
|
|
@@ -15,5 +19,6 @@ export async function placeLimitOrder({ accountAddress, payCoinType, targetCoinT
|
|
|
15
19
|
],
|
|
16
20
|
typeArguments: [payCoinType, targetCoinType],
|
|
17
21
|
});
|
|
22
|
+
tx.setSenderIfNotSet(accountAddress);
|
|
18
23
|
return tx;
|
|
19
24
|
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import { Transaction, } from "@mysten/sui/transactions";
|
|
1
|
+
import { coinWithBalance, Transaction, } from "@mysten/sui/transactions";
|
|
2
2
|
import { isValidSuiAddress, toBase64, toHex } from "@mysten/sui/utils";
|
|
3
3
|
import { Config } from "../../config";
|
|
4
4
|
import { _7K_CONFIG, _7K_PACKAGE_ID, _7K_VAULT } from "../../constants/_7k";
|
|
5
|
-
import { getSplitCoinForTx } from "../../libs/getSplitCoinForTx";
|
|
6
5
|
import { groupSwapRoutes } from "../../libs/groupSwapRoutes";
|
|
7
6
|
import { sponsorBluefinX } from "../../libs/protocols/bluefinx/client";
|
|
8
7
|
import { BluefinXTx } from "../../libs/protocols/bluefinx/types";
|
|
9
8
|
import { swapWithRoute } from "../../libs/swapWithRoute";
|
|
10
9
|
import { isBluefinXRouting, } from "../../types/aggregator";
|
|
11
10
|
import { SuiUtils } from "../../utils/sui";
|
|
12
|
-
import { denormalizeTokenType } from "../../utils/token";
|
|
13
11
|
import { getConfig } from "./config";
|
|
14
12
|
import { ORACLE_BASED_SOURCES } from "./getQuote";
|
|
15
13
|
export const buildTx = async ({ quoteResponse, accountAddress, slippage, commission: __commission, devInspect, extendTx, isSponsored, }) => {
|
|
@@ -19,7 +17,7 @@ export const buildTx = async ({ quoteResponse, accountAddress, slippage, commiss
|
|
|
19
17
|
// commission is ignored for bluefinx
|
|
20
18
|
commissionBps: isBluefinX ? 0 : __commission.commissionBps,
|
|
21
19
|
};
|
|
22
|
-
const { tx: _tx, coinIn } = extendTx || {};
|
|
20
|
+
const { tx: _tx, coinIn: _coinIn } = extendTx || {};
|
|
23
21
|
let coinOut;
|
|
24
22
|
if (isBluefinX && devInspect) {
|
|
25
23
|
throw new Error("BluefinX tx is sponsored, skip devInspect");
|
|
@@ -37,16 +35,15 @@ export const buildTx = async ({ quoteResponse, accountAddress, slippage, commiss
|
|
|
37
35
|
const routes = groupSwapRoutes(quoteResponse);
|
|
38
36
|
validateRoutes(routes, isSponsored);
|
|
39
37
|
const splits = routes.map((group) => group[0]?.amount ?? "0");
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const pythMap = await updatePythPriceFeedsIfAny(tx, quoteResponse);
|
|
38
|
+
const coinIn = _coinIn ||
|
|
39
|
+
tx.add(coinWithBalance({
|
|
40
|
+
type: quoteResponse.tokenIn,
|
|
41
|
+
balance: BigInt(quoteResponse.swapAmountWithDecimal),
|
|
42
|
+
useGasCoin: !isSponsored && !isBluefinX,
|
|
43
|
+
}));
|
|
44
|
+
const coinData = tx.splitCoins(coinIn, splits);
|
|
45
|
+
SuiUtils.transferOrDestroyZeroCoin(tx, quoteResponse.tokenIn, coinIn, accountAddress);
|
|
46
|
+
const pythMap = await updatePythPriceFeedsIfAny(tx, [quoteResponse]);
|
|
50
47
|
const coinObjects = [];
|
|
51
48
|
const config = await getConfig();
|
|
52
49
|
await Promise.all(routes.map(async (route, index) => {
|
|
@@ -64,75 +61,38 @@ export const buildTx = async ({ quoteResponse, accountAddress, slippage, commiss
|
|
|
64
61
|
}
|
|
65
62
|
}));
|
|
66
63
|
if (coinObjects.length > 0) {
|
|
67
|
-
const
|
|
68
|
-
? SuiUtils.mergeCoins(coinObjects, tx)
|
|
69
|
-
: coinObjects[0];
|
|
70
|
-
const returnAmountAfterCommission = (BigInt(10000 - _commission.commissionBps) *
|
|
71
|
-
BigInt(quoteResponse.returnAmountWithDecimal)) /
|
|
72
|
-
BigInt(10000);
|
|
73
|
-
const minReceived = (BigInt(1e9 - +slippage * 1e9) * BigInt(returnAmountAfterCommission)) /
|
|
74
|
-
BigInt(1e9);
|
|
75
|
-
tx.moveCall({
|
|
76
|
-
target: `${_7K_PACKAGE_ID}::settle::settle`,
|
|
77
|
-
typeArguments: [quoteResponse.tokenIn, quoteResponse.tokenOut],
|
|
78
|
-
arguments: [
|
|
79
|
-
tx.object(_7K_CONFIG),
|
|
80
|
-
tx.object(_7K_VAULT),
|
|
81
|
-
tx.pure.u64(quoteResponse.swapAmountWithDecimal),
|
|
82
|
-
mergeCoin,
|
|
83
|
-
tx.pure.u64(minReceived), // minimum received
|
|
84
|
-
tx.pure.u64(returnAmountAfterCommission), // expected amount out
|
|
85
|
-
tx.pure.option("address", isValidSuiAddress(_commission.partner) ? _commission.partner : null),
|
|
86
|
-
tx.pure.u64(_commission.commissionBps),
|
|
87
|
-
tx.pure.u64(0),
|
|
88
|
-
],
|
|
89
|
-
});
|
|
64
|
+
const mergedCoin = tx.add(settle(coinObjects, quoteResponse, Math.floor(+slippage * 10000), _commission));
|
|
90
65
|
if (!extendTx) {
|
|
91
|
-
tx.transferObjects([
|
|
66
|
+
tx.transferObjects([mergedCoin], tx.pure.address(accountAddress));
|
|
92
67
|
}
|
|
93
68
|
else {
|
|
94
|
-
coinOut =
|
|
69
|
+
coinOut = mergedCoin;
|
|
95
70
|
}
|
|
96
71
|
}
|
|
97
72
|
if (isBluefinX) {
|
|
98
|
-
const extra = quoteResponse.swaps[0].extra;
|
|
99
|
-
if (extra.quoteExpiresAtUtcMillis < Date.now()) {
|
|
100
|
-
throw new Error("Quote expired");
|
|
101
|
-
}
|
|
102
|
-
tx.setSenderIfNotSet(accountAddress);
|
|
103
|
-
const bytes = await tx.build({
|
|
104
|
-
client: Config.getSuiClient(),
|
|
105
|
-
onlyTransactionKind: true,
|
|
106
|
-
});
|
|
107
|
-
const res = await sponsorBluefinX({
|
|
108
|
-
quoteId: extra.quoteId,
|
|
109
|
-
txBytes: toBase64(bytes),
|
|
110
|
-
sender: accountAddress,
|
|
111
|
-
});
|
|
112
|
-
if (!res.success) {
|
|
113
|
-
throw new Error("Sponsor failed");
|
|
114
|
-
}
|
|
115
73
|
return {
|
|
116
|
-
tx:
|
|
74
|
+
tx: await buildBluefinXTx(tx, accountAddress, quoteResponse),
|
|
117
75
|
coinOut,
|
|
118
76
|
};
|
|
119
77
|
}
|
|
78
|
+
tx.setSenderIfNotSet(accountAddress);
|
|
120
79
|
return { tx, coinOut };
|
|
121
80
|
};
|
|
122
|
-
const getPythPriceFeeds = (
|
|
81
|
+
export const getPythPriceFeeds = (responses) => {
|
|
123
82
|
const ids = new Set();
|
|
124
|
-
for (const
|
|
125
|
-
for (const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
83
|
+
for (const res of responses) {
|
|
84
|
+
for (const s of res.swaps) {
|
|
85
|
+
for (const o of s.extra?.oracles || []) {
|
|
86
|
+
const bytes = o.Pyth?.price_identifier?.bytes || o.Pyth?.bytes;
|
|
87
|
+
if (bytes) {
|
|
88
|
+
ids.add("0x" + toHex(Uint8Array.from(bytes)));
|
|
89
|
+
}
|
|
130
90
|
}
|
|
131
91
|
}
|
|
132
92
|
}
|
|
133
93
|
return Array.from(ids);
|
|
134
94
|
};
|
|
135
|
-
const updatePythPriceFeedsIfAny = async (tx, quoteResponse) => {
|
|
95
|
+
export const updatePythPriceFeedsIfAny = async (tx, quoteResponse) => {
|
|
136
96
|
// update oracles price if any
|
|
137
97
|
const pythMap = {};
|
|
138
98
|
const pythIds = getPythPriceFeeds(quoteResponse);
|
|
@@ -145,7 +105,7 @@ const updatePythPriceFeedsIfAny = async (tx, quoteResponse) => {
|
|
|
145
105
|
}
|
|
146
106
|
return pythMap;
|
|
147
107
|
};
|
|
148
|
-
const validateRoutes = (routes, isSponsored) => {
|
|
108
|
+
export const validateRoutes = (routes, isSponsored) => {
|
|
149
109
|
if (!isSponsored) {
|
|
150
110
|
return;
|
|
151
111
|
}
|
|
@@ -154,3 +114,72 @@ const validateRoutes = (routes, isSponsored) => {
|
|
|
154
114
|
throw new Error("Oracle based sources are not supported for sponsored tx");
|
|
155
115
|
}
|
|
156
116
|
};
|
|
117
|
+
export const getExpectedReturn = (returnAmount, slippageBps, commissionBps, tipBps = 0) => {
|
|
118
|
+
if (slippageBps > 10000) {
|
|
119
|
+
throw new Error("Slippage must be less than 100%");
|
|
120
|
+
}
|
|
121
|
+
if (commissionBps > 10000) {
|
|
122
|
+
throw new Error("Commission must be less than 100%");
|
|
123
|
+
}
|
|
124
|
+
if (tipBps > 10000) {
|
|
125
|
+
throw new Error("Tip must be less than 100%");
|
|
126
|
+
}
|
|
127
|
+
const returnAmountWithDecimal = BigInt(returnAmount);
|
|
128
|
+
const tipAmountWithDecimal = (returnAmountWithDecimal * BigInt(tipBps || 0)) / 10000n;
|
|
129
|
+
const commissionAmountWithDecimal = ((returnAmountWithDecimal - tipAmountWithDecimal) * BigInt(commissionBps)) /
|
|
130
|
+
10000n;
|
|
131
|
+
const expectedReturnWithDecimal = returnAmountWithDecimal -
|
|
132
|
+
tipAmountWithDecimal -
|
|
133
|
+
commissionAmountWithDecimal;
|
|
134
|
+
const minAmountWithDecimal = (expectedReturnWithDecimal * BigInt(1e4 - slippageBps)) / 10000n;
|
|
135
|
+
return {
|
|
136
|
+
tipAmount: tipAmountWithDecimal,
|
|
137
|
+
minAmount: minAmountWithDecimal,
|
|
138
|
+
commissionAmount: commissionAmountWithDecimal,
|
|
139
|
+
expectedAmount: expectedReturnWithDecimal.toString(10),
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
export const settle = (coinObjects, quoteResponse, slippageBps, _commission) => {
|
|
143
|
+
return (tx) => {
|
|
144
|
+
const mergeCoin = coinObjects.length > 1
|
|
145
|
+
? (tx.mergeCoins(coinObjects[0], coinObjects.slice(1)), coinObjects[0])
|
|
146
|
+
: coinObjects[0];
|
|
147
|
+
const { minAmount, expectedAmount } = getExpectedReturn(quoteResponse.returnAmountWithDecimal, slippageBps, _commission.commissionBps);
|
|
148
|
+
tx.moveCall({
|
|
149
|
+
target: `${_7K_PACKAGE_ID}::settle::settle`,
|
|
150
|
+
typeArguments: [quoteResponse.tokenIn, quoteResponse.tokenOut],
|
|
151
|
+
arguments: [
|
|
152
|
+
tx.object(_7K_CONFIG),
|
|
153
|
+
tx.object(_7K_VAULT),
|
|
154
|
+
tx.pure.u64(quoteResponse.swapAmountWithDecimal),
|
|
155
|
+
mergeCoin,
|
|
156
|
+
tx.pure.u64(minAmount), // minimum received
|
|
157
|
+
tx.pure.u64(expectedAmount), // expected amount out
|
|
158
|
+
tx.pure.option("address", isValidSuiAddress(_commission.partner) ? _commission.partner : null),
|
|
159
|
+
tx.pure.u64(_commission.commissionBps),
|
|
160
|
+
tx.pure.u64(0),
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
return mergeCoin;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
export const buildBluefinXTx = async (tx, accountAddress, quoteResponse) => {
|
|
167
|
+
const extra = quoteResponse.swaps[0].extra;
|
|
168
|
+
if (extra.quoteExpiresAtUtcMillis < Date.now()) {
|
|
169
|
+
throw new Error("Quote expired");
|
|
170
|
+
}
|
|
171
|
+
tx.setSenderIfNotSet(accountAddress);
|
|
172
|
+
const bytes = await tx.build({
|
|
173
|
+
client: Config.getSuiClient(),
|
|
174
|
+
onlyTransactionKind: true,
|
|
175
|
+
});
|
|
176
|
+
const res = await sponsorBluefinX({
|
|
177
|
+
quoteId: extra.quoteId,
|
|
178
|
+
txBytes: toBase64(bytes),
|
|
179
|
+
sender: accountAddress,
|
|
180
|
+
});
|
|
181
|
+
if (!res.success) {
|
|
182
|
+
throw new Error("Sponsor failed");
|
|
183
|
+
}
|
|
184
|
+
return new BluefinXTx(res.quoteId, res.data.txBytes);
|
|
185
|
+
};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { coinWithBalance, Transaction, } from "@mysten/sui/transactions";
|
|
2
|
+
import { isValidSuiAddress, normalizeStructTag } from "@mysten/sui/utils";
|
|
3
|
+
import { groupSwapRoutes } from "../../libs/groupSwapRoutes";
|
|
4
|
+
import { swapWithRoute } from "../../libs/swapWithRoute";
|
|
5
|
+
import { isBluefinXRouting, } from "../../types/aggregator";
|
|
6
|
+
import { SuiUtils } from "../../utils/sui";
|
|
7
|
+
import { buildBluefinXTx, settle, updatePythPriceFeedsIfAny, validateRoutes, } from "./buildTx";
|
|
8
|
+
import { getConfig } from "./config";
|
|
9
|
+
/**
|
|
10
|
+
* Wave-based transaction builder that optimizes swap execution by:
|
|
11
|
+
* 1. Grouping swaps into execution waves based on readiness
|
|
12
|
+
* 2. Merging redundant swaps to the same pool within each wave
|
|
13
|
+
* 3. Processing waves sequentially, passing intermediate tokens between waves
|
|
14
|
+
*/
|
|
15
|
+
export const buildTxV2 = async ({ quoteResponse, accountAddress, slippage, commission: __commission, devInspect, extendTx, isSponsored, }) => {
|
|
16
|
+
const isBluefinX = isBluefinXRouting(quoteResponse);
|
|
17
|
+
const _commission = {
|
|
18
|
+
...__commission,
|
|
19
|
+
commissionBps: isBluefinX ? 0 : __commission.commissionBps,
|
|
20
|
+
};
|
|
21
|
+
const { tx: _tx, coinIn: _coinIn } = extendTx || {};
|
|
22
|
+
let coinOut;
|
|
23
|
+
if (isBluefinX && devInspect) {
|
|
24
|
+
throw new Error("BluefinX tx is sponsored, skip devInspect");
|
|
25
|
+
}
|
|
26
|
+
if (!accountAddress) {
|
|
27
|
+
throw new Error("Sender address is required");
|
|
28
|
+
}
|
|
29
|
+
if (!quoteResponse.routes) {
|
|
30
|
+
throw new Error("Invalid quote response: 'routes' are required");
|
|
31
|
+
}
|
|
32
|
+
if (!isValidSuiAddress(_commission.partner)) {
|
|
33
|
+
throw new Error("Invalid commission partner address");
|
|
34
|
+
}
|
|
35
|
+
const tx = _tx || new Transaction();
|
|
36
|
+
const routes = groupSwapRoutes(quoteResponse);
|
|
37
|
+
validateRoutes(routes, isSponsored);
|
|
38
|
+
const splits = routes.map((group) => group[0]?.amount ?? "0");
|
|
39
|
+
const coinIn = _coinIn ||
|
|
40
|
+
tx.add(coinWithBalance({
|
|
41
|
+
type: quoteResponse.tokenIn,
|
|
42
|
+
balance: BigInt(quoteResponse.swapAmountWithDecimal),
|
|
43
|
+
useGasCoin: !isSponsored && !isBluefinX,
|
|
44
|
+
}));
|
|
45
|
+
const coinData = tx.splitCoins(coinIn, splits);
|
|
46
|
+
SuiUtils.transferOrDestroyZeroCoin(tx, quoteResponse.tokenIn, coinIn, accountAddress);
|
|
47
|
+
const pythMap = await updatePythPriceFeedsIfAny(tx, [quoteResponse]);
|
|
48
|
+
const config = await getConfig();
|
|
49
|
+
const finalCoins = await optimize(pythMap, config, routes, coinData, tx, accountAddress);
|
|
50
|
+
// Merge all final coins
|
|
51
|
+
if (finalCoins.length > 0) {
|
|
52
|
+
let mergeCoin = tx.add(settle(finalCoins, quoteResponse, Math.floor(+slippage * 10000), _commission));
|
|
53
|
+
if (!extendTx) {
|
|
54
|
+
tx.transferObjects([mergeCoin], tx.pure.address(accountAddress));
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
coinOut = mergeCoin;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (isBluefinX) {
|
|
61
|
+
return {
|
|
62
|
+
tx: await buildBluefinXTx(tx, accountAddress, quoteResponse),
|
|
63
|
+
coinOut,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
tx.setSenderIfNotSet(accountAddress);
|
|
67
|
+
return { tx, coinOut };
|
|
68
|
+
};
|
|
69
|
+
export const optimize = async (pythMap, config, routes, coinData, tx, accountAddress) => {
|
|
70
|
+
// Initialize route states with split coins
|
|
71
|
+
const routeStates = routes.map((route, index) => ({
|
|
72
|
+
routeIndex: index,
|
|
73
|
+
currentHopIndex: 0,
|
|
74
|
+
currentCoin: coinData[index],
|
|
75
|
+
swaps: route,
|
|
76
|
+
completed: false,
|
|
77
|
+
}));
|
|
78
|
+
const finalCoins = [];
|
|
79
|
+
const coinTypeOut = routes[0][routes[0].length - 1].assetOut;
|
|
80
|
+
// Execute swaps in waves
|
|
81
|
+
let waveNumber = 0;
|
|
82
|
+
while (true) {
|
|
83
|
+
const readyHops = [];
|
|
84
|
+
routeStates.forEach((state) => {
|
|
85
|
+
if (state.completed)
|
|
86
|
+
return;
|
|
87
|
+
if (state.currentHopIndex >= state.swaps.length) {
|
|
88
|
+
state.completed = true;
|
|
89
|
+
finalCoins.push(state.currentCoin);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const swap = state.swaps[state.currentHopIndex];
|
|
93
|
+
readyHops.push({
|
|
94
|
+
routeIndex: state.routeIndex,
|
|
95
|
+
hopIndex: state.currentHopIndex,
|
|
96
|
+
swap,
|
|
97
|
+
inputCoin: state.currentCoin,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
if (readyHops.length === 0)
|
|
101
|
+
break;
|
|
102
|
+
// Group hops by pool for merging opportunities
|
|
103
|
+
const poolGroups = new Map();
|
|
104
|
+
readyHops.forEach((hop) => {
|
|
105
|
+
const poolKey = `${hop.swap.poolId}|${hop.swap.assetIn}|${hop.swap.assetOut}`;
|
|
106
|
+
if (!poolGroups.has(poolKey)) {
|
|
107
|
+
poolGroups.set(poolKey, {
|
|
108
|
+
swap: hop.swap,
|
|
109
|
+
hops: [],
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
poolGroups.get(poolKey).hops.push(hop);
|
|
113
|
+
});
|
|
114
|
+
// Execute each pool group in this wave
|
|
115
|
+
const wavePromises = [];
|
|
116
|
+
for (const group of poolGroups.values()) {
|
|
117
|
+
const { swap, hops } = group;
|
|
118
|
+
// For merged swaps, we need to combine input coins first
|
|
119
|
+
const wavePromise = (async () => {
|
|
120
|
+
let combinedCoin;
|
|
121
|
+
if (hops.length > 1) {
|
|
122
|
+
// Merge all input coins for this pool
|
|
123
|
+
const inputCoins = hops.map((h) => h.inputCoin);
|
|
124
|
+
tx.mergeCoins(inputCoins[0], inputCoins.slice(1));
|
|
125
|
+
combinedCoin = inputCoins[0];
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
combinedCoin = hops[0].inputCoin;
|
|
129
|
+
}
|
|
130
|
+
// Execute the swap with the combined coin
|
|
131
|
+
const resultCoin = await swapWithRoute({
|
|
132
|
+
route: [swap],
|
|
133
|
+
inputCoinObject: combinedCoin,
|
|
134
|
+
currentAccount: accountAddress,
|
|
135
|
+
tx,
|
|
136
|
+
config,
|
|
137
|
+
pythMap,
|
|
138
|
+
});
|
|
139
|
+
if (!resultCoin) {
|
|
140
|
+
throw new Error(`Swap failed for pool ${swap.poolId}`);
|
|
141
|
+
}
|
|
142
|
+
if (hops[0].swap.assetOut === coinTypeOut) {
|
|
143
|
+
finalCoins.push(resultCoin);
|
|
144
|
+
hops.forEach((hop) => {
|
|
145
|
+
const state = routeStates[hop.routeIndex];
|
|
146
|
+
state.completed = true;
|
|
147
|
+
state.currentHopIndex++;
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
// For merged swaps, we need to split the output proportionally
|
|
152
|
+
if (hops.length > 1) {
|
|
153
|
+
// Split output proportionally (except the last one which gets the remainder)
|
|
154
|
+
const splitAmounts = hops
|
|
155
|
+
.slice(1)
|
|
156
|
+
.map((hop) => hop.swap.returnAmount);
|
|
157
|
+
const splitCoins = splitAmounts.length > 0
|
|
158
|
+
? [resultCoin, ...tx.splitCoins(resultCoin, splitAmounts)]
|
|
159
|
+
: [resultCoin];
|
|
160
|
+
// Assign split coins back to routes
|
|
161
|
+
hops.forEach((hop, index) => {
|
|
162
|
+
const state = routeStates[hop.routeIndex];
|
|
163
|
+
state.currentCoin = splitCoins[index];
|
|
164
|
+
state.currentHopIndex++;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// Single swap - simply update the route state
|
|
169
|
+
const state = routeStates[hops[0].routeIndex];
|
|
170
|
+
state.currentCoin = resultCoin;
|
|
171
|
+
state.currentHopIndex++;
|
|
172
|
+
}
|
|
173
|
+
})();
|
|
174
|
+
wavePromises.push(wavePromise);
|
|
175
|
+
}
|
|
176
|
+
// Wait for all swaps in this wave to complete
|
|
177
|
+
await Promise.all(wavePromises);
|
|
178
|
+
waveNumber++;
|
|
179
|
+
}
|
|
180
|
+
return finalCoins;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* execute multiple swap in single transaction
|
|
184
|
+
*
|
|
185
|
+
* User must handle the coins from return
|
|
186
|
+
* @param param - MultiSwapParams
|
|
187
|
+
* @returns a map of coinType to coinObject
|
|
188
|
+
*/
|
|
189
|
+
export const multiSwap = async ({ sender, slippageBps, swaps, tx, commission, }) => {
|
|
190
|
+
if (swaps.some((s) => isBluefinXRouting(s.quote))) {
|
|
191
|
+
throw Error("BluefinX routing not supported yet");
|
|
192
|
+
}
|
|
193
|
+
// update oracles price if any
|
|
194
|
+
const pythMap = await updatePythPriceFeedsIfAny(tx, swaps.map((s) => s.quote));
|
|
195
|
+
const map = {};
|
|
196
|
+
const config = await getConfig();
|
|
197
|
+
for (const { quote: sorResponse, coinIn } of swaps) {
|
|
198
|
+
const routes = groupSwapRoutes(sorResponse);
|
|
199
|
+
const splits = routes.map((group) => group[0]?.amount ?? "0");
|
|
200
|
+
const coinData = splits.length === 1
|
|
201
|
+
? [coinIn]
|
|
202
|
+
: [coinIn, ...tx.splitCoins(coinIn, splits.slice(1))];
|
|
203
|
+
const coinObjects = await optimize(pythMap, config, routes, coinData, tx, sender);
|
|
204
|
+
if (coinObjects.length > 0) {
|
|
205
|
+
const mergeCoin = tx.add(settle(coinObjects, sorResponse, slippageBps, commission));
|
|
206
|
+
if (!map[normalizeStructTag(sorResponse.tokenOut)]) {
|
|
207
|
+
map[normalizeStructTag(sorResponse.tokenOut)] = [];
|
|
208
|
+
}
|
|
209
|
+
map[normalizeStructTag(sorResponse.tokenOut)].push(mergeCoin);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const result = {};
|
|
213
|
+
for (const [tokenOut, coins] of Object.entries(map)) {
|
|
214
|
+
if (coins.length > 1) {
|
|
215
|
+
tx.mergeCoins(coins[0], coins.slice(1));
|
|
216
|
+
}
|
|
217
|
+
result[tokenOut] = coins[0];
|
|
218
|
+
}
|
|
219
|
+
tx.setSenderIfNotSet(sender);
|
|
220
|
+
return result;
|
|
221
|
+
};
|
package/lib/esm/index.mjs
CHANGED
|
@@ -2,24 +2,24 @@ export * from "./types/aggregator";
|
|
|
2
2
|
import { Config } from "./config";
|
|
3
3
|
import { getSuiPrice, getTokenPrice, getTokenPrices } from "./features/prices";
|
|
4
4
|
import { executeBluefinTx } from "./libs/protocols/bluefinx/client";
|
|
5
|
-
import { buildTx, estimateGasFee, executeTx, getQuote, getSwapHistory,
|
|
5
|
+
import { buildTx, buildTxV2, DEFAULT_SOURCES, estimateGasFee, executeTx, getQuote, getSwapHistory, multiSwap, } from "./features/swap";
|
|
6
6
|
import { cancelDcaOrder, cancelLimitOrder, claimExpiredLimitOrder, getClosedDcaOrders, getClosedLimitOrders, getDcaOrderExecutions, getOpenDcaOrders, getOpenLimitOrders, placeDcaOrder, placeLimitOrder, } from "./features/limitDca";
|
|
7
7
|
// avoid breaking changes
|
|
8
8
|
const getSuiClient = Config.getSuiClient;
|
|
9
9
|
const setSuiClient = Config.setSuiClient;
|
|
10
|
-
export {
|
|
10
|
+
export { buildTx, buildTxV2, cancelDcaOrder, cancelLimitOrder, claimExpiredLimitOrder,
|
|
11
11
|
// config
|
|
12
|
-
Config,
|
|
12
|
+
Config, DEFAULT_SOURCES, estimateGasFee, executeBluefinTx, executeTx, getClosedDcaOrders, getClosedLimitOrders, getDcaOrderExecutions, getOpenDcaOrders, getOpenLimitOrders,
|
|
13
|
+
// swap
|
|
14
|
+
getQuote,
|
|
13
15
|
// sui client
|
|
14
|
-
getSuiClient,
|
|
16
|
+
getSuiClient, getSuiPrice, getSwapHistory,
|
|
15
17
|
// prices
|
|
16
|
-
getTokenPrice, getTokenPrices,
|
|
17
|
-
// swap
|
|
18
|
-
getQuote, estimateGasFee, buildTx, getSwapHistory, executeTx, executeBluefinTx, DEFAULT_SOURCES,
|
|
19
|
-
// limit order
|
|
20
|
-
placeLimitOrder, getOpenLimitOrders, cancelLimitOrder, claimExpiredLimitOrder, getClosedLimitOrders,
|
|
18
|
+
getTokenPrice, getTokenPrices, multiSwap,
|
|
21
19
|
// dca
|
|
22
|
-
placeDcaOrder,
|
|
20
|
+
placeDcaOrder,
|
|
21
|
+
// limit order
|
|
22
|
+
placeLimitOrder, setSuiClient, };
|
|
23
23
|
export default {
|
|
24
24
|
// config
|
|
25
25
|
Config,
|
|
@@ -34,6 +34,8 @@ export default {
|
|
|
34
34
|
getQuote,
|
|
35
35
|
estimateGasFee,
|
|
36
36
|
buildTx,
|
|
37
|
+
buildTxV2,
|
|
38
|
+
multiSwap,
|
|
37
39
|
getSwapHistory,
|
|
38
40
|
executeTx,
|
|
39
41
|
executeBluefinTx,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
1
2
|
export interface PlaceLimitOrderParams {
|
|
2
3
|
accountAddress: string;
|
|
3
4
|
payCoinType: string;
|
|
@@ -8,5 +9,5 @@ export interface PlaceLimitOrderParams {
|
|
|
8
9
|
expireTs: bigint;
|
|
9
10
|
devInspect?: boolean;
|
|
10
11
|
}
|
|
11
|
-
export declare function placeLimitOrder({ accountAddress, payCoinType, targetCoinType, payCoinAmount, rate, slippage, expireTs, devInspect, }: PlaceLimitOrderParams): Promise<
|
|
12
|
+
export declare function placeLimitOrder({ accountAddress, payCoinType, targetCoinType, payCoinAmount, rate, slippage, expireTs, devInspect, }: PlaceLimitOrderParams): Promise<Transaction>;
|
|
12
13
|
//# sourceMappingURL=placeLimitOrder.d.ts.map
|