@mysten/sui 1.14.0 → 1.14.2
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 +13 -0
- package/dist/cjs/client/http-transport.js +0 -1
- package/dist/cjs/client/http-transport.js.map +2 -2
- package/dist/cjs/transactions/intents/CoinWithBalance.js +8 -1
- package/dist/cjs/transactions/intents/CoinWithBalance.js.map +2 -2
- package/dist/cjs/version.d.ts +2 -2
- package/dist/cjs/version.js +2 -2
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/client/http-transport.js +0 -1
- package/dist/esm/client/http-transport.js.map +2 -2
- package/dist/esm/transactions/intents/CoinWithBalance.js +8 -1
- package/dist/esm/transactions/intents/CoinWithBalance.js.map +2 -2
- package/dist/esm/version.d.ts +2 -2
- package/dist/esm/version.js +2 -2
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/client/http-transport.ts +0 -1
- package/src/transactions/intents/CoinWithBalance.ts +9 -1
- package/src/version.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# @mysten/sui.js
|
|
2
2
|
|
|
3
|
+
## 1.14.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- e7bc63e: Allow 0 amounts with `coinWithBalance` intent when the wallet has no coin objects of the
|
|
8
|
+
required type.
|
|
9
|
+
|
|
10
|
+
## 1.14.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 69ef100: revert adding client-request-method header
|
|
15
|
+
|
|
3
16
|
## 1.14.0
|
|
4
17
|
|
|
5
18
|
### Minor Changes
|
|
@@ -59,7 +59,6 @@ class SuiHTTPTransport {
|
|
|
59
59
|
"Client-Sdk-Type": "typescript",
|
|
60
60
|
"Client-Sdk-Version": import_version.PACKAGE_VERSION,
|
|
61
61
|
"Client-Target-Api-Version": import_version.TARGETED_RPC_VERSION,
|
|
62
|
-
"Client-Request-Method": input.method,
|
|
63
62
|
...__privateGet(this, _options).rpc?.headers
|
|
64
63
|
},
|
|
65
64
|
body: JSON.stringify({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/client/http-transport.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { PACKAGE_VERSION, TARGETED_RPC_VERSION } from '../version.js';\nimport { JsonRpcError, SuiHTTPStatusError } from './errors.js';\nimport type { WebsocketClientOptions } from './rpc-websocket-client.js';\nimport { WebsocketClient } from './rpc-websocket-client.js';\n\n/**\n * An object defining headers to be passed to the RPC server\n */\nexport type HttpHeaders = { [header: string]: string };\n\nexport interface SuiHTTPTransportOptions {\n\tfetch?: typeof fetch;\n\tWebSocketConstructor?: typeof WebSocket;\n\turl: string;\n\trpc?: {\n\t\theaders?: HttpHeaders;\n\t\turl?: string;\n\t};\n\twebsocket?: WebsocketClientOptions & {\n\t\turl?: string;\n\t};\n}\n\nexport interface SuiTransportRequestOptions {\n\tmethod: string;\n\tparams: unknown[];\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\n\nexport interface SuiTransportSubscribeOptions<T> {\n\tmethod: string;\n\tunsubscribe: string;\n\tparams: unknown[];\n\tonMessage: (event: T) => void;\n}\n\nexport interface SuiTransport {\n\trequest<T = unknown>(input: SuiTransportRequestOptions): Promise<T>;\n\tsubscribe<T = unknown>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>>;\n}\n\nexport class SuiHTTPTransport implements SuiTransport {\n\t#requestId = 0;\n\t#options: SuiHTTPTransportOptions;\n\t#websocketClient?: WebsocketClient;\n\n\tconstructor(options: SuiHTTPTransportOptions) {\n\t\tthis.#options = options;\n\t}\n\n\tfetch(input: RequestInfo, init?: RequestInit): Promise<Response> {\n\t\tconst fetchFn = this.#options.fetch ?? fetch;\n\n\t\tif (!fetchFn) {\n\t\t\tthrow new Error(\n\t\t\t\t'The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.',\n\t\t\t);\n\t\t}\n\n\t\treturn fetchFn(input, init);\n\t}\n\n\t#getWebsocketClient(): WebsocketClient {\n\t\tif (!this.#websocketClient) {\n\t\t\tconst WebSocketConstructor = this.#options.WebSocketConstructor ?? WebSocket;\n\t\t\tif (!WebSocketConstructor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The current environment does not support WebSocket, you can provide a WebSocketConstructor in the options for SuiHTTPTransport.',\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.#websocketClient = new WebsocketClient(\n\t\t\t\tthis.#options.websocket?.url ?? this.#options.url,\n\t\t\t\t{\n\t\t\t\t\tWebSocketConstructor,\n\t\t\t\t\t...this.#options.websocket,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\treturn this.#websocketClient;\n\t}\n\n\tasync request<T>(input: SuiTransportRequestOptions): Promise<T> {\n\t\tthis.#requestId += 1;\n\n\t\tconst res = await this.fetch(this.#options.rpc?.url ?? this.#options.url, {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t\t\t'Client-Target-Api-Version': TARGETED_RPC_VERSION,\n\t\t\t\t
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAsD;AACtD,oBAAiD;AAEjD,kCAAgC;AANhC;AA6CO,MAAM,iBAAyC;AAAA,EAKrD,YAAY,SAAkC;AALxC;AACN,mCAAa;AACb;AACA;AAGC,uBAAK,UAAW;AAAA,EACjB;AAAA,EAEA,MAAM,OAAoB,MAAuC;AAChE,UAAM,UAAU,mBAAK,UAAS,SAAS;AAEvC,QAAI,CAAC,SAAS;AACb,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC3B;AAAA,EAuBA,MAAM,QAAW,OAA+C;AAC/D,uBAAK,YAAL,mBAAK,cAAc;AAEnB,UAAM,MAAM,MAAM,KAAK,MAAM,mBAAK,UAAS,KAAK,OAAO,mBAAK,UAAS,KAAK;AAAA,MACzE,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,mBAAmB;AAAA,QACnB,sBAAsB;AAAA,QACtB,6BAA6B;AAAA,QAC7B,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { PACKAGE_VERSION, TARGETED_RPC_VERSION } from '../version.js';\nimport { JsonRpcError, SuiHTTPStatusError } from './errors.js';\nimport type { WebsocketClientOptions } from './rpc-websocket-client.js';\nimport { WebsocketClient } from './rpc-websocket-client.js';\n\n/**\n * An object defining headers to be passed to the RPC server\n */\nexport type HttpHeaders = { [header: string]: string };\n\nexport interface SuiHTTPTransportOptions {\n\tfetch?: typeof fetch;\n\tWebSocketConstructor?: typeof WebSocket;\n\turl: string;\n\trpc?: {\n\t\theaders?: HttpHeaders;\n\t\turl?: string;\n\t};\n\twebsocket?: WebsocketClientOptions & {\n\t\turl?: string;\n\t};\n}\n\nexport interface SuiTransportRequestOptions {\n\tmethod: string;\n\tparams: unknown[];\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\n\nexport interface SuiTransportSubscribeOptions<T> {\n\tmethod: string;\n\tunsubscribe: string;\n\tparams: unknown[];\n\tonMessage: (event: T) => void;\n}\n\nexport interface SuiTransport {\n\trequest<T = unknown>(input: SuiTransportRequestOptions): Promise<T>;\n\tsubscribe<T = unknown>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>>;\n}\n\nexport class SuiHTTPTransport implements SuiTransport {\n\t#requestId = 0;\n\t#options: SuiHTTPTransportOptions;\n\t#websocketClient?: WebsocketClient;\n\n\tconstructor(options: SuiHTTPTransportOptions) {\n\t\tthis.#options = options;\n\t}\n\n\tfetch(input: RequestInfo, init?: RequestInit): Promise<Response> {\n\t\tconst fetchFn = this.#options.fetch ?? fetch;\n\n\t\tif (!fetchFn) {\n\t\t\tthrow new Error(\n\t\t\t\t'The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.',\n\t\t\t);\n\t\t}\n\n\t\treturn fetchFn(input, init);\n\t}\n\n\t#getWebsocketClient(): WebsocketClient {\n\t\tif (!this.#websocketClient) {\n\t\t\tconst WebSocketConstructor = this.#options.WebSocketConstructor ?? WebSocket;\n\t\t\tif (!WebSocketConstructor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The current environment does not support WebSocket, you can provide a WebSocketConstructor in the options for SuiHTTPTransport.',\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.#websocketClient = new WebsocketClient(\n\t\t\t\tthis.#options.websocket?.url ?? this.#options.url,\n\t\t\t\t{\n\t\t\t\t\tWebSocketConstructor,\n\t\t\t\t\t...this.#options.websocket,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\treturn this.#websocketClient;\n\t}\n\n\tasync request<T>(input: SuiTransportRequestOptions): Promise<T> {\n\t\tthis.#requestId += 1;\n\n\t\tconst res = await this.fetch(this.#options.rpc?.url ?? this.#options.url, {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t\t\t'Client-Target-Api-Version': TARGETED_RPC_VERSION,\n\t\t\t\t...this.#options.rpc?.headers,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tjsonrpc: '2.0',\n\t\t\t\tid: this.#requestId,\n\t\t\t\tmethod: input.method,\n\t\t\t\tparams: input.params,\n\t\t\t}),\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new SuiHTTPStatusError(\n\t\t\t\t`Unexpected status code: ${res.status}`,\n\t\t\t\tres.status,\n\t\t\t\tres.statusText,\n\t\t\t);\n\t\t}\n\n\t\tconst data = await res.json();\n\n\t\tif ('error' in data && data.error != null) {\n\t\t\tthrow new JsonRpcError(data.error.message, data.error.code);\n\t\t}\n\n\t\treturn data.result;\n\t}\n\n\tasync subscribe<T>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>> {\n\t\tconst unsubscribe = await this.#getWebsocketClient().subscribe(input);\n\n\t\treturn async () => !!(await unsubscribe());\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAsD;AACtD,oBAAiD;AAEjD,kCAAgC;AANhC;AA6CO,MAAM,iBAAyC;AAAA,EAKrD,YAAY,SAAkC;AALxC;AACN,mCAAa;AACb;AACA;AAGC,uBAAK,UAAW;AAAA,EACjB;AAAA,EAEA,MAAM,OAAoB,MAAuC;AAChE,UAAM,UAAU,mBAAK,UAAS,SAAS;AAEvC,QAAI,CAAC,SAAS;AACb,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC3B;AAAA,EAuBA,MAAM,QAAW,OAA+C;AAC/D,uBAAK,YAAL,mBAAK,cAAc;AAEnB,UAAM,MAAM,MAAM,KAAK,MAAM,mBAAK,UAAS,KAAK,OAAO,mBAAK,UAAS,KAAK;AAAA,MACzE,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,mBAAmB;AAAA,QACnB,sBAAsB;AAAA,QACtB,6BAA6B;AAAA,QAC7B,GAAG,mBAAK,UAAS,KAAK;AAAA,MACvB;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACpB,SAAS;AAAA,QACT,IAAI,mBAAK;AAAA,QACT,QAAQ,MAAM;AAAA,QACd,QAAQ,MAAM;AAAA,MACf,CAAC;AAAA,IACF,CAAC;AAED,QAAI,CAAC,IAAI,IAAI;AACZ,YAAM,IAAI;AAAA,QACT,2BAA2B,IAAI,MAAM;AAAA,QACrC,IAAI;AAAA,QACJ,IAAI;AAAA,MACL;AAAA,IACD;AAEA,UAAM,OAAO,MAAM,IAAI,KAAK;AAE5B,QAAI,WAAW,QAAQ,KAAK,SAAS,MAAM;AAC1C,YAAM,IAAI,2BAAa,KAAK,MAAM,SAAS,KAAK,MAAM,IAAI;AAAA,IAC3D;AAEA,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,UAAa,OAAyE;AAC3F,UAAM,cAAc,MAAM,sBAAK,oDAAL,WAA2B,UAAU,KAAK;AAEpE,WAAO,YAAY,CAAC,CAAE,MAAM,YAAY;AAAA,EACzC;AACD;AAnFC;AACA;AACA;AAHM;AAqBN,wBAAmB,WAAoB;AACtC,MAAI,CAAC,mBAAK,mBAAkB;AAC3B,UAAM,uBAAuB,mBAAK,UAAS,wBAAwB;AACnE,QAAI,CAAC,sBAAsB;AAC1B,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,uBAAK,kBAAmB,IAAI;AAAA,MAC3B,mBAAK,UAAS,WAAW,OAAO,mBAAK,UAAS;AAAA,MAC9C;AAAA,QACC;AAAA,QACA,GAAG,mBAAK,UAAS;AAAA,MAClB;AAAA,IACD;AAAA,EACD;AAEA,SAAO,mBAAK;AACb;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -62,7 +62,7 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
62
62
|
for (const command of transactionData.commands) {
|
|
63
63
|
if (command.$kind === "$Intent" && command.$Intent.name === COIN_WITH_BALANCE) {
|
|
64
64
|
const { type, balance } = (0, import_valibot.parse)(CoinWithBalanceData, command.$Intent.data);
|
|
65
|
-
if (type !== "gas") {
|
|
65
|
+
if (type !== "gas" && balance > 0n) {
|
|
66
66
|
coinTypes.add(type);
|
|
67
67
|
}
|
|
68
68
|
totalByType.set(type, (totalByType.get(type) ?? 0n) + balance);
|
|
@@ -100,6 +100,13 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
100
100
|
continue;
|
|
101
101
|
}
|
|
102
102
|
const { type, balance } = transaction.$Intent.data;
|
|
103
|
+
if (balance === 0n) {
|
|
104
|
+
transactionData.replaceCommand(
|
|
105
|
+
index,
|
|
106
|
+
import_Commands.Commands.MoveCall({ target: "0x2::coin::zero", typeArguments: [type] })
|
|
107
|
+
);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
103
110
|
const commands = [];
|
|
104
111
|
if (!mergedCoins.has(type)) {
|
|
105
112
|
const [first, ...rest] = coinsByType.get(type).map(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/transactions/intents/CoinWithBalance.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, parse, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport type { CoinStruct, SuiClient } from '../../client/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { Commands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../json-rpc-resolver.js';\nimport { getClient } from '../json-rpc-resolver.js';\nimport type { Transaction } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\n\nconst 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}) {\n\treturn (tx: Transaction) => {\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\treturn tx.add(\n\t\t\tCommands.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} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n});\n\nasync function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\tfor (const command of transactionData.commands) {\n\t\tif (command.$kind === '$Intent' && command.$Intent.name === COIN_WITH_BALANCE) {\n\t\t\tconst { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t\tif (type !== 'gas') {\n\t\t\t\tcoinTypes.add(type);\n\t\t\t}\n\n\t\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\t\t}\n\t}\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, CoinStruct[]>();\n\tconst client = getClient(buildOptions);\n\tawait Promise.all(\n\t\t[...coinTypes].map(async (coinType) => {\n\t\t\tcoinsByType.set(\n\t\t\t\tcoinType,\n\t\t\t\tawait getCoinsOfType({\n\t\t\t\t\tcoinType,\n\t\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\t\tclient,\n\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\tusedIds,\n\t\t\t\t}),\n\t\t\t);\n\t\t}),\n\t);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\n\tmergedCoins.set('gas', { $kind: 'GasCoin', GasCoin: true });\n\n\tfor (const [index, transaction] of transactionData.commands.entries()) {\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\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\n\t\tconst commands = [];\n\n\t\tif (!mergedCoins.has(type)) {\n\t\t\tconst [first, ...rest] = coinsByType.get(type)!.map((coin) =>\n\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t'object',\n\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\tobjectId: coin.coinObjectId,\n\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t);\n\n\t\t\tif (rest.length > 0) {\n\t\t\t\tcommands.push(Commands.MergeCoins(first, rest));\n\t\t\t}\n\n\t\t\tmergedCoins.set(type, first);\n\t\t}\n\n\t\tcommands.push(\n\t\t\tCommands.SplitCoins(mergedCoins.get(type)!, [\n\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(balance))),\n\t\t\t]),\n\t\t);\n\n\t\ttransactionData.replaceCommand(index, commands);\n\n\t\ttransactionData.mapArguments((arg) => {\n\t\t\tif (arg.$kind === 'Result' && arg.Result === index) {\n\t\t\t\treturn {\n\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn arg;\n\t\t});\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: SuiClient;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<CoinStruct[]> {\n\tlet remainingBalance = balance;\n\tconst coins: CoinStruct[] = [];\n\n\treturn loadMoreCoins();\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<CoinStruct[]> {\n\t\tconst { data, hasNextPage, nextCursor } = await client.getCoins({ owner, coinType, cursor });\n\n\t\tconst sortedCoins = data.sort((a, b) => Number(BigInt(b.balance) - BigInt(a.balance)));\n\n\t\tfor (const coin of sortedCoins) {\n\t\t\tif (usedIds.has(coin.coinObjectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst coinBalance = BigInt(coin.balance);\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= coinBalance;\n\n\t\t\tif (remainingBalance <= 0) {\n\t\t\t\treturn coins;\n\t\t\t}\n\t\t}\n\n\t\tif (hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\tthrow new Error(`Not enough coins of type ${coinType} to satisfy requested balance`);\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,qBAA8C;AAE9C,iBAAoB;AAEpB,uBAAmC;AACnC,sBAAyB;AAEzB,oBAAuB;AAEvB,+BAA0B;AAI1B,MAAM,oBAAoB;AAC1B,MAAM,eAAW,qCAAmB,eAAe;AAE5C,SAAS,gBAAgB;AAAA,EAC/B,OAAO;AAAA,EACP;AAAA,EACA,aAAa;AACd,GAIG;AACF,SAAO,CAAC,OAAoB;AAC3B,OAAG,kBAAkB,mBAAmB,kBAAkB;AAC1D,UAAM,WAAW,SAAS,QAAQ,WAAO,qCAAmB,IAAI;AAEhE,WAAO,GAAG;AAAA,MACT,yBAAS,OAAO;AAAA,QACf,MAAM;AAAA,QACN,QAAQ,CAAC;AAAA,QACT,MAAM;AAAA,UACL,MAAM,aAAa,YAAY,aAAa,QAAQ;AAAA,UACpD,SAAS,OAAO,OAAO;AAAA,QACxB;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;AAEA,MAAM,0BAAsB,uBAAO;AAAA,EAClC,UAAM,uBAAO;AAAA,EACb,aAAS,uBAAO;AACjB,CAAC;AAED,eAAe,mBACd,iBACA,cACA,MACC;AACD,QAAM,YAAY,oBAAI,IAAY;AAClC,QAAM,cAAc,oBAAI,IAAoB;AAE5C,MAAI,CAAC,gBAAgB,QAAQ;AAC5B,UAAM,IAAI,MAAM,+CAA+C;AAAA,EAChE;AAEA,aAAW,WAAW,gBAAgB,UAAU;AAC/C,QAAI,QAAQ,UAAU,aAAa,QAAQ,QAAQ,SAAS,mBAAmB;AAC9E,YAAM,EAAE,MAAM,QAAQ,QAAI,sBAAM,qBAAqB,QAAQ,QAAQ,IAAI;AAEzE,UAAI,SAAS,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, parse, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport type { CoinStruct, SuiClient } from '../../client/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { Commands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../json-rpc-resolver.js';\nimport { getClient } from '../json-rpc-resolver.js';\nimport type { Transaction } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\n\nconst 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}) {\n\treturn (tx: Transaction) => {\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\treturn tx.add(\n\t\t\tCommands.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} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n});\n\nasync function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\tfor (const command of transactionData.commands) {\n\t\tif (command.$kind === '$Intent' && command.$Intent.name === COIN_WITH_BALANCE) {\n\t\t\tconst { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t\tif (type !== 'gas' && balance > 0n) {\n\t\t\t\tcoinTypes.add(type);\n\t\t\t}\n\n\t\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\t\t}\n\t}\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, CoinStruct[]>();\n\tconst client = getClient(buildOptions);\n\tawait Promise.all(\n\t\t[...coinTypes].map(async (coinType) => {\n\t\t\tcoinsByType.set(\n\t\t\t\tcoinType,\n\t\t\t\tawait getCoinsOfType({\n\t\t\t\t\tcoinType,\n\t\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\t\tclient,\n\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\tusedIds,\n\t\t\t\t}),\n\t\t\t);\n\t\t}),\n\t);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\n\tmergedCoins.set('gas', { $kind: 'GasCoin', GasCoin: true });\n\n\tfor (const [index, transaction] of transactionData.commands.entries()) {\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\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\n\t\tif (balance === 0n) {\n\t\t\ttransactionData.replaceCommand(\n\t\t\t\tindex,\n\t\t\t\tCommands.MoveCall({ target: '0x2::coin::zero', typeArguments: [type] }),\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst commands = [];\n\n\t\tif (!mergedCoins.has(type)) {\n\t\t\tconst [first, ...rest] = coinsByType.get(type)!.map((coin) =>\n\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t'object',\n\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\tobjectId: coin.coinObjectId,\n\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t);\n\n\t\t\tif (rest.length > 0) {\n\t\t\t\tcommands.push(Commands.MergeCoins(first, rest));\n\t\t\t}\n\n\t\t\tmergedCoins.set(type, first);\n\t\t}\n\n\t\tcommands.push(\n\t\t\tCommands.SplitCoins(mergedCoins.get(type)!, [\n\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(balance))),\n\t\t\t]),\n\t\t);\n\n\t\ttransactionData.replaceCommand(index, commands);\n\n\t\ttransactionData.mapArguments((arg) => {\n\t\t\tif (arg.$kind === 'Result' && arg.Result === index) {\n\t\t\t\treturn {\n\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn arg;\n\t\t});\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: SuiClient;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<CoinStruct[]> {\n\tlet remainingBalance = balance;\n\tconst coins: CoinStruct[] = [];\n\n\treturn loadMoreCoins();\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<CoinStruct[]> {\n\t\tconst { data, hasNextPage, nextCursor } = await client.getCoins({ owner, coinType, cursor });\n\n\t\tconst sortedCoins = data.sort((a, b) => Number(BigInt(b.balance) - BigInt(a.balance)));\n\n\t\tfor (const coin of sortedCoins) {\n\t\t\tif (usedIds.has(coin.coinObjectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst coinBalance = BigInt(coin.balance);\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= coinBalance;\n\n\t\t\tif (remainingBalance <= 0) {\n\t\t\t\treturn coins;\n\t\t\t}\n\t\t}\n\n\t\tif (hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\tthrow new Error(`Not enough coins of type ${coinType} to satisfy requested balance`);\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,qBAA8C;AAE9C,iBAAoB;AAEpB,uBAAmC;AACnC,sBAAyB;AAEzB,oBAAuB;AAEvB,+BAA0B;AAI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AM,4BAA4B,QAAQ,+BAA+B;AAAA,EACpF;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PACKAGE_VERSION = "1.14.
|
|
2
|
-
export declare const TARGETED_RPC_VERSION = "1.
|
|
1
|
+
export declare const PACKAGE_VERSION = "1.14.2";
|
|
2
|
+
export declare const TARGETED_RPC_VERSION = "1.38.0";
|
package/dist/cjs/version.js
CHANGED
|
@@ -22,6 +22,6 @@ __export(version_exports, {
|
|
|
22
22
|
TARGETED_RPC_VERSION: () => TARGETED_RPC_VERSION
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(version_exports);
|
|
25
|
-
const PACKAGE_VERSION = "1.14.
|
|
26
|
-
const TARGETED_RPC_VERSION = "1.
|
|
25
|
+
const PACKAGE_VERSION = "1.14.2";
|
|
26
|
+
const TARGETED_RPC_VERSION = "1.38.0";
|
|
27
27
|
//# sourceMappingURL=version.js.map
|
package/dist/cjs/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/version.ts"],
|
|
4
|
-
"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 = '1.14.
|
|
4
|
+
"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 = '1.14.2';\nexport const TARGETED_RPC_VERSION = '1.38.0';\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -36,7 +36,6 @@ class SuiHTTPTransport {
|
|
|
36
36
|
"Client-Sdk-Type": "typescript",
|
|
37
37
|
"Client-Sdk-Version": PACKAGE_VERSION,
|
|
38
38
|
"Client-Target-Api-Version": TARGETED_RPC_VERSION,
|
|
39
|
-
"Client-Request-Method": input.method,
|
|
40
39
|
...__privateGet(this, _options).rpc?.headers
|
|
41
40
|
},
|
|
42
41
|
body: JSON.stringify({
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/client/http-transport.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { PACKAGE_VERSION, TARGETED_RPC_VERSION } from '../version.js';\nimport { JsonRpcError, SuiHTTPStatusError } from './errors.js';\nimport type { WebsocketClientOptions } from './rpc-websocket-client.js';\nimport { WebsocketClient } from './rpc-websocket-client.js';\n\n/**\n * An object defining headers to be passed to the RPC server\n */\nexport type HttpHeaders = { [header: string]: string };\n\nexport interface SuiHTTPTransportOptions {\n\tfetch?: typeof fetch;\n\tWebSocketConstructor?: typeof WebSocket;\n\turl: string;\n\trpc?: {\n\t\theaders?: HttpHeaders;\n\t\turl?: string;\n\t};\n\twebsocket?: WebsocketClientOptions & {\n\t\turl?: string;\n\t};\n}\n\nexport interface SuiTransportRequestOptions {\n\tmethod: string;\n\tparams: unknown[];\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\n\nexport interface SuiTransportSubscribeOptions<T> {\n\tmethod: string;\n\tunsubscribe: string;\n\tparams: unknown[];\n\tonMessage: (event: T) => void;\n}\n\nexport interface SuiTransport {\n\trequest<T = unknown>(input: SuiTransportRequestOptions): Promise<T>;\n\tsubscribe<T = unknown>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>>;\n}\n\nexport class SuiHTTPTransport implements SuiTransport {\n\t#requestId = 0;\n\t#options: SuiHTTPTransportOptions;\n\t#websocketClient?: WebsocketClient;\n\n\tconstructor(options: SuiHTTPTransportOptions) {\n\t\tthis.#options = options;\n\t}\n\n\tfetch(input: RequestInfo, init?: RequestInit): Promise<Response> {\n\t\tconst fetchFn = this.#options.fetch ?? fetch;\n\n\t\tif (!fetchFn) {\n\t\t\tthrow new Error(\n\t\t\t\t'The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.',\n\t\t\t);\n\t\t}\n\n\t\treturn fetchFn(input, init);\n\t}\n\n\t#getWebsocketClient(): WebsocketClient {\n\t\tif (!this.#websocketClient) {\n\t\t\tconst WebSocketConstructor = this.#options.WebSocketConstructor ?? WebSocket;\n\t\t\tif (!WebSocketConstructor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The current environment does not support WebSocket, you can provide a WebSocketConstructor in the options for SuiHTTPTransport.',\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.#websocketClient = new WebsocketClient(\n\t\t\t\tthis.#options.websocket?.url ?? this.#options.url,\n\t\t\t\t{\n\t\t\t\t\tWebSocketConstructor,\n\t\t\t\t\t...this.#options.websocket,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\treturn this.#websocketClient;\n\t}\n\n\tasync request<T>(input: SuiTransportRequestOptions): Promise<T> {\n\t\tthis.#requestId += 1;\n\n\t\tconst res = await this.fetch(this.#options.rpc?.url ?? this.#options.url, {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t\t\t'Client-Target-Api-Version': TARGETED_RPC_VERSION,\n\t\t\t\t
|
|
5
|
-
"mappings": ";;;;;;;;AAAA;AAGA,SAAS,iBAAiB,4BAA4B;AACtD,SAAS,cAAc,0BAA0B;AAEjD,SAAS,uBAAuB;AAuCzB,MAAM,iBAAyC;AAAA,EAKrD,YAAY,SAAkC;AALxC;AACN,mCAAa;AACb;AACA;AAGC,uBAAK,UAAW;AAAA,EACjB;AAAA,EAEA,MAAM,OAAoB,MAAuC;AAChE,UAAM,UAAU,mBAAK,UAAS,SAAS;AAEvC,QAAI,CAAC,SAAS;AACb,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC3B;AAAA,EAuBA,MAAM,QAAW,OAA+C;AAC/D,uBAAK,YAAL,mBAAK,cAAc;AAEnB,UAAM,MAAM,MAAM,KAAK,MAAM,mBAAK,UAAS,KAAK,OAAO,mBAAK,UAAS,KAAK;AAAA,MACzE,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,mBAAmB;AAAA,QACnB,sBAAsB;AAAA,QACtB,6BAA6B;AAAA,QAC7B,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { PACKAGE_VERSION, TARGETED_RPC_VERSION } from '../version.js';\nimport { JsonRpcError, SuiHTTPStatusError } from './errors.js';\nimport type { WebsocketClientOptions } from './rpc-websocket-client.js';\nimport { WebsocketClient } from './rpc-websocket-client.js';\n\n/**\n * An object defining headers to be passed to the RPC server\n */\nexport type HttpHeaders = { [header: string]: string };\n\nexport interface SuiHTTPTransportOptions {\n\tfetch?: typeof fetch;\n\tWebSocketConstructor?: typeof WebSocket;\n\turl: string;\n\trpc?: {\n\t\theaders?: HttpHeaders;\n\t\turl?: string;\n\t};\n\twebsocket?: WebsocketClientOptions & {\n\t\turl?: string;\n\t};\n}\n\nexport interface SuiTransportRequestOptions {\n\tmethod: string;\n\tparams: unknown[];\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\n\nexport interface SuiTransportSubscribeOptions<T> {\n\tmethod: string;\n\tunsubscribe: string;\n\tparams: unknown[];\n\tonMessage: (event: T) => void;\n}\n\nexport interface SuiTransport {\n\trequest<T = unknown>(input: SuiTransportRequestOptions): Promise<T>;\n\tsubscribe<T = unknown>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>>;\n}\n\nexport class SuiHTTPTransport implements SuiTransport {\n\t#requestId = 0;\n\t#options: SuiHTTPTransportOptions;\n\t#websocketClient?: WebsocketClient;\n\n\tconstructor(options: SuiHTTPTransportOptions) {\n\t\tthis.#options = options;\n\t}\n\n\tfetch(input: RequestInfo, init?: RequestInit): Promise<Response> {\n\t\tconst fetchFn = this.#options.fetch ?? fetch;\n\n\t\tif (!fetchFn) {\n\t\t\tthrow new Error(\n\t\t\t\t'The current environment does not support fetch, you can provide a fetch implementation in the options for SuiHTTPTransport.',\n\t\t\t);\n\t\t}\n\n\t\treturn fetchFn(input, init);\n\t}\n\n\t#getWebsocketClient(): WebsocketClient {\n\t\tif (!this.#websocketClient) {\n\t\t\tconst WebSocketConstructor = this.#options.WebSocketConstructor ?? WebSocket;\n\t\t\tif (!WebSocketConstructor) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The current environment does not support WebSocket, you can provide a WebSocketConstructor in the options for SuiHTTPTransport.',\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.#websocketClient = new WebsocketClient(\n\t\t\t\tthis.#options.websocket?.url ?? this.#options.url,\n\t\t\t\t{\n\t\t\t\t\tWebSocketConstructor,\n\t\t\t\t\t...this.#options.websocket,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\treturn this.#websocketClient;\n\t}\n\n\tasync request<T>(input: SuiTransportRequestOptions): Promise<T> {\n\t\tthis.#requestId += 1;\n\n\t\tconst res = await this.fetch(this.#options.rpc?.url ?? this.#options.url, {\n\t\t\tmethod: 'POST',\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t'Client-Sdk-Type': 'typescript',\n\t\t\t\t'Client-Sdk-Version': PACKAGE_VERSION,\n\t\t\t\t'Client-Target-Api-Version': TARGETED_RPC_VERSION,\n\t\t\t\t...this.#options.rpc?.headers,\n\t\t\t},\n\t\t\tbody: JSON.stringify({\n\t\t\t\tjsonrpc: '2.0',\n\t\t\t\tid: this.#requestId,\n\t\t\t\tmethod: input.method,\n\t\t\t\tparams: input.params,\n\t\t\t}),\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new SuiHTTPStatusError(\n\t\t\t\t`Unexpected status code: ${res.status}`,\n\t\t\t\tres.status,\n\t\t\t\tres.statusText,\n\t\t\t);\n\t\t}\n\n\t\tconst data = await res.json();\n\n\t\tif ('error' in data && data.error != null) {\n\t\t\tthrow new JsonRpcError(data.error.message, data.error.code);\n\t\t}\n\n\t\treturn data.result;\n\t}\n\n\tasync subscribe<T>(input: SuiTransportSubscribeOptions<T>): Promise<() => Promise<boolean>> {\n\t\tconst unsubscribe = await this.#getWebsocketClient().subscribe(input);\n\n\t\treturn async () => !!(await unsubscribe());\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;AAAA;AAGA,SAAS,iBAAiB,4BAA4B;AACtD,SAAS,cAAc,0BAA0B;AAEjD,SAAS,uBAAuB;AAuCzB,MAAM,iBAAyC;AAAA,EAKrD,YAAY,SAAkC;AALxC;AACN,mCAAa;AACb;AACA;AAGC,uBAAK,UAAW;AAAA,EACjB;AAAA,EAEA,MAAM,OAAoB,MAAuC;AAChE,UAAM,UAAU,mBAAK,UAAS,SAAS;AAEvC,QAAI,CAAC,SAAS;AACb,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC3B;AAAA,EAuBA,MAAM,QAAW,OAA+C;AAC/D,uBAAK,YAAL,mBAAK,cAAc;AAEnB,UAAM,MAAM,MAAM,KAAK,MAAM,mBAAK,UAAS,KAAK,OAAO,mBAAK,UAAS,KAAK;AAAA,MACzE,QAAQ;AAAA,MACR,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,mBAAmB;AAAA,QACnB,sBAAsB;AAAA,QACtB,6BAA6B;AAAA,QAC7B,GAAG,mBAAK,UAAS,KAAK;AAAA,MACvB;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACpB,SAAS;AAAA,QACT,IAAI,mBAAK;AAAA,QACT,QAAQ,MAAM;AAAA,QACd,QAAQ,MAAM;AAAA,MACf,CAAC;AAAA,IACF,CAAC;AAED,QAAI,CAAC,IAAI,IAAI;AACZ,YAAM,IAAI;AAAA,QACT,2BAA2B,IAAI,MAAM;AAAA,QACrC,IAAI;AAAA,QACJ,IAAI;AAAA,MACL;AAAA,IACD;AAEA,UAAM,OAAO,MAAM,IAAI,KAAK;AAE5B,QAAI,WAAW,QAAQ,KAAK,SAAS,MAAM;AAC1C,YAAM,IAAI,aAAa,KAAK,MAAM,SAAS,KAAK,MAAM,IAAI;AAAA,IAC3D;AAEA,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,UAAa,OAAyE;AAC3F,UAAM,cAAc,MAAM,sBAAK,oDAAL,WAA2B,UAAU,KAAK;AAEpE,WAAO,YAAY,CAAC,CAAE,MAAM,YAAY;AAAA,EACzC;AACD;AAnFC;AACA;AACA;AAHM;AAqBN,wBAAmB,WAAoB;AACtC,MAAI,CAAC,mBAAK,mBAAkB;AAC3B,UAAM,uBAAuB,mBAAK,UAAS,wBAAwB;AACnE,QAAI,CAAC,sBAAsB;AAC1B,YAAM,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAEA,uBAAK,kBAAmB,IAAI;AAAA,MAC3B,mBAAK,UAAS,WAAW,OAAO,mBAAK,UAAS;AAAA,MAC9C;AAAA,QACC;AAAA,QACA,GAAG,mBAAK,UAAS;AAAA,MAClB;AAAA,IACD;AAAA,EACD;AAEA,SAAO,mBAAK;AACb;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -39,7 +39,7 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
39
39
|
for (const command of transactionData.commands) {
|
|
40
40
|
if (command.$kind === "$Intent" && command.$Intent.name === COIN_WITH_BALANCE) {
|
|
41
41
|
const { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);
|
|
42
|
-
if (type !== "gas") {
|
|
42
|
+
if (type !== "gas" && balance > 0n) {
|
|
43
43
|
coinTypes.add(type);
|
|
44
44
|
}
|
|
45
45
|
totalByType.set(type, (totalByType.get(type) ?? 0n) + balance);
|
|
@@ -77,6 +77,13 @@ async function resolveCoinBalance(transactionData, buildOptions, next) {
|
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
79
79
|
const { type, balance } = transaction.$Intent.data;
|
|
80
|
+
if (balance === 0n) {
|
|
81
|
+
transactionData.replaceCommand(
|
|
82
|
+
index,
|
|
83
|
+
Commands.MoveCall({ target: "0x2::coin::zero", typeArguments: [type] })
|
|
84
|
+
);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
80
87
|
const commands = [];
|
|
81
88
|
if (!mergedCoins.has(type)) {
|
|
82
89
|
const [first, ...rest] = coinsByType.get(type).map(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/transactions/intents/CoinWithBalance.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, parse, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport type { CoinStruct, SuiClient } from '../../client/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { Commands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../json-rpc-resolver.js';\nimport { getClient } from '../json-rpc-resolver.js';\nimport type { Transaction } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\n\nconst 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}) {\n\treturn (tx: Transaction) => {\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\treturn tx.add(\n\t\t\tCommands.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} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n});\n\nasync function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\tfor (const command of transactionData.commands) {\n\t\tif (command.$kind === '$Intent' && command.$Intent.name === COIN_WITH_BALANCE) {\n\t\t\tconst { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t\tif (type !== 'gas') {\n\t\t\t\tcoinTypes.add(type);\n\t\t\t}\n\n\t\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\t\t}\n\t}\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, CoinStruct[]>();\n\tconst client = getClient(buildOptions);\n\tawait Promise.all(\n\t\t[...coinTypes].map(async (coinType) => {\n\t\t\tcoinsByType.set(\n\t\t\t\tcoinType,\n\t\t\t\tawait getCoinsOfType({\n\t\t\t\t\tcoinType,\n\t\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\t\tclient,\n\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\tusedIds,\n\t\t\t\t}),\n\t\t\t);\n\t\t}),\n\t);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\n\tmergedCoins.set('gas', { $kind: 'GasCoin', GasCoin: true });\n\n\tfor (const [index, transaction] of transactionData.commands.entries()) {\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\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\n\t\tconst commands = [];\n\n\t\tif (!mergedCoins.has(type)) {\n\t\t\tconst [first, ...rest] = coinsByType.get(type)!.map((coin) =>\n\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t'object',\n\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\tobjectId: coin.coinObjectId,\n\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t);\n\n\t\t\tif (rest.length > 0) {\n\t\t\t\tcommands.push(Commands.MergeCoins(first, rest));\n\t\t\t}\n\n\t\t\tmergedCoins.set(type, first);\n\t\t}\n\n\t\tcommands.push(\n\t\t\tCommands.SplitCoins(mergedCoins.get(type)!, [\n\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(balance))),\n\t\t\t]),\n\t\t);\n\n\t\ttransactionData.replaceCommand(index, commands);\n\n\t\ttransactionData.mapArguments((arg) => {\n\t\t\tif (arg.$kind === 'Result' && arg.Result === index) {\n\t\t\t\treturn {\n\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn arg;\n\t\t});\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: SuiClient;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<CoinStruct[]> {\n\tlet remainingBalance = balance;\n\tconst coins: CoinStruct[] = [];\n\n\treturn loadMoreCoins();\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<CoinStruct[]> {\n\t\tconst { data, hasNextPage, nextCursor } = await client.getCoins({ owner, coinType, cursor });\n\n\t\tconst sortedCoins = data.sort((a, b) => Number(BigInt(b.balance) - BigInt(a.balance)));\n\n\t\tfor (const coin of sortedCoins) {\n\t\t\tif (usedIds.has(coin.coinObjectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst coinBalance = BigInt(coin.balance);\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= coinBalance;\n\n\t\t\tif (remainingBalance <= 0) {\n\t\t\t\treturn coins;\n\t\t\t}\n\t\t}\n\n\t\tif (hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\tthrow new Error(`Not enough coins of type ${coinType} to satisfy requested balance`);\n\t}\n}\n"],
|
|
5
|
-
"mappings": "AAIA,SAAS,QAAQ,QAAQ,OAAO,cAAc;AAE9C,SAAS,WAAW;AAEpB,SAAS,0BAA0B;AACnC,SAAS,gBAAgB;AAEzB,SAAS,cAAc;AAEvB,SAAS,iBAAiB;AAI1B,MAAM,oBAAoB;AAC1B,MAAM,WAAW,mBAAmB,eAAe;AAE5C,SAAS,gBAAgB;AAAA,EAC/B,OAAO;AAAA,EACP;AAAA,EACA,aAAa;AACd,GAIG;AACF,SAAO,CAAC,OAAoB;AAC3B,OAAG,kBAAkB,mBAAmB,kBAAkB;AAC1D,UAAM,WAAW,SAAS,QAAQ,OAAO,mBAAmB,IAAI;AAEhE,WAAO,GAAG;AAAA,MACT,SAAS,OAAO;AAAA,QACf,MAAM;AAAA,QACN,QAAQ,CAAC;AAAA,QACT,MAAM;AAAA,UACL,MAAM,aAAa,YAAY,aAAa,QAAQ;AAAA,UACpD,SAAS,OAAO,OAAO;AAAA,QACxB;AAAA,MACD,CAAC;AAAA,IACF;AAAA,EACD;AACD;AAEA,MAAM,sBAAsB,OAAO;AAAA,EAClC,MAAM,OAAO;AAAA,EACb,SAAS,OAAO;AACjB,CAAC;AAED,eAAe,mBACd,iBACA,cACA,MACC;AACD,QAAM,YAAY,oBAAI,IAAY;AAClC,QAAM,cAAc,oBAAI,IAAoB;AAE5C,MAAI,CAAC,gBAAgB,QAAQ;AAC5B,UAAM,IAAI,MAAM,+CAA+C;AAAA,EAChE;AAEA,aAAW,WAAW,gBAAgB,UAAU;AAC/C,QAAI,QAAQ,UAAU,aAAa,QAAQ,QAAQ,SAAS,mBAAmB;AAC9E,YAAM,EAAE,MAAM,QAAQ,IAAI,MAAM,qBAAqB,QAAQ,QAAQ,IAAI;AAEzE,UAAI,SAAS,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { InferInput } from 'valibot';\nimport { bigint, object, parse, string } from 'valibot';\n\nimport { bcs } from '../../bcs/index.js';\nimport type { CoinStruct, SuiClient } from '../../client/index.js';\nimport { normalizeStructTag } from '../../utils/sui-types.js';\nimport { Commands } from '../Commands.js';\nimport type { Argument } from '../data/internal.js';\nimport { Inputs } from '../Inputs.js';\nimport type { BuildTransactionOptions } from '../json-rpc-resolver.js';\nimport { getClient } from '../json-rpc-resolver.js';\nimport type { Transaction } from '../Transaction.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\n\nconst 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}) {\n\treturn (tx: Transaction) => {\n\t\ttx.addIntentResolver(COIN_WITH_BALANCE, resolveCoinBalance);\n\t\tconst coinType = type === 'gas' ? type : normalizeStructTag(type);\n\n\t\treturn tx.add(\n\t\t\tCommands.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} satisfies InferInput<typeof CoinWithBalanceData>,\n\t\t\t}),\n\t\t);\n\t};\n}\n\nconst CoinWithBalanceData = object({\n\ttype: string(),\n\tbalance: bigint(),\n});\n\nasync function resolveCoinBalance(\n\ttransactionData: TransactionDataBuilder,\n\tbuildOptions: BuildTransactionOptions,\n\tnext: () => Promise<void>,\n) {\n\tconst coinTypes = new Set<string>();\n\tconst totalByType = new Map<string, bigint>();\n\n\tif (!transactionData.sender) {\n\t\tthrow new Error('Sender must be set to resolve CoinWithBalance');\n\t}\n\n\tfor (const command of transactionData.commands) {\n\t\tif (command.$kind === '$Intent' && command.$Intent.name === COIN_WITH_BALANCE) {\n\t\t\tconst { type, balance } = parse(CoinWithBalanceData, command.$Intent.data);\n\n\t\t\tif (type !== 'gas' && balance > 0n) {\n\t\t\t\tcoinTypes.add(type);\n\t\t\t}\n\n\t\t\ttotalByType.set(type, (totalByType.get(type) ?? 0n) + balance);\n\t\t}\n\t}\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, CoinStruct[]>();\n\tconst client = getClient(buildOptions);\n\tawait Promise.all(\n\t\t[...coinTypes].map(async (coinType) => {\n\t\t\tcoinsByType.set(\n\t\t\t\tcoinType,\n\t\t\t\tawait getCoinsOfType({\n\t\t\t\t\tcoinType,\n\t\t\t\t\tbalance: totalByType.get(coinType)!,\n\t\t\t\t\tclient,\n\t\t\t\t\towner: transactionData.sender!,\n\t\t\t\t\tusedIds,\n\t\t\t\t}),\n\t\t\t);\n\t\t}),\n\t);\n\n\tconst mergedCoins = new Map<string, Argument>();\n\n\tmergedCoins.set('gas', { $kind: 'GasCoin', GasCoin: true });\n\n\tfor (const [index, transaction] of transactionData.commands.entries()) {\n\t\tif (transaction.$kind !== '$Intent' || transaction.$Intent.name !== COIN_WITH_BALANCE) {\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\n\t\tif (balance === 0n) {\n\t\t\ttransactionData.replaceCommand(\n\t\t\t\tindex,\n\t\t\t\tCommands.MoveCall({ target: '0x2::coin::zero', typeArguments: [type] }),\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst commands = [];\n\n\t\tif (!mergedCoins.has(type)) {\n\t\t\tconst [first, ...rest] = coinsByType.get(type)!.map((coin) =>\n\t\t\t\ttransactionData.addInput(\n\t\t\t\t\t'object',\n\t\t\t\t\tInputs.ObjectRef({\n\t\t\t\t\t\tobjectId: coin.coinObjectId,\n\t\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t\t\tversion: coin.version,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t\t);\n\n\t\t\tif (rest.length > 0) {\n\t\t\t\tcommands.push(Commands.MergeCoins(first, rest));\n\t\t\t}\n\n\t\t\tmergedCoins.set(type, first);\n\t\t}\n\n\t\tcommands.push(\n\t\t\tCommands.SplitCoins(mergedCoins.get(type)!, [\n\t\t\t\ttransactionData.addInput('pure', Inputs.Pure(bcs.u64().serialize(balance))),\n\t\t\t]),\n\t\t);\n\n\t\ttransactionData.replaceCommand(index, commands);\n\n\t\ttransactionData.mapArguments((arg) => {\n\t\t\tif (arg.$kind === 'Result' && arg.Result === index) {\n\t\t\t\treturn {\n\t\t\t\t\t$kind: 'NestedResult',\n\t\t\t\t\tNestedResult: [index + commands.length - 1, 0],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\treturn arg;\n\t\t});\n\t}\n\n\treturn next();\n}\n\nasync function getCoinsOfType({\n\tcoinType,\n\tbalance,\n\tclient,\n\towner,\n\tusedIds,\n}: {\n\tcoinType: string;\n\tbalance: bigint;\n\tclient: SuiClient;\n\towner: string;\n\tusedIds: Set<string>;\n}): Promise<CoinStruct[]> {\n\tlet remainingBalance = balance;\n\tconst coins: CoinStruct[] = [];\n\n\treturn loadMoreCoins();\n\n\tasync function loadMoreCoins(cursor: string | null = null): Promise<CoinStruct[]> {\n\t\tconst { data, hasNextPage, nextCursor } = await client.getCoins({ owner, coinType, cursor });\n\n\t\tconst sortedCoins = data.sort((a, b) => Number(BigInt(b.balance) - BigInt(a.balance)));\n\n\t\tfor (const coin of sortedCoins) {\n\t\t\tif (usedIds.has(coin.coinObjectId)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst coinBalance = BigInt(coin.balance);\n\n\t\t\tcoins.push(coin);\n\t\t\tremainingBalance -= coinBalance;\n\n\t\t\tif (remainingBalance <= 0) {\n\t\t\t\treturn coins;\n\t\t\t}\n\t\t}\n\n\t\tif (hasNextPage) {\n\t\t\treturn loadMoreCoins(nextCursor);\n\t\t}\n\n\t\tthrow new Error(`Not enough coins of type ${coinType} to satisfy requested balance`);\n\t}\n}\n"],
|
|
5
|
+
"mappings": "AAIA,SAAS,QAAQ,QAAQ,OAAO,cAAc;AAE9C,SAAS,WAAW;AAEpB,SAAS,0BAA0B;AACnC,SAAS,gBAAgB;AAEzB,SAAS,cAAc;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AM,IAAI,MAAM,4BAA4B,QAAQ,+BAA+B;AAAA,EACpF;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const PACKAGE_VERSION = "1.14.
|
|
2
|
-
export declare const TARGETED_RPC_VERSION = "1.
|
|
1
|
+
export declare const PACKAGE_VERSION = "1.14.2";
|
|
2
|
+
export declare const TARGETED_RPC_VERSION = "1.38.0";
|
package/dist/esm/version.js
CHANGED
package/dist/esm/version.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/version.ts"],
|
|
4
|
-
"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 = '1.14.
|
|
4
|
+
"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 = '1.14.2';\nexport const TARGETED_RPC_VERSION = '1.38.0';\n"],
|
|
5
5
|
"mappings": "AAKO,MAAM,kBAAkB;AACxB,MAAM,uBAAuB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|