@layerzerolabs/oft-sui-sdk-v2 0.0.0-stage → 1.2.104

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 (105) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +172 -2
  3. package/dist/2PVXPTJR.js +57 -0
  4. package/dist/2PVXPTJR.js.map +1 -0
  5. package/dist/4DKNPMZP.js +82 -0
  6. package/dist/4DKNPMZP.js.map +1 -0
  7. package/dist/5YQUATNE.js +1292 -0
  8. package/dist/5YQUATNE.js.map +1 -0
  9. package/dist/A3B5LALR.js +67 -0
  10. package/dist/A3B5LALR.js.map +1 -0
  11. package/dist/AQUMEEKG.js +108 -0
  12. package/dist/AQUMEEKG.js.map +1 -0
  13. package/dist/DUMP7ZFT.js +200 -0
  14. package/dist/DUMP7ZFT.js.map +1 -0
  15. package/dist/F5PNGH74.js +212 -0
  16. package/dist/F5PNGH74.js.map +1 -0
  17. package/dist/GLVZWDOK.js +49 -0
  18. package/dist/GLVZWDOK.js.map +1 -0
  19. package/dist/HD4WZG6B.js +38 -0
  20. package/dist/HD4WZG6B.js.map +1 -0
  21. package/dist/I5Z2GNDD.js +9 -0
  22. package/dist/I5Z2GNDD.js.map +1 -0
  23. package/dist/JUS4ARN3.js +78 -0
  24. package/dist/JUS4ARN3.js.map +1 -0
  25. package/dist/TF6IBJWJ.js +3 -0
  26. package/dist/TF6IBJWJ.js.map +1 -0
  27. package/dist/TL7X5YWY.js +39 -0
  28. package/dist/TL7X5YWY.js.map +1 -0
  29. package/dist/U4WYCHEV.js +54 -0
  30. package/dist/U4WYCHEV.js.map +1 -0
  31. package/dist/UIOGD65B.js +35 -0
  32. package/dist/UIOGD65B.js.map +1 -0
  33. package/dist/VUOMXK5T.js +6 -0
  34. package/dist/VUOMXK5T.js.map +1 -0
  35. package/dist/XCARM3YF.js +219 -0
  36. package/dist/XCARM3YF.js.map +1 -0
  37. package/dist/bcs/oft.d.ts +23 -0
  38. package/dist/bcs/oft.d.ts.map +1 -0
  39. package/dist/bcs/oft.js +4 -0
  40. package/dist/bcs/oft.js.map +1 -0
  41. package/dist/index.d.ts +6 -0
  42. package/dist/index.d.ts.map +1 -0
  43. package/dist/index.js +19 -0
  44. package/dist/index.js.map +1 -0
  45. package/dist/modules/oft-composer-manager.d.ts +55 -0
  46. package/dist/modules/oft-composer-manager.d.ts.map +1 -0
  47. package/dist/modules/oft-composer-manager.js +16 -0
  48. package/dist/modules/oft-composer-manager.js.map +1 -0
  49. package/dist/modules/oft.d.ts +502 -0
  50. package/dist/modules/oft.d.ts.map +1 -0
  51. package/dist/modules/oft.js +17 -0
  52. package/dist/modules/oft.js.map +1 -0
  53. package/dist/protocol/args.d.ts +22 -0
  54. package/dist/protocol/args.d.ts.map +1 -0
  55. package/dist/protocol/args.js +4 -0
  56. package/dist/protocol/args.js.map +1 -0
  57. package/dist/protocol/deployments.d.ts +25 -0
  58. package/dist/protocol/deployments.d.ts.map +1 -0
  59. package/dist/protocol/deployments.js +4 -0
  60. package/dist/protocol/deployments.js.map +1 -0
  61. package/dist/protocol/endpoint.d.ts +40 -0
  62. package/dist/protocol/endpoint.d.ts.map +1 -0
  63. package/dist/protocol/endpoint.js +8 -0
  64. package/dist/protocol/endpoint.js.map +1 -0
  65. package/dist/protocol/index.d.ts +12 -0
  66. package/dist/protocol/index.d.ts.map +1 -0
  67. package/dist/protocol/index.js +15 -0
  68. package/dist/protocol/index.js.map +1 -0
  69. package/dist/protocol/oapp.d.ts +14 -0
  70. package/dist/protocol/oapp.d.ts.map +1 -0
  71. package/dist/protocol/oapp.js +6 -0
  72. package/dist/protocol/oapp.js.map +1 -0
  73. package/dist/protocol/ptb-builder.d.ts +17 -0
  74. package/dist/protocol/ptb-builder.d.ts.map +1 -0
  75. package/dist/protocol/ptb-builder.js +7 -0
  76. package/dist/protocol/ptb-builder.js.map +1 -0
  77. package/dist/protocol/sdk.d.ts +27 -0
  78. package/dist/protocol/sdk.d.ts.map +1 -0
  79. package/dist/protocol/sdk.js +14 -0
  80. package/dist/protocol/sdk.js.map +1 -0
  81. package/dist/protocol/simulate.d.ts +10 -0
  82. package/dist/protocol/simulate.d.ts.map +1 -0
  83. package/dist/protocol/simulate.js +5 -0
  84. package/dist/protocol/simulate.js.map +1 -0
  85. package/dist/protocol/types.d.ts +63 -0
  86. package/dist/protocol/types.d.ts.map +1 -0
  87. package/dist/protocol/types.js +4 -0
  88. package/dist/protocol/types.js.map +1 -0
  89. package/dist/protocol/utils.d.ts +11 -0
  90. package/dist/protocol/utils.d.ts.map +1 -0
  91. package/dist/protocol/utils.js +5 -0
  92. package/dist/protocol/utils.js.map +1 -0
  93. package/dist/protocol/validators.d.ts +44 -0
  94. package/dist/protocol/validators.d.ts.map +1 -0
  95. package/dist/protocol/validators.js +7 -0
  96. package/dist/protocol/validators.js.map +1 -0
  97. package/dist/protocol/zro.d.ts +13 -0
  98. package/dist/protocol/zro.d.ts.map +1 -0
  99. package/dist/protocol/zro.js +4 -0
  100. package/dist/protocol/zro.js.map +1 -0
  101. package/dist/types.d.ts +31 -0
  102. package/dist/types.d.ts.map +1 -0
  103. package/dist/types.js +4 -0
  104. package/dist/types.js.map +1 -0
  105. package/package.json +45 -3
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2026 - LayerZero Labs Ltd.
2
+
3
+ Permission is hereby granted, free of charge, to any
4
+ person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the
6
+ Software without restriction, including without
7
+ limitation the rights to use, copy, modify, merge,
8
+ publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software
10
+ is furnished to do so, subject to the following
11
+ conditions:
12
+ The above copyright notice and this permission notice
13
+ shall be included in all copies or substantial portions
14
+ of the Software.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
22
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,173 @@
1
- # Temporary Holding Version
1
+ # @layerzerolabs/oft-sui-sdk-v2
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ LayerZero V2 SUI OFT (Omnichain Fungible Token) SDK
4
+
5
+ ## Overview
6
+
7
+ This SDK provides TypeScript bindings for interacting with LayerZero V2 OFT contracts on the SUI blockchain. It enables developers to create, manage, and transfer omnichain fungible tokens across different blockchains using the LayerZero protocol.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @layerzerolabs/oft-sui-sdk-v2
13
+ # or
14
+ yarn add @layerzerolabs/oft-sui-sdk-v2
15
+ # or
16
+ pnpm add @layerzerolabs/oft-sui-sdk-v2
17
+ ```
18
+
19
+ The package is ESM only. It replaces `@layerzerolabs/lz-sui-oft-sdk-v2`, which also shipped a CommonJS build. CommonJS code can `require` it only on Node versions with `require(esm)` (20.19+ or 22.12+). Otherwise, use `import` or a dynamic `import()`.
20
+
21
+ TypeScript consumers need `moduleResolution: bundler`. Under `node16` or `nodenext`, the package's type declarations do not resolve.
22
+
23
+ The package uses `@mysten/sui` 2.x and does not depend on `@layerzerolabs/lz-sui-sdk-v2`. It brings its own client for the LayerZero endpoint send path, `SuiProtocolSDK`. Pass it any 2.x client (`SuiGrpcClient`, `SuiJsonRpcClient` or `SuiGraphQLClient`). For `@mysten/sui` 1.x, use `@layerzerolabs/oft-sui-sdk`, which keeps the 1.x API and depends on `lz-sui-sdk-v2`.
24
+
25
+ `sendMoveCall` takes the native fee with `tx.coin()`. If the sender's address balance covers the fee, the fee is withdrawn from the address balance, so parallel sends from one account do not compete for the gas coin. Otherwise it is split from the gas coin, as before.
26
+
27
+ ## Features
28
+
29
+ - **Cross-chain Transfers**: Send tokens across different blockchains
30
+ - **OFT & OFT Adapter**: Interact with deployed OFT & OFT Adapter
31
+ - **OFT Composer Manager**: Advanced composition functionality for complex cross-chain operations
32
+
33
+ ## Usage
34
+
35
+ ### Initialize OFT
36
+
37
+ Create a new OFT that mints/burns tokens for cross-chain transfers:
38
+
39
+ ```typescript
40
+ import { OFT, SuiProtocolSDK } from '@layerzerolabs/oft-sui-sdk-v2';
41
+ import { SuiGrpcClient } from '@mysten/sui/grpc';
42
+ import { Transaction } from '@mysten/sui/transactions';
43
+
44
+ const suiClient = new SuiGrpcClient({
45
+ network: 'mainnet',
46
+ baseUrl: 'https://fullnode.mainnet.sui.io:443',
47
+ });
48
+
49
+ // Initialize LayerZero protocol SDK
50
+ const protocolSDK = new SuiProtocolSDK({
51
+ client: suiClient,
52
+ stage: 'mainnet',
53
+ });
54
+
55
+ // Create OFT instance with its call cap ID and coin type
56
+ const oft = new OFT(protocolSDK, oftCallCapId, undefined, coinType);
57
+
58
+ // Initialize OFT with treasury capability (for minting/burning)
59
+ const initTx = new Transaction();
60
+ const [adminCap, migrationCap] = oft.initOftMoveCall(
61
+ initTx,
62
+ coinType, // e.g., "0x123::my_coin::MY_COIN"
63
+ oftInitTicketId, // OFT creation ticket
64
+ oappObjectId, // Associated OApp object
65
+ treasuryCapId, // Treasury cap for minting/burning
66
+ metadataId, // Coin metadata
67
+ 6, // Shared decimals for cross-chain compatibility
68
+ );
69
+
70
+ // Transfer capabilities to your address
71
+ initTx.transferObjects([adminCap, migrationCap], initTx.pure.address(yourAddress));
72
+
73
+ // Execute the transaction
74
+ const result = await suiClient.signAndExecuteTransaction({ transaction: initTx, signer });
75
+ ```
76
+
77
+ ### Initialize OFT Adapter
78
+
79
+ Create an OFT Adapter that wraps existing tokens:
80
+
81
+ ```typescript
82
+ // Initialize OFT Adapter (wraps existing tokens without minting/burning)
83
+ const adapterTx = new Transaction();
84
+ const [adminCap, migrationCap] = oft.initOftAdapterMoveCall(
85
+ adapterTx,
86
+ coinType, // e.g., "0x123::my_coin::MY_COIN"
87
+ oftInitTicketId, // OFT creation ticket
88
+ oappObjectId, // Associated OApp object
89
+ metadataId, // Coin metadata
90
+ 6, // Shared decimals for cross-chain compatibility
91
+ );
92
+
93
+ // Transfer capabilities to your address
94
+ adapterTx.transferObjects([adminCap, migrationCap], adapterTx.pure.address(yourAddress));
95
+
96
+ // Execute the transaction
97
+ const result = await suiClient.signAndExecuteTransaction({ transaction: adapterTx, signer });
98
+ ```
99
+
100
+ ### Register OFT with LayerZero Endpoint
101
+
102
+ ```typescript
103
+ // Register the OFT as an OApp with the LayerZero endpoint
104
+ const registerTx = new Transaction();
105
+
106
+ await oft.registerOAppMoveCall(
107
+ registerTx,
108
+ coinType, // Coin type
109
+ oftObjectId, // Created OFT object ID
110
+ oappObjectId, // Associated OApp object ID
111
+ composerManagerId, // OFT Composer Manager for handling compose messages
112
+ );
113
+
114
+ // Execute the transaction
115
+ const result = await suiClient.signAndExecuteTransaction({ transaction: registerTx, signer });
116
+ ```
117
+
118
+ ### Set Peer for Cross-Chain Communication
119
+
120
+ OApp configuration (peers, libraries, enforced options) is not part of this package. Use the OApp tooling for it.
121
+
122
+ ### Quote and Send Tokens
123
+
124
+ ```typescript
125
+ // Create OFT instance with its OFT object ID and coin type
126
+ const oft = new OFT(protocolSDK, oftCallCapId, oftObjectId, coinType);
127
+
128
+ // Prepare send parameters
129
+ const sendParam = {
130
+ dstEid: 30102, // Destination endpoint ID
131
+ to: recipientAddressBytes, // Recipient address as Uint8Array (32 bytes)
132
+ amountLd: 1000000n, // Amount in local decimals
133
+ minAmountLd: 990000n, // Minimum amount (slippage protection)
134
+ extraOptions: new Uint8Array(0), // LayerZero execution options
135
+ composeMsg: new Uint8Array(0), // Optional compose message
136
+ oftCmd: new Uint8Array(0), // Optional OFT command (unused in default OFT)
137
+ };
138
+
139
+ // Quote the transfer fees
140
+ const messagingFee = await oft.quoteSend(
141
+ senderAddress,
142
+ sendParam,
143
+ false, // payInZro: false = pay in native token
144
+ );
145
+
146
+ // Execute the transfer
147
+ const tx = new Transaction();
148
+
149
+ // Take the amount from the sender's address balance or coins
150
+ const coin = tx.coin({ type: coinType, balance: sendParam.amountLd });
151
+
152
+ // Send the tokens
153
+ await oft.sendMoveCall(
154
+ tx,
155
+ senderAddress,
156
+ sendParam,
157
+ coin,
158
+ messagingFee.nativeFee,
159
+ messagingFee.zroFee,
160
+ senderAddress, // refund address
161
+ );
162
+
163
+ // Transfer any remaining coins back to sender
164
+ tx.transferObjects([coin], senderAddress);
165
+ ```
166
+
167
+ ## Documentation
168
+
169
+ For detailed documentation and examples, please visit the [LayerZero documentation](https://docs.layerzero.network/).
170
+
171
+ ## License
172
+
173
+ MIT
@@ -0,0 +1,57 @@
1
+ import { OApp } from './HD4WZG6B.js';
2
+ import { PtbBuilder } from './DUMP7ZFT.js';
3
+ import { Utils } from './4DKNPMZP.js';
4
+ import { Zro } from './GLVZWDOK.js';
5
+ import { mergeDefaultPackageConfig, mergeDefaultObjectConfig } from './JUS4ARN3.js';
6
+ import { Endpoint } from './F5PNGH74.js';
7
+ import { __name } from './VUOMXK5T.js';
8
+
9
+ // src/protocol/sdk.ts
10
+ var SuiProtocolSDK = class {
11
+ static {
12
+ __name(this, "SuiProtocolSDK");
13
+ }
14
+ client;
15
+ stage;
16
+ packages;
17
+ objects;
18
+ #utils;
19
+ #zro;
20
+ #ptbBuilder;
21
+ #endpoint;
22
+ #oapps = /* @__PURE__ */ new Map();
23
+ constructor(options) {
24
+ this.client = options.client;
25
+ this.stage = options.stage ?? "mainnet";
26
+ this.packages = mergeDefaultPackageConfig(this.stage, options.packages);
27
+ this.objects = mergeDefaultObjectConfig(this.stage, options.objects);
28
+ this.#utils = new Utils(this.packages.utils, this.client);
29
+ this.#zro = new Zro(this.packages.zro, this.#utils);
30
+ this.#ptbBuilder = new PtbBuilder(this.packages.ptbMoveCall, this.client);
31
+ this.#endpoint = new Endpoint(this.client, this.packages, this.objects, this.#ptbBuilder);
32
+ }
33
+ getEndpoint() {
34
+ return this.#endpoint;
35
+ }
36
+ getZro() {
37
+ return this.#zro;
38
+ }
39
+ getUtils() {
40
+ return this.#utils;
41
+ }
42
+ getPtbBuilder() {
43
+ return this.#ptbBuilder;
44
+ }
45
+ getOApp(callCapId) {
46
+ let oapp = this.#oapps.get(callCapId);
47
+ if (oapp === void 0) {
48
+ oapp = new OApp(this.packages.oapp, callCapId, this.client, this.#endpoint);
49
+ this.#oapps.set(callCapId, oapp);
50
+ }
51
+ return oapp;
52
+ }
53
+ };
54
+
55
+ export { SuiProtocolSDK };
56
+ //# sourceMappingURL=2PVXPTJR.js.map
57
+ //# sourceMappingURL=2PVXPTJR.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/protocol/sdk.ts"],"names":["SuiProtocolSDK","client","stage","packages","objects","Map","options","mergeDefaultPackageConfig","mergeDefaultObjectConfig","Utils","utils","Zro","zro","PtbBuilder","ptbMoveCall","Endpoint","getEndpoint","getZro","getUtils","getPtbBuilder","getOApp","callCapId","oapp","get","undefined","OApp","set"],"mappings":";;;;;;;;;AAsBO,IAAMA,iBAAN,MAAMA;EApBb;;;AAqBaC,EAAAA,MAAAA;AACAC,EAAAA,KAAAA;AACAC,EAAAA,QAAAA;AACAC,EAAAA,OAAAA;AACA,EAAA,MAAA;AACA,EAAA,IAAA;AACA,EAAA,WAAA;AACA,EAAA,SAAA;AACA,EAAA,MAAA,uBAAaC,GAAAA,EAAAA;AAEtB,EAAA,WAAA,CAAYC,OAAAA,EAAgC;AACxC,IAAA,IAAA,CAAKL,SAASK,OAAAA,CAAQL,MAAAA;AACtB,IAAA,IAAA,CAAKC,KAAAA,GAAQI,QAAQJ,KAAAA,IAAS,SAAA;AAC9B,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AA,EAAQ,IAAA,CAAK,SAAS,CAAA;AAC1E,MAAA,IAAA,CAAK,MAAA,CAAOyB,GAAAA,CAAIL,SAAAA,EAAWC,IAAAA,CAAAA;AAC/B,IAAA;AACA,IAAA,OAAOA,IAAAA;AACX,EAAA;AACJ","file":"2PVXPTJR.js","sourcesContent":["import type { ClientWithCoreApi } from '@mysten/sui/client';\n\nimport {\n mergeDefaultObjectConfig,\n mergeDefaultPackageConfig,\n type ObjectOptions,\n type PackageOptions,\n type Stage,\n} from './deployments';\nimport { Endpoint } from './endpoint';\nimport { OApp } from './oapp';\nimport { PtbBuilder } from './ptb-builder';\nimport { Utils } from './utils';\nimport { Zro } from './zro';\n\nexport interface SuiProtocolSDKOptions {\n client: ClientWithCoreApi;\n stage?: Stage;\n packages?: Partial<PackageOptions>;\n objects?: Partial<ObjectOptions>;\n}\n\nexport class SuiProtocolSDK {\n readonly client: ClientWithCoreApi;\n readonly stage: Stage;\n readonly packages: PackageOptions;\n readonly objects: ObjectOptions;\n readonly #utils: Utils;\n readonly #zro: Zro;\n readonly #ptbBuilder: PtbBuilder;\n readonly #endpoint: Endpoint;\n readonly #oapps = new Map<string, OApp>();\n\n constructor(options: SuiProtocolSDKOptions) {\n this.client = options.client;\n this.stage = options.stage ?? 'mainnet';\n this.packages = mergeDefaultPackageConfig(this.stage, options.packages);\n this.objects = mergeDefaultObjectConfig(this.stage, options.objects);\n this.#utils = new Utils(this.packages.utils, this.client);\n this.#zro = new Zro(this.packages.zro, this.#utils);\n this.#ptbBuilder = new PtbBuilder(this.packages.ptbMoveCall, this.client);\n this.#endpoint = new Endpoint(this.client, this.packages, this.objects, this.#ptbBuilder);\n }\n\n getEndpoint(): Endpoint {\n return this.#endpoint;\n }\n\n getZro(): Zro {\n return this.#zro;\n }\n\n getUtils(): Utils {\n return this.#utils;\n }\n\n getPtbBuilder(): PtbBuilder {\n return this.#ptbBuilder;\n }\n\n getOApp(callCapId: string): OApp {\n let oapp = this.#oapps.get(callCapId);\n if (oapp === undefined) {\n oapp = new OApp(this.packages.oapp, callCapId, this.client, this.#endpoint);\n this.#oapps.set(callCapId, oapp);\n }\n return oapp;\n }\n}\n"]}
@@ -0,0 +1,82 @@
1
+ import { asBytes, asU64 } from './UIOGD65B.js';
2
+ import { __name } from './VUOMXK5T.js';
3
+
4
+ // src/protocol/utils.ts
5
+ var Utils = class {
6
+ static {
7
+ __name(this, "Utils");
8
+ }
9
+ packageId;
10
+ client;
11
+ constructor(packageId, client) {
12
+ this.packageId = packageId;
13
+ this.client = client;
14
+ }
15
+ fromBytesMoveCall(tx, peer) {
16
+ return tx.moveCall({
17
+ target: `${this.packageId}::bytes32::from_bytes`,
18
+ arguments: [
19
+ asBytes(tx, peer)
20
+ ]
21
+ });
22
+ }
23
+ async splitCoinMoveCall(tx, coinType, owner, amount, limit = 200, pageSize = 50) {
24
+ const sufficientCoins = await this.#fetchSufficientCoins(owner, coinType, amount, limit, pageSize);
25
+ const totalBalance = sufficientCoins.reduce((sum, coin) => sum + BigInt(coin.balance), 0n);
26
+ const primaryCoin = sufficientCoins.find((coin) => BigInt(coin.balance) >= amount) ?? sufficientCoins[0];
27
+ const primaryCoinObj = tx.object(primaryCoin.objectId);
28
+ if (primaryCoin === sufficientCoins[0] && sufficientCoins.length > 1) {
29
+ tx.mergeCoins(primaryCoinObj, sufficientCoins.slice(1).map((coin) => tx.object(coin.objectId)));
30
+ }
31
+ const splitCoin = tx.splitCoins(primaryCoinObj, [
32
+ asU64(tx, amount)
33
+ ]);
34
+ if (totalBalance === amount) {
35
+ tx.moveCall({
36
+ target: "0x2::coin::destroy_zero",
37
+ arguments: [
38
+ primaryCoinObj
39
+ ],
40
+ typeArguments: [
41
+ coinType
42
+ ]
43
+ });
44
+ }
45
+ return splitCoin;
46
+ }
47
+ async #fetchSufficientCoins(owner, coinType, amount, limit, pageSize) {
48
+ const coins = [];
49
+ let accumulatedBalance = 0n;
50
+ let cursor = null;
51
+ do {
52
+ const coinsResponse = await this.client.core.listCoins({
53
+ owner,
54
+ coinType,
55
+ cursor,
56
+ limit: pageSize
57
+ });
58
+ if (coinsResponse.objects.length === 0) {
59
+ break;
60
+ }
61
+ for (const coin of coinsResponse.objects) {
62
+ coins.push(coin);
63
+ accumulatedBalance += BigInt(coin.balance);
64
+ if (accumulatedBalance >= amount) {
65
+ return coins;
66
+ }
67
+ if (coins.length >= limit) {
68
+ throw new Error(`Insufficient ${coinType} balance: reached maximum coin limit (${limit}) but still need ${amount - accumulatedBalance} more`);
69
+ }
70
+ }
71
+ cursor = coinsResponse.hasNextPage ? coinsResponse.cursor : null;
72
+ } while (cursor !== null);
73
+ if (accumulatedBalance < amount) {
74
+ throw new Error(`Insufficient ${coinType} balance: only found ${accumulatedBalance} but need ${amount} (shortfall: ${amount - accumulatedBalance})`);
75
+ }
76
+ return coins;
77
+ }
78
+ };
79
+
80
+ export { Utils };
81
+ //# sourceMappingURL=4DKNPMZP.js.map
82
+ //# sourceMappingURL=4DKNPMZP.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/protocol/utils.ts"],"names":["Utils","packageId","client","fromBytesMoveCall","tx","peer","moveCall","target","arguments","asBytes","splitCoinMoveCall","coinType","owner","amount","limit","pageSize","sufficientCoins","totalBalance","reduce","sum","coin","BigInt","balance","primaryCoin","find","primaryCoinObj","object","objectId","length","mergeCoins","slice","map","splitCoin","splitCoins","asU64","typeArguments","coins","accumulatedBalance","cursor","coinsResponse","core","listCoins","objects","push","Error","hasNextPage"],"mappings":";;;;AAKO,IAAMA,QAAN,MAAMA;EAFb;;;;;AAGI,EAAA,WAAA,CACoBC,WACAC,MAAAA,EAClB;SAFkBD,SAAAA,GAAAA,SAAAA;SACAC,MAAAA,GAAAA,MAAAA;AACjB,EAAA;AAEHC,EAAAA,iBAAAA,CAAkBC,IAAiBC,IAAAA,EAA2D;AAC1F,IAAA,OAAOD,GAAGE,QAAAA,CAAS;MACfC,MAAAA,EAAQ,CAAA,EAAG,KAAKN,SAAS,CAAA,qBAAA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;AACJ,MAAA;AACAC,MAAAA,MAAAA,GAASC,aAAAA,CAAcM,WAAAA,GAAcN,aAAAA,CAAcD,MAAAA,GAAS,IAAA;AAChE,IAAA,CAAA,QAASA,MAAAA,KAAW,IAAA;AACpB,IAAA,IAAID,qBAAqBxB,MAAAA,EAAQ;AAC7B,MAAA,MAAM,IAAI+B,KAAAA,CACN,CAAA,aAAA,EAAgBjC,QAAAA,CAAAA,qBAAAA,EAAgC0B,kBAAAA,CAAAA,UAAAA,EAA+BxB,MAAAA,CAAAA,aAAAA,EAAsBA,MAAAA,GAASwB,kBAAAA,CAAAA,CAAAA,CAAqB,CAAA;AAE3I,IAAA;AACA,IAAA,OAAOD,KAAAA;AACX,EAAA;AACJ","file":"4DKNPMZP.js","sourcesContent":["import type { ClientWithCoreApi, SuiClientTypes } from '@mysten/sui/client';\nimport type { Transaction, TransactionArgument, TransactionResult } from '@mysten/sui/transactions';\n\nimport { asBytes, asU64 } from './args';\n\nexport class Utils {\n constructor(\n public readonly packageId: string,\n public readonly client: ClientWithCoreApi,\n ) {}\n\n fromBytesMoveCall(tx: Transaction, peer: Uint8Array | TransactionArgument): TransactionResult {\n return tx.moveCall({\n target: `${this.packageId}::bytes32::from_bytes`,\n arguments: [asBytes(tx, peer)],\n });\n }\n\n async splitCoinMoveCall(\n tx: Transaction,\n coinType: string,\n owner: string,\n amount: bigint,\n limit = 200,\n pageSize = 50,\n ): Promise<TransactionResult> {\n const sufficientCoins = await this.#fetchSufficientCoins(\n owner,\n coinType,\n amount,\n limit,\n pageSize,\n );\n const totalBalance = sufficientCoins.reduce((sum, coin) => sum + BigInt(coin.balance), 0n);\n const primaryCoin =\n sufficientCoins.find((coin) => BigInt(coin.balance) >= amount) ?? sufficientCoins[0];\n const primaryCoinObj = tx.object(primaryCoin.objectId);\n if (primaryCoin === sufficientCoins[0] && sufficientCoins.length > 1) {\n tx.mergeCoins(\n primaryCoinObj,\n sufficientCoins.slice(1).map((coin) => tx.object(coin.objectId)),\n );\n }\n const splitCoin = tx.splitCoins(primaryCoinObj, [asU64(tx, amount)]);\n if (totalBalance === amount) {\n tx.moveCall({\n target: '0x2::coin::destroy_zero',\n arguments: [primaryCoinObj],\n typeArguments: [coinType],\n });\n }\n return splitCoin;\n }\n\n async #fetchSufficientCoins(\n owner: string,\n coinType: string,\n amount: bigint,\n limit: number,\n pageSize: number,\n ): Promise<SuiClientTypes.Coin[]> {\n const coins: SuiClientTypes.Coin[] = [];\n let accumulatedBalance = 0n;\n let cursor: string | null = null;\n do {\n const coinsResponse: SuiClientTypes.ListCoinsResponse =\n await this.client.core.listCoins({\n owner,\n coinType,\n cursor,\n limit: pageSize,\n });\n if (coinsResponse.objects.length === 0) {\n break;\n }\n for (const coin of coinsResponse.objects) {\n coins.push(coin);\n accumulatedBalance += BigInt(coin.balance);\n if (accumulatedBalance >= amount) {\n return coins;\n }\n if (coins.length >= limit) {\n throw new Error(\n `Insufficient ${coinType} balance: reached maximum coin limit (${limit}) but still need ${amount - accumulatedBalance} more`,\n );\n }\n }\n cursor = coinsResponse.hasNextPage ? coinsResponse.cursor : null;\n } while (cursor !== null);\n if (accumulatedBalance < amount) {\n throw new Error(\n `Insufficient ${coinType} balance: only found ${accumulatedBalance} but need ${amount} (shortfall: ${amount - accumulatedBalance})`,\n );\n }\n return coins;\n }\n}\n"]}