@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.
Files changed (201) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -0
  3. package/lib/cjs/buildTx.js +93 -0
  4. package/lib/cjs/constants/explorer.js +6 -0
  5. package/lib/cjs/constants/tokens.js +5 -0
  6. package/lib/cjs/estimateGasFee.js +52 -0
  7. package/lib/cjs/getQuote.js +22 -0
  8. package/lib/cjs/getSuiPrice.js +31 -0
  9. package/lib/cjs/index.js +37 -0
  10. package/lib/cjs/libs/getCoinOjectIdsByAmount.js +74 -0
  11. package/lib/cjs/libs/getSplitCoinForTx.js +55 -0
  12. package/lib/cjs/libs/groupSwapRoutes.js +62 -0
  13. package/lib/cjs/libs/protocols/aftermath/index.js +52 -0
  14. package/lib/cjs/libs/protocols/base.js +20 -0
  15. package/lib/cjs/libs/protocols/bluemove/index.js +36 -0
  16. package/lib/cjs/libs/protocols/cetus/index.js +56 -0
  17. package/lib/cjs/libs/protocols/constants.js +5 -0
  18. package/lib/cjs/libs/protocols/deepbook/index.js +96 -0
  19. package/lib/cjs/libs/protocols/flowx/index.js +31 -0
  20. package/lib/cjs/libs/protocols/index.js +21 -0
  21. package/lib/cjs/libs/protocols/kriya/index.js +42 -0
  22. package/lib/cjs/libs/protocols/suiswap/index.js +45 -0
  23. package/lib/cjs/libs/protocols/turbos/constants.js +7 -0
  24. package/lib/cjs/libs/protocols/turbos/index.js +53 -0
  25. package/lib/cjs/libs/protocols/utils.js +11 -0
  26. package/lib/cjs/libs/swapWithRoute.js +31 -0
  27. package/lib/cjs/suiClient.js +14 -0
  28. package/lib/cjs/types/aggregator.js +2 -0
  29. package/lib/cjs/types/buildTx.d.ts +4 -0
  30. package/lib/cjs/types/buildTx.d.ts.map +1 -0
  31. package/lib/cjs/types/constants/explorer.d.ts +4 -0
  32. package/lib/cjs/types/constants/explorer.d.ts.map +1 -0
  33. package/lib/cjs/types/constants/tokens.d.ts +3 -0
  34. package/lib/cjs/types/constants/tokens.d.ts.map +1 -0
  35. package/lib/cjs/types/estimateGasFee.d.ts +3 -0
  36. package/lib/cjs/types/estimateGasFee.d.ts.map +1 -0
  37. package/lib/cjs/types/getQuote.d.ts +9 -0
  38. package/lib/cjs/types/getQuote.d.ts.map +1 -0
  39. package/lib/cjs/types/getSuiPrice.d.ts +2 -0
  40. package/lib/cjs/types/getSuiPrice.d.ts.map +1 -0
  41. package/lib/cjs/types/index.d.ts +17 -0
  42. package/lib/cjs/types/index.d.ts.map +1 -0
  43. package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts +7 -0
  44. package/lib/cjs/types/libs/getCoinOjectIdsByAmount.d.ts.map +1 -0
  45. package/lib/cjs/types/libs/getSplitCoinForTx.d.ts +6 -0
  46. package/lib/cjs/types/libs/getSplitCoinForTx.d.ts.map +1 -0
  47. package/lib/cjs/types/libs/groupSwapRoutes.d.ts +3 -0
  48. package/lib/cjs/types/libs/groupSwapRoutes.d.ts.map +1 -0
  49. package/lib/cjs/types/libs/protocols/aftermath/index.d.ts +10 -0
  50. package/lib/cjs/types/libs/protocols/aftermath/index.d.ts.map +1 -0
  51. package/lib/cjs/types/libs/protocols/base.d.ts +37 -0
  52. package/lib/cjs/types/libs/protocols/base.d.ts.map +1 -0
  53. package/lib/cjs/types/libs/protocols/bluemove/index.d.ts +10 -0
  54. package/lib/cjs/types/libs/protocols/bluemove/index.d.ts.map +1 -0
  55. package/lib/cjs/types/libs/protocols/cetus/index.d.ts +10 -0
  56. package/lib/cjs/types/libs/protocols/cetus/index.d.ts.map +1 -0
  57. package/lib/cjs/types/libs/protocols/constants.d.ts +3 -0
  58. package/lib/cjs/types/libs/protocols/constants.d.ts.map +1 -0
  59. package/lib/cjs/types/libs/protocols/deepbook/index.d.ts +12 -0
  60. package/lib/cjs/types/libs/protocols/deepbook/index.d.ts.map +1 -0
  61. package/lib/cjs/types/libs/protocols/flowx/index.d.ts +10 -0
  62. package/lib/cjs/types/libs/protocols/flowx/index.d.ts.map +1 -0
  63. package/lib/cjs/types/libs/protocols/index.d.ts +19 -0
  64. package/lib/cjs/types/libs/protocols/index.d.ts.map +1 -0
  65. package/lib/cjs/types/libs/protocols/kriya/index.d.ts +10 -0
  66. package/lib/cjs/types/libs/protocols/kriya/index.d.ts.map +1 -0
  67. package/lib/cjs/types/libs/protocols/suiswap/index.d.ts +10 -0
  68. package/lib/cjs/types/libs/protocols/suiswap/index.d.ts.map +1 -0
  69. package/lib/cjs/types/libs/protocols/turbos/constants.d.ts +5 -0
  70. package/lib/cjs/types/libs/protocols/turbos/constants.d.ts.map +1 -0
  71. package/lib/cjs/types/libs/protocols/turbos/index.d.ts +10 -0
  72. package/lib/cjs/types/libs/protocols/turbos/index.d.ts.map +1 -0
  73. package/lib/cjs/types/libs/protocols/utils.d.ts +3 -0
  74. package/lib/cjs/types/libs/protocols/utils.d.ts.map +1 -0
  75. package/lib/cjs/types/libs/swapWithRoute.d.ts +10 -0
  76. package/lib/cjs/types/libs/swapWithRoute.d.ts.map +1 -0
  77. package/lib/cjs/types/sui.js +2 -0
  78. package/lib/cjs/types/suiClient.d.ts +4 -0
  79. package/lib/cjs/types/suiClient.d.ts.map +1 -0
  80. package/lib/cjs/types/token.js +2 -0
  81. package/lib/cjs/types/tx.js +2 -0
  82. package/lib/cjs/types/types/aggregator.d.ts +69 -0
  83. package/lib/cjs/types/types/aggregator.d.ts.map +1 -0
  84. package/lib/cjs/types/types/sui.d.ts +4 -0
  85. package/lib/cjs/types/types/sui.d.ts.map +1 -0
  86. package/lib/cjs/types/types/token.d.ts +30 -0
  87. package/lib/cjs/types/types/token.d.ts.map +1 -0
  88. package/lib/cjs/types/types/tx.d.ts +14 -0
  89. package/lib/cjs/types/types/tx.d.ts.map +1 -0
  90. package/lib/cjs/types/types/utilities.d.ts +5 -0
  91. package/lib/cjs/types/types/utilities.d.ts.map +1 -0
  92. package/lib/cjs/types/utilities.js +2 -0
  93. package/lib/cjs/types/utils/number.d.ts +4 -0
  94. package/lib/cjs/types/utils/number.d.ts.map +1 -0
  95. package/lib/cjs/types/utils/sui.d.ts +39 -0
  96. package/lib/cjs/types/utils/sui.d.ts.map +1 -0
  97. package/lib/cjs/types/utils/token.d.ts +6 -0
  98. package/lib/cjs/types/utils/token.d.ts.map +1 -0
  99. package/lib/cjs/utils/number.js +15 -0
  100. package/lib/cjs/utils/sui.js +180 -0
  101. package/lib/cjs/utils/token.js +36 -0
  102. package/lib/esm/buildTx.js +77 -0
  103. package/lib/esm/constants/explorer.js +3 -0
  104. package/lib/esm/constants/tokens.js +2 -0
  105. package/lib/esm/estimateGasFee.js +35 -0
  106. package/lib/esm/getQuote.js +8 -0
  107. package/lib/esm/getSuiPrice.js +13 -0
  108. package/lib/esm/index.mjs +15 -0
  109. package/lib/esm/libs/getCoinOjectIdsByAmount.js +61 -0
  110. package/lib/esm/libs/getSplitCoinForTx.js +39 -0
  111. package/lib/esm/libs/groupSwapRoutes.js +58 -0
  112. package/lib/esm/libs/protocols/aftermath/index.js +37 -0
  113. package/lib/esm/libs/protocols/base.js +18 -0
  114. package/lib/esm/libs/protocols/bluemove/index.js +21 -0
  115. package/lib/esm/libs/protocols/cetus/index.js +41 -0
  116. package/lib/esm/libs/protocols/constants.js +2 -0
  117. package/lib/esm/libs/protocols/deepbook/index.js +80 -0
  118. package/lib/esm/libs/protocols/flowx/index.js +16 -0
  119. package/lib/esm/libs/protocols/index.js +18 -0
  120. package/lib/esm/libs/protocols/kriya/index.js +27 -0
  121. package/lib/esm/libs/protocols/suiswap/index.js +30 -0
  122. package/lib/esm/libs/protocols/turbos/constants.js +4 -0
  123. package/lib/esm/libs/protocols/turbos/index.js +38 -0
  124. package/lib/esm/libs/protocols/utils.js +5 -0
  125. package/lib/esm/libs/swapWithRoute.js +17 -0
  126. package/lib/esm/suiClient.js +10 -0
  127. package/lib/esm/types/aggregator.js +1 -0
  128. package/lib/esm/types/buildTx.d.ts +4 -0
  129. package/lib/esm/types/buildTx.d.ts.map +1 -0
  130. package/lib/esm/types/constants/explorer.d.ts +4 -0
  131. package/lib/esm/types/constants/explorer.d.ts.map +1 -0
  132. package/lib/esm/types/constants/tokens.d.ts +3 -0
  133. package/lib/esm/types/constants/tokens.d.ts.map +1 -0
  134. package/lib/esm/types/estimateGasFee.d.ts +3 -0
  135. package/lib/esm/types/estimateGasFee.d.ts.map +1 -0
  136. package/lib/esm/types/getQuote.d.ts +9 -0
  137. package/lib/esm/types/getQuote.d.ts.map +1 -0
  138. package/lib/esm/types/getSuiPrice.d.ts +2 -0
  139. package/lib/esm/types/getSuiPrice.d.ts.map +1 -0
  140. package/lib/esm/types/index.d.ts +17 -0
  141. package/lib/esm/types/index.d.ts.map +1 -0
  142. package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts +7 -0
  143. package/lib/esm/types/libs/getCoinOjectIdsByAmount.d.ts.map +1 -0
  144. package/lib/esm/types/libs/getSplitCoinForTx.d.ts +6 -0
  145. package/lib/esm/types/libs/getSplitCoinForTx.d.ts.map +1 -0
  146. package/lib/esm/types/libs/groupSwapRoutes.d.ts +3 -0
  147. package/lib/esm/types/libs/groupSwapRoutes.d.ts.map +1 -0
  148. package/lib/esm/types/libs/protocols/aftermath/index.d.ts +10 -0
  149. package/lib/esm/types/libs/protocols/aftermath/index.d.ts.map +1 -0
  150. package/lib/esm/types/libs/protocols/base.d.ts +37 -0
  151. package/lib/esm/types/libs/protocols/base.d.ts.map +1 -0
  152. package/lib/esm/types/libs/protocols/bluemove/index.d.ts +10 -0
  153. package/lib/esm/types/libs/protocols/bluemove/index.d.ts.map +1 -0
  154. package/lib/esm/types/libs/protocols/cetus/index.d.ts +10 -0
  155. package/lib/esm/types/libs/protocols/cetus/index.d.ts.map +1 -0
  156. package/lib/esm/types/libs/protocols/constants.d.ts +3 -0
  157. package/lib/esm/types/libs/protocols/constants.d.ts.map +1 -0
  158. package/lib/esm/types/libs/protocols/deepbook/index.d.ts +12 -0
  159. package/lib/esm/types/libs/protocols/deepbook/index.d.ts.map +1 -0
  160. package/lib/esm/types/libs/protocols/flowx/index.d.ts +10 -0
  161. package/lib/esm/types/libs/protocols/flowx/index.d.ts.map +1 -0
  162. package/lib/esm/types/libs/protocols/index.d.ts +19 -0
  163. package/lib/esm/types/libs/protocols/index.d.ts.map +1 -0
  164. package/lib/esm/types/libs/protocols/kriya/index.d.ts +10 -0
  165. package/lib/esm/types/libs/protocols/kriya/index.d.ts.map +1 -0
  166. package/lib/esm/types/libs/protocols/suiswap/index.d.ts +10 -0
  167. package/lib/esm/types/libs/protocols/suiswap/index.d.ts.map +1 -0
  168. package/lib/esm/types/libs/protocols/turbos/constants.d.ts +5 -0
  169. package/lib/esm/types/libs/protocols/turbos/constants.d.ts.map +1 -0
  170. package/lib/esm/types/libs/protocols/turbos/index.d.ts +10 -0
  171. package/lib/esm/types/libs/protocols/turbos/index.d.ts.map +1 -0
  172. package/lib/esm/types/libs/protocols/utils.d.ts +3 -0
  173. package/lib/esm/types/libs/protocols/utils.d.ts.map +1 -0
  174. package/lib/esm/types/libs/swapWithRoute.d.ts +10 -0
  175. package/lib/esm/types/libs/swapWithRoute.d.ts.map +1 -0
  176. package/lib/esm/types/sui.js +1 -0
  177. package/lib/esm/types/suiClient.d.ts +4 -0
  178. package/lib/esm/types/suiClient.d.ts.map +1 -0
  179. package/lib/esm/types/token.js +1 -0
  180. package/lib/esm/types/tx.js +1 -0
  181. package/lib/esm/types/types/aggregator.d.ts +69 -0
  182. package/lib/esm/types/types/aggregator.d.ts.map +1 -0
  183. package/lib/esm/types/types/sui.d.ts +4 -0
  184. package/lib/esm/types/types/sui.d.ts.map +1 -0
  185. package/lib/esm/types/types/token.d.ts +30 -0
  186. package/lib/esm/types/types/token.d.ts.map +1 -0
  187. package/lib/esm/types/types/tx.d.ts +14 -0
  188. package/lib/esm/types/types/tx.d.ts.map +1 -0
  189. package/lib/esm/types/types/utilities.d.ts +5 -0
  190. package/lib/esm/types/types/utilities.d.ts.map +1 -0
  191. package/lib/esm/types/utilities.js +1 -0
  192. package/lib/esm/types/utils/number.d.ts +4 -0
  193. package/lib/esm/types/utils/number.d.ts.map +1 -0
  194. package/lib/esm/types/utils/sui.d.ts +39 -0
  195. package/lib/esm/types/utils/sui.d.ts.map +1 -0
  196. package/lib/esm/types/utils/token.d.ts +6 -0
  197. package/lib/esm/types/utils/token.d.ts.map +1 -0
  198. package/lib/esm/utils/number.js +8 -0
  199. package/lib/esm/utils/sui.js +167 -0
  200. package/lib/esm/utils/token.js +19 -0
  201. 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
+ }