@mysten/sui 2.14.1 → 2.16.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/CHANGELOG.md +29 -0
- package/dist/bcs/bcs.d.mts +6 -6
- package/dist/bcs/index.d.mts +20 -20
- package/dist/client/mvr.d.mts.map +1 -1
- package/dist/client/mvr.mjs +3 -0
- package/dist/client/mvr.mjs.map +1 -1
- package/dist/grpc/index.d.mts +3 -1
- package/dist/grpc/index.mjs +2 -1
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
- package/dist/transactions/Transaction.d.mts +9 -9
- package/dist/transactions/Transaction.d.mts.map +1 -1
- package/dist/transactions/data/v1.d.mts +220 -220
- package/dist/transactions/data/v1.d.mts.map +1 -1
- package/dist/transactions/data/v2.d.mts +16 -16
- package/dist/transactions/data/v2.d.mts.map +1 -1
- package/dist/transactions/intents/CoinWithBalance.mjs +35 -5
- package/dist/transactions/intents/CoinWithBalance.mjs.map +1 -1
- package/dist/utils/format.d.mts +5 -1
- package/dist/utils/format.d.mts.map +1 -1
- package/dist/utils/format.mjs +19 -1
- package/dist/utils/format.mjs.map +1 -1
- package/dist/utils/index.d.mts +2 -2
- package/dist/utils/index.mjs +2 -2
- package/dist/version.mjs +2 -2
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/bcs.d.mts +14 -14
- package/dist/zklogin/bcs.d.mts.map +1 -1
- package/docs/clients/grpc.md +55 -2
- package/docs/llms-index.md +5 -5
- package/docs/utils/index.md +7 -0
- package/package.json +4 -2
- package/src/client/mvr.ts +12 -0
- package/src/grpc/index.ts +6 -0
- package/src/transactions/intents/CoinWithBalance.ts +41 -6
- package/src/utils/format.ts +36 -0
- package/src/utils/index.ts +1 -1
- package/src/version.ts +2 -2
|
@@ -72,6 +72,7 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
72
72
|
outputKind: outputKind ?? "coin"
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
+
if (totalByType.has("gas") && totalByType.has(SUI_TYPE)) throw new Error("Cannot mix SUI CoinWithBalance intents that use the gas coin with ones that do not (useGasCoin: false). Use one or the other.");
|
|
75
76
|
const usedIds = /* @__PURE__ */ new Set();
|
|
76
77
|
for (const input of transactionData.inputs) {
|
|
77
78
|
if (input.Object?.ImmOrOwnedObject) usedIds.add(input.Object.ImmOrOwnedObject.objectId);
|
|
@@ -99,6 +100,7 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
99
100
|
}) : null]);
|
|
100
101
|
const mergedCoins = /* @__PURE__ */ new Map();
|
|
101
102
|
const exactBalanceByType = /* @__PURE__ */ new Map();
|
|
103
|
+
const usedAddressBalance = /* @__PURE__ */ new Set();
|
|
102
104
|
const typeState = /* @__PURE__ */ new Map();
|
|
103
105
|
let index = 0;
|
|
104
106
|
while (index < transactionData.commands.length) {
|
|
@@ -141,7 +143,31 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
141
143
|
if (!typeState.has(type)) {
|
|
142
144
|
const intents = intentsForType;
|
|
143
145
|
const sources = [];
|
|
144
|
-
if (
|
|
146
|
+
if (addressBalance >= totalRequired) {
|
|
147
|
+
usedAddressBalance.add(type);
|
|
148
|
+
commands.push(TransactionCommands.MoveCall({
|
|
149
|
+
target: "0x2::coin::redeem_funds",
|
|
150
|
+
typeArguments: [coinType],
|
|
151
|
+
arguments: [transactionData.addInput("withdrawal", Inputs.FundsWithdrawal({
|
|
152
|
+
reservation: {
|
|
153
|
+
$kind: "MaxAmountU64",
|
|
154
|
+
MaxAmountU64: String(totalRequired)
|
|
155
|
+
},
|
|
156
|
+
typeArg: {
|
|
157
|
+
$kind: "Balance",
|
|
158
|
+
Balance: coinType
|
|
159
|
+
},
|
|
160
|
+
withdrawFrom: {
|
|
161
|
+
$kind: "Sender",
|
|
162
|
+
Sender: true
|
|
163
|
+
}
|
|
164
|
+
}))]
|
|
165
|
+
}));
|
|
166
|
+
sources.push({
|
|
167
|
+
$kind: "Result",
|
|
168
|
+
Result: index + commands.length - 1
|
|
169
|
+
});
|
|
170
|
+
} else if (type === "gas") sources.push({
|
|
145
171
|
$kind: "GasCoin",
|
|
146
172
|
GasCoin: true
|
|
147
173
|
});
|
|
@@ -156,6 +182,7 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
156
182
|
version: coin.version
|
|
157
183
|
})));
|
|
158
184
|
if (abNeeded > 0n) {
|
|
185
|
+
usedAddressBalance.add(type);
|
|
159
186
|
commands.push(TransactionCommands.MoveCall({
|
|
160
187
|
target: "0x2::coin::redeem_funds",
|
|
161
188
|
typeArguments: [coinType],
|
|
@@ -216,15 +243,18 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
216
243
|
index += commands.length;
|
|
217
244
|
}
|
|
218
245
|
for (const [type, mergedCoin] of mergedCoins) {
|
|
219
|
-
if (type === "gas") continue;
|
|
220
|
-
|
|
246
|
+
if (type === "gas" && !usedAddressBalance.has(type)) continue;
|
|
247
|
+
const coinType = type === "gas" ? SUI_TYPE : type;
|
|
248
|
+
const hasBalanceIntent = intentsByType.get(type)?.some((i) => i.outputKind === "balance");
|
|
249
|
+
const sourcedFromAB = usedAddressBalance.has(type);
|
|
250
|
+
if (hasBalanceIntent || sourcedFromAB) transactionData.commands.push(TransactionCommands.MoveCall({
|
|
221
251
|
target: "0x2::coin::send_funds",
|
|
222
|
-
typeArguments: [
|
|
252
|
+
typeArguments: [coinType],
|
|
223
253
|
arguments: [mergedCoin, transactionData.addInput("pure", Inputs.Pure(suiBcs.Address.serialize(transactionData.sender)))]
|
|
224
254
|
}));
|
|
225
255
|
else if (exactBalanceByType.get(type)) transactionData.commands.push(TransactionCommands.MoveCall({
|
|
226
256
|
target: "0x2::coin::destroy_zero",
|
|
227
|
-
typeArguments: [
|
|
257
|
+
typeArguments: [coinType],
|
|
228
258
|
arguments: [mergedCoin]
|
|
229
259
|
}));
|
|
230
260
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CoinWithBalance.mjs","names":["bcs","balance"],"sources":["../../../src/transactions/intents/CoinWithBalance.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, optional, parse, picklist, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { TransactionCommands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../resolve.js';\nimport type { Transaction, TransactionResult } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\nimport type { ClientWithCoreApi, SuiClientTypes } from '../../client/index.js';\n\nexport const COIN_WITH_BALANCE = 'CoinWithBalance';\nconst SUI_TYPE = normalizeStructTag('0x2::sui::SUI');\n\nexport function coinWithBalance({\n\ttype = SUI_TYPE,\n\tbalance,\n\tuseGasCoin = true,\n}: {\n\tbalance: bigint | number;\n\ttype?: string;\n\tuseGasCoin?: boolean;\n}): (tx: Transaction) => TransactionResult {\n\tlet coinResult: TransactionResult | null = null;\n\n\treturn (tx: Transaction) => {\n\t\tif (coinResult) {\n\t\t\treturn coinResult;\n\t\t}\n\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\tcoinResult = tx.add(\n\t\t\tTransactionCommands.Intent({\n\t\t\t\tname: COIN_WITH_BALANCE,\n\t\t\t\tinputs: {},\n\t\t\t\tdata: {\n\t\t\t\t\ttype: coinType === SUI_TYPE && useGasCoin ? 'gas' : coinType,\n\t\t\t\t\tbalance: BigInt(balance),\n\t\t\t\t\toutputKind: 'coin',\n\t\t\t\t} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\n\t\treturn coinResult;\n\t};\n}\n\nexport function createBalance({\n\ttype = SUI_TYPE,\n\tbalance,\n\tuseGasCoin = true,\n}: {\n\tbalance: bigint | number;\n\ttype?: string;\n\tuseGasCoin?: boolean;\n}): (tx: Transaction) => TransactionResult {\n\tlet balanceResult: TransactionResult | null = null;\n\n\treturn (tx: Transaction) => {\n\t\tif (balanceResult) {\n\t\t\treturn balanceResult;\n\t\t}\n\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\tbalanceResult = tx.add(\n\t\t\tTransactionCommands.Intent({\n\t\t\t\tname: COIN_WITH_BALANCE,\n\t\t\t\tinputs: {},\n\t\t\t\tdata: {\n\t\t\t\t\ttype: coinType === SUI_TYPE && useGasCoin ? 'gas' : coinType,\n\t\t\t\t\tbalance: BigInt(balance),\n\t\t\t\t\toutputKind: 'balance',\n\t\t\t\t} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\n\t\treturn balanceResult;\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n\toutputKind: optional(picklist(['coin', 'balance'])),\n});\n\nexport async function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\ttype IntentInfo = { balance: bigint; outputKind: 'coin' | 'balance' };\n\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\tconst intentsByType = new Map<string, IntentInfo[]>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\t// First pass: scan intents, collect per-type data, and resolve zero-balance intents in place.\n\tfor (const [i, command] of transactionData.commands.entries()) {\n\t\tif (command.$kind !== '$Intent' || command.$Intent.name !== COIN_WITH_BALANCE) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { type, balance, outputKind } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t// Zero-balance intents are resolved immediately — no coins or AB needed.\n\t\t// This is a 1:1 replacement so indices don't shift.\n\t\tif (balance === 0n) {\n\t\t\tconst coinType = type === 'gas' ? SUI_TYPE : type;\n\t\t\ttransactionData.replaceCommand(\n\t\t\t\ti,\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: (outputKind ?? 'coin') === 'balance' ? '0x2::balance::zero' : '0x2::coin::zero',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t}),\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (type !== 'gas') {\n\t\t\tcoinTypes.add(type);\n\t\t}\n\n\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\n\t\tif (!intentsByType.has(type)) intentsByType.set(type, []);\n\t\tintentsByType.get(type)!.push({ balance, outputKind: outputKind ?? 'coin' });\n\t}\n\n\tconst usedIds = new Set<string>();\n\n\tfor (const input of transactionData.inputs) {\n\t\tif (input.Object?.ImmOrOwnedObject) {\n\t\t\tusedIds.add(input.Object.ImmOrOwnedObject.objectId);\n\t\t}\n\t\tif (input.UnresolvedObject?.objectId) {\n\t\t\tusedIds.add(input.UnresolvedObject.objectId);\n\t\t}\n\t}\n\n\tconst coinsByType = new Map<string, SuiClientTypes.Coin[]>();\n\tconst addressBalanceByType = new Map<string, bigint>();\n\tconst client = buildOptions.client;\n\n\tif (!client) {\n\t\tthrow new Error(\n\t\t\t'Client must be provided to build or serialize transactions with CoinWithBalance intents',\n\t\t);\n\t}\n\n\tawait Promise.all([\n\t\t...[...coinTypes].map(async (coinType) => {\n\t\t\tconst { coins, addressBalance } = await getCoinsAndBalanceOfType({\n\t\t\t\tcoinType,\n\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\tclient,\n\t\t\t\towner: transactionData.sender!,\n\t\t\t\tusedIds,\n\t\t\t});\n\n\t\t\tcoinsByType.set(coinType, coins);\n\t\t\taddressBalanceByType.set(coinType, addressBalance);\n\t\t}),\n\t\ttotalByType.has('gas')\n\t\t\t? await client.core\n\t\t\t\t\t.getBalance({\n\t\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\t\tcoinType: SUI_TYPE,\n\t\t\t\t\t})\n\t\t\t\t\t.then(({ balance }) => {\n\t\t\t\t\t\taddressBalanceByType.set('gas', BigInt(balance.addressBalance));\n\t\t\t\t\t})\n\t\t\t: null,\n\t]);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\tconst exactBalanceByType = new Map<string, boolean>();\n\n\t// Per-type state for Path 2 combined splits\n\ttype TypeState = { results: Argument[]; nextIntent: number };\n\tconst typeState = new Map<string, TypeState>();\n\n\tlet index = 0;\n\twhile (index < transactionData.commands.length) {\n\t\tconst transaction = transactionData.commands[index];\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\n\t\t\tindex++;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { type, balance } = transaction.$Intent.data as {\n\t\t\ttype: string;\n\t\t\tbalance: bigint;\n\t\t};\n\t\tconst coinType = type === 'gas' ? SUI_TYPE : type;\n\t\tconst totalRequired = totalByType.get(type)!;\n\t\tconst addressBalance = addressBalanceByType.get(type) ?? 0n;\n\n\t\tconst commands = [];\n\t\tlet intentResult: Argument;\n\n\t\tconst intentsForType = intentsByType.get(type) ?? [];\n\t\tconst allBalance = intentsForType.every((i) => i.outputKind === 'balance');\n\n\t\tif (allBalance && addressBalance >= totalRequired) {\n\t\t\t// Path 1: All balance intents and AB sufficient — direct per-intent withdrawal.\n\t\t\t// No coins touched, enables parallel execution.\n\t\t\tcommands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::balance::redeem_funds',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\targuments: [\n\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t'withdrawal',\n\t\t\t\t\t\t\tInputs.FundsWithdrawal({\n\t\t\t\t\t\t\t\treservation: {\n\t\t\t\t\t\t\t\t\t$kind: 'MaxAmountU64',\n\t\t\t\t\t\t\t\t\tMaxAmountU64: String(balance),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\ttypeArg: { $kind: 'Balance', Balance: coinType },\n\t\t\t\t\t\t\t\twithdrawFrom: { $kind: 'Sender', Sender: true },\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\tintentResult = {\n\t\t\t\t$kind: 'NestedResult',\n\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t};\n\t\t} else {\n\t\t\t// Path 2: Merge and Split — build a merged coin, split all intents at once.\n\n\t\t\tif (!typeState.has(type)) {\n\t\t\t\tconst intents = intentsForType;\n\n\t\t\t\t// Step 1: Build sources and merge\n\t\t\t\tconst sources: Argument[] = [];\n\n\t\t\t\tif (type === 'gas') {\n\t\t\t\t\tsources.push({ $kind: 'GasCoin', GasCoin: true });\n\t\t\t\t} else {\n\t\t\t\t\tconst coins = coinsByType.get(type)!;\n\t\t\t\t\tconst loadedCoinBalance = coins.reduce((sum, c) => sum + BigInt(c.balance), 0n);\n\t\t\t\t\tconst abNeeded =\n\t\t\t\t\t\ttotalRequired > loadedCoinBalance ? totalRequired - loadedCoinBalance : 0n;\n\n\t\t\t\t\texactBalanceByType.set(type, loadedCoinBalance + abNeeded === totalRequired);\n\n\t\t\t\t\tfor (const coin of coins) {\n\t\t\t\t\t\tsources.push(\n\t\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t\t'object',\n\t\t\t\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\t\t\t\tobjectId: coin.objectId,\n\t\t\t\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (abNeeded > 0n) {\n\t\t\t\t\t\tcommands.push(\n\t\t\t\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\t\t\t\ttarget: '0x2::coin::redeem_funds',\n\t\t\t\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\t\t\t\targuments: [\n\t\t\t\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t\t\t\t'withdrawal',\n\t\t\t\t\t\t\t\t\t\tInputs.FundsWithdrawal({\n\t\t\t\t\t\t\t\t\t\t\treservation: {\n\t\t\t\t\t\t\t\t\t\t\t\t$kind: 'MaxAmountU64',\n\t\t\t\t\t\t\t\t\t\t\t\tMaxAmountU64: String(abNeeded),\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\ttypeArg: { $kind: 'Balance', Balance: coinType },\n\t\t\t\t\t\t\t\t\t\t\twithdrawFrom: { $kind: 'Sender', Sender: true },\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tsources.push({ $kind: 'Result', Result: index + commands.length - 1 });\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst baseCoin = sources[0];\n\t\t\t\tconst rest = sources.slice(1);\n\t\t\t\tfor (let i = 0; i < rest.length; i += 500) {\n\t\t\t\t\tcommands.push(TransactionCommands.MergeCoins(baseCoin, rest.slice(i, i + 500)));\n\t\t\t\t}\n\n\t\t\t\tmergedCoins.set(type, baseCoin);\n\n\t\t\t\t// Step 2: Combined SplitCoins for all intents of this type\n\t\t\t\tconst splitCmdIndex = index + commands.length;\n\t\t\t\tcommands.push(\n\t\t\t\t\tTransactionCommands.SplitCoins(\n\t\t\t\t\t\tbaseCoin,\n\t\t\t\t\t\tintents.map((i) =>\n\t\t\t\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(i.balance))),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t);\n\n\t\t\t\t// Build per-intent results, adding into_balance conversions for balance intents\n\t\t\t\tconst results: Argument[] = [];\n\t\t\t\tfor (let i = 0; i < intents.length; i++) {\n\t\t\t\t\tconst splitResult: Argument = {\n\t\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\t\tNestedResult: [splitCmdIndex, i],\n\t\t\t\t\t};\n\n\t\t\t\t\tif (intents[i].outputKind === 'balance') {\n\t\t\t\t\t\tcommands.push(\n\t\t\t\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\t\t\t\ttarget: '0x2::coin::into_balance',\n\t\t\t\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\t\t\t\targuments: [splitResult],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tresults.push({\n\t\t\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresults.push(splitResult);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttypeState.set(type, { results, nextIntent: 0 });\n\t\t\t}\n\n\t\t\tconst state = typeState.get(type)!;\n\t\t\tintentResult = state.results[state.nextIntent++];\n\t\t}\n\n\t\ttransactionData.replaceCommand(\n\t\t\tindex,\n\t\t\tcommands,\n\t\t\tintentResult as { NestedResult: [number, number] },\n\t\t);\n\n\t\t// Advance past the replacement. When commands is empty (subsequent intents\n\t\t// of a combined split), the command was removed and the next command shifted\n\t\t// into this position — so we stay at the same index.\n\t\tindex += commands.length;\n\t}\n\n\t// Step 3: Remainder handling\n\tfor (const [type, mergedCoin] of mergedCoins) {\n\t\tif (type === 'gas') continue;\n\n\t\tconst hasBalanceIntent = intentsByType.get(type)?.some((i) => i.outputKind === 'balance');\n\n\t\tif (hasBalanceIntent) {\n\t\t\t// Balance intents exist: send remainder coin back to sender's address balance.\n\t\t\t// coin::send_funds is gasless-eligible and handles zero amounts.\n\t\t\ttransactionData.commands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::coin::send_funds',\n\t\t\t\t\ttypeArguments: [type],\n\t\t\t\t\targuments: [\n\t\t\t\t\t\tmergedCoin,\n\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t'pure',\n\t\t\t\t\t\t\tInputs.Pure(bcs.Address.serialize(transactionData.sender!)),\n\t\t\t\t\t\t),\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t);\n\t\t} else if (exactBalanceByType.get(type)) {\n\t\t\t// Coin-only with exact match: destroy the zero-value dust coin.\n\t\t\ttransactionData.commands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::coin::destroy_zero',\n\t\t\t\t\ttypeArguments: [type],\n\t\t\t\t\targuments: [mergedCoin],\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\t// Coin-only with surplus: merged coin stays with sender as an owned object\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsAndBalanceOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: ClientWithCoreApi;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<{\n\tcoins: SuiClientTypes.Coin[];\n\tbalance: bigint;\n\taddressBalance: bigint;\n\tcoinBalance: bigint;\n}> {\n\tlet remainingBalance = balance;\n\tconst coins: SuiClientTypes.Coin[] = [];\n\tconst balanceRequest = client.core.getBalance({ owner, coinType }).then(({ balance }) => {\n\t\tremainingBalance -= BigInt(balance.addressBalance);\n\n\t\treturn balance;\n\t});\n\n\tconst [allCoins, balanceResponse] = await Promise.all([loadMoreCoins(), balanceRequest]);\n\n\tif (BigInt(balanceResponse.balance) < balance) {\n\t\tthrow new Error(\n\t\t\t`Insufficient balance of ${coinType} for owner ${owner}. Required: ${balance}, Available: ${\n\t\t\t\tbalance - remainingBalance\n\t\t\t}`,\n\t\t);\n\t}\n\n\treturn {\n\t\tcoins: allCoins,\n\t\tbalance: BigInt(balanceResponse.coinBalance),\n\t\taddressBalance: BigInt(balanceResponse.addressBalance),\n\t\tcoinBalance: BigInt(balanceResponse.coinBalance),\n\t};\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<SuiClientTypes.Coin[]> {\n\t\tconst {\n\t\t\tobjects,\n\t\t\thasNextPage,\n\t\t\tcursor: nextCursor,\n\t\t} = await client.core.listCoins({\n\t\t\towner,\n\t\t\tcoinType,\n\t\t\tcursor,\n\t\t});\n\n\t\tawait balanceRequest;\n\n\t\t// Always load all coins from the page (except already-used ones).\n\t\t// This merges all available coins rather than leaving dust.\n\t\tfor (const coin of objects) {\n\t\t\tif (usedIds.has(coin.objectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= BigInt(coin.balance);\n\t\t}\n\n\t\t// Only paginate if loaded coins + AB are still insufficient\n\t\tif (remainingBalance > 0n && hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\treturn coins;\n\t}\n}\n"],"mappings":";;;;;;;AAgBA,MAAa,oBAAoB;AACjC,MAAM,WAAW,mBAAmB,gBAAgB;AAEpD,SAAgB,gBAAgB,EAC/B,OAAO,UACP,SACA,aAAa,QAK6B;CAC1C,IAAI,aAAuC;AAE3C,SAAQ,OAAoB;AAC3B,MAAI,WACH,QAAO;AAGR,KAAG,kBAAkB,mBAAmB,mBAAmB;EAC3D,MAAM,WAAW,SAAS,QAAQ,OAAO,mBAAmB,KAAK;AAEjE,eAAa,GAAG,IACf,oBAAoB,OAAO;GAC1B,MAAM;GACN,QAAQ,EAAE;GACV,MAAM;IACL,MAAM,aAAa,YAAY,aAAa,QAAQ;IACpD,SAAS,OAAO,QAAQ;IACxB,YAAY;IACZ;G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,OAAO,gBAAgB,eAAe;EACtD,aAAa,OAAO,gBAAgB,YAAY;EAChD;CAED,eAAe,cAAc,SAAwB,MAAsC;EAC1F,MAAM,EACL,SACA,aACA,QAAQ,eACL,MAAM,OAAO,KAAK,UAAU;GAC/B;GACA;GACA;GACA,CAAC;AAEF,QAAM;AAIN,OAAK,MAAM,QAAQ,SAAS;AAC3B,OAAI,QAAQ,IAAI,KAAK,SAAS,CAC7B;AAGD,SAAM,KAAK,KAAK;AAChB,uBAAoB,OAAO,KAAK,QAAQ;;AAIzC,MAAI,mBAAmB,MAAM,YAC5B,QAAO,cAAc,WAAW;AAGjC,SAAO"}
|
|
1
|
+
{"version":3,"file":"CoinWithBalance.mjs","names":["bcs","balance"],"sources":["../../../src/transactions/intents/CoinWithBalance.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, optional, parse, picklist, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { TransactionCommands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../resolve.js';\nimport type { Transaction, TransactionResult } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\nimport type { ClientWithCoreApi, SuiClientTypes } from '../../client/index.js';\n\nexport const COIN_WITH_BALANCE = 'CoinWithBalance';\nconst SUI_TYPE = normalizeStructTag('0x2::sui::SUI');\n\nexport function coinWithBalance({\n\ttype = SUI_TYPE,\n\tbalance,\n\tuseGasCoin = true,\n}: {\n\tbalance: bigint | number;\n\ttype?: string;\n\tuseGasCoin?: boolean;\n}): (tx: Transaction) => TransactionResult {\n\tlet coinResult: TransactionResult | null = null;\n\n\treturn (tx: Transaction) => {\n\t\tif (coinResult) {\n\t\t\treturn coinResult;\n\t\t}\n\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\tcoinResult = tx.add(\n\t\t\tTransactionCommands.Intent({\n\t\t\t\tname: COIN_WITH_BALANCE,\n\t\t\t\tinputs: {},\n\t\t\t\tdata: {\n\t\t\t\t\ttype: coinType === SUI_TYPE && useGasCoin ? 'gas' : coinType,\n\t\t\t\t\tbalance: BigInt(balance),\n\t\t\t\t\toutputKind: 'coin',\n\t\t\t\t} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\n\t\treturn coinResult;\n\t};\n}\n\nexport function createBalance({\n\ttype = SUI_TYPE,\n\tbalance,\n\tuseGasCoin = true,\n}: {\n\tbalance: bigint | number;\n\ttype?: string;\n\tuseGasCoin?: boolean;\n}): (tx: Transaction) => TransactionResult {\n\tlet balanceResult: TransactionResult | null = null;\n\n\treturn (tx: Transaction) => {\n\t\tif (balanceResult) {\n\t\t\treturn balanceResult;\n\t\t}\n\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\tbalanceResult = tx.add(\n\t\t\tTransactionCommands.Intent({\n\t\t\t\tname: COIN_WITH_BALANCE,\n\t\t\t\tinputs: {},\n\t\t\t\tdata: {\n\t\t\t\t\ttype: coinType === SUI_TYPE && useGasCoin ? 'gas' : coinType,\n\t\t\t\t\tbalance: BigInt(balance),\n\t\t\t\t\toutputKind: 'balance',\n\t\t\t\t} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\n\t\treturn balanceResult;\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n\toutputKind: optional(picklist(['coin', 'balance'])),\n});\n\nexport async function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\ttype IntentInfo = { balance: bigint; outputKind: 'coin' | 'balance' };\n\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\tconst intentsByType = new Map<string, IntentInfo[]>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\t// First pass: scan intents, collect per-type data, and resolve zero-balance intents in place.\n\tfor (const [i, command] of transactionData.commands.entries()) {\n\t\tif (command.$kind !== '$Intent' || command.$Intent.name !== COIN_WITH_BALANCE) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { type, balance, outputKind } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t// Zero-balance intents are resolved immediately — no coins or AB needed.\n\t\t// This is a 1:1 replacement so indices don't shift.\n\t\tif (balance === 0n) {\n\t\t\tconst coinType = type === 'gas' ? SUI_TYPE : type;\n\t\t\ttransactionData.replaceCommand(\n\t\t\t\ti,\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: (outputKind ?? 'coin') === 'balance' ? '0x2::balance::zero' : '0x2::coin::zero',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t}),\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (type !== 'gas') {\n\t\t\tcoinTypes.add(type);\n\t\t}\n\n\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\n\t\tif (!intentsByType.has(type)) intentsByType.set(type, []);\n\t\tintentsByType.get(type)!.push({ balance, outputKind: outputKind ?? 'coin' });\n\t}\n\n\tif (totalByType.has('gas') && totalByType.has(SUI_TYPE)) {\n\t\tthrow new Error(\n\t\t\t'Cannot mix SUI CoinWithBalance intents that use the gas coin with ones that do not (useGasCoin: false). Use one or the other.',\n\t\t);\n\t}\n\n\tconst usedIds = new Set<string>();\n\n\tfor (const input of transactionData.inputs) {\n\t\tif (input.Object?.ImmOrOwnedObject) {\n\t\t\tusedIds.add(input.Object.ImmOrOwnedObject.objectId);\n\t\t}\n\t\tif (input.UnresolvedObject?.objectId) {\n\t\t\tusedIds.add(input.UnresolvedObject.objectId);\n\t\t}\n\t}\n\n\tconst coinsByType = new Map<string, SuiClientTypes.Coin[]>();\n\tconst addressBalanceByType = new Map<string, bigint>();\n\tconst client = buildOptions.client;\n\n\tif (!client) {\n\t\tthrow new Error(\n\t\t\t'Client must be provided to build or serialize transactions with CoinWithBalance intents',\n\t\t);\n\t}\n\n\tawait Promise.all([\n\t\t...[...coinTypes].map(async (coinType) => {\n\t\t\tconst { coins, addressBalance } = await getCoinsAndBalanceOfType({\n\t\t\t\tcoinType,\n\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\tclient,\n\t\t\t\towner: transactionData.sender!,\n\t\t\t\tusedIds,\n\t\t\t});\n\n\t\t\tcoinsByType.set(coinType, coins);\n\t\t\taddressBalanceByType.set(coinType, addressBalance);\n\t\t}),\n\t\ttotalByType.has('gas')\n\t\t\t? await client.core\n\t\t\t\t\t.getBalance({\n\t\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\t\tcoinType: SUI_TYPE,\n\t\t\t\t\t})\n\t\t\t\t\t.then(({ balance }) => {\n\t\t\t\t\t\taddressBalanceByType.set('gas', BigInt(balance.addressBalance));\n\t\t\t\t\t})\n\t\t\t: null,\n\t]);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\tconst exactBalanceByType = new Map<string, boolean>();\n\tconst usedAddressBalance = new Set<string>();\n\n\t// Per-type state for Path 2 combined splits\n\ttype TypeState = { results: Argument[]; nextIntent: number };\n\tconst typeState = new Map<string, TypeState>();\n\n\tlet index = 0;\n\twhile (index < transactionData.commands.length) {\n\t\tconst transaction = transactionData.commands[index];\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\n\t\t\tindex++;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { type, balance } = transaction.$Intent.data as {\n\t\t\ttype: string;\n\t\t\tbalance: bigint;\n\t\t};\n\t\tconst coinType = type === 'gas' ? SUI_TYPE : type;\n\t\tconst totalRequired = totalByType.get(type)!;\n\t\tconst addressBalance = addressBalanceByType.get(type) ?? 0n;\n\n\t\tconst commands = [];\n\t\tlet intentResult: Argument;\n\n\t\tconst intentsForType = intentsByType.get(type) ?? [];\n\t\tconst allBalance = intentsForType.every((i) => i.outputKind === 'balance');\n\n\t\tif (allBalance && addressBalance >= totalRequired) {\n\t\t\t// Path 1: All balance intents and AB sufficient — direct per-intent withdrawal.\n\t\t\t// No coins touched, enables parallel execution.\n\t\t\tcommands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::balance::redeem_funds',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\targuments: [\n\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t'withdrawal',\n\t\t\t\t\t\t\tInputs.FundsWithdrawal({\n\t\t\t\t\t\t\t\treservation: {\n\t\t\t\t\t\t\t\t\t$kind: 'MaxAmountU64',\n\t\t\t\t\t\t\t\t\tMaxAmountU64: String(balance),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\ttypeArg: { $kind: 'Balance', Balance: coinType },\n\t\t\t\t\t\t\t\twithdrawFrom: { $kind: 'Sender', Sender: true },\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\tintentResult = {\n\t\t\t\t$kind: 'NestedResult',\n\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t};\n\t\t} else {\n\t\t\t// Path 2: Merge and Split — build a merged coin, split all intents at once.\n\n\t\t\tif (!typeState.has(type)) {\n\t\t\t\tconst intents = intentsForType;\n\n\t\t\t\t// Step 1: Build sources and merge\n\t\t\t\tconst sources: Argument[] = [];\n\n\t\t\t\tif (addressBalance >= totalRequired) {\n\t\t\t\t\t// AB sufficient — source entirely from address balance, no coins needed.\n\t\t\t\t\tusedAddressBalance.add(type);\n\n\t\t\t\t\tcommands.push(\n\t\t\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\t\t\ttarget: '0x2::coin::redeem_funds',\n\t\t\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\t\t\targuments: [\n\t\t\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t\t\t'withdrawal',\n\t\t\t\t\t\t\t\t\tInputs.FundsWithdrawal({\n\t\t\t\t\t\t\t\t\t\treservation: {\n\t\t\t\t\t\t\t\t\t\t\t$kind: 'MaxAmountU64',\n\t\t\t\t\t\t\t\t\t\t\tMaxAmountU64: String(totalRequired),\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\ttypeArg: { $kind: 'Balance', Balance: coinType },\n\t\t\t\t\t\t\t\t\t\twithdrawFrom: { $kind: 'Sender', Sender: true },\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t\tsources.push({ $kind: 'Result', Result: index + commands.length - 1 });\n\t\t\t\t} else if (type === 'gas') {\n\t\t\t\t\tsources.push({ $kind: 'GasCoin', GasCoin: true });\n\t\t\t\t} else {\n\t\t\t\t\tconst coins = coinsByType.get(type)!;\n\t\t\t\t\tconst loadedCoinBalance = coins.reduce((sum, c) => sum + BigInt(c.balance), 0n);\n\t\t\t\t\tconst abNeeded =\n\t\t\t\t\t\ttotalRequired > loadedCoinBalance ? totalRequired - loadedCoinBalance : 0n;\n\n\t\t\t\t\texactBalanceByType.set(type, loadedCoinBalance + abNeeded === totalRequired);\n\n\t\t\t\t\tfor (const coin of coins) {\n\t\t\t\t\t\tsources.push(\n\t\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t\t'object',\n\t\t\t\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\t\t\t\tobjectId: coin.objectId,\n\t\t\t\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (abNeeded > 0n) {\n\t\t\t\t\t\tusedAddressBalance.add(type);\n\t\t\t\t\t\tcommands.push(\n\t\t\t\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\t\t\t\ttarget: '0x2::coin::redeem_funds',\n\t\t\t\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\t\t\t\targuments: [\n\t\t\t\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t\t\t\t'withdrawal',\n\t\t\t\t\t\t\t\t\t\tInputs.FundsWithdrawal({\n\t\t\t\t\t\t\t\t\t\t\treservation: {\n\t\t\t\t\t\t\t\t\t\t\t\t$kind: 'MaxAmountU64',\n\t\t\t\t\t\t\t\t\t\t\t\tMaxAmountU64: String(abNeeded),\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\ttypeArg: { $kind: 'Balance', Balance: coinType },\n\t\t\t\t\t\t\t\t\t\t\twithdrawFrom: { $kind: 'Sender', Sender: true },\n\t\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tsources.push({ $kind: 'Result', Result: index + commands.length - 1 });\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst baseCoin = sources[0];\n\t\t\t\tconst rest = sources.slice(1);\n\t\t\t\tfor (let i = 0; i < rest.length; i += 500) {\n\t\t\t\t\tcommands.push(TransactionCommands.MergeCoins(baseCoin, rest.slice(i, i + 500)));\n\t\t\t\t}\n\n\t\t\t\tmergedCoins.set(type, baseCoin);\n\n\t\t\t\t// Step 2: Combined SplitCoins for all intents of this type\n\t\t\t\tconst splitCmdIndex = index + commands.length;\n\t\t\t\tcommands.push(\n\t\t\t\t\tTransactionCommands.SplitCoins(\n\t\t\t\t\t\tbaseCoin,\n\t\t\t\t\t\tintents.map((i) =>\n\t\t\t\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(i.balance))),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t);\n\n\t\t\t\t// Build per-intent results, adding into_balance conversions for balance intents\n\t\t\t\tconst results: Argument[] = [];\n\t\t\t\tfor (let i = 0; i < intents.length; i++) {\n\t\t\t\t\tconst splitResult: Argument = {\n\t\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\t\tNestedResult: [splitCmdIndex, i],\n\t\t\t\t\t};\n\n\t\t\t\t\tif (intents[i].outputKind === 'balance') {\n\t\t\t\t\t\tcommands.push(\n\t\t\t\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\t\t\t\ttarget: '0x2::coin::into_balance',\n\t\t\t\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\t\t\t\targuments: [splitResult],\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tresults.push({\n\t\t\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tresults.push(splitResult);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttypeState.set(type, { results, nextIntent: 0 });\n\t\t\t}\n\n\t\t\tconst state = typeState.get(type)!;\n\t\t\tintentResult = state.results[state.nextIntent++];\n\t\t}\n\n\t\ttransactionData.replaceCommand(\n\t\t\tindex,\n\t\t\tcommands,\n\t\t\tintentResult as { NestedResult: [number, number] },\n\t\t);\n\n\t\t// Advance past the replacement. When commands is empty (subsequent intents\n\t\t// of a combined split), the command was removed and the next command shifted\n\t\t// into this position — so we stay at the same index.\n\t\tindex += commands.length;\n\t}\n\n\t// Step 3: Remainder handling\n\tfor (const [type, mergedCoin] of mergedCoins) {\n\t\t// When gas type used GasCoin (not AB), leftover stays in the gas coin — no remainder needed.\n\t\tif (type === 'gas' && !usedAddressBalance.has(type)) continue;\n\n\t\tconst coinType = type === 'gas' ? SUI_TYPE : type;\n\t\tconst hasBalanceIntent = intentsByType.get(type)?.some((i) => i.outputKind === 'balance');\n\t\tconst sourcedFromAB = usedAddressBalance.has(type);\n\n\t\tif (hasBalanceIntent || sourcedFromAB) {\n\t\t\t// Sourced from AB or balance intents exist: send remainder back to sender's address balance.\n\t\t\t// coin::send_funds is gasless-eligible and handles zero amounts.\n\t\t\ttransactionData.commands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::coin::send_funds',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\targuments: [\n\t\t\t\t\t\tmergedCoin,\n\t\t\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t\t\t'pure',\n\t\t\t\t\t\t\tInputs.Pure(bcs.Address.serialize(transactionData.sender!)),\n\t\t\t\t\t\t),\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t);\n\t\t} else if (exactBalanceByType.get(type)) {\n\t\t\t// Coin-only with exact match: destroy the zero-value dust coin.\n\t\t\ttransactionData.commands.push(\n\t\t\t\tTransactionCommands.MoveCall({\n\t\t\t\t\ttarget: '0x2::coin::destroy_zero',\n\t\t\t\t\ttypeArguments: [coinType],\n\t\t\t\t\targuments: [mergedCoin],\n\t\t\t\t}),\n\t\t\t);\n\t\t}\n\t\t// Coin-only with surplus: merged coin stays with sender as an owned object\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsAndBalanceOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: ClientWithCoreApi;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<{\n\tcoins: SuiClientTypes.Coin[];\n\tbalance: bigint;\n\taddressBalance: bigint;\n\tcoinBalance: bigint;\n}> {\n\tlet remainingBalance = balance;\n\tconst coins: SuiClientTypes.Coin[] = [];\n\tconst balanceRequest = client.core.getBalance({ owner, coinType }).then(({ balance }) => {\n\t\tremainingBalance -= BigInt(balance.addressBalance);\n\n\t\treturn balance;\n\t});\n\n\tconst [allCoins, balanceResponse] = await Promise.all([loadMoreCoins(), balanceRequest]);\n\n\tif (BigInt(balanceResponse.balance) < balance) {\n\t\tthrow new Error(\n\t\t\t`Insufficient balance of ${coinType} for owner ${owner}. Required: ${balance}, Available: ${\n\t\t\t\tbalance - remainingBalance\n\t\t\t}`,\n\t\t);\n\t}\n\n\treturn {\n\t\tcoins: allCoins,\n\t\tbalance: BigInt(balanceResponse.coinBalance),\n\t\taddressBalance: BigInt(balanceResponse.addressBalance),\n\t\tcoinBalance: BigInt(balanceResponse.coinBalance),\n\t};\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<SuiClientTypes.Coin[]> {\n\t\tconst {\n\t\t\tobjects,\n\t\t\thasNextPage,\n\t\t\tcursor: nextCursor,\n\t\t} = await client.core.listCoins({\n\t\t\towner,\n\t\t\tcoinType,\n\t\t\tcursor,\n\t\t});\n\n\t\tawait balanceRequest;\n\n\t\t// Always load all coins from the page (except already-used ones).\n\t\t// This merges all available coins rather than leaving dust.\n\t\tfor (const coin of objects) {\n\t\t\tif (usedIds.has(coin.objectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= BigInt(coin.balance);\n\t\t}\n\n\t\t// Only paginate if loaded coins + AB are still insufficient\n\t\tif (remainingBalance > 0n && hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\treturn coins;\n\t}\n}\n"],"mappings":";;;;;;;AAgBA,MAAa,oBAAoB;AACjC,MAAM,WAAW,mBAAmB,gBAAgB;AAEpD,SAAgB,gBAAgB,EAC/B,OAAO,UACP,SACA,aAAa,QAK6B;CAC1C,IAAI,aAAuC;AAE3C,SAAQ,OA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,KAAK,SAAS,CAC7B;AAGD,SAAM,KAAK,KAAK;AAChB,uBAAoB,OAAO,KAAK,QAAQ;;AAIzC,MAAI,mBAAmB,MAAM,YAC5B,QAAO,cAAc,WAAW;AAGjC,SAAO"}
|
package/dist/utils/format.d.mts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
//#region src/utils/format.d.ts
|
|
2
2
|
declare function formatAddress(address: string): string;
|
|
3
3
|
declare function formatDigest(digest: string): string;
|
|
4
|
+
/** Parse a decimal string into its smallest-unit bigint representation. No floating point. */
|
|
5
|
+
declare function parseToUnits(amount: string, decimals: number): bigint;
|
|
6
|
+
/** Parse a SUI decimal string into MIST. */
|
|
7
|
+
declare function parseToMist(amount: string): bigint;
|
|
4
8
|
//#endregion
|
|
5
|
-
export { formatAddress, formatDigest };
|
|
9
|
+
export { formatAddress, formatDigest, parseToMist, parseToUnits };
|
|
6
10
|
//# sourceMappingURL=format.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.d.mts","names":[],"sources":["../../src/utils/format.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"format.d.mts","names":[],"sources":["../../src/utils/format.ts"],"mappings":";iBAOgB,aAAA,CAAc,OAAA;AAAA,iBAUd,YAAA,CAAa,MAAA;;iBAQb,YAAA,CAAa,MAAA,UAAgB,QAAA;;iBA2B7B,WAAA,CAAY,MAAA"}
|
package/dist/utils/format.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SUI_DECIMALS } from "./constants.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/utils/format.ts
|
|
2
4
|
const ELLIPSIS = "…";
|
|
3
5
|
function formatAddress(address) {
|
|
@@ -8,7 +10,23 @@ function formatAddress(address) {
|
|
|
8
10
|
function formatDigest(digest) {
|
|
9
11
|
return `${digest.slice(0, 10)}${ELLIPSIS}`;
|
|
10
12
|
}
|
|
13
|
+
const AMOUNT_REGEX = /^-?(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)$/;
|
|
14
|
+
/** Parse a decimal string into its smallest-unit bigint representation. No floating point. */
|
|
15
|
+
function parseToUnits(amount, decimals) {
|
|
16
|
+
if (decimals < 0 || !Number.isInteger(decimals)) throw new Error(`Invalid decimals: ${decimals}`);
|
|
17
|
+
if (!AMOUNT_REGEX.test(amount)) throw new Error(`Invalid amount: "${amount}"`);
|
|
18
|
+
const negative = amount.startsWith("-");
|
|
19
|
+
const [whole, fraction = ""] = (negative ? amount.slice(1) : amount).split(".");
|
|
20
|
+
if (fraction.length > decimals) throw new Error(`Too many decimal places: "${amount}" has ${fraction.length} but max is ${decimals}`);
|
|
21
|
+
const paddedFraction = fraction.padEnd(decimals, "0") || "0";
|
|
22
|
+
const result = BigInt(whole || "0") * 10n ** BigInt(decimals) + BigInt(paddedFraction);
|
|
23
|
+
return negative ? -result : result;
|
|
24
|
+
}
|
|
25
|
+
/** Parse a SUI decimal string into MIST. */
|
|
26
|
+
function parseToMist(amount) {
|
|
27
|
+
return parseToUnits(amount, SUI_DECIMALS);
|
|
28
|
+
}
|
|
11
29
|
|
|
12
30
|
//#endregion
|
|
13
|
-
export { formatAddress, formatDigest };
|
|
31
|
+
export { formatAddress, formatDigest, parseToMist, parseToUnits };
|
|
14
32
|
//# sourceMappingURL=format.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format.mjs","names":[],"sources":["../../src/utils/format.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nconst ELLIPSIS = '\\u{2026}';\n\nexport function formatAddress(address: string) {\n\tif (address.length <= 6) {\n\t\treturn address;\n\t}\n\n\tconst offset = address.startsWith('0x') ? 2 : 0;\n\n\treturn `0x${address.slice(offset, offset + 4)}${ELLIPSIS}${address.slice(-4)}`;\n}\n\nexport function formatDigest(digest: string) {\n\t// Use 10 first characters\n\treturn `${digest.slice(0, 10)}${ELLIPSIS}`;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"format.mjs","names":[],"sources":["../../src/utils/format.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { SUI_DECIMALS } from './constants.js';\n\nconst ELLIPSIS = '\\u{2026}';\n\nexport function formatAddress(address: string) {\n\tif (address.length <= 6) {\n\t\treturn address;\n\t}\n\n\tconst offset = address.startsWith('0x') ? 2 : 0;\n\n\treturn `0x${address.slice(offset, offset + 4)}${ELLIPSIS}${address.slice(-4)}`;\n}\n\nexport function formatDigest(digest: string) {\n\t// Use 10 first characters\n\treturn `${digest.slice(0, 10)}${ELLIPSIS}`;\n}\n\nconst AMOUNT_REGEX = /^-?(?:[0-9]+(?:\\.[0-9]+)?|\\.[0-9]+)$/;\n\n/** Parse a decimal string into its smallest-unit bigint representation. No floating point. */\nexport function parseToUnits(amount: string, decimals: number): bigint {\n\tif (decimals < 0 || !Number.isInteger(decimals)) {\n\t\tthrow new Error(`Invalid decimals: ${decimals}`);\n\t}\n\n\tif (!AMOUNT_REGEX.test(amount)) {\n\t\tthrow new Error(`Invalid amount: \"${amount}\"`);\n\t}\n\n\tconst negative = amount.startsWith('-');\n\tconst stripped = negative ? amount.slice(1) : amount;\n\n\tconst [whole, fraction = ''] = stripped.split('.');\n\n\tif (fraction.length > decimals) {\n\t\tthrow new Error(\n\t\t\t`Too many decimal places: \"${amount}\" has ${fraction.length} but max is ${decimals}`,\n\t\t);\n\t}\n\n\tconst paddedFraction = fraction.padEnd(decimals, '0') || '0';\n\tconst result = BigInt(whole || '0') * 10n ** BigInt(decimals) + BigInt(paddedFraction);\n\n\treturn negative ? -result : result;\n}\n\n/** Parse a SUI decimal string into MIST. */\nexport function parseToMist(amount: string): bigint {\n\treturn parseToUnits(amount, SUI_DECIMALS);\n}\n"],"mappings":";;;AAKA,MAAM,WAAW;AAEjB,SAAgB,cAAc,SAAiB;AAC9C,KAAI,QAAQ,UAAU,EACrB,QAAO;CAGR,MAAM,SAAS,QAAQ,WAAW,KAAK,GAAG,IAAI;AAE9C,QAAO,KAAK,QAAQ,MAAM,QAAQ,SAAS,EAAE,GAAG,WAAW,QAAQ,MAAM,GAAG;;AAG7E,SAAgB,aAAa,QAAgB;AAE5C,QAAO,GAAG,OAAO,MAAM,GAAG,GAAG,GAAG;;AAGjC,MAAM,eAAe;;AAGrB,SAAgB,aAAa,QAAgB,UAA0B;AACtE,KAAI,WAAW,KAAK,CAAC,OAAO,UAAU,SAAS,CAC9C,OAAM,IAAI,MAAM,qBAAqB,WAAW;AAGjD,KAAI,CAAC,aAAa,KAAK,OAAO,CAC7B,OAAM,IAAI,MAAM,oBAAoB,OAAO,GAAG;CAG/C,MAAM,WAAW,OAAO,WAAW,IAAI;CAGvC,MAAM,CAAC,OAAO,WAAW,OAFR,WAAW,OAAO,MAAM,EAAE,GAAG,QAEN,MAAM,IAAI;AAElD,KAAI,SAAS,SAAS,SACrB,OAAM,IAAI,MACT,6BAA6B,OAAO,QAAQ,SAAS,OAAO,cAAc,WAC1E;CAGF,MAAM,iBAAiB,SAAS,OAAO,UAAU,IAAI,IAAI;CACzD,MAAM,SAAS,OAAO,SAAS,IAAI,GAAG,OAAO,OAAO,SAAS,GAAG,OAAO,eAAe;AAEtF,QAAO,WAAW,CAAC,SAAS;;;AAI7B,SAAgB,YAAY,QAAwB;AACnD,QAAO,aAAa,QAAQ,aAAa"}
|
package/dist/utils/index.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { normalizeTypeTag } from "../bcs/type-tag-serializer.mjs";
|
|
2
2
|
import { SUI_ADDRESS_LENGTH, isValidStructTag, isValidSuiAddress, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiObjectId, parseStructTag } from "./sui-types.mjs";
|
|
3
|
-
import { formatAddress, formatDigest } from "./format.mjs";
|
|
3
|
+
import { formatAddress, formatDigest, parseToMist, parseToUnits } from "./format.mjs";
|
|
4
4
|
import { isValidSuiNSName, normalizeSuiNSName } from "./suins.mjs";
|
|
5
5
|
import { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG } from "./constants.mjs";
|
|
6
6
|
import { isValidNamedPackage, isValidNamedType } from "./move-registry.mjs";
|
|
7
7
|
import { deriveDynamicFieldID } from "./dynamic-fields.mjs";
|
|
8
8
|
import { deriveObjectID } from "./derived-objects.mjs";
|
|
9
9
|
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@mysten/bcs";
|
|
10
|
-
export { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_ADDRESS_LENGTH, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG, deriveDynamicFieldID, deriveObjectID, formatAddress, formatDigest, fromBase58, fromBase64, fromHex, isValidNamedPackage, isValidNamedType, isValidStructTag, isValidSuiAddress, isValidSuiNSName, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiNSName, normalizeSuiObjectId, normalizeTypeTag, parseStructTag, toBase58, toBase64, toHex };
|
|
10
|
+
export { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_ADDRESS_LENGTH, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG, deriveDynamicFieldID, deriveObjectID, formatAddress, formatDigest, fromBase58, fromBase64, fromHex, isValidNamedPackage, isValidNamedType, isValidStructTag, isValidSuiAddress, isValidSuiNSName, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiNSName, normalizeSuiObjectId, normalizeTypeTag, parseStructTag, parseToMist, parseToUnits, toBase58, toBase64, toHex };
|
package/dist/utils/index.mjs
CHANGED
|
@@ -4,8 +4,8 @@ import { SUI_ADDRESS_LENGTH, isValidStructTag, isValidSuiAddress, isValidSuiObje
|
|
|
4
4
|
import { normalizeTypeTag } from "../bcs/type-tag-serializer.mjs";
|
|
5
5
|
import { deriveDynamicFieldID } from "./dynamic-fields.mjs";
|
|
6
6
|
import { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG } from "./constants.mjs";
|
|
7
|
-
import { formatAddress, formatDigest } from "./format.mjs";
|
|
7
|
+
import { formatAddress, formatDigest, parseToMist, parseToUnits } from "./format.mjs";
|
|
8
8
|
import { deriveObjectID } from "./derived-objects.mjs";
|
|
9
9
|
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@mysten/bcs";
|
|
10
10
|
|
|
11
|
-
export { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_ADDRESS_LENGTH, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG, deriveDynamicFieldID, deriveObjectID, formatAddress, formatDigest, fromBase58, fromBase64, fromHex, isValidNamedPackage, isValidNamedType, isValidStructTag, isValidSuiAddress, isValidSuiNSName, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiNSName, normalizeSuiObjectId, normalizeTypeTag, parseStructTag, toBase58, toBase64, toHex };
|
|
11
|
+
export { MIST_PER_SUI, MOVE_STDLIB_ADDRESS, SUI_ADDRESS_LENGTH, SUI_CLOCK_OBJECT_ID, SUI_COIN_REGISTRY_OBJECT_ID, SUI_DECIMALS, SUI_DENY_LIST_OBJECT_ID, SUI_FRAMEWORK_ADDRESS, SUI_RANDOM_OBJECT_ID, SUI_SYSTEM_ADDRESS, SUI_SYSTEM_MODULE_NAME, SUI_SYSTEM_STATE_OBJECT_ID, SUI_TYPE_ARG, deriveDynamicFieldID, deriveObjectID, formatAddress, formatDigest, fromBase58, fromBase64, fromHex, isValidNamedPackage, isValidNamedType, isValidStructTag, isValidSuiAddress, isValidSuiNSName, isValidSuiObjectId, isValidTransactionDigest, normalizeStructTag, normalizeSuiAddress, normalizeSuiNSName, normalizeSuiObjectId, normalizeTypeTag, parseStructTag, parseToMist, parseToUnits, toBase58, toBase64, toHex };
|
package/dist/version.mjs
CHANGED
package/dist/version.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.
|
|
1
|
+
{"version":3,"file":"version.mjs","names":[],"sources":["../src/version.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// This file is generated by genversion.mjs. Do not edit it directly.\n\nexport const PACKAGE_VERSION = '2.16.0';\nexport const TARGETED_RPC_VERSION = '1.71.0';\n"],"mappings":";AAKA,MAAa,kBAAkB;AAC/B,MAAa,uBAAuB"}
|
package/dist/zklogin/bcs.d.mts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _mysten_bcs1118 from "@mysten/bcs";
|
|
2
2
|
import { InferBcsInput } from "@mysten/bcs";
|
|
3
3
|
|
|
4
4
|
//#region src/zklogin/bcs.d.ts
|
|
5
|
-
declare const zkLoginSignature:
|
|
6
|
-
inputs:
|
|
7
|
-
proofPoints:
|
|
8
|
-
a:
|
|
5
|
+
declare const zkLoginSignature: _mysten_bcs1118.BcsStruct<{
|
|
6
|
+
inputs: _mysten_bcs1118.BcsStruct<{
|
|
7
|
+
proofPoints: _mysten_bcs1118.BcsStruct<{
|
|
8
|
+
a: _mysten_bcs1118.BcsType<string[], Iterable<string> & {
|
|
9
9
|
length: number;
|
|
10
10
|
}, string>;
|
|
11
|
-
b:
|
|
11
|
+
b: _mysten_bcs1118.BcsType<string[][], Iterable<Iterable<string> & {
|
|
12
12
|
length: number;
|
|
13
13
|
}> & {
|
|
14
14
|
length: number;
|
|
15
15
|
}, string>;
|
|
16
|
-
c:
|
|
16
|
+
c: _mysten_bcs1118.BcsType<string[], Iterable<string> & {
|
|
17
17
|
length: number;
|
|
18
18
|
}, string>;
|
|
19
19
|
}, string>;
|
|
20
|
-
issBase64Details:
|
|
21
|
-
value:
|
|
22
|
-
indexMod4:
|
|
20
|
+
issBase64Details: _mysten_bcs1118.BcsStruct<{
|
|
21
|
+
value: _mysten_bcs1118.BcsType<string, string, "string">;
|
|
22
|
+
indexMod4: _mysten_bcs1118.BcsType<number, number, "u8">;
|
|
23
23
|
}, string>;
|
|
24
|
-
headerBase64:
|
|
25
|
-
addressSeed:
|
|
24
|
+
headerBase64: _mysten_bcs1118.BcsType<string, string, "string">;
|
|
25
|
+
addressSeed: _mysten_bcs1118.BcsType<string, string, "string">;
|
|
26
26
|
}, string>;
|
|
27
|
-
maxEpoch:
|
|
28
|
-
userSignature:
|
|
27
|
+
maxEpoch: _mysten_bcs1118.BcsType<string, string | number | bigint, "u64">;
|
|
28
|
+
userSignature: _mysten_bcs1118.BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
|
|
29
29
|
}, string>;
|
|
30
30
|
type ZkLoginSignature = InferBcsInput<typeof zkLoginSignature>;
|
|
31
31
|
type ZkLoginSignatureInputs = ZkLoginSignature['inputs'];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../../src/zklogin/bcs.ts"],"mappings":";;;;cAMa,gBAAA,
|
|
1
|
+
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../../src/zklogin/bcs.ts"],"mappings":";;;;cAMa,gBAAA,kBAAgB,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;KAkBjB,gBAAA,GAAmB,aAAA,QAAqB,gBAAA;AAAA,KACxC,sBAAA,GAAyB,gBAAA"}
|
package/docs/clients/grpc.md
CHANGED
|
@@ -27,12 +27,25 @@ const grpcClient = new SuiGrpcClient({
|
|
|
27
27
|
});
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
## Transport options
|
|
31
|
+
|
|
32
|
+
By default, `SuiGrpcClient` uses `GrpcWebFetchTransport` from
|
|
33
|
+
[protobuf-ts](https://github.com/timostamm/protobuf-ts), which works in browsers and Node.js via the
|
|
34
|
+
Fetch API. You can also provide a custom transport for advanced use cases.
|
|
35
|
+
|
|
36
|
+
The `GrpcWebFetchTransport` class, `GrpcWebOptions` type, and `RpcTransport` type are all
|
|
37
|
+
re-exported from `@mysten/sui/grpc` for convenience.
|
|
38
|
+
|
|
39
|
+
### gRPC-web transport (default)
|
|
40
|
+
|
|
41
|
+
The default transport uses the gRPC-web protocol over HTTP/1.1 or HTTP/2. You can customize it by
|
|
42
|
+
passing `GrpcWebFetchTransport` options directly:
|
|
31
43
|
|
|
32
44
|
```typescript
|
|
33
45
|
const transport = new GrpcWebFetchTransport({
|
|
34
46
|
baseUrl: 'https://your-custom-grpc-endpoint.com',
|
|
35
|
-
//
|
|
47
|
+
format: 'binary', // default is 'text' (base64-encoded)
|
|
48
|
+
// Additional transport options like fetchInit
|
|
36
49
|
});
|
|
37
50
|
|
|
38
51
|
const grpcClient = new SuiGrpcClient({
|
|
@@ -41,6 +54,46 @@ const grpcClient = new SuiGrpcClient({
|
|
|
41
54
|
});
|
|
42
55
|
```
|
|
43
56
|
|
|
57
|
+
### Native gRPC transport
|
|
58
|
+
|
|
59
|
+
For server-side applications (Node.js, Bun, etc.), you can use the native gRPC transport with
|
|
60
|
+
`@protobuf-ts/grpc-transport` and `@grpc/grpc-js`. This uses HTTP/2 with the native gRPC protocol
|
|
61
|
+
rather than the gRPC-web translation layer.
|
|
62
|
+
|
|
63
|
+
Install the required packages:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @protobuf-ts/grpc-transport @grpc/grpc-js
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Then create the client with a `GrpcTransport`:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const transport = new GrpcTransport({
|
|
73
|
+
host: 'fullnode.testnet.sui.io:443',
|
|
74
|
+
channelCredentials: ChannelCredentials.createSsl(),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const grpcClient = new SuiGrpcClient({
|
|
78
|
+
network: 'testnet',
|
|
79
|
+
transport,
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
For local development without TLS:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const transport = new GrpcTransport({
|
|
87
|
+
host: '127.0.0.1:9000',
|
|
88
|
+
channelCredentials: ChannelCredentials.createInsecure(),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const grpcClient = new SuiGrpcClient({
|
|
92
|
+
network: 'localnet',
|
|
93
|
+
transport,
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
44
97
|
## Using service clients
|
|
45
98
|
|
|
46
99
|
The `SuiGrpcClient` exposes several service clients for lower-level access to the gRPC API. These
|
package/docs/llms-index.md
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
> TypeScript interfaces for Sui
|
|
4
4
|
|
|
5
|
-
- [Sui TypeScript SDK Quick Start](
|
|
5
|
+
- [Sui TypeScript SDK Quick Start](..md): TypeScript SDK for building on the Sui blockchain
|
|
6
6
|
- [Install Sui TypeScript SDK](./install.md): Install the @mysten/sui package and configure your
|
|
7
7
|
project
|
|
8
8
|
- [LLM Documentation](./llm-docs.md): Give AI agents access to Sui SDK documentation in your project
|
|
9
9
|
- [Hello Sui](./hello-sui.md): Build your first Sui application with the TypeScript SDK
|
|
10
10
|
- [Faucet](./faucet.md): Request test SUI tokens from the faucet
|
|
11
|
-
- [Sui Clients](./clients
|
|
11
|
+
- [Sui Clients](./clients.md): Choose and configure gRPC, GraphQL, or JSON-RPC clients
|
|
12
12
|
- [Core API](./clients/core.md): Transport-agnostic Core API shared by all Sui clients
|
|
13
13
|
- [SuiGrpcClient](./clients/grpc.md): Connect to Sui via gRPC with SuiGrpcClient
|
|
14
14
|
- [SuiGraphQLClient](./clients/graphql.md): Connect to Sui via GraphQL with SuiGraphQLClient
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
- [Passkey](./cryptography/passkey.md): Use WebAuthn passkeys for Sui transaction signing
|
|
31
31
|
- [Web Crypto Signer](./cryptography/webcrypto-signer.md): Sign transactions using the Web Crypto
|
|
32
32
|
API
|
|
33
|
-
- [The `@mysten/sui/utils` package](./utils
|
|
34
|
-
|
|
33
|
+
- [The `@mysten/sui/utils` package](./utils.md): Utility functions for addresses, coins, and common
|
|
34
|
+
operations
|
|
35
35
|
- [Derived Objects](./utils/derived_objects.md): Compute derived object IDs from parent objects
|
|
36
36
|
- [BCS](./bcs.md): Binary Canonical Serialization for encoding Sui Move types
|
|
37
37
|
- [ZkLogin](./zklogin.md): Zero-knowledge authentication with OAuth providers on Sui
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
strategies
|
|
40
40
|
- [Transaction Plugins](./plugins.md): Extend transaction building with reusable plugins
|
|
41
41
|
- [Building SDKs](./sdk-building.md): Build custom SDKs on top of the Sui TypeScript SDK
|
|
42
|
-
- [Migrate to 2.0](./migrations/sui-2.0
|
|
42
|
+
- [Migrate to 2.0](./migrations/sui-2.0.md): Migration guide for Sui TypeScript SDK 2.0
|
|
43
43
|
- [Agent Migration Prompt](./migrations/sui-2.0/agent-prompt.md): AI agent prompt for automated SDK
|
|
44
44
|
2.0 migration
|
|
45
45
|
- [@mysten/sui](./migrations/sui-2.0/sui.md): Migrate @mysten/sui from 1.x to 2.0
|
package/docs/utils/index.md
CHANGED
|
@@ -31,6 +31,13 @@ You can use the following helpers to format various values:
|
|
|
31
31
|
- `normalizeSuiNSName`
|
|
32
32
|
- `normalizeSuiNSName`
|
|
33
33
|
|
|
34
|
+
## Parsers
|
|
35
|
+
|
|
36
|
+
You can use the following helpers to parse decimal strings into smallest-unit bigints:
|
|
37
|
+
|
|
38
|
+
- `parseToUnits`: Parse a decimal string into a `bigint`, given a coin's `decimals`
|
|
39
|
+
- `parseToMist`: Parse a SUI decimal string into MIST (9 decimals)
|
|
40
|
+
|
|
34
41
|
## Validators
|
|
35
42
|
|
|
36
43
|
You can use the following helpers to validate the format of various values (this only validates that
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
4
4
|
"description": "Sui TypeScript API",
|
|
5
5
|
"homepage": "https://sdk.mystenlabs.com",
|
|
6
|
-
"version": "2.
|
|
6
|
+
"version": "2.16.0",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"files": [
|
|
@@ -138,8 +138,10 @@
|
|
|
138
138
|
"@graphql-codegen/typescript": "5.0.7",
|
|
139
139
|
"@graphql-codegen/typescript-document-nodes": "5.0.7",
|
|
140
140
|
"@graphql-codegen/typescript-operations": "^5.0.7",
|
|
141
|
+
"@grpc/grpc-js": "^1.13.3",
|
|
141
142
|
"@iarna/toml": "^2.2.5",
|
|
142
143
|
"@parcel/watcher": "^2.5.4",
|
|
144
|
+
"@protobuf-ts/grpc-transport": "^2.11.1",
|
|
143
145
|
"@types/node": "^25.0.8",
|
|
144
146
|
"@types/tmp": "^0.2.6",
|
|
145
147
|
"cross-env": "^10.1.0",
|
|
@@ -148,7 +150,7 @@
|
|
|
148
150
|
"tmp": "^0.2.5",
|
|
149
151
|
"ts-retry-promise": "^0.8.1",
|
|
150
152
|
"typescript": "^5.9.3",
|
|
151
|
-
"vite": "^
|
|
153
|
+
"vite": "^8.0.5",
|
|
152
154
|
"vite-tsconfig-paths": "^6.0.4",
|
|
153
155
|
"vitest": "^4.0.17",
|
|
154
156
|
"wait-on": "^9.0.3"
|
package/src/client/mvr.ts
CHANGED
|
@@ -332,6 +332,10 @@ function validateOverrides(overrides?: {
|
|
|
332
332
|
export function extractMvrTypes(type: string | StructTag, types = new Set<string>()) {
|
|
333
333
|
if (typeof type === 'string' && !hasMvrName(type)) return types;
|
|
334
334
|
|
|
335
|
+
if (typeof type === 'string' && type.startsWith('vector<') && type.endsWith('>')) {
|
|
336
|
+
return extractMvrTypes(type.slice(7, -1), types);
|
|
337
|
+
}
|
|
338
|
+
|
|
335
339
|
const tag = isStructTag(type) ? type : parseStructTag(type);
|
|
336
340
|
|
|
337
341
|
if (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`);
|
|
@@ -348,6 +352,14 @@ export function extractMvrTypes(type: string | StructTag, types = new Set<string
|
|
|
348
352
|
* based on the supplied type cache.
|
|
349
353
|
*/
|
|
350
354
|
function replaceMvrNames(tag: string | StructTag, typeCache: Record<string, string>): string {
|
|
355
|
+
if (typeof tag === 'string' && !tag.includes('::')) {
|
|
356
|
+
return tag;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (typeof tag === 'string' && tag.startsWith('vector<') && tag.endsWith('>')) {
|
|
360
|
+
return `vector<${replaceMvrNames(tag.slice(7, -1), typeCache)}>`;
|
|
361
|
+
}
|
|
362
|
+
|
|
351
363
|
const type = isStructTag(tag) ? tag : parseStructTag(tag);
|
|
352
364
|
|
|
353
365
|
const typeTag = `${type.address}::${type.module}::${type.name}`;
|
package/src/grpc/index.ts
CHANGED
|
@@ -6,6 +6,12 @@ export { GrpcCoreClient } from './core.js';
|
|
|
6
6
|
export type { SuiGrpcClientOptions } from './client.js';
|
|
7
7
|
export type { GrpcCoreClientOptions } from './core.js';
|
|
8
8
|
|
|
9
|
+
// Re-export transports and types so users can configure custom transports
|
|
10
|
+
// without adding @protobuf-ts/* as direct dependencies.
|
|
11
|
+
export { GrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport';
|
|
12
|
+
export type { GrpcWebOptions } from '@protobuf-ts/grpcweb-transport';
|
|
13
|
+
export type { RpcTransport } from '@protobuf-ts/runtime-rpc';
|
|
14
|
+
|
|
9
15
|
// Export all gRPC proto types as a namespace
|
|
10
16
|
import * as GrpcTypes from './proto/types.js';
|
|
11
17
|
export { GrpcTypes };
|