@mysten/zksend 0.3.0 → 0.4.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 +16 -0
- package/dist/cjs/channel/events.d.ts +27 -27
- package/dist/cjs/channel/index.d.ts +1 -1
- package/dist/cjs/index.d.ts +4 -2
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/links/builder.d.ts +13 -2
- package/dist/cjs/links/builder.js +130 -42
- package/dist/cjs/links/builder.js.map +2 -2
- package/dist/cjs/links/claim.d.ts +13 -47
- package/dist/cjs/links/claim.js +176 -150
- package/dist/cjs/links/claim.js.map +3 -3
- package/dist/cjs/links/list-created-links.d.ts +22 -0
- package/dist/cjs/links/list-created-links.js +145 -0
- package/dist/cjs/links/list-created-links.js.map +7 -0
- package/dist/cjs/links/utils.d.ts +32 -15
- package/dist/cjs/links/utils.js +86 -100
- package/dist/cjs/links/utils.js.map +3 -3
- package/dist/cjs/links/zk-bag.d.ts +1 -0
- package/dist/cjs/links/zk-bag.js +6 -0
- package/dist/cjs/links/zk-bag.js.map +2 -2
- package/dist/esm/channel/events.d.ts +27 -27
- package/dist/esm/channel/index.d.ts +1 -1
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +7 -2
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/links/builder.d.ts +13 -2
- package/dist/esm/links/builder.js +131 -43
- package/dist/esm/links/builder.js.map +2 -2
- package/dist/esm/links/claim.d.ts +13 -47
- package/dist/esm/links/claim.js +174 -148
- package/dist/esm/links/claim.js.map +2 -2
- package/dist/esm/links/list-created-links.d.ts +22 -0
- package/dist/esm/links/list-created-links.js +125 -0
- package/dist/esm/links/list-created-links.js.map +7 -0
- package/dist/esm/links/utils.d.ts +32 -15
- package/dist/esm/links/utils.js +87 -101
- package/dist/esm/links/utils.js.map +3 -3
- package/dist/esm/links/zk-bag.d.ts +1 -0
- package/dist/esm/links/zk-bag.js +6 -0
- package/dist/esm/links/zk-bag.js.map +2 -2
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -20,20 +20,20 @@ var __privateMethod = (obj, member, method) => {
|
|
|
20
20
|
__accessCheck(obj, member, "access private method");
|
|
21
21
|
return method;
|
|
22
22
|
};
|
|
23
|
-
var _host, _path,
|
|
23
|
+
var _host, _path, _client, _redirect, _coinsByType, _contract, _objectsToTransfer, objectsToTransfer_fn, _createSendTransactionWithoutContract, createSendTransactionWithoutContract_fn, _estimateClaimGasFee, estimateClaimGasFee_fn, _getCoinsByType, getCoinsByType_fn;
|
|
24
24
|
import { getFullnodeUrl, SuiClient } from "@mysten/sui.js/client";
|
|
25
25
|
import { decodeSuiPrivateKey } from "@mysten/sui.js/cryptography";
|
|
26
26
|
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
|
|
27
27
|
import { TransactionBlock } from "@mysten/sui.js/transactions";
|
|
28
28
|
import { normalizeStructTag, normalizeSuiAddress, SUI_TYPE_ARG, toB64 } from "@mysten/sui.js/utils";
|
|
29
|
-
import { ZkBag } from "./zk-bag.js";
|
|
29
|
+
import { MAINNET_CONTRACT_IDS, ZkBag } from "./zk-bag.js";
|
|
30
30
|
const DEFAULT_ZK_SEND_LINK_OPTIONS = {
|
|
31
31
|
host: "https://zksend.com",
|
|
32
32
|
path: "/claim",
|
|
33
33
|
network: "mainnet"
|
|
34
34
|
};
|
|
35
35
|
const SUI_COIN_TYPE = normalizeStructTag(SUI_TYPE_ARG);
|
|
36
|
-
class
|
|
36
|
+
const _ZkSendLinkBuilder = class {
|
|
37
37
|
constructor({
|
|
38
38
|
host = DEFAULT_ZK_SEND_LINK_OPTIONS.host,
|
|
39
39
|
path = DEFAULT_ZK_SEND_LINK_OPTIONS.path,
|
|
@@ -42,28 +42,26 @@ class ZkSendLinkBuilder {
|
|
|
42
42
|
client = new SuiClient({ url: getFullnodeUrl(network) }),
|
|
43
43
|
sender,
|
|
44
44
|
redirect,
|
|
45
|
-
contract
|
|
45
|
+
contract = network === "mainnet" ? MAINNET_CONTRACT_IDS : void 0
|
|
46
46
|
}) {
|
|
47
47
|
__privateAdd(this, _objectsToTransfer);
|
|
48
48
|
__privateAdd(this, _createSendTransactionWithoutContract);
|
|
49
49
|
__privateAdd(this, _estimateClaimGasFee);
|
|
50
50
|
__privateAdd(this, _getCoinsByType);
|
|
51
|
+
this.objectIds = /* @__PURE__ */ new Set();
|
|
52
|
+
this.balances = /* @__PURE__ */ new Map();
|
|
51
53
|
__privateAdd(this, _host, void 0);
|
|
52
54
|
__privateAdd(this, _path, void 0);
|
|
53
|
-
__privateAdd(this, _keypair, void 0);
|
|
54
55
|
__privateAdd(this, _client, void 0);
|
|
55
56
|
__privateAdd(this, _redirect, void 0);
|
|
56
|
-
__privateAdd(this, _objects, /* @__PURE__ */ new Set());
|
|
57
|
-
__privateAdd(this, _balances, /* @__PURE__ */ new Map());
|
|
58
|
-
__privateAdd(this, _sender, void 0);
|
|
59
57
|
__privateAdd(this, _coinsByType, /* @__PURE__ */ new Map());
|
|
60
58
|
__privateAdd(this, _contract, void 0);
|
|
61
59
|
__privateSet(this, _host, host);
|
|
62
60
|
__privateSet(this, _path, path);
|
|
63
61
|
__privateSet(this, _redirect, redirect);
|
|
64
|
-
|
|
62
|
+
this.keypair = keypair;
|
|
65
63
|
__privateSet(this, _client, client);
|
|
66
|
-
|
|
64
|
+
this.sender = normalizeSuiAddress(sender);
|
|
67
65
|
if (contract) {
|
|
68
66
|
__privateSet(this, _contract, new ZkBag(contract.packageId, contract));
|
|
69
67
|
}
|
|
@@ -73,16 +71,16 @@ class ZkSendLinkBuilder {
|
|
|
73
71
|
}
|
|
74
72
|
addClaimableBalance(coinType, amount) {
|
|
75
73
|
const normalizedType = normalizeStructTag(coinType);
|
|
76
|
-
|
|
74
|
+
this.balances.set(normalizedType, (this.balances.get(normalizedType) ?? 0n) + amount);
|
|
77
75
|
}
|
|
78
76
|
addClaimableObject(id) {
|
|
79
|
-
|
|
77
|
+
this.objectIds.add(id);
|
|
80
78
|
}
|
|
81
79
|
getLink() {
|
|
82
80
|
const link = new URL(__privateGet(this, _host));
|
|
83
81
|
link.pathname = __privateGet(this, _path);
|
|
84
82
|
link.hash = `${__privateGet(this, _contract) ? "$" : ""}${toB64(
|
|
85
|
-
decodeSuiPrivateKey(
|
|
83
|
+
decodeSuiPrivateKey(this.keypair.getSecretKey()).secretKey
|
|
86
84
|
)}`;
|
|
87
85
|
if (__privateGet(this, _redirect)) {
|
|
88
86
|
link.searchParams.set("redirect_url", __privateGet(this, _redirect).url);
|
|
@@ -107,39 +105,129 @@ class ZkSendLinkBuilder {
|
|
|
107
105
|
return result;
|
|
108
106
|
}
|
|
109
107
|
async createSendTransaction({
|
|
110
|
-
transactionBlock
|
|
108
|
+
transactionBlock = new TransactionBlock(),
|
|
111
109
|
calculateGas
|
|
112
110
|
} = {}) {
|
|
113
111
|
if (!__privateGet(this, _contract)) {
|
|
114
|
-
return __privateMethod(this, _createSendTransactionWithoutContract, createSendTransactionWithoutContract_fn).call(this, { transactionBlock
|
|
112
|
+
return __privateMethod(this, _createSendTransactionWithoutContract, createSendTransactionWithoutContract_fn).call(this, { transactionBlock, calculateGas });
|
|
115
113
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
114
|
+
transactionBlock.setSenderIfNotSet(this.sender);
|
|
115
|
+
return _ZkSendLinkBuilder.createLinks({
|
|
116
|
+
transactionBlock,
|
|
117
|
+
client: __privateGet(this, _client),
|
|
118
|
+
contract: __privateGet(this, _contract).ids,
|
|
119
|
+
links: [this]
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
static async createLinks({
|
|
123
|
+
links,
|
|
124
|
+
network = "mainnet",
|
|
125
|
+
client = new SuiClient({ url: getFullnodeUrl(network) }),
|
|
126
|
+
transactionBlock = new TransactionBlock(),
|
|
127
|
+
contract: contractIds = MAINNET_CONTRACT_IDS
|
|
128
|
+
}) {
|
|
129
|
+
const contract = new ZkBag(contractIds.packageId, contractIds);
|
|
130
|
+
const store = transactionBlock.object(contract.ids.bagStoreId);
|
|
131
|
+
const coinsByType = /* @__PURE__ */ new Map();
|
|
132
|
+
const allIds = links.flatMap((link) => [...link.objectIds]);
|
|
133
|
+
await Promise.all(
|
|
134
|
+
[...new Set(links.flatMap((link) => [...link.balances.keys()]))].map(async (coinType) => {
|
|
135
|
+
const coins = await client.getCoins({
|
|
136
|
+
coinType,
|
|
137
|
+
owner: links[0].sender
|
|
138
|
+
});
|
|
139
|
+
coinsByType.set(
|
|
140
|
+
coinType,
|
|
141
|
+
coins.data.filter((coin) => !allIds.includes(coin.coinObjectId))
|
|
142
|
+
);
|
|
143
|
+
})
|
|
144
|
+
);
|
|
145
|
+
const objectRefs = /* @__PURE__ */ new Map();
|
|
146
|
+
const pageSize = 50;
|
|
147
|
+
let offset = 0;
|
|
148
|
+
while (offset < allIds.length) {
|
|
149
|
+
let chunk = allIds.slice(offset, offset + pageSize);
|
|
150
|
+
offset += pageSize;
|
|
151
|
+
const objects = await client.multiGetObjects({
|
|
152
|
+
ids: chunk,
|
|
153
|
+
options: {
|
|
154
|
+
showType: true
|
|
155
|
+
}
|
|
125
156
|
});
|
|
157
|
+
for (const [i, res] of objects.entries()) {
|
|
158
|
+
if (!res.data || res.error) {
|
|
159
|
+
throw new Error(`Failed to load object ${chunk[i]} (${res.error?.code})`);
|
|
160
|
+
}
|
|
161
|
+
objectRefs.set(chunk[i], {
|
|
162
|
+
ref: transactionBlock.objectRef({
|
|
163
|
+
version: res.data.version,
|
|
164
|
+
digest: res.data.digest,
|
|
165
|
+
objectId: res.data.objectId
|
|
166
|
+
}),
|
|
167
|
+
type: res.data.type
|
|
168
|
+
});
|
|
169
|
+
}
|
|
126
170
|
}
|
|
127
|
-
|
|
171
|
+
const mergedCoins = /* @__PURE__ */ new Map([
|
|
172
|
+
[SUI_COIN_TYPE, transactionBlock.gas]
|
|
173
|
+
]);
|
|
174
|
+
for (const [coinType, coins] of coinsByType) {
|
|
175
|
+
if (coinType === SUI_COIN_TYPE) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const [first, ...rest] = coins.map(
|
|
179
|
+
(coin) => transactionBlock.objectRef({
|
|
180
|
+
objectId: coin.coinObjectId,
|
|
181
|
+
version: coin.version,
|
|
182
|
+
digest: coin.digest
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
if (rest.length > 0) {
|
|
186
|
+
transactionBlock.mergeCoins(first, rest);
|
|
187
|
+
}
|
|
188
|
+
mergedCoins.set(coinType, transactionBlock.object(first));
|
|
189
|
+
}
|
|
190
|
+
for (const link of links) {
|
|
191
|
+
const receiver = link.keypair.toSuiAddress();
|
|
192
|
+
contract.new(transactionBlock, { arguments: [store, receiver] });
|
|
193
|
+
link.objectIds.forEach((id) => {
|
|
194
|
+
const object = objectRefs.get(id);
|
|
195
|
+
if (!object) {
|
|
196
|
+
throw new Error(`Object ${id} not found`);
|
|
197
|
+
}
|
|
198
|
+
contract.add(transactionBlock, {
|
|
199
|
+
arguments: [store, receiver, object.ref],
|
|
200
|
+
typeArguments: [object.type]
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
for (const [coinType, merged] of mergedCoins) {
|
|
205
|
+
const linksWithCoin = links.filter((link) => link.balances.has(coinType));
|
|
206
|
+
if (linksWithCoin.length === 0) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
const balances = linksWithCoin.map((link) => link.balances.get(coinType));
|
|
210
|
+
const splits = transactionBlock.splitCoins(merged, balances);
|
|
211
|
+
for (const [i, link] of linksWithCoin.entries()) {
|
|
212
|
+
contract.add(transactionBlock, {
|
|
213
|
+
arguments: [store, link.keypair.toSuiAddress(), splits[i]],
|
|
214
|
+
typeArguments: [`0x2::coin::Coin<${coinType}>`]
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return transactionBlock;
|
|
128
219
|
}
|
|
129
|
-
}
|
|
220
|
+
};
|
|
221
|
+
let ZkSendLinkBuilder = _ZkSendLinkBuilder;
|
|
130
222
|
_host = new WeakMap();
|
|
131
223
|
_path = new WeakMap();
|
|
132
|
-
_keypair = new WeakMap();
|
|
133
224
|
_client = new WeakMap();
|
|
134
225
|
_redirect = new WeakMap();
|
|
135
|
-
_objects = new WeakMap();
|
|
136
|
-
_balances = new WeakMap();
|
|
137
|
-
_sender = new WeakMap();
|
|
138
226
|
_coinsByType = new WeakMap();
|
|
139
227
|
_contract = new WeakMap();
|
|
140
228
|
_objectsToTransfer = new WeakSet();
|
|
141
229
|
objectsToTransfer_fn = async function(txb) {
|
|
142
|
-
const objectIDs = [...
|
|
230
|
+
const objectIDs = [...this.objectIds];
|
|
143
231
|
const refsWithType = (await __privateGet(this, _client).multiGetObjects({
|
|
144
232
|
ids: objectIDs,
|
|
145
233
|
options: {
|
|
@@ -158,8 +246,8 @@ objectsToTransfer_fn = async function(txb) {
|
|
|
158
246
|
type: res.data.type
|
|
159
247
|
};
|
|
160
248
|
});
|
|
161
|
-
txb.setSenderIfNotSet(
|
|
162
|
-
for (const [coinType, amount] of
|
|
249
|
+
txb.setSenderIfNotSet(this.sender);
|
|
250
|
+
for (const [coinType, amount] of this.balances) {
|
|
163
251
|
if (coinType === SUI_COIN_TYPE) {
|
|
164
252
|
const [sui] = txb.splitCoins(txb.gas, [amount]);
|
|
165
253
|
refsWithType.push({
|
|
@@ -187,28 +275,28 @@ createSendTransactionWithoutContract_fn = async function({
|
|
|
187
275
|
} = {}) {
|
|
188
276
|
const gasEstimateFromDryRun = await __privateMethod(this, _estimateClaimGasFee, estimateClaimGasFee_fn).call(this);
|
|
189
277
|
const baseGasAmount = calculateGas ? await calculateGas({
|
|
190
|
-
balances:
|
|
191
|
-
objects: [...
|
|
278
|
+
balances: this.balances,
|
|
279
|
+
objects: [...this.objectIds],
|
|
192
280
|
gasEstimateFromDryRun
|
|
193
281
|
}) : gasEstimateFromDryRun * 2n;
|
|
194
282
|
const gasWithBuffer = baseGasAmount + 1013n;
|
|
195
283
|
const roundedGasAmount = gasWithBuffer - gasWithBuffer % 1000n - 13n;
|
|
196
|
-
const address =
|
|
284
|
+
const address = this.keypair.toSuiAddress();
|
|
197
285
|
const objectsToTransfer = (await __privateMethod(this, _objectsToTransfer, objectsToTransfer_fn).call(this, txb)).map((obj) => obj.ref);
|
|
198
286
|
const [gas] = txb.splitCoins(txb.gas, [roundedGasAmount]);
|
|
199
287
|
objectsToTransfer.push(gas);
|
|
200
|
-
txb.setSenderIfNotSet(
|
|
288
|
+
txb.setSenderIfNotSet(this.sender);
|
|
201
289
|
txb.transferObjects(objectsToTransfer, address);
|
|
202
290
|
return txb;
|
|
203
291
|
};
|
|
204
292
|
_estimateClaimGasFee = new WeakSet();
|
|
205
293
|
estimateClaimGasFee_fn = async function() {
|
|
206
294
|
const txb = new TransactionBlock();
|
|
207
|
-
txb.setSender(
|
|
295
|
+
txb.setSender(this.sender);
|
|
208
296
|
txb.setGasPayment([]);
|
|
209
|
-
txb.transferObjects([txb.gas],
|
|
210
|
-
const idsToTransfer = [...
|
|
211
|
-
for (const [coinType] of
|
|
297
|
+
txb.transferObjects([txb.gas], this.keypair.toSuiAddress());
|
|
298
|
+
const idsToTransfer = [...this.objectIds];
|
|
299
|
+
for (const [coinType] of this.balances) {
|
|
212
300
|
const coins = await __privateMethod(this, _getCoinsByType, getCoinsByType_fn).call(this, coinType);
|
|
213
301
|
if (!coins.length) {
|
|
214
302
|
throw new Error(`Sending account does not contain any coins of type ${coinType}`);
|
|
@@ -218,7 +306,7 @@ estimateClaimGasFee_fn = async function() {
|
|
|
218
306
|
if (idsToTransfer.length > 0) {
|
|
219
307
|
txb.transferObjects(
|
|
220
308
|
idsToTransfer.map((id) => txb.object(id)),
|
|
221
|
-
|
|
309
|
+
this.keypair.toSuiAddress()
|
|
222
310
|
);
|
|
223
311
|
}
|
|
224
312
|
const result = await __privateGet(this, _client).dryRunTransactionBlock({
|
|
@@ -233,7 +321,7 @@ getCoinsByType_fn = async function(coinType) {
|
|
|
233
321
|
}
|
|
234
322
|
const coins = await __privateGet(this, _client).getCoins({
|
|
235
323
|
coinType,
|
|
236
|
-
owner:
|
|
324
|
+
owner: this.sender
|
|
237
325
|
});
|
|
238
326
|
__privateGet(this, _coinsByType).set(coinType, coins.data);
|
|
239
327
|
return coins.data;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/links/builder.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getFullnodeUrl, SuiClient } from '@mysten/sui.js/client';\nimport type { CoinStruct } from '@mysten/sui.js/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui.js/cryptography';\nimport type { Keypair, Signer } from '@mysten/sui.js/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport type {\n\tTransactionObjectArgument,\n\tTransactionObjectInput,\n} from '@mysten/sui.js/transactions';\nimport { TransactionBlock } from '@mysten/sui.js/transactions';\nimport { normalizeStructTag, normalizeSuiAddress, SUI_TYPE_ARG, toB64 } from '@mysten/sui.js/utils';\n\nimport type { ZkBagContractOptions } from './zk-bag.js';\nimport { ZkBag } from './zk-bag.js';\n\ninterface ZkSendLinkRedirect {\n\turl: string;\n\tname?: string;\n}\n\nexport interface ZkSendLinkBuilderOptions {\n\thost?: string;\n\tpath?: string;\n\tkeypair?: Keypair;\n\tnetwork?: 'mainnet' | 'testnet';\n\tclient?: SuiClient;\n\tsender: string;\n\tredirect?: ZkSendLinkRedirect;\n\tcontract?: ZkBagContractOptions;\n}\n\nconst DEFAULT_ZK_SEND_LINK_OPTIONS = {\n\thost: 'https://zksend.com',\n\tpath: '/claim',\n\tnetwork: 'mainnet' as const,\n};\n\nconst SUI_COIN_TYPE = normalizeStructTag(SUI_TYPE_ARG);\n\nexport interface CreateZkSendLinkOptions {\n\ttransactionBlock?: TransactionBlock;\n\tcalculateGas?: (options: {\n\t\tbalances: Map<string, bigint>;\n\t\tobjects: TransactionObjectInput[];\n\t\tgasEstimateFromDryRun: bigint;\n\t}) => Promise<bigint> | bigint;\n}\n\nexport class ZkSendLinkBuilder {\n\t#host: string;\n\t#path: string;\n\t#keypair: Keypair;\n\t#client: SuiClient;\n\t#redirect?: ZkSendLinkRedirect;\n\t#objects = new Set<string>();\n\t#balances = new Map<string, bigint>();\n\t#sender: string;\n\n\t#coinsByType = new Map<string, CoinStruct[]>();\n\t#contract?: ZkBag<ZkBagContractOptions>;\n\n\tconstructor({\n\t\thost = DEFAULT_ZK_SEND_LINK_OPTIONS.host,\n\t\tpath = DEFAULT_ZK_SEND_LINK_OPTIONS.path,\n\t\tkeypair = new Ed25519Keypair(),\n\t\tnetwork = DEFAULT_ZK_SEND_LINK_OPTIONS.network,\n\t\tclient = new SuiClient({ url: getFullnodeUrl(network) }),\n\t\tsender,\n\t\tredirect,\n\t\tcontract,\n\t}: ZkSendLinkBuilderOptions) {\n\t\tthis.#host = host;\n\t\tthis.#path = path;\n\t\tthis.#redirect = redirect;\n\t\tthis.#keypair = keypair;\n\t\tthis.#client = client;\n\t\tthis.#sender = normalizeSuiAddress(sender);\n\n\t\tif (contract) {\n\t\t\tthis.#contract = new ZkBag(contract.packageId, contract);\n\t\t}\n\t}\n\n\taddClaimableMist(amount: bigint) {\n\t\tthis.addClaimableBalance(SUI_COIN_TYPE, amount);\n\t}\n\n\taddClaimableBalance(coinType: string, amount: bigint) {\n\t\tconst normalizedType = normalizeStructTag(coinType);\n\t\tthis.#balances.set(normalizedType, (this.#balances.get(normalizedType) ?? 0n) + amount);\n\t}\n\n\taddClaimableObject(id: string) {\n\t\tthis.#objects.add(id);\n\t}\n\n\tgetLink(): string {\n\t\tconst link = new URL(this.#host);\n\t\tlink.pathname = this.#path;\n\t\tlink.hash = `${this.#contract ? '$' : ''}${toB64(\n\t\t\tdecodeSuiPrivateKey(this.#keypair.getSecretKey()).secretKey,\n\t\t)}`;\n\n\t\tif (this.#redirect) {\n\t\t\tlink.searchParams.set('redirect_url', this.#redirect.url);\n\t\t\tif (this.#redirect.name) {\n\t\t\t\tlink.searchParams.set('name', this.#redirect.name);\n\t\t\t}\n\t\t}\n\n\t\treturn link.toString();\n\t}\n\n\tasync create({\n\t\tsigner,\n\t\t...options\n\t}: CreateZkSendLinkOptions & {\n\t\tsigner: Signer;\n\t\twaitForTransactionBlock?: boolean;\n\t}) {\n\t\tconst txb = await this.createSendTransaction(options);\n\n\t\tconst result = await this.#client.signAndExecuteTransactionBlock({\n\t\t\ttransactionBlock: await txb.build({ client: this.#client }),\n\t\t\tsigner,\n\t\t});\n\n\t\tif (options.waitForTransactionBlock) {\n\t\t\tawait this.#client.waitForTransactionBlock({ digest: result.digest });\n\t\t}\n\n\t\treturn result;\n\t}\n\tasync createSendTransaction({\n\t\ttransactionBlock: txb = new TransactionBlock(),\n\t\tcalculateGas,\n\t}: CreateZkSendLinkOptions = {}) {\n\t\tif (!this.#contract) {\n\t\t\treturn this.#createSendTransactionWithoutContract({ transactionBlock: txb, calculateGas });\n\t\t}\n\t\tconst receiver = txb.pure.address(this.#keypair.toSuiAddress());\n\t\tconst store = txb.object(this.#contract.ids.bagStoreId);\n\n\t\tthis.#contract.new(txb, { arguments: [store, receiver] });\n\t\ttxb.setSenderIfNotSet(this.#sender);\n\n\t\tconst objectsToTransfer = await this.#objectsToTransfer(txb);\n\n\t\tfor (const object of objectsToTransfer) {\n\t\t\tthis.#contract.add(txb, {\n\t\t\t\targuments: [store, receiver, object.ref],\n\t\t\t\ttypeArguments: [object.type],\n\t\t\t});\n\t\t}\n\n\t\treturn txb;\n\t}\n\n\tasync #objectsToTransfer(txb: TransactionBlock) {\n\t\tconst objectIDs = [...this.#objects];\n\t\tconst refsWithType: {\n\t\t\tref: TransactionObjectArgument;\n\t\t\ttype: string;\n\t\t}[] = (\n\t\t\tawait this.#client.multiGetObjects({\n\t\t\t\tids: objectIDs,\n\t\t\t\toptions: {\n\t\t\t\t\tshowType: true,\n\t\t\t\t},\n\t\t\t})\n\t\t).map((res, i) => {\n\t\t\tif (!res.data || res.error) {\n\t\t\t\tthrow new Error(`Failed to load object ${objectIDs[i]} (${res.error?.code})`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tref: txb.objectRef({\n\t\t\t\t\tversion: res.data.version,\n\t\t\t\t\tdigest: res.data.digest,\n\t\t\t\t\tobjectId: res.data.objectId,\n\t\t\t\t}),\n\t\t\t\ttype: res.data.type!,\n\t\t\t};\n\t\t});\n\n\t\ttxb.setSenderIfNotSet(this.#sender);\n\n\t\tfor (const [coinType, amount] of this.#balances) {\n\t\t\tif (coinType === SUI_COIN_TYPE) {\n\t\t\t\tconst [sui] = txb.splitCoins(txb.gas, [amount]);\n\t\t\t\trefsWithType.push({\n\t\t\t\t\tref: sui,\n\t\t\t\t\ttype: `0x2::coin::Coin<${coinType}>`,\n\t\t\t\t} as never);\n\t\t\t} else {\n\t\t\t\tconst coins = (await this.#getCoinsByType(coinType)).map((coin) => coin.coinObjectId);\n\n\t\t\t\tif (coins.length > 1) {\n\t\t\t\t\ttxb.mergeCoins(coins[0], coins.slice(1));\n\t\t\t\t}\n\t\t\t\tconst [split] = txb.splitCoins(coins[0], [amount]);\n\t\t\t\trefsWithType.push({\n\t\t\t\t\tref: split,\n\t\t\t\t\ttype: `0x2::coin:Coin<${coinType}>`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn refsWithType;\n\t}\n\n\tasync #createSendTransactionWithoutContract({\n\t\ttransactionBlock: txb = new TransactionBlock(),\n\t\tcalculateGas,\n\t}: CreateZkSendLinkOptions = {}) {\n\t\tconst gasEstimateFromDryRun = await this.#estimateClaimGasFee();\n\t\tconst baseGasAmount = calculateGas\n\t\t\t? await calculateGas({\n\t\t\t\t\tbalances: this.#balances,\n\t\t\t\t\tobjects: [...this.#objects],\n\t\t\t\t\tgasEstimateFromDryRun,\n\t\t\t })\n\t\t\t: gasEstimateFromDryRun * 2n;\n\n\t\t// Ensure that rounded gas is not less than the calculated gas\n\t\tconst gasWithBuffer = baseGasAmount + 1013n;\n\t\t// Ensure that gas amount ends in 987\n\t\tconst roundedGasAmount = gasWithBuffer - (gasWithBuffer % 1000n) - 13n;\n\n\t\tconst address = this.#keypair.toSuiAddress();\n\t\tconst objectsToTransfer = (await this.#objectsToTransfer(txb)).map((obj) => obj.ref);\n\t\tconst [gas] = txb.splitCoins(txb.gas, [roundedGasAmount]);\n\t\tobjectsToTransfer.push(gas);\n\n\t\ttxb.setSenderIfNotSet(this.#sender);\n\t\ttxb.transferObjects(objectsToTransfer, address);\n\n\t\treturn txb;\n\t}\n\n\tasync #estimateClaimGasFee(): Promise<bigint> {\n\t\tconst txb = new TransactionBlock();\n\t\ttxb.setSender(this.#sender);\n\t\ttxb.setGasPayment([]);\n\t\ttxb.transferObjects([txb.gas], this.#keypair.toSuiAddress());\n\n\t\tconst idsToTransfer = [...this.#objects];\n\n\t\tfor (const [coinType] of this.#balances) {\n\t\t\tconst coins = await this.#getCoinsByType(coinType);\n\n\t\t\tif (!coins.length) {\n\t\t\t\tthrow new Error(`Sending account does not contain any coins of type ${coinType}`);\n\t\t\t}\n\n\t\t\tidsToTransfer.push(coins[0].coinObjectId);\n\t\t}\n\n\t\tif (idsToTransfer.length > 0) {\n\t\t\ttxb.transferObjects(\n\t\t\t\tidsToTransfer.map((id) => txb.object(id)),\n\t\t\t\tthis.#keypair.toSuiAddress(),\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.#client.dryRunTransactionBlock({\n\t\t\ttransactionBlock: await txb.build({ client: this.#client }),\n\t\t});\n\n\t\treturn (\n\t\t\tBigInt(result.effects.gasUsed.computationCost) +\n\t\t\tBigInt(result.effects.gasUsed.storageCost) -\n\t\t\tBigInt(result.effects.gasUsed.storageRebate)\n\t\t);\n\t}\n\n\tasync #getCoinsByType(coinType: string) {\n\t\tif (this.#coinsByType.has(coinType)) {\n\t\t\treturn this.#coinsByType.get(coinType)!;\n\t\t}\n\n\t\tconst coins = await this.#client.getCoins({\n\t\t\tcoinType,\n\t\t\towner: this.#sender,\n\t\t});\n\n\t\tthis.#coinsByType.set(coinType, coins.data);\n\n\t\treturn coins.data;\n\t}\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAGA,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,2BAA2B;AAEpC,SAAS,sBAAsB;AAK/B,SAAS,wBAAwB;AACjC,SAAS,oBAAoB,qBAAqB,cAAc,aAAa;AAG7E,SAAS,aAAa;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getFullnodeUrl, SuiClient } from '@mysten/sui.js/client';\nimport type { CoinStruct } from '@mysten/sui.js/client';\nimport { decodeSuiPrivateKey } from '@mysten/sui.js/cryptography';\nimport type { Keypair, Signer } from '@mysten/sui.js/cryptography';\nimport { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';\nimport type {\n\tTransactionObjectArgument,\n\tTransactionObjectInput,\n} from '@mysten/sui.js/transactions';\nimport { TransactionBlock } from '@mysten/sui.js/transactions';\nimport { normalizeStructTag, normalizeSuiAddress, SUI_TYPE_ARG, toB64 } from '@mysten/sui.js/utils';\n\nimport type { ZkBagContractOptions } from './zk-bag.js';\nimport { MAINNET_CONTRACT_IDS, ZkBag } from './zk-bag.js';\n\ninterface ZkSendLinkRedirect {\n\turl: string;\n\tname?: string;\n}\n\nexport interface ZkSendLinkBuilderOptions {\n\thost?: string;\n\tpath?: string;\n\tkeypair?: Keypair;\n\tnetwork?: 'mainnet' | 'testnet';\n\tclient?: SuiClient;\n\tsender: string;\n\tredirect?: ZkSendLinkRedirect;\n\tcontract?: ZkBagContractOptions | null;\n}\n\nconst DEFAULT_ZK_SEND_LINK_OPTIONS = {\n\thost: 'https://zksend.com',\n\tpath: '/claim',\n\tnetwork: 'mainnet' as const,\n};\n\nconst SUI_COIN_TYPE = normalizeStructTag(SUI_TYPE_ARG);\n\nexport interface CreateZkSendLinkOptions {\n\ttransactionBlock?: TransactionBlock;\n\tcalculateGas?: (options: {\n\t\tbalances: Map<string, bigint>;\n\t\tobjects: TransactionObjectInput[];\n\t\tgasEstimateFromDryRun: bigint;\n\t}) => Promise<bigint> | bigint;\n}\n\nexport class ZkSendLinkBuilder {\n\tobjectIds = new Set<string>();\n\tbalances = new Map<string, bigint>();\n\tsender: string;\n\t#host: string;\n\t#path: string;\n\tkeypair: Keypair;\n\t#client: SuiClient;\n\t#redirect?: ZkSendLinkRedirect;\n\t#coinsByType = new Map<string, CoinStruct[]>();\n\t#contract?: ZkBag<ZkBagContractOptions>;\n\n\tconstructor({\n\t\thost = DEFAULT_ZK_SEND_LINK_OPTIONS.host,\n\t\tpath = DEFAULT_ZK_SEND_LINK_OPTIONS.path,\n\t\tkeypair = new Ed25519Keypair(),\n\t\tnetwork = DEFAULT_ZK_SEND_LINK_OPTIONS.network,\n\t\tclient = new SuiClient({ url: getFullnodeUrl(network) }),\n\t\tsender,\n\t\tredirect,\n\t\tcontract = network === 'mainnet' ? MAINNET_CONTRACT_IDS : undefined,\n\t}: ZkSendLinkBuilderOptions) {\n\t\tthis.#host = host;\n\t\tthis.#path = path;\n\t\tthis.#redirect = redirect;\n\t\tthis.keypair = keypair;\n\t\tthis.#client = client;\n\t\tthis.sender = normalizeSuiAddress(sender);\n\n\t\tif (contract) {\n\t\t\tthis.#contract = new ZkBag(contract.packageId, contract);\n\t\t}\n\t}\n\n\taddClaimableMist(amount: bigint) {\n\t\tthis.addClaimableBalance(SUI_COIN_TYPE, amount);\n\t}\n\n\taddClaimableBalance(coinType: string, amount: bigint) {\n\t\tconst normalizedType = normalizeStructTag(coinType);\n\t\tthis.balances.set(normalizedType, (this.balances.get(normalizedType) ?? 0n) + amount);\n\t}\n\n\taddClaimableObject(id: string) {\n\t\tthis.objectIds.add(id);\n\t}\n\n\tgetLink(): string {\n\t\tconst link = new URL(this.#host);\n\t\tlink.pathname = this.#path;\n\t\tlink.hash = `${this.#contract ? '$' : ''}${toB64(\n\t\t\tdecodeSuiPrivateKey(this.keypair.getSecretKey()).secretKey,\n\t\t)}`;\n\n\t\tif (this.#redirect) {\n\t\t\tlink.searchParams.set('redirect_url', this.#redirect.url);\n\t\t\tif (this.#redirect.name) {\n\t\t\t\tlink.searchParams.set('name', this.#redirect.name);\n\t\t\t}\n\t\t}\n\n\t\treturn link.toString();\n\t}\n\n\tasync create({\n\t\tsigner,\n\t\t...options\n\t}: CreateZkSendLinkOptions & {\n\t\tsigner: Signer;\n\t\twaitForTransactionBlock?: boolean;\n\t}) {\n\t\tconst txb = await this.createSendTransaction(options);\n\n\t\tconst result = await this.#client.signAndExecuteTransactionBlock({\n\t\t\ttransactionBlock: await txb.build({ client: this.#client }),\n\t\t\tsigner,\n\t\t});\n\n\t\tif (options.waitForTransactionBlock) {\n\t\t\tawait this.#client.waitForTransactionBlock({ digest: result.digest });\n\t\t}\n\n\t\treturn result;\n\t}\n\tasync createSendTransaction({\n\t\ttransactionBlock = new TransactionBlock(),\n\t\tcalculateGas,\n\t}: CreateZkSendLinkOptions = {}) {\n\t\tif (!this.#contract) {\n\t\t\treturn this.#createSendTransactionWithoutContract({ transactionBlock, calculateGas });\n\t\t}\n\n\t\ttransactionBlock.setSenderIfNotSet(this.sender);\n\n\t\treturn ZkSendLinkBuilder.createLinks({\n\t\t\ttransactionBlock,\n\t\t\tclient: this.#client,\n\t\t\tcontract: this.#contract.ids,\n\t\t\tlinks: [this],\n\t\t});\n\t}\n\n\tasync #objectsToTransfer(txb: TransactionBlock) {\n\t\tconst objectIDs = [...this.objectIds];\n\t\tconst refsWithType: {\n\t\t\tref: TransactionObjectArgument;\n\t\t\ttype: string;\n\t\t}[] = (\n\t\t\tawait this.#client.multiGetObjects({\n\t\t\t\tids: objectIDs,\n\t\t\t\toptions: {\n\t\t\t\t\tshowType: true,\n\t\t\t\t},\n\t\t\t})\n\t\t).map((res, i) => {\n\t\t\tif (!res.data || res.error) {\n\t\t\t\tthrow new Error(`Failed to load object ${objectIDs[i]} (${res.error?.code})`);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tref: txb.objectRef({\n\t\t\t\t\tversion: res.data.version,\n\t\t\t\t\tdigest: res.data.digest,\n\t\t\t\t\tobjectId: res.data.objectId,\n\t\t\t\t}),\n\t\t\t\ttype: res.data.type!,\n\t\t\t};\n\t\t});\n\n\t\ttxb.setSenderIfNotSet(this.sender);\n\n\t\tfor (const [coinType, amount] of this.balances) {\n\t\t\tif (coinType === SUI_COIN_TYPE) {\n\t\t\t\tconst [sui] = txb.splitCoins(txb.gas, [amount]);\n\t\t\t\trefsWithType.push({\n\t\t\t\t\tref: sui,\n\t\t\t\t\ttype: `0x2::coin::Coin<${coinType}>`,\n\t\t\t\t} as never);\n\t\t\t} else {\n\t\t\t\tconst coins = (await this.#getCoinsByType(coinType)).map((coin) => coin.coinObjectId);\n\n\t\t\t\tif (coins.length > 1) {\n\t\t\t\t\ttxb.mergeCoins(coins[0], coins.slice(1));\n\t\t\t\t}\n\t\t\t\tconst [split] = txb.splitCoins(coins[0], [amount]);\n\t\t\t\trefsWithType.push({\n\t\t\t\t\tref: split,\n\t\t\t\t\ttype: `0x2::coin:Coin<${coinType}>`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn refsWithType;\n\t}\n\n\tasync #createSendTransactionWithoutContract({\n\t\ttransactionBlock: txb = new TransactionBlock(),\n\t\tcalculateGas,\n\t}: CreateZkSendLinkOptions = {}) {\n\t\tconst gasEstimateFromDryRun = await this.#estimateClaimGasFee();\n\t\tconst baseGasAmount = calculateGas\n\t\t\t? await calculateGas({\n\t\t\t\t\tbalances: this.balances,\n\t\t\t\t\tobjects: [...this.objectIds],\n\t\t\t\t\tgasEstimateFromDryRun,\n\t\t\t })\n\t\t\t: gasEstimateFromDryRun * 2n;\n\n\t\t// Ensure that rounded gas is not less than the calculated gas\n\t\tconst gasWithBuffer = baseGasAmount + 1013n;\n\t\t// Ensure that gas amount ends in 987\n\t\tconst roundedGasAmount = gasWithBuffer - (gasWithBuffer % 1000n) - 13n;\n\n\t\tconst address = this.keypair.toSuiAddress();\n\t\tconst objectsToTransfer = (await this.#objectsToTransfer(txb)).map((obj) => obj.ref);\n\t\tconst [gas] = txb.splitCoins(txb.gas, [roundedGasAmount]);\n\t\tobjectsToTransfer.push(gas);\n\n\t\ttxb.setSenderIfNotSet(this.sender);\n\t\ttxb.transferObjects(objectsToTransfer, address);\n\n\t\treturn txb;\n\t}\n\n\tasync #estimateClaimGasFee(): Promise<bigint> {\n\t\tconst txb = new TransactionBlock();\n\t\ttxb.setSender(this.sender);\n\t\ttxb.setGasPayment([]);\n\t\ttxb.transferObjects([txb.gas], this.keypair.toSuiAddress());\n\n\t\tconst idsToTransfer = [...this.objectIds];\n\n\t\tfor (const [coinType] of this.balances) {\n\t\t\tconst coins = await this.#getCoinsByType(coinType);\n\n\t\t\tif (!coins.length) {\n\t\t\t\tthrow new Error(`Sending account does not contain any coins of type ${coinType}`);\n\t\t\t}\n\n\t\t\tidsToTransfer.push(coins[0].coinObjectId);\n\t\t}\n\n\t\tif (idsToTransfer.length > 0) {\n\t\t\ttxb.transferObjects(\n\t\t\t\tidsToTransfer.map((id) => txb.object(id)),\n\t\t\t\tthis.keypair.toSuiAddress(),\n\t\t\t);\n\t\t}\n\n\t\tconst result = await this.#client.dryRunTransactionBlock({\n\t\t\ttransactionBlock: await txb.build({ client: this.#client }),\n\t\t});\n\n\t\treturn (\n\t\t\tBigInt(result.effects.gasUsed.computationCost) +\n\t\t\tBigInt(result.effects.gasUsed.storageCost) -\n\t\t\tBigInt(result.effects.gasUsed.storageRebate)\n\t\t);\n\t}\n\n\tasync #getCoinsByType(coinType: string) {\n\t\tif (this.#coinsByType.has(coinType)) {\n\t\t\treturn this.#coinsByType.get(coinType)!;\n\t\t}\n\n\t\tconst coins = await this.#client.getCoins({\n\t\t\tcoinType,\n\t\t\towner: this.sender,\n\t\t});\n\n\t\tthis.#coinsByType.set(coinType, coins.data);\n\n\t\treturn coins.data;\n\t}\n\n\tstatic async createLinks({\n\t\tlinks,\n\t\tnetwork = 'mainnet',\n\t\tclient = new SuiClient({ url: getFullnodeUrl(network) }),\n\t\ttransactionBlock = new TransactionBlock(),\n\t\tcontract: contractIds = MAINNET_CONTRACT_IDS,\n\t}: {\n\t\ttransactionBlock?: TransactionBlock;\n\t\tclient?: SuiClient;\n\t\tnetwork?: 'mainnet' | 'testnet';\n\t\tlinks: ZkSendLinkBuilder[];\n\t\tcontract: ZkBagContractOptions;\n\t}) {\n\t\tconst contract = new ZkBag(contractIds.packageId, contractIds);\n\t\tconst store = transactionBlock.object(contract.ids.bagStoreId);\n\n\t\tconst coinsByType = new Map<string, CoinStruct[]>();\n\t\tconst allIds = links.flatMap((link) => [...link.objectIds]);\n\n\t\tawait Promise.all(\n\t\t\t[...new Set(links.flatMap((link) => [...link.balances.keys()]))].map(async (coinType) => {\n\t\t\t\tconst coins = await client.getCoins({\n\t\t\t\t\tcoinType,\n\t\t\t\t\towner: links[0].sender,\n\t\t\t\t});\n\n\t\t\t\tcoinsByType.set(\n\t\t\t\t\tcoinType,\n\t\t\t\t\tcoins.data.filter((coin) => !allIds.includes(coin.coinObjectId)),\n\t\t\t\t);\n\t\t\t}),\n\t\t);\n\n\t\tconst objectRefs = new Map<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\tref: TransactionObjectArgument;\n\t\t\t\ttype: string;\n\t\t\t}\n\t\t>();\n\n\t\tconst pageSize = 50;\n\t\tlet offset = 0;\n\t\twhile (offset < allIds.length) {\n\t\t\tlet chunk = allIds.slice(offset, offset + pageSize);\n\t\t\toffset += pageSize;\n\n\t\t\tconst objects = await client.multiGetObjects({\n\t\t\t\tids: chunk,\n\t\t\t\toptions: {\n\t\t\t\t\tshowType: true,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tfor (const [i, res] of objects.entries()) {\n\t\t\t\tif (!res.data || res.error) {\n\t\t\t\t\tthrow new Error(`Failed to load object ${chunk[i]} (${res.error?.code})`);\n\t\t\t\t}\n\t\t\t\tobjectRefs.set(chunk[i], {\n\t\t\t\t\tref: transactionBlock.objectRef({\n\t\t\t\t\t\tversion: res.data.version,\n\t\t\t\t\t\tdigest: res.data.digest,\n\t\t\t\t\t\tobjectId: res.data.objectId,\n\t\t\t\t\t}),\n\t\t\t\t\ttype: res.data.type!,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst mergedCoins = new Map<string, TransactionObjectArgument>([\n\t\t\t[SUI_COIN_TYPE, transactionBlock.gas],\n\t\t]);\n\n\t\tfor (const [coinType, coins] of coinsByType) {\n\t\t\tif (coinType === SUI_COIN_TYPE) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst [first, ...rest] = coins.map((coin) =>\n\t\t\t\ttransactionBlock.objectRef({\n\t\t\t\t\tobjectId: coin.coinObjectId,\n\t\t\t\t\tversion: coin.version,\n\t\t\t\t\tdigest: coin.digest,\n\t\t\t\t}),\n\t\t\t);\n\t\t\tif (rest.length > 0) {\n\t\t\t\ttransactionBlock.mergeCoins(first, rest);\n\t\t\t}\n\t\t\tmergedCoins.set(coinType, transactionBlock.object(first));\n\t\t}\n\n\t\tfor (const link of links) {\n\t\t\tconst receiver = link.keypair.toSuiAddress();\n\t\t\tcontract.new(transactionBlock, { arguments: [store, receiver] });\n\n\t\t\tlink.objectIds.forEach((id) => {\n\t\t\t\tconst object = objectRefs.get(id);\n\t\t\t\tif (!object) {\n\t\t\t\t\tthrow new Error(`Object ${id} not found`);\n\t\t\t\t}\n\t\t\t\tcontract.add(transactionBlock, {\n\t\t\t\t\targuments: [store, receiver, object.ref],\n\t\t\t\t\ttypeArguments: [object.type],\n\t\t\t\t});\n\t\t\t});\n\t\t}\n\n\t\tfor (const [coinType, merged] of mergedCoins) {\n\t\t\tconst linksWithCoin = links.filter((link) => link.balances.has(coinType));\n\t\t\tif (linksWithCoin.length === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst balances = linksWithCoin.map((link) => link.balances.get(coinType)!);\n\t\t\tconst splits = transactionBlock.splitCoins(merged, balances);\n\t\t\tfor (const [i, link] of linksWithCoin.entries()) {\n\t\t\t\tcontract.add(transactionBlock, {\n\t\t\t\t\targuments: [store, link.keypair.toSuiAddress(), splits[i]],\n\t\t\t\t\ttypeArguments: [`0x2::coin::Coin<${coinType}>`],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn transactionBlock;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAGA,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,2BAA2B;AAEpC,SAAS,sBAAsB;AAK/B,SAAS,wBAAwB;AACjC,SAAS,oBAAoB,qBAAqB,cAAc,aAAa;AAG7E,SAAS,sBAAsB,aAAa;AAkB5C,MAAM,+BAA+B;AAAA,EACpC,MAAM;AAAA,EACN,MAAM;AAAA,EACN,SAAS;AACV;AAEA,MAAM,gBAAgB,mBAAmB,YAAY;AAW9C,MAAM,qBAAN,MAAwB;AAAA,EAY9B,YAAY;AAAA,IACX,OAAO,6BAA6B;AAAA,IACpC,OAAO,6BAA6B;AAAA,IACpC,UAAU,IAAI,eAAe;AAAA,IAC7B,UAAU,6BAA6B;AAAA,IACvC,SAAS,IAAI,UAAU,EAAE,KAAK,eAAe,OAAO,EAAE,CAAC;AAAA,IACvD;AAAA,IACA;AAAA,IACA,WAAW,YAAY,YAAY,uBAAuB;AAAA,EAC3D,GAA6B;AAiF7B,uBAAM;AAqDN,uBAAM;AA6BN,uBAAM;AAoCN,uBAAM;AA3NN,qBAAY,oBAAI,IAAY;AAC5B,oBAAW,oBAAI,IAAoB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MAAM,mBAAK,SAAQ,uBAAuB;AAAA,IACxD,kBAAkB,MAAM,IAAI,MAAM,EAAE,QAAQ,mBAAK,SAAQ,CAAC;AAAA,EAC3D,CAAC;AAED,SACC,OAAO,OAAO,QAAQ,QAAQ,eAAe,IAC7C,OAAO,OAAO,QAAQ,QAAQ,WAAW,IACzC,OAAO,OAAO,QAAQ,QAAQ,aAAa;AAE7C;AAEM;AAAA,oBAAe,eAAC,UAAkB;AACvC,MAAI,mBAAK,cAAa,IAAI,QAAQ,GAAG;AACpC,WAAO,mBAAK,cAAa,IAAI,QAAQ;AAAA,EACtC;AAEA,QAAM,QAAQ,MAAM,mBAAK,SAAQ,SAAS;AAAA,IACzC;AAAA,IACA,OAAO,KAAK;AAAA,EACb,CAAC;AAED,qBAAK,cAAa,IAAI,UAAU,MAAM,IAAI;AAE1C,SAAO,MAAM;AACd;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -2,6 +2,7 @@ import { SuiClient } from '@mysten/sui.js/client';
|
|
|
2
2
|
import type { Keypair } from '@mysten/sui.js/cryptography';
|
|
3
3
|
import { TransactionBlock } from '@mysten/sui.js/transactions';
|
|
4
4
|
import type { ZkSendLinkBuilderOptions } from './builder.js';
|
|
5
|
+
import type { LinkAssets } from './utils.js';
|
|
5
6
|
import type { ZkBagContractOptions } from './zk-bag.js';
|
|
6
7
|
export type ZkSendLinkOptions = {
|
|
7
8
|
claimApi?: string;
|
|
@@ -11,63 +12,28 @@ export type ZkSendLinkOptions = {
|
|
|
11
12
|
host?: string;
|
|
12
13
|
path?: string;
|
|
13
14
|
address?: string;
|
|
15
|
+
isContractLink: boolean;
|
|
16
|
+
contract?: ZkBagContractOptions | null;
|
|
14
17
|
} & ({
|
|
15
18
|
address: string;
|
|
16
19
|
keypair?: never;
|
|
17
20
|
} | {
|
|
18
21
|
keypair: Keypair;
|
|
19
22
|
address?: never;
|
|
20
|
-
}) & ({
|
|
21
|
-
isContractLink: true;
|
|
22
|
-
contract: ZkBagContractOptions;
|
|
23
|
-
} | {
|
|
24
|
-
isContractLink: false;
|
|
25
|
-
contract?: never;
|
|
26
23
|
});
|
|
27
24
|
export declare class ZkSendLink {
|
|
28
25
|
#private;
|
|
26
|
+
address: string;
|
|
27
|
+
keypair?: Keypair;
|
|
28
|
+
creatorAddress?: string;
|
|
29
|
+
assets?: LinkAssets;
|
|
30
|
+
claimed?: boolean;
|
|
29
31
|
constructor({ network, claimApi, client, keypair, contract, address, host, path, isContractLink, }: ZkSendLinkOptions);
|
|
30
|
-
static fromUrl(url: string,
|
|
31
|
-
static fromAddress(address: string, options: Omit<ZkSendLinkOptions, 'keypair' | 'address' | 'isContractLink'>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
listClaimableAssets(address: string, options?: {
|
|
36
|
-
claimObjectsAddedAfterCreation?: boolean;
|
|
37
|
-
coinTypes?: string[];
|
|
38
|
-
objects?: string[];
|
|
39
|
-
}): Promise<{
|
|
40
|
-
balances: {
|
|
41
|
-
coinType: string;
|
|
42
|
-
amount: bigint;
|
|
43
|
-
}[];
|
|
44
|
-
nfts: {
|
|
45
|
-
objectId: string;
|
|
46
|
-
type: string;
|
|
47
|
-
version: string;
|
|
48
|
-
digest: string;
|
|
49
|
-
}[];
|
|
50
|
-
coins: {
|
|
51
|
-
objectId: string;
|
|
52
|
-
type: string;
|
|
53
|
-
version: string;
|
|
54
|
-
digest: string;
|
|
55
|
-
}[];
|
|
56
|
-
}>;
|
|
57
|
-
claimAssets(address: string,
|
|
58
|
-
/** @deprecated filtering claims is not supported in contract based links */
|
|
59
|
-
options?: {
|
|
60
|
-
claimObjectsAddedAfterCreation?: boolean;
|
|
61
|
-
coinTypes?: string[];
|
|
62
|
-
objects?: string[];
|
|
63
|
-
}): Promise<import("@mysten/sui.js/client").SuiTransactionBlockResponse>;
|
|
64
|
-
createClaimTransaction(address: string, { reclaim, ...options }?: {
|
|
65
|
-
/** @deprecated filtering claims is not supported in contract based links */
|
|
66
|
-
claimObjectsAddedAfterCreation?: boolean;
|
|
67
|
-
/** @deprecated filtering claims is not supported in contract based links */
|
|
68
|
-
coinTypes?: string[];
|
|
69
|
-
/** @deprecated filtering claims is not supported in contract based links */
|
|
70
|
-
objects?: string[];
|
|
32
|
+
static fromUrl(url: string, options?: Omit<ZkSendLinkOptions, 'keypair' | 'address' | 'isContractLink'>): Promise<ZkSendLink>;
|
|
33
|
+
static fromAddress(address: string, options: Omit<ZkSendLinkOptions, 'keypair' | 'address' | 'isContractLink'>): Promise<ZkSendLink>;
|
|
34
|
+
loadAssets(): Promise<void>;
|
|
35
|
+
claimAssets(address: string): Promise<import("@mysten/sui.js/client").SuiTransactionBlockResponse>;
|
|
36
|
+
createClaimTransaction(address: string, { reclaim, }?: {
|
|
71
37
|
reclaim?: boolean;
|
|
72
38
|
}): TransactionBlock;
|
|
73
39
|
createRegenerateTransaction(sender: string, options?: Omit<ZkSendLinkBuilderOptions, 'sender'>): Promise<{
|