zkcloudworker 0.18.29 → 0.20.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/dist/node/index.cjs +160 -1568
- package/dist/node/mina/api/api.js +15 -3
- package/dist/node/mina/api/api.js.map +1 -1
- package/dist/node/mina/token/api.js +1 -4
- package/dist/node/mina/token/api.js.map +1 -1
- package/dist/node/mina/token/index.d.ts +0 -7
- package/dist/node/mina/token/index.js +0 -7
- package/dist/node/mina/token/index.js.map +1 -1
- package/dist/node/mina/transactions/transaction.d.ts +15 -3
- package/dist/node/mina/transactions/transaction.js +49 -6
- package/dist/node/mina/transactions/transaction.js.map +1 -1
- package/dist/tsconfig.web.tsbuildinfo +1 -1
- package/dist/web/mina/api/api.js +15 -3
- package/dist/web/mina/api/api.js.map +1 -1
- package/dist/web/mina/token/api.js +1 -4
- package/dist/web/mina/token/api.js.map +1 -1
- package/dist/web/mina/token/index.d.ts +0 -7
- package/dist/web/mina/token/index.js +0 -7
- package/dist/web/mina/token/index.js.map +1 -1
- package/dist/web/mina/transactions/transaction.d.ts +15 -3
- package/dist/web/mina/transactions/transaction.js +49 -6
- package/dist/web/mina/transactions/transaction.js.map +1 -1
- package/package.json +5 -5
- package/src/mina/api/api.ts +15 -3
- package/src/mina/token/api.ts +2 -4
- package/src/mina/token/index.ts +0 -7
- package/src/mina/transactions/transaction.ts +73 -13
- package/src/mina/token/FungibleTokenAdmin.ts +0 -89
- package/src/mina/token/FungibleTokenContract.ts +0 -327
- package/src/mina/token/FungibleTokenWhitelistedAdmin.ts +0 -112
- package/src/mina/token/bid.ts +0 -173
- package/src/mina/token/build.ts +0 -620
- package/src/mina/token/fee.ts +0 -2
- package/src/mina/token/offer.ts +0 -174
- package/src/mina/token/token.ts +0 -127
- package/src/mina/token/vk.ts +0 -42
- package/src/mina/token/whitelist.ts +0 -204
package/src/mina/token/build.ts
DELETED
@@ -1,620 +0,0 @@
|
|
1
|
-
import { Whitelist, WhitelistedAddressList } from "./whitelist.js";
|
2
|
-
import { FungibleTokenTransactionType } from "./api.js";
|
3
|
-
import { blockchain } from "../../cloud/networks.js";
|
4
|
-
import { fetchMinaAccount } from "../utils/fetch.js";
|
5
|
-
import { accountBalanceMina } from "../utils/mina.js";
|
6
|
-
import {
|
7
|
-
FungibleToken,
|
8
|
-
WhitelistedFungibleToken,
|
9
|
-
FungibleTokenAdmin,
|
10
|
-
FungibleTokenWhitelistedAdmin,
|
11
|
-
FungibleTokenOfferContract,
|
12
|
-
FungibleTokenBidContract,
|
13
|
-
tokenVerificationKeys,
|
14
|
-
} from "./token.js";
|
15
|
-
import {
|
16
|
-
PublicKey,
|
17
|
-
Mina,
|
18
|
-
AccountUpdate,
|
19
|
-
UInt64,
|
20
|
-
UInt8,
|
21
|
-
Bool,
|
22
|
-
Transaction,
|
23
|
-
Struct,
|
24
|
-
VerificationKey,
|
25
|
-
Field,
|
26
|
-
} from "o1js";
|
27
|
-
|
28
|
-
export async function buildTokenDeployTransaction(params: {
|
29
|
-
chain: blockchain;
|
30
|
-
fee: UInt64;
|
31
|
-
sender: PublicKey;
|
32
|
-
nonce: number;
|
33
|
-
tokenAddress: PublicKey;
|
34
|
-
adminContractAddress: PublicKey;
|
35
|
-
adminAddress: PublicKey;
|
36
|
-
uri: string;
|
37
|
-
symbol: string;
|
38
|
-
memo?: string;
|
39
|
-
whitelist?: WhitelistedAddressList | string;
|
40
|
-
developerAddress?: PublicKey;
|
41
|
-
developerFee?: UInt64;
|
42
|
-
provingKey: PublicKey;
|
43
|
-
provingFee: UInt64;
|
44
|
-
decimals: UInt8;
|
45
|
-
}): Promise<{
|
46
|
-
tx: Transaction<false, false>;
|
47
|
-
isWhitelisted: boolean;
|
48
|
-
adminVerificationKey: VerificationKey;
|
49
|
-
tokenVerificationKey: VerificationKey;
|
50
|
-
whitelist: string | undefined;
|
51
|
-
}> {
|
52
|
-
const {
|
53
|
-
fee,
|
54
|
-
sender,
|
55
|
-
nonce,
|
56
|
-
memo,
|
57
|
-
tokenAddress,
|
58
|
-
adminContractAddress,
|
59
|
-
uri,
|
60
|
-
symbol,
|
61
|
-
developerAddress,
|
62
|
-
developerFee,
|
63
|
-
provingKey,
|
64
|
-
provingFee,
|
65
|
-
decimals,
|
66
|
-
chain,
|
67
|
-
} = params;
|
68
|
-
const isWhitelisted = params.whitelist !== undefined;
|
69
|
-
if (memo && typeof memo !== "string")
|
70
|
-
throw new Error("Memo must be a string");
|
71
|
-
if (memo && memo.length > 30)
|
72
|
-
throw new Error("Memo must be less than 30 characters");
|
73
|
-
if (!symbol || typeof symbol !== "string")
|
74
|
-
throw new Error("Symbol must be a string");
|
75
|
-
if (symbol.length >= 7)
|
76
|
-
throw new Error("Symbol must be less than 7 characters");
|
77
|
-
|
78
|
-
const adminContract = isWhitelisted
|
79
|
-
? FungibleTokenWhitelistedAdmin
|
80
|
-
: FungibleTokenAdmin;
|
81
|
-
const tokenContract = isWhitelisted
|
82
|
-
? WhitelistedFungibleToken
|
83
|
-
: FungibleToken;
|
84
|
-
const vk =
|
85
|
-
tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "testnet"].vk;
|
86
|
-
if (
|
87
|
-
!vk ||
|
88
|
-
!vk.FungibleTokenWhitelistedAdmin ||
|
89
|
-
!vk.FungibleTokenWhitelistedAdmin.hash ||
|
90
|
-
!vk.FungibleTokenWhitelistedAdmin.data ||
|
91
|
-
!vk.FungibleTokenAdmin ||
|
92
|
-
!vk.FungibleTokenAdmin.hash ||
|
93
|
-
!vk.FungibleTokenAdmin.data ||
|
94
|
-
!vk.WhitelistedFungibleToken ||
|
95
|
-
!vk.WhitelistedFungibleToken.hash ||
|
96
|
-
!vk.WhitelistedFungibleToken.data ||
|
97
|
-
!vk.FungibleToken ||
|
98
|
-
!vk.FungibleToken.hash ||
|
99
|
-
!vk.FungibleToken.data
|
100
|
-
)
|
101
|
-
throw new Error("Cannot get verification keys");
|
102
|
-
const adminVerificationKey = isWhitelisted
|
103
|
-
? vk.FungibleTokenWhitelistedAdmin
|
104
|
-
: vk.FungibleTokenAdmin;
|
105
|
-
const tokenVerificationKey = isWhitelisted
|
106
|
-
? vk.WhitelistedFungibleToken
|
107
|
-
: vk.FungibleToken;
|
108
|
-
|
109
|
-
if (!adminVerificationKey || !tokenVerificationKey)
|
110
|
-
throw new Error("Cannot get verification keys");
|
111
|
-
await fetchMinaAccount({
|
112
|
-
publicKey: sender,
|
113
|
-
force: true,
|
114
|
-
});
|
115
|
-
|
116
|
-
if (!Mina.hasAccount(sender)) {
|
117
|
-
throw new Error("Sender does not have account");
|
118
|
-
}
|
119
|
-
|
120
|
-
console.log("Sender balance:", await accountBalanceMina(sender));
|
121
|
-
const whitelist = params.whitelist
|
122
|
-
? typeof params.whitelist === "string"
|
123
|
-
? Whitelist.fromString(params.whitelist)
|
124
|
-
: await Whitelist.create({ list: params.whitelist, name: symbol })
|
125
|
-
: undefined;
|
126
|
-
|
127
|
-
const zkToken = new tokenContract(tokenAddress);
|
128
|
-
const zkAdmin = new adminContract(adminContractAddress);
|
129
|
-
|
130
|
-
const tx = await Mina.transaction(
|
131
|
-
{ sender, fee, memo: memo ?? `deploy ${symbol}`, nonce },
|
132
|
-
async () => {
|
133
|
-
const feeAccountUpdate = AccountUpdate.createSigned(sender);
|
134
|
-
feeAccountUpdate.balance.subInPlace(3_000_000_000);
|
135
|
-
feeAccountUpdate.send({
|
136
|
-
to: provingKey,
|
137
|
-
amount: provingFee,
|
138
|
-
});
|
139
|
-
if (developerAddress && developerFee) {
|
140
|
-
feeAccountUpdate.send({
|
141
|
-
to: developerAddress,
|
142
|
-
amount: developerFee,
|
143
|
-
});
|
144
|
-
}
|
145
|
-
if (isWhitelisted && !whitelist) {
|
146
|
-
throw new Error("Whitelisted addresses not found");
|
147
|
-
}
|
148
|
-
await zkAdmin.deploy({
|
149
|
-
adminPublicKey: sender,
|
150
|
-
verificationKey: adminVerificationKey,
|
151
|
-
whitelist: whitelist!,
|
152
|
-
});
|
153
|
-
zkAdmin.account.zkappUri.set(uri);
|
154
|
-
await zkToken.deploy({
|
155
|
-
symbol,
|
156
|
-
src: uri,
|
157
|
-
verificationKey: tokenVerificationKey,
|
158
|
-
});
|
159
|
-
await zkToken.initialize(
|
160
|
-
adminContractAddress,
|
161
|
-
decimals,
|
162
|
-
// We can set `startPaused` to `Bool(false)` here, because we are doing an atomic deployment
|
163
|
-
// If you are not deploying the admin and token contracts in the same transaction,
|
164
|
-
// it is safer to start the tokens paused, and resume them only after verifying that
|
165
|
-
// the admin contract has been deployed
|
166
|
-
Bool(false)
|
167
|
-
);
|
168
|
-
}
|
169
|
-
);
|
170
|
-
return {
|
171
|
-
tx,
|
172
|
-
isWhitelisted,
|
173
|
-
adminVerificationKey: {
|
174
|
-
hash: Field(adminVerificationKey.hash),
|
175
|
-
data: adminVerificationKey.data,
|
176
|
-
},
|
177
|
-
tokenVerificationKey: {
|
178
|
-
hash: Field(tokenVerificationKey.hash),
|
179
|
-
data: tokenVerificationKey.data,
|
180
|
-
},
|
181
|
-
whitelist: whitelist?.toString(),
|
182
|
-
};
|
183
|
-
}
|
184
|
-
|
185
|
-
export function getTokenTransactionSender(params: {
|
186
|
-
txType: FungibleTokenTransactionType;
|
187
|
-
from: PublicKey;
|
188
|
-
to: PublicKey;
|
189
|
-
}) {
|
190
|
-
const { txType, from, to } = params;
|
191
|
-
if (
|
192
|
-
txType === "buy" ||
|
193
|
-
txType === "withdrawOffer" ||
|
194
|
-
txType === "withdrawBid"
|
195
|
-
) {
|
196
|
-
return to;
|
197
|
-
}
|
198
|
-
return from;
|
199
|
-
}
|
200
|
-
|
201
|
-
export async function buildTokenTransaction(params: {
|
202
|
-
txType: FungibleTokenTransactionType;
|
203
|
-
chain: blockchain;
|
204
|
-
fee: UInt64;
|
205
|
-
nonce: number;
|
206
|
-
memo?: string;
|
207
|
-
tokenAddress: PublicKey;
|
208
|
-
from: PublicKey;
|
209
|
-
to: PublicKey;
|
210
|
-
amount?: UInt64;
|
211
|
-
price?: UInt64;
|
212
|
-
whitelist?: WhitelistedAddressList | string;
|
213
|
-
developerAddress?: PublicKey;
|
214
|
-
developerFee?: UInt64;
|
215
|
-
provingKey: PublicKey;
|
216
|
-
provingFee: UInt64;
|
217
|
-
}): Promise<{
|
218
|
-
tx: Transaction<false, false>;
|
219
|
-
isWhitelisted: boolean;
|
220
|
-
adminContractAddress: PublicKey;
|
221
|
-
adminAddress: PublicKey;
|
222
|
-
symbol: string;
|
223
|
-
adminVerificationKey: VerificationKey;
|
224
|
-
tokenVerificationKey: VerificationKey;
|
225
|
-
offerVerificationKey: VerificationKey;
|
226
|
-
bidVerificationKey: VerificationKey;
|
227
|
-
whitelist: string | undefined;
|
228
|
-
}> {
|
229
|
-
const {
|
230
|
-
txType,
|
231
|
-
chain,
|
232
|
-
fee,
|
233
|
-
nonce,
|
234
|
-
tokenAddress,
|
235
|
-
from,
|
236
|
-
to,
|
237
|
-
amount,
|
238
|
-
price,
|
239
|
-
developerAddress,
|
240
|
-
developerFee,
|
241
|
-
provingKey,
|
242
|
-
provingFee,
|
243
|
-
} = params;
|
244
|
-
console.log(txType, "tx for", tokenAddress.toBase58());
|
245
|
-
|
246
|
-
const sender = getTokenTransactionSender({ txType, from, to });
|
247
|
-
console.log("Sender:", sender.toBase58());
|
248
|
-
|
249
|
-
await fetchMinaAccount({
|
250
|
-
publicKey: sender,
|
251
|
-
force: true,
|
252
|
-
});
|
253
|
-
|
254
|
-
if (!Mina.hasAccount(sender)) {
|
255
|
-
console.error("Sender does not have account");
|
256
|
-
throw new Error("Sender does not have account");
|
257
|
-
}
|
258
|
-
|
259
|
-
const { symbol, adminContractAddress, adminAddress, isWhitelisted } =
|
260
|
-
await getTokenSymbolAndAdmin({
|
261
|
-
tokenAddress,
|
262
|
-
chain,
|
263
|
-
});
|
264
|
-
const memo = params.memo ?? `${txType} ${symbol}`;
|
265
|
-
|
266
|
-
const whitelistedAdminContract = new FungibleTokenWhitelistedAdmin(
|
267
|
-
adminContractAddress
|
268
|
-
);
|
269
|
-
const tokenContract =
|
270
|
-
isWhitelisted && txType === "mint"
|
271
|
-
? WhitelistedFungibleToken
|
272
|
-
: FungibleToken;
|
273
|
-
|
274
|
-
if (
|
275
|
-
(txType === "whitelistAdmin" ||
|
276
|
-
txType === "whitelistBid" ||
|
277
|
-
txType === "whitelistOffer") &&
|
278
|
-
!params.whitelist
|
279
|
-
) {
|
280
|
-
throw new Error("Whitelist is required");
|
281
|
-
}
|
282
|
-
|
283
|
-
const whitelist = params.whitelist
|
284
|
-
? typeof params.whitelist === "string"
|
285
|
-
? Whitelist.fromString(params.whitelist)
|
286
|
-
: await Whitelist.create({ list: params.whitelist, name: symbol })
|
287
|
-
: undefined;
|
288
|
-
|
289
|
-
const zkToken = new tokenContract(tokenAddress);
|
290
|
-
const tokenId = zkToken.deriveTokenId();
|
291
|
-
|
292
|
-
if (txType === "mint" && adminAddress.toBase58() !== sender.toBase58())
|
293
|
-
throw new Error("Invalid sender for mint");
|
294
|
-
|
295
|
-
await fetchMinaAccount({
|
296
|
-
publicKey: tokenAddress,
|
297
|
-
tokenId,
|
298
|
-
force: true,
|
299
|
-
});
|
300
|
-
await fetchMinaAccount({
|
301
|
-
publicKey: from,
|
302
|
-
tokenId,
|
303
|
-
force: (
|
304
|
-
[
|
305
|
-
"offer",
|
306
|
-
"whitelistOffer",
|
307
|
-
"bid",
|
308
|
-
"whitelistBid",
|
309
|
-
"sell",
|
310
|
-
"transfer",
|
311
|
-
"withdrawOffer",
|
312
|
-
] satisfies FungibleTokenTransactionType[] as FungibleTokenTransactionType[]
|
313
|
-
).includes(txType),
|
314
|
-
});
|
315
|
-
|
316
|
-
await fetchMinaAccount({
|
317
|
-
publicKey: to,
|
318
|
-
tokenId,
|
319
|
-
force: (
|
320
|
-
[
|
321
|
-
"sell",
|
322
|
-
"whitelistAdmin",
|
323
|
-
"withdrawBid",
|
324
|
-
"withdrawOffer",
|
325
|
-
] satisfies FungibleTokenTransactionType[] as FungibleTokenTransactionType[]
|
326
|
-
).includes(txType),
|
327
|
-
});
|
328
|
-
|
329
|
-
const isNewAccount = Mina.hasAccount(to, tokenId) === false;
|
330
|
-
const offerContract = new FungibleTokenOfferContract(
|
331
|
-
(
|
332
|
-
[
|
333
|
-
"offer",
|
334
|
-
"whitelistOffer",
|
335
|
-
] satisfies FungibleTokenTransactionType[] as FungibleTokenTransactionType[]
|
336
|
-
).includes(txType)
|
337
|
-
? to
|
338
|
-
: from,
|
339
|
-
tokenId
|
340
|
-
);
|
341
|
-
const bidContract = new FungibleTokenBidContract(
|
342
|
-
(
|
343
|
-
[
|
344
|
-
"bid",
|
345
|
-
"whitelistBid",
|
346
|
-
] satisfies FungibleTokenTransactionType[] as FungibleTokenTransactionType[]
|
347
|
-
).includes(txType)
|
348
|
-
? from
|
349
|
-
: to,
|
350
|
-
tokenId
|
351
|
-
);
|
352
|
-
const offerContractDeployment = new FungibleTokenOfferContract(to, tokenId);
|
353
|
-
const bidContractDeployment = new FungibleTokenBidContract(from, tokenId);
|
354
|
-
const vk =
|
355
|
-
tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "testnet"].vk;
|
356
|
-
if (
|
357
|
-
!vk ||
|
358
|
-
!vk.FungibleTokenOfferContract ||
|
359
|
-
!vk.FungibleTokenOfferContract.hash ||
|
360
|
-
!vk.FungibleTokenOfferContract.data ||
|
361
|
-
!vk.FungibleTokenBidContract ||
|
362
|
-
!vk.FungibleTokenBidContract.hash ||
|
363
|
-
!vk.FungibleTokenBidContract.data ||
|
364
|
-
!vk.FungibleTokenWhitelistedAdmin ||
|
365
|
-
!vk.FungibleTokenWhitelistedAdmin.hash ||
|
366
|
-
!vk.FungibleTokenWhitelistedAdmin.data ||
|
367
|
-
!vk.FungibleTokenAdmin ||
|
368
|
-
!vk.FungibleTokenAdmin.hash ||
|
369
|
-
!vk.FungibleTokenAdmin.data ||
|
370
|
-
!vk.WhitelistedFungibleToken ||
|
371
|
-
!vk.WhitelistedFungibleToken.hash ||
|
372
|
-
!vk.WhitelistedFungibleToken.data ||
|
373
|
-
!vk.FungibleToken ||
|
374
|
-
!vk.FungibleToken.hash ||
|
375
|
-
!vk.FungibleToken.data
|
376
|
-
)
|
377
|
-
throw new Error("Cannot get verification key");
|
378
|
-
|
379
|
-
const adminVerificationKey = isWhitelisted
|
380
|
-
? vk.FungibleTokenWhitelistedAdmin
|
381
|
-
: vk.FungibleTokenAdmin;
|
382
|
-
const tokenVerificationKey = isWhitelisted
|
383
|
-
? vk.WhitelistedFungibleToken
|
384
|
-
: vk.FungibleToken;
|
385
|
-
const offerVerificationKey = FungibleTokenOfferContract._verificationKey ?? {
|
386
|
-
hash: Field(vk.FungibleTokenOfferContract.hash),
|
387
|
-
data: vk.FungibleTokenOfferContract.data,
|
388
|
-
};
|
389
|
-
const bidVerificationKey = FungibleTokenBidContract._verificationKey ?? {
|
390
|
-
hash: Field(vk.FungibleTokenBidContract.hash),
|
391
|
-
data: vk.FungibleTokenBidContract.data,
|
392
|
-
};
|
393
|
-
|
394
|
-
console.log("Sender balance:", await accountBalanceMina(sender));
|
395
|
-
console.log("New account:", isNewAccount);
|
396
|
-
|
397
|
-
const tx = await Mina.transaction({ sender, fee, memo, nonce }, async () => {
|
398
|
-
const feeAccountUpdate = AccountUpdate.createSigned(sender);
|
399
|
-
if (isNewAccount) {
|
400
|
-
feeAccountUpdate.balance.subInPlace(1_000_000_000);
|
401
|
-
}
|
402
|
-
feeAccountUpdate.send({
|
403
|
-
to: provingKey,
|
404
|
-
amount: provingFee,
|
405
|
-
});
|
406
|
-
if (developerAddress && developerFee) {
|
407
|
-
feeAccountUpdate.send({
|
408
|
-
to: developerAddress,
|
409
|
-
amount: developerFee,
|
410
|
-
});
|
411
|
-
}
|
412
|
-
switch (txType) {
|
413
|
-
case "mint":
|
414
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
415
|
-
await zkToken.mint(to, amount);
|
416
|
-
break;
|
417
|
-
|
418
|
-
case "transfer":
|
419
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
420
|
-
await zkToken.transfer(from, to, amount);
|
421
|
-
break;
|
422
|
-
|
423
|
-
case "offer":
|
424
|
-
if (price === undefined) throw new Error("Error: Price is required");
|
425
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
426
|
-
if (isNewAccount) {
|
427
|
-
await offerContractDeployment.deploy({
|
428
|
-
verificationKey: offerVerificationKey,
|
429
|
-
whitelist: whitelist ?? Whitelist.empty(),
|
430
|
-
});
|
431
|
-
offerContract.account.zkappUri.set(`Offer for ${symbol}`);
|
432
|
-
await offerContract.initialize(sender, tokenAddress, amount, price);
|
433
|
-
await zkToken.approveAccountUpdates([
|
434
|
-
offerContractDeployment.self,
|
435
|
-
offerContract.self,
|
436
|
-
]);
|
437
|
-
} else {
|
438
|
-
await offerContract.offer(amount, price);
|
439
|
-
await zkToken.approveAccountUpdate(offerContract.self);
|
440
|
-
}
|
441
|
-
|
442
|
-
break;
|
443
|
-
|
444
|
-
case "buy":
|
445
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
446
|
-
await offerContract.buy(amount);
|
447
|
-
await zkToken.approveAccountUpdate(offerContract.self);
|
448
|
-
break;
|
449
|
-
|
450
|
-
case "withdrawOffer":
|
451
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
452
|
-
await offerContract.withdraw(amount);
|
453
|
-
await zkToken.approveAccountUpdate(offerContract.self);
|
454
|
-
break;
|
455
|
-
|
456
|
-
case "bid":
|
457
|
-
if (price === undefined) throw new Error("Error: Price is required");
|
458
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
459
|
-
if (isNewAccount) {
|
460
|
-
await bidContractDeployment.deploy({
|
461
|
-
verificationKey: bidVerificationKey,
|
462
|
-
whitelist: whitelist ?? Whitelist.empty(),
|
463
|
-
});
|
464
|
-
bidContract.account.zkappUri.set(`Bid for ${symbol}`);
|
465
|
-
await bidContract.initialize(tokenAddress, amount, price);
|
466
|
-
await zkToken.approveAccountUpdates([
|
467
|
-
bidContractDeployment.self,
|
468
|
-
bidContract.self,
|
469
|
-
]);
|
470
|
-
} else {
|
471
|
-
await bidContract.bid(amount, price);
|
472
|
-
await zkToken.approveAccountUpdate(bidContract.self);
|
473
|
-
}
|
474
|
-
break;
|
475
|
-
|
476
|
-
case "sell":
|
477
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
478
|
-
await bidContract.sell(amount);
|
479
|
-
await zkToken.approveAccountUpdate(bidContract.self);
|
480
|
-
break;
|
481
|
-
|
482
|
-
case "withdrawBid":
|
483
|
-
if (amount === undefined) throw new Error("Error: Amount is required");
|
484
|
-
await bidContract.withdraw(amount);
|
485
|
-
await zkToken.approveAccountUpdate(bidContract.self);
|
486
|
-
break;
|
487
|
-
|
488
|
-
case "whitelistAdmin":
|
489
|
-
if (!whitelist) throw new Error("Whitelist is required");
|
490
|
-
await whitelistedAdminContract.updateWhitelist(whitelist);
|
491
|
-
break;
|
492
|
-
|
493
|
-
case "whitelistBid":
|
494
|
-
if (!whitelist) throw new Error("Whitelist is required");
|
495
|
-
await bidContract.updateWhitelist(whitelist);
|
496
|
-
break;
|
497
|
-
|
498
|
-
case "whitelistOffer":
|
499
|
-
if (!whitelist) throw new Error("Whitelist is required");
|
500
|
-
await offerContract.updateWhitelist(whitelist);
|
501
|
-
await zkToken.approveAccountUpdate(offerContract.self);
|
502
|
-
break;
|
503
|
-
|
504
|
-
default:
|
505
|
-
throw new Error(`Unknown transaction type: ${txType}`);
|
506
|
-
}
|
507
|
-
});
|
508
|
-
return {
|
509
|
-
tx,
|
510
|
-
isWhitelisted,
|
511
|
-
adminContractAddress,
|
512
|
-
adminAddress,
|
513
|
-
symbol,
|
514
|
-
adminVerificationKey: {
|
515
|
-
hash: Field(adminVerificationKey.hash),
|
516
|
-
data: adminVerificationKey.data,
|
517
|
-
},
|
518
|
-
tokenVerificationKey: {
|
519
|
-
hash: Field(tokenVerificationKey.hash),
|
520
|
-
data: tokenVerificationKey.data,
|
521
|
-
},
|
522
|
-
offerVerificationKey,
|
523
|
-
bidVerificationKey,
|
524
|
-
whitelist: whitelist?.toString(),
|
525
|
-
};
|
526
|
-
}
|
527
|
-
|
528
|
-
export async function getTokenSymbolAndAdmin(params: {
|
529
|
-
tokenAddress: PublicKey;
|
530
|
-
chain: blockchain;
|
531
|
-
}): Promise<{
|
532
|
-
adminContractAddress: PublicKey;
|
533
|
-
adminAddress: PublicKey;
|
534
|
-
symbol: string;
|
535
|
-
isWhitelisted: boolean;
|
536
|
-
}> {
|
537
|
-
const { tokenAddress, chain } = params;
|
538
|
-
const vk =
|
539
|
-
tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "testnet"].vk;
|
540
|
-
class FungibleTokenState extends Struct({
|
541
|
-
decimals: UInt8,
|
542
|
-
admin: PublicKey,
|
543
|
-
paused: Bool,
|
544
|
-
}) {}
|
545
|
-
const FungibleTokenStateSize = FungibleTokenState.sizeInFields();
|
546
|
-
class FungibleTokenAdminState extends Struct({
|
547
|
-
adminPublicKey: PublicKey,
|
548
|
-
}) {}
|
549
|
-
const FungibleTokenAdminStateSize = FungibleTokenAdminState.sizeInFields();
|
550
|
-
|
551
|
-
await fetchMinaAccount({ publicKey: tokenAddress, force: true });
|
552
|
-
if (!Mina.hasAccount(tokenAddress)) {
|
553
|
-
throw new Error("Token contract account not found");
|
554
|
-
}
|
555
|
-
|
556
|
-
const account = Mina.getAccount(tokenAddress);
|
557
|
-
const verificationKey = account.zkapp?.verificationKey;
|
558
|
-
if (!verificationKey) {
|
559
|
-
throw new Error("Token contract verification key not found");
|
560
|
-
}
|
561
|
-
if (
|
562
|
-
verificationKey.hash.toJSON() !== vk.FungibleToken.hash ||
|
563
|
-
verificationKey.data !== vk.FungibleToken.data ||
|
564
|
-
verificationKey.hash.toJSON() !== vk.WhitelistedFungibleToken.hash ||
|
565
|
-
verificationKey.data !== vk.WhitelistedFungibleToken.data
|
566
|
-
) {
|
567
|
-
throw new Error("Unknown token verification key");
|
568
|
-
}
|
569
|
-
if (account.zkapp?.appState === undefined) {
|
570
|
-
throw new Error("Token contract state not found");
|
571
|
-
}
|
572
|
-
|
573
|
-
const state = FungibleTokenState.fromFields(
|
574
|
-
account.zkapp?.appState.slice(0, FungibleTokenStateSize)
|
575
|
-
);
|
576
|
-
const symbol = account.tokenSymbol;
|
577
|
-
const adminContractPublicKey = state.admin;
|
578
|
-
await fetchMinaAccount({
|
579
|
-
publicKey: adminContractPublicKey,
|
580
|
-
force: true,
|
581
|
-
});
|
582
|
-
if (!Mina.hasAccount(adminContractPublicKey)) {
|
583
|
-
throw new Error("Admin contract account not found");
|
584
|
-
}
|
585
|
-
|
586
|
-
const adminContract = Mina.getAccount(adminContractPublicKey);
|
587
|
-
const adminVerificationKey = adminContract.zkapp?.verificationKey;
|
588
|
-
|
589
|
-
if (!adminVerificationKey) {
|
590
|
-
throw new Error("Admin verification key not found");
|
591
|
-
}
|
592
|
-
let isWhitelisted = false;
|
593
|
-
if (
|
594
|
-
vk.FungibleTokenWhitelistedAdmin.hash ===
|
595
|
-
adminVerificationKey.hash.toJSON() &&
|
596
|
-
vk.FungibleTokenWhitelistedAdmin.data === adminVerificationKey.data
|
597
|
-
) {
|
598
|
-
isWhitelisted = true;
|
599
|
-
} else if (
|
600
|
-
vk.FungibleTokenAdmin.hash === adminVerificationKey.hash.toJSON() &&
|
601
|
-
vk.FungibleTokenAdmin.data === adminVerificationKey.data
|
602
|
-
) {
|
603
|
-
isWhitelisted = false;
|
604
|
-
} else {
|
605
|
-
throw new Error("Unknown admin verification key");
|
606
|
-
}
|
607
|
-
const adminAddress0 = adminContract.zkapp?.appState[0];
|
608
|
-
const adminAddress1 = adminContract.zkapp?.appState[1];
|
609
|
-
if (adminAddress0 === undefined || adminAddress1 === undefined) {
|
610
|
-
throw new Error("Cannot fetch admin address from admin contract");
|
611
|
-
}
|
612
|
-
const adminAddress = PublicKey.fromFields([adminAddress0, adminAddress1]);
|
613
|
-
|
614
|
-
return {
|
615
|
-
adminContractAddress: adminContractPublicKey,
|
616
|
-
adminAddress: adminAddress,
|
617
|
-
symbol,
|
618
|
-
isWhitelisted,
|
619
|
-
};
|
620
|
-
}
|
package/src/mina/token/fee.ts
DELETED