@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../../../src/utils/number.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,cAAc,CAAC;AAErC,wBAAgB,aAAa,CAAC,OAAO,EAAE,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,aAEvE;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,aAK1E"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { TransactionArgument, TransactionBlock } from "@mysten/sui.js/transactions";
|
|
2
|
+
import { CoinStruct, SuiObjectResponseQuery } from "@mysten/sui.js/client";
|
|
3
|
+
type DataPage<T> = {
|
|
4
|
+
data: T[];
|
|
5
|
+
nextCursor?: any;
|
|
6
|
+
hasNextPage: boolean;
|
|
7
|
+
};
|
|
8
|
+
type PageQuery = {
|
|
9
|
+
cursor?: any;
|
|
10
|
+
limit?: number | null;
|
|
11
|
+
};
|
|
12
|
+
type PaginationArgs = "all" | PageQuery;
|
|
13
|
+
export declare const SuiUtils: {
|
|
14
|
+
getSuiCoin(amount: bigint | TransactionArgument, txb: TransactionBlock): TransactionArgument;
|
|
15
|
+
mergeCoins(coinObjects: Array<string | TransactionArgument>, txb: TransactionBlock): TransactionArgument | undefined;
|
|
16
|
+
getCoinValue(coinType: string, coinObject: string | TransactionArgument, txb: TransactionBlock): TransactionArgument;
|
|
17
|
+
getExactCoinByAmount(coinType: string, coins: {
|
|
18
|
+
objectId: string;
|
|
19
|
+
balance: bigint;
|
|
20
|
+
}[], amount: bigint, txb: TransactionBlock): {
|
|
21
|
+
index: number;
|
|
22
|
+
resultIndex: number;
|
|
23
|
+
kind: "NestedResult";
|
|
24
|
+
};
|
|
25
|
+
mergeAllUserCoins(coinType: string, signerAddress: string): Promise<TransactionBlock | undefined>;
|
|
26
|
+
mergeAllCoinsWithoutFetch(coins: CoinStruct[], coinType: string, txb: TransactionBlock): void;
|
|
27
|
+
getAllUserCoins({ address, type }: {
|
|
28
|
+
type: string;
|
|
29
|
+
address: string;
|
|
30
|
+
}): Promise<CoinStruct[]>;
|
|
31
|
+
getCoinsGreaterThanAmount(amount: bigint, coins: {
|
|
32
|
+
objectId: string;
|
|
33
|
+
balance: bigint;
|
|
34
|
+
}[]): string[];
|
|
35
|
+
getOwnedObjectsByPage(owner: string, query: SuiObjectResponseQuery, paginationArgs?: PaginationArgs): Promise<DataPage<any>>;
|
|
36
|
+
isValidStructTag(value: string): boolean;
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=sui.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sui.d.ts","sourceRoot":"","sources":["../../../../src/utils/sui.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,UAAU,EAEV,sBAAsB,EACvB,MAAM,uBAAuB,CAAC;AAK/B,KAAK,QAAQ,CAAC,CAAC,IAAI;IACjB,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,UAAU,CAAC,EAAE,GAAG,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF,KAAK,SAAS,GAAG;IACf,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvB,CAAC;AAEF,KAAK,cAAc,GAAG,KAAK,GAAG,SAAS,CAAC;AAExC,eAAO,MAAM,QAAQ;uBAET,MAAM,GAAG,mBAAmB,OAC/B,gBAAgB,GACpB,mBAAmB;4BAQP,KAAK,CAAC,MAAM,GAAG,mBAAmB,CAAC,OAC3C,gBAAgB,GACpB,mBAAmB,GAAG,SAAS;2BAsBtB,MAAM,cACJ,MAAM,GAAG,mBAAmB,OACnC,gBAAgB,GACpB,mBAAmB;mCAYV,MAAM,SACT;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,UACtC,MAAM,OACT,gBAAgB;;;;;gCAoBW,MAAM,iBAAiB,MAAM;qCAoCtD,UAAU,EAAE,YACT,MAAM,OACX,gBAAgB;uCAsBkB;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;sCA8BhE,MAAM,SACP;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE;iCA6BvC,MAAM,SACN,sBAAsB,mBACb,cAAc,GAC7B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;4BAyBD,MAAM;CAO/B,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { SuiscanToken } from "../types/token";
|
|
2
|
+
export declare function normalizeTokenType(type: string): string;
|
|
3
|
+
export declare function denormalizeTokenType(type: string): string;
|
|
4
|
+
export declare function checkIsSui(type: string): type is "0x2::sui::SUI" | "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
|
|
5
|
+
export declare function getSuiscanTokenMetadata(type: string): Promise<SuiscanToken>;
|
|
6
|
+
//# sourceMappingURL=token.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../../../src/utils/token.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,UAE9C;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,UAEhD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,4GAEtC;AAED,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,MAAM,yBASzD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import BigNumber from "bignumber.js";
|
|
2
|
+
export function formatBalance(balance, decimals) {
|
|
3
|
+
return new BigNumber(balance).dividedBy(new BigNumber(10).pow(decimals));
|
|
4
|
+
}
|
|
5
|
+
export function formatRawBalance(balance, decimals) {
|
|
6
|
+
const rawBalance = new BigNumber(balance).multipliedBy(new BigNumber(10).pow(decimals));
|
|
7
|
+
return new BigNumber(rawBalance.toFixed(0));
|
|
8
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { TransactionBlock, } from "@mysten/sui.js/transactions";
|
|
2
|
+
import { parseStructTag } from "@mysten/sui.js/utils";
|
|
3
|
+
import { checkIsSui } from "./token";
|
|
4
|
+
import { getSuiClient } from "../suiClient";
|
|
5
|
+
export const SuiUtils = {
|
|
6
|
+
getSuiCoin(amount, txb) {
|
|
7
|
+
const inputCoinAmount = typeof amount === "bigint" ? txb.pure(amount) : amount;
|
|
8
|
+
const [coin] = txb.splitCoins(txb.gas, [inputCoinAmount]);
|
|
9
|
+
return coin;
|
|
10
|
+
},
|
|
11
|
+
mergeCoins(coinObjects, txb) {
|
|
12
|
+
if (coinObjects.length == 1) {
|
|
13
|
+
return typeof coinObjects[0] == "string"
|
|
14
|
+
? txb.object(coinObjects[0])
|
|
15
|
+
: coinObjects[0];
|
|
16
|
+
}
|
|
17
|
+
const firstCoin = typeof coinObjects[0] == "string"
|
|
18
|
+
? txb.object(coinObjects[0])
|
|
19
|
+
: coinObjects[0];
|
|
20
|
+
txb.mergeCoins(
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
firstCoin, coinObjects
|
|
24
|
+
.slice(1)
|
|
25
|
+
.map((coin) => (typeof coin == "string" ? txb.object(coin) : coin)));
|
|
26
|
+
return firstCoin;
|
|
27
|
+
},
|
|
28
|
+
getCoinValue(coinType, coinObject, txb) {
|
|
29
|
+
const inputCoinObject = typeof coinObject == "string" ? txb.object(coinObject) : coinObject;
|
|
30
|
+
const [value] = txb.moveCall({
|
|
31
|
+
target: `0x2::coin::value`,
|
|
32
|
+
typeArguments: [coinType],
|
|
33
|
+
arguments: [inputCoinObject],
|
|
34
|
+
});
|
|
35
|
+
return value;
|
|
36
|
+
},
|
|
37
|
+
getExactCoinByAmount(coinType, coins, amount, txb) {
|
|
38
|
+
if (checkIsSui(coinType)) {
|
|
39
|
+
const [coinA] = txb.splitCoins(txb.gas, [txb.pure(amount)]);
|
|
40
|
+
return coinA;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const coinsX = SuiUtils.getCoinsGreaterThanAmount(amount, coins);
|
|
44
|
+
if (coinsX.length > 1) {
|
|
45
|
+
txb.mergeCoins(txb.object(coinsX[0]), coinsX.slice(1).map((coin) => txb.object(coin)));
|
|
46
|
+
}
|
|
47
|
+
const [coinA] = txb.splitCoins(txb.object(coinsX[0]), [txb.pure(amount)]);
|
|
48
|
+
return coinA;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
async mergeAllUserCoins(coinType, signerAddress) {
|
|
52
|
+
try {
|
|
53
|
+
const coins = await SuiUtils.getAllUserCoins({
|
|
54
|
+
address: signerAddress,
|
|
55
|
+
type: coinType,
|
|
56
|
+
});
|
|
57
|
+
let totalBalance = BigInt(0);
|
|
58
|
+
coins.forEach((coin) => {
|
|
59
|
+
totalBalance += BigInt(coin.balance);
|
|
60
|
+
});
|
|
61
|
+
const txb = new TransactionBlock();
|
|
62
|
+
if (checkIsSui(coinType)) {
|
|
63
|
+
totalBalance = totalBalance - BigInt("1000");
|
|
64
|
+
txb.splitCoins(txb.gas, [txb.pure(totalBalance.toString())]);
|
|
65
|
+
}
|
|
66
|
+
const coinObjectsIds = coins.map((coin) => coin.coinObjectId);
|
|
67
|
+
if (coins.length > 1) {
|
|
68
|
+
txb.mergeCoins(txb.object(coinObjectsIds[0]), coinObjectsIds.slice(1).map((coin) => txb.object(coin)));
|
|
69
|
+
}
|
|
70
|
+
return txb;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.log(error);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
mergeAllCoinsWithoutFetch(coins, coinType, txb) {
|
|
77
|
+
let totalBalance = BigInt(0);
|
|
78
|
+
coins.forEach((coin) => {
|
|
79
|
+
totalBalance += BigInt(coin.balance);
|
|
80
|
+
});
|
|
81
|
+
if (checkIsSui(coinType)) {
|
|
82
|
+
totalBalance = totalBalance - BigInt("1000");
|
|
83
|
+
txb.splitCoins(txb.gas, [txb.pure(totalBalance.toString())]);
|
|
84
|
+
}
|
|
85
|
+
const coinObjectsIds = coins.map((coin) => coin.coinObjectId);
|
|
86
|
+
if (coins.length > 1) {
|
|
87
|
+
txb.mergeCoins(txb.object(coinObjectsIds[0]), coinObjectsIds.slice(1).map((coin) => txb.object(coin)));
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
async getAllUserCoins({ address, type }) {
|
|
91
|
+
let cursor = undefined;
|
|
92
|
+
let coins = [];
|
|
93
|
+
let iter = 0;
|
|
94
|
+
do {
|
|
95
|
+
try {
|
|
96
|
+
const res = await getSuiClient().getCoins({
|
|
97
|
+
owner: address,
|
|
98
|
+
coinType: type,
|
|
99
|
+
cursor: cursor,
|
|
100
|
+
limit: 50,
|
|
101
|
+
});
|
|
102
|
+
coins = coins.concat(res.data);
|
|
103
|
+
cursor = res.nextCursor;
|
|
104
|
+
if (!res.hasNextPage || iter === 8) {
|
|
105
|
+
cursor = null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
console.log(error);
|
|
110
|
+
cursor = null;
|
|
111
|
+
}
|
|
112
|
+
iter++;
|
|
113
|
+
} while (cursor !== null);
|
|
114
|
+
return coins;
|
|
115
|
+
},
|
|
116
|
+
getCoinsGreaterThanAmount(amount, coins) {
|
|
117
|
+
const coinsWithBalance = [];
|
|
118
|
+
let collectedAmount = BigInt(0);
|
|
119
|
+
for (const coin of coins) {
|
|
120
|
+
if (collectedAmount < amount &&
|
|
121
|
+
!coinsWithBalance.includes(coin.objectId)) {
|
|
122
|
+
coinsWithBalance.push(coin.objectId);
|
|
123
|
+
collectedAmount = collectedAmount + coin.balance;
|
|
124
|
+
}
|
|
125
|
+
if (coin.balance === BigInt(0) &&
|
|
126
|
+
!coinsWithBalance.includes(coin.objectId))
|
|
127
|
+
coinsWithBalance.push(coin.objectId);
|
|
128
|
+
}
|
|
129
|
+
if (collectedAmount >= amount) {
|
|
130
|
+
return coinsWithBalance;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
throw new Error("Insufficient balance");
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
async getOwnedObjectsByPage(owner, query, paginationArgs = "all") {
|
|
137
|
+
let result = [];
|
|
138
|
+
let hasNextPage = true;
|
|
139
|
+
const queryAll = paginationArgs === "all";
|
|
140
|
+
let nextCursor = queryAll ? null : paginationArgs.cursor;
|
|
141
|
+
do {
|
|
142
|
+
const res = await getSuiClient().getOwnedObjects({
|
|
143
|
+
owner,
|
|
144
|
+
...query,
|
|
145
|
+
cursor: nextCursor,
|
|
146
|
+
limit: queryAll ? null : paginationArgs.limit,
|
|
147
|
+
});
|
|
148
|
+
if (res.data) {
|
|
149
|
+
result = [...result, ...res.data];
|
|
150
|
+
hasNextPage = res.hasNextPage;
|
|
151
|
+
nextCursor = res.nextCursor;
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
hasNextPage = false;
|
|
155
|
+
}
|
|
156
|
+
} while (queryAll && hasNextPage);
|
|
157
|
+
return { data: result, nextCursor, hasNextPage };
|
|
158
|
+
},
|
|
159
|
+
isValidStructTag(value) {
|
|
160
|
+
try {
|
|
161
|
+
return !!parseStructTag(value);
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EXPLORER } from "../constants/explorer";
|
|
2
|
+
import { SUI_FULL_TYPE, SUI_TYPE } from "../constants/tokens";
|
|
3
|
+
export function normalizeTokenType(type) {
|
|
4
|
+
return type === SUI_TYPE ? SUI_FULL_TYPE : type;
|
|
5
|
+
}
|
|
6
|
+
export function denormalizeTokenType(type) {
|
|
7
|
+
return type === SUI_FULL_TYPE ? SUI_TYPE : type;
|
|
8
|
+
}
|
|
9
|
+
export function checkIsSui(type) {
|
|
10
|
+
return type === SUI_FULL_TYPE || type === SUI_TYPE;
|
|
11
|
+
}
|
|
12
|
+
export async function getSuiscanTokenMetadata(type) {
|
|
13
|
+
const response = await fetch(`${EXPLORER.ADDRESS}/api/sui-backend/mainnet/api/coins/${denormalizeTokenType(type)}`);
|
|
14
|
+
if (!response.ok) {
|
|
15
|
+
throw new Error("Failed to fetch token metadata");
|
|
16
|
+
}
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
return { ...data, type: normalizeTokenType(data.type || "") };
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@7kprotocol/sdk-ts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"import": {
|
|
8
|
+
"types": "./lib/esm/types/index.d.ts",
|
|
9
|
+
"default": "./lib/esm/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"require": {
|
|
12
|
+
"types": "./lib/cjs/types/index.d.ts",
|
|
13
|
+
"default": "./lib/cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"types": "./lib/cjs/types/index.d.ts",
|
|
18
|
+
"main": "./lib/cjs/index.js",
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/**/*"
|
|
21
|
+
],
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"clean": "rm -rf ./lib",
|
|
27
|
+
"prepare": "husky",
|
|
28
|
+
"tsc": "npx tsc -p ./configs/tsconfig.base.json --noEmit",
|
|
29
|
+
"lint": "npx eslint ./src",
|
|
30
|
+
"prettier": "npx prettier --write .",
|
|
31
|
+
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
|
32
|
+
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs",
|
|
33
|
+
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
|
|
34
|
+
"prepack": "npm run build",
|
|
35
|
+
"test": "mocha",
|
|
36
|
+
"publish:patch": "npm version patch --no-git-tag-version && npm publish --access public",
|
|
37
|
+
"publish:minor": "npm version minor --no-git-tag-version && npm publish --access public",
|
|
38
|
+
"publish:major": "npm version major --no-git-tag-version && npm publish --access public"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"7k Protocol",
|
|
42
|
+
"DEX Aggregator",
|
|
43
|
+
"Sui Network"
|
|
44
|
+
],
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/7k-ag/7k-sdk-ts"
|
|
48
|
+
},
|
|
49
|
+
"author": "7k Protocol",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@eslint/js": "^9.6.0",
|
|
53
|
+
"@types/bn.js": "^5.1.5",
|
|
54
|
+
"@types/chai": "^4.3.3",
|
|
55
|
+
"@types/eslint__js": "^8.42.3",
|
|
56
|
+
"@types/mocha": "^9.1.1",
|
|
57
|
+
"chai": "^4.3.6",
|
|
58
|
+
"eslint": "^8.57.0",
|
|
59
|
+
"husky": "^9.0.11",
|
|
60
|
+
"mocha": "^10.0.0",
|
|
61
|
+
"ts-node": "^10.9.2",
|
|
62
|
+
"typescript": "^5.5.3",
|
|
63
|
+
"typescript-eslint": "^7.15.0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"@mysten/sui.js": "^0.54.1",
|
|
67
|
+
"bignumber.js": "^9.1.2",
|
|
68
|
+
"bn.js": "^5.2.1"
|
|
69
|
+
}
|
|
70
|
+
}
|