@silvana-one/abi 0.1.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.
Files changed (117) hide show
  1. package/README.md +1 -0
  2. package/dist/node/contracts.d.ts +17 -0
  3. package/dist/node/contracts.js +19 -0
  4. package/dist/node/contracts.js.map +1 -0
  5. package/dist/node/fee.d.ts +2 -0
  6. package/dist/node/fee.js +3 -0
  7. package/dist/node/fee.js.map +1 -0
  8. package/dist/node/fetch.d.ts +14 -0
  9. package/dist/node/fetch.js +59 -0
  10. package/dist/node/fetch.js.map +1 -0
  11. package/dist/node/index.cjs +1119 -0
  12. package/dist/node/index.d.ts +6 -0
  13. package/dist/node/index.js +7 -0
  14. package/dist/node/index.js.map +1 -0
  15. package/dist/node/info/address.d.ts +1 -0
  16. package/dist/node/info/address.js +20 -0
  17. package/dist/node/info/address.js.map +1 -0
  18. package/dist/node/info/index.d.ts +1 -0
  19. package/dist/node/info/index.js +2 -0
  20. package/dist/node/info/index.js.map +1 -0
  21. package/dist/node/token/build.d.ts +46 -0
  22. package/dist/node/token/build.js +789 -0
  23. package/dist/node/token/build.js.map +1 -0
  24. package/dist/node/token/contracts.d.ts +17 -0
  25. package/dist/node/token/contracts.js +13 -0
  26. package/dist/node/token/contracts.js.map +1 -0
  27. package/dist/node/token/index.d.ts +3 -0
  28. package/dist/node/token/index.js +4 -0
  29. package/dist/node/token/index.js.map +1 -0
  30. package/dist/node/token/info.d.ts +11 -0
  31. package/dist/node/token/info.js +528 -0
  32. package/dist/node/token/info.js.map +1 -0
  33. package/dist/node/types.d.ts +4 -0
  34. package/dist/node/types.js +2 -0
  35. package/dist/node/types.js.map +1 -0
  36. package/dist/node/vk/devnet.d.ts +2 -0
  37. package/dist/node/vk/devnet.js +86 -0
  38. package/dist/node/vk/devnet.js.map +1 -0
  39. package/dist/node/vk/index.d.ts +4 -0
  40. package/dist/node/vk/index.js +5 -0
  41. package/dist/node/vk/index.js.map +1 -0
  42. package/dist/node/vk/mainnet.d.ts +2 -0
  43. package/dist/node/vk/mainnet.js +86 -0
  44. package/dist/node/vk/mainnet.js.map +1 -0
  45. package/dist/node/vk/types.d.ts +10 -0
  46. package/dist/node/vk/types.js +2 -0
  47. package/dist/node/vk/types.js.map +1 -0
  48. package/dist/node/vk/vk.d.ts +2 -0
  49. package/dist/node/vk/vk.js +7 -0
  50. package/dist/node/vk/vk.js.map +1 -0
  51. package/dist/tsconfig.tsbuildinfo +1 -0
  52. package/dist/tsconfig.web.tsbuildinfo +1 -0
  53. package/dist/web/contracts.d.ts +17 -0
  54. package/dist/web/contracts.js +19 -0
  55. package/dist/web/contracts.js.map +1 -0
  56. package/dist/web/fee.d.ts +2 -0
  57. package/dist/web/fee.js +3 -0
  58. package/dist/web/fee.js.map +1 -0
  59. package/dist/web/fetch.d.ts +14 -0
  60. package/dist/web/fetch.js +59 -0
  61. package/dist/web/fetch.js.map +1 -0
  62. package/dist/web/index.d.ts +6 -0
  63. package/dist/web/index.js +7 -0
  64. package/dist/web/index.js.map +1 -0
  65. package/dist/web/info/address.d.ts +1 -0
  66. package/dist/web/info/address.js +20 -0
  67. package/dist/web/info/address.js.map +1 -0
  68. package/dist/web/info/index.d.ts +1 -0
  69. package/dist/web/info/index.js +2 -0
  70. package/dist/web/info/index.js.map +1 -0
  71. package/dist/web/token/build.d.ts +46 -0
  72. package/dist/web/token/build.js +789 -0
  73. package/dist/web/token/build.js.map +1 -0
  74. package/dist/web/token/contracts.d.ts +17 -0
  75. package/dist/web/token/contracts.js +13 -0
  76. package/dist/web/token/contracts.js.map +1 -0
  77. package/dist/web/token/index.d.ts +3 -0
  78. package/dist/web/token/index.js +4 -0
  79. package/dist/web/token/index.js.map +1 -0
  80. package/dist/web/token/info.d.ts +11 -0
  81. package/dist/web/token/info.js +528 -0
  82. package/dist/web/token/info.js.map +1 -0
  83. package/dist/web/types.d.ts +4 -0
  84. package/dist/web/types.js +2 -0
  85. package/dist/web/types.js.map +1 -0
  86. package/dist/web/vk/devnet.d.ts +2 -0
  87. package/dist/web/vk/devnet.js +86 -0
  88. package/dist/web/vk/devnet.js.map +1 -0
  89. package/dist/web/vk/index.d.ts +4 -0
  90. package/dist/web/vk/index.js +5 -0
  91. package/dist/web/vk/index.js.map +1 -0
  92. package/dist/web/vk/mainnet.d.ts +2 -0
  93. package/dist/web/vk/mainnet.js +86 -0
  94. package/dist/web/vk/mainnet.js.map +1 -0
  95. package/dist/web/vk/types.d.ts +10 -0
  96. package/dist/web/vk/types.js +2 -0
  97. package/dist/web/vk/types.js.map +1 -0
  98. package/dist/web/vk/vk.d.ts +2 -0
  99. package/dist/web/vk/vk.js +7 -0
  100. package/dist/web/vk/vk.js.map +1 -0
  101. package/package.json +69 -0
  102. package/src/contracts.ts +50 -0
  103. package/src/fee.ts +2 -0
  104. package/src/fetch.ts +73 -0
  105. package/src/index.ts +6 -0
  106. package/src/info/address.ts +25 -0
  107. package/src/info/index.ts +1 -0
  108. package/src/token/build.ts +1014 -0
  109. package/src/token/contracts.ts +40 -0
  110. package/src/token/index.ts +3 -0
  111. package/src/token/info.ts +600 -0
  112. package/src/types.ts +4 -0
  113. package/src/vk/devnet.ts +88 -0
  114. package/src/vk/index.ts +4 -0
  115. package/src/vk/mainnet.ts +88 -0
  116. package/src/vk/types.ts +16 -0
  117. package/src/vk/vk.ts +8 -0
@@ -0,0 +1,789 @@
1
+ import { Whitelist } from "@silvana-one/storage";
2
+ import { fetchMinaAccount } from "../fetch.js";
3
+ import { FungibleToken, AdvancedFungibleToken, BondingCurveFungibleToken, FungibleTokenAdmin, FungibleTokenAdvancedAdmin, FungibleTokenBidContract, FungibleTokenOfferContract, FungibleTokenBondingCurveAdmin, } from "@silvana-one/token";
4
+ import { tokenVerificationKeys } from "../vk/index.js";
5
+ import { PublicKey, Mina, AccountUpdate, UInt64, UInt8, Bool, Struct, Field, TokenId, UInt32, } from "o1js";
6
+ export async function buildTokenLaunchTransaction(params) {
7
+ const { chain, args } = params;
8
+ const { uri, symbol, memo, nonce, adminContract: adminType } = args;
9
+ if (memo && typeof memo !== "string")
10
+ throw new Error("Memo must be a string");
11
+ if (memo && memo.length > 30)
12
+ throw new Error("Memo must be less than 30 characters");
13
+ if (!symbol || typeof symbol !== "string")
14
+ throw new Error("Symbol must be a string");
15
+ if (symbol.length >= 7)
16
+ throw new Error("Symbol must be less than 7 characters");
17
+ const sender = PublicKey.fromBase58(args.sender);
18
+ if (nonce === undefined)
19
+ throw new Error("Nonce is required");
20
+ if (typeof nonce !== "number")
21
+ throw new Error("Nonce must be a number");
22
+ const fee = 100_000_000;
23
+ if (uri && typeof uri !== "string")
24
+ throw new Error("Uri must be a string");
25
+ if (!args.tokenAddress || typeof args.tokenAddress !== "string")
26
+ throw new Error("tokenAddress is required");
27
+ if (!args.adminContractAddress ||
28
+ typeof args.adminContractAddress !== "string")
29
+ throw new Error("adminContractAddress is required");
30
+ const adminContract = adminType === "advanced"
31
+ ? FungibleTokenAdvancedAdmin
32
+ : adminType === "bondingCurve"
33
+ ? FungibleTokenBondingCurveAdmin
34
+ : FungibleTokenAdmin;
35
+ const tokenContract = adminType === "advanced"
36
+ ? AdvancedFungibleToken
37
+ : adminType === "bondingCurve"
38
+ ? BondingCurveFungibleToken
39
+ : FungibleToken;
40
+ const vk = tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "devnet"].vk;
41
+ if (!vk ||
42
+ !vk.FungibleTokenOfferContract ||
43
+ !vk.FungibleTokenOfferContract.hash ||
44
+ !vk.FungibleTokenOfferContract.data ||
45
+ !vk.FungibleTokenBidContract ||
46
+ !vk.FungibleTokenBidContract.hash ||
47
+ !vk.FungibleTokenBidContract.data ||
48
+ !vk.FungibleTokenAdvancedAdmin ||
49
+ !vk.FungibleTokenAdvancedAdmin.hash ||
50
+ !vk.FungibleTokenAdvancedAdmin.data ||
51
+ !vk.FungibleTokenAdmin ||
52
+ !vk.FungibleTokenAdmin.hash ||
53
+ !vk.FungibleTokenAdmin.data ||
54
+ !vk.FungibleTokenBondingCurveAdmin ||
55
+ !vk.FungibleTokenBondingCurveAdmin.hash ||
56
+ !vk.FungibleTokenBondingCurveAdmin.data ||
57
+ !vk.AdvancedFungibleToken ||
58
+ !vk.AdvancedFungibleToken.hash ||
59
+ !vk.AdvancedFungibleToken.data ||
60
+ !vk.FungibleToken ||
61
+ !vk.FungibleToken.hash ||
62
+ !vk.FungibleToken.data)
63
+ throw new Error("Cannot get verification key from vk");
64
+ const adminVerificationKey = adminType === "advanced"
65
+ ? vk.FungibleTokenAdvancedAdmin
66
+ : adminType === "bondingCurve"
67
+ ? vk.FungibleTokenBondingCurveAdmin
68
+ : vk.FungibleTokenAdmin;
69
+ const tokenVerificationKey = adminType === "advanced"
70
+ ? vk.AdvancedFungibleToken
71
+ : adminType === "bondingCurve"
72
+ ? vk.BondingCurveFungibleToken
73
+ : vk.FungibleToken;
74
+ if (!adminVerificationKey || !tokenVerificationKey)
75
+ throw new Error("Cannot get verification keys");
76
+ await fetchMinaAccount({
77
+ publicKey: sender,
78
+ force: true,
79
+ });
80
+ if (!Mina.hasAccount(sender)) {
81
+ throw new Error("Sender does not have account");
82
+ }
83
+ const whitelist = "whitelist" in args && args.whitelist
84
+ ? typeof args.whitelist === "string"
85
+ ? Whitelist.fromString(args.whitelist)
86
+ : (await Whitelist.create({ list: args.whitelist, name: symbol }))
87
+ .whitelist
88
+ : Whitelist.empty();
89
+ const tokenAddress = PublicKey.fromBase58(args.tokenAddress);
90
+ const adminContractAddress = PublicKey.fromBase58(args.adminContractAddress);
91
+ const zkToken = new tokenContract(tokenAddress);
92
+ const zkAdmin = new adminContract(adminContractAddress);
93
+ const provingKey = params.provingKey
94
+ ? PublicKey.fromBase58(params.provingKey)
95
+ : sender;
96
+ const provingFee = params.provingFee
97
+ ? UInt64.from(Math.round(params.provingFee))
98
+ : undefined;
99
+ const developerFee = args.developerFee
100
+ ? UInt64.from(Math.round(args.developerFee))
101
+ : undefined;
102
+ const developerAddress = params.developerAddress
103
+ ? PublicKey.fromBase58(params.developerAddress)
104
+ : undefined;
105
+ const totalSupply = "totalSupply" in args && args.totalSupply
106
+ ? UInt64.from(Math.round(args.totalSupply))
107
+ : UInt64.MAXINT();
108
+ const requireAdminSignatureForMint = "requireAdminSignatureForMint" in args && args.requireAdminSignatureForMint
109
+ ? Bool(args.requireAdminSignatureForMint)
110
+ : Bool(false);
111
+ const anyoneCanMint = "canMint" in args && args.canMint
112
+ ? Bool(args.canMint === "anyone")
113
+ : Bool(false);
114
+ const decimals = UInt8.from(args.decimals ? Math.round(args.decimals) : 9);
115
+ const tx = await Mina.transaction({ sender, fee, memo: memo ?? `launch ${symbol}`, nonce }, async () => {
116
+ if (zkAdmin instanceof FungibleTokenBondingCurveAdmin) {
117
+ console.log("deploying bonding curve admin", {
118
+ verificationKey: adminVerificationKey.hash,
119
+ tokenAddress: tokenAddress.toBase58(),
120
+ feeMaster: provingKey.toBase58(),
121
+ adminContractAddress: adminContractAddress.toBase58(),
122
+ });
123
+ await zkAdmin.deploy({
124
+ verificationKey: adminVerificationKey,
125
+ });
126
+ zkAdmin.account.tokenSymbol.set("BC");
127
+ zkAdmin.account.zkappUri.set(uri);
128
+ await zkAdmin.initialize({
129
+ tokenAddress,
130
+ startPrice: UInt64.from(10_000),
131
+ curveK: UInt64.from(10_000),
132
+ feeMaster: provingKey,
133
+ fee: UInt32.from(1000), // 1000 = 1%
134
+ launchFee: UInt64.from(10_000_000_000),
135
+ numberOfNewAccounts: UInt64.from(4),
136
+ });
137
+ }
138
+ else {
139
+ const feeAccountUpdate = AccountUpdate.createSigned(sender);
140
+ feeAccountUpdate.balance.subInPlace(3_000_000_000 + (adminType === "advanced" ? 1_000_000_000 : 0));
141
+ if (provingFee && provingKey)
142
+ feeAccountUpdate.send({
143
+ to: provingKey,
144
+ amount: provingFee,
145
+ });
146
+ if (developerAddress && developerFee) {
147
+ feeAccountUpdate.send({
148
+ to: developerAddress,
149
+ amount: developerFee,
150
+ });
151
+ }
152
+ await zkAdmin.deploy({
153
+ adminPublicKey: sender,
154
+ tokenContract: tokenAddress,
155
+ verificationKey: adminVerificationKey,
156
+ whitelist,
157
+ totalSupply,
158
+ requireAdminSignatureForMint,
159
+ anyoneCanMint,
160
+ });
161
+ if (adminType === "advanced") {
162
+ const adminUpdate = AccountUpdate.create(adminContractAddress, TokenId.derive(adminContractAddress));
163
+ zkAdmin.approve(adminUpdate);
164
+ }
165
+ zkAdmin.account.zkappUri.set(uri);
166
+ }
167
+ await zkToken.deploy({
168
+ symbol,
169
+ src: uri,
170
+ verificationKey: tokenVerificationKey,
171
+ allowUpdates: true,
172
+ });
173
+ await zkToken.initialize(adminContractAddress, decimals,
174
+ // We can set `startPaused` to `Bool(false)` here, because we are doing an atomic deployment
175
+ // If you are not deploying the admin and token contracts in the same transaction,
176
+ // it is safer to start the tokens paused, and resume them only after verifying that
177
+ // the admin contract has been deployed
178
+ Bool(false));
179
+ });
180
+ return {
181
+ request: adminType === "advanced"
182
+ ? {
183
+ ...args,
184
+ whitelist: whitelist.toString(),
185
+ }
186
+ : args,
187
+ tx,
188
+ adminType,
189
+ verificationKeyHashes: [
190
+ adminVerificationKey.hash,
191
+ tokenVerificationKey.hash,
192
+ ],
193
+ };
194
+ }
195
+ export async function buildTokenTransaction(params) {
196
+ const { chain, args } = params;
197
+ const { nonce, txType } = args;
198
+ if (nonce === undefined)
199
+ throw new Error("Nonce is required");
200
+ if (typeof nonce !== "number")
201
+ throw new Error("Nonce must be a number");
202
+ if (txType === undefined)
203
+ throw new Error("Tx type is required");
204
+ if (typeof txType !== "string")
205
+ throw new Error("Tx type must be a string");
206
+ const sender = PublicKey.fromBase58(args.sender);
207
+ const tokenAddress = PublicKey.fromBase58(args.tokenAddress);
208
+ const offerAddress = "offerAddress" in args && args.offerAddress
209
+ ? PublicKey.fromBase58(args.offerAddress)
210
+ : undefined;
211
+ if (!offerAddress &&
212
+ (txType === "token:offer:create" ||
213
+ txType === "token:offer:buy" ||
214
+ txType === "token:offer:withdraw"))
215
+ throw new Error("Offer address is required");
216
+ const bidAddress = "bidAddress" in args && args.bidAddress
217
+ ? PublicKey.fromBase58(args.bidAddress)
218
+ : undefined;
219
+ if (!bidAddress &&
220
+ (txType === "token:bid:create" ||
221
+ txType === "token:bid:sell" ||
222
+ txType === "token:bid:withdraw"))
223
+ throw new Error("Bid address is required");
224
+ const to = "to" in args && args.to ? PublicKey.fromBase58(args.to) : undefined;
225
+ if (!to &&
226
+ (txType === "token:transfer" ||
227
+ txType === "token:airdrop" ||
228
+ txType === "token:mint"))
229
+ throw new Error("To address is required");
230
+ const from = "from" in args && args.from ? PublicKey.fromBase58(args.from) : undefined;
231
+ if (!from && txType === "token:burn")
232
+ throw new Error("From address is required for token:burn");
233
+ const amount = "amount" in args ? UInt64.from(Math.round(args.amount)) : undefined;
234
+ const price = "price" in args && args.price
235
+ ? UInt64.from(Math.round(args.price))
236
+ : undefined;
237
+ const slippage = UInt32.from(Math.round("slippage" in args && args.slippage !== undefined ? args.slippage : 50));
238
+ await fetchMinaAccount({
239
+ publicKey: sender,
240
+ force: true,
241
+ });
242
+ if (!Mina.hasAccount(sender)) {
243
+ throw new Error("Sender does not have account");
244
+ }
245
+ const { symbol, adminContractAddress, adminAddress, adminType, isToNewAccount, verificationKeyHashes, } = await getTokenSymbolAndAdmin({
246
+ txType,
247
+ tokenAddress,
248
+ chain,
249
+ to,
250
+ offerAddress,
251
+ bidAddress,
252
+ });
253
+ const memo = args.memo ?? `${txType} ${symbol}`;
254
+ const fee = 100_000_000;
255
+ const provingKey = params.provingKey
256
+ ? PublicKey.fromBase58(params.provingKey)
257
+ : sender;
258
+ const provingFee = params.provingFee
259
+ ? UInt64.from(Math.round(params.provingFee))
260
+ : undefined;
261
+ const developerFee = args.developerFee
262
+ ? UInt64.from(Math.round(args.developerFee))
263
+ : undefined;
264
+ const developerAddress = params.developerAddress
265
+ ? PublicKey.fromBase58(params.developerAddress)
266
+ : undefined;
267
+ //const adminContract = new FungibleTokenAdmin(adminContractAddress);
268
+ const advancedAdminContract = new FungibleTokenAdvancedAdmin(adminContractAddress);
269
+ const bondingCurveAdminContract = new FungibleTokenBondingCurveAdmin(adminContractAddress);
270
+ const tokenContract = adminType === "advanced" && txType === "token:mint"
271
+ ? AdvancedFungibleToken
272
+ : adminType === "bondingCurve" &&
273
+ (txType === "token:mint" || txType === "token:redeem")
274
+ ? BondingCurveFungibleToken
275
+ : FungibleToken;
276
+ if ((txType === "token:admin:whitelist" ||
277
+ txType === "token:bid:whitelist" ||
278
+ txType === "token:offer:whitelist") &&
279
+ !args.whitelist) {
280
+ throw new Error("Whitelist is required");
281
+ }
282
+ const whitelist = "whitelist" in args && args.whitelist
283
+ ? typeof args.whitelist === "string"
284
+ ? Whitelist.fromString(args.whitelist)
285
+ : (await Whitelist.create({ list: args.whitelist, name: symbol }))
286
+ .whitelist
287
+ : Whitelist.empty();
288
+ const zkToken = new tokenContract(tokenAddress);
289
+ const tokenId = zkToken.deriveTokenId();
290
+ if (txType === "token:mint" &&
291
+ adminType === "standard" &&
292
+ adminAddress.toBase58() !== sender.toBase58())
293
+ throw new Error("Invalid sender for FungibleToken mint with standard admin");
294
+ await fetchMinaAccount({
295
+ publicKey: sender,
296
+ tokenId,
297
+ force: [
298
+ "token:transfer",
299
+ "token:airdrop",
300
+ ].includes(txType),
301
+ });
302
+ if (to) {
303
+ await fetchMinaAccount({
304
+ publicKey: to,
305
+ tokenId,
306
+ force: false,
307
+ });
308
+ }
309
+ if (from) {
310
+ await fetchMinaAccount({
311
+ publicKey: from,
312
+ tokenId,
313
+ force: false,
314
+ });
315
+ }
316
+ if (offerAddress)
317
+ await fetchMinaAccount({
318
+ publicKey: offerAddress,
319
+ tokenId,
320
+ force: [
321
+ "token:offer:whitelist",
322
+ "token:offer:buy",
323
+ "token:offer:withdraw",
324
+ ].includes(txType),
325
+ });
326
+ if (bidAddress)
327
+ await fetchMinaAccount({
328
+ publicKey: bidAddress,
329
+ force: [
330
+ "token:bid:whitelist",
331
+ "token:bid:sell",
332
+ "token:bid:withdraw",
333
+ ].includes(txType),
334
+ });
335
+ const offerContract = offerAddress
336
+ ? new FungibleTokenOfferContract(offerAddress, tokenId)
337
+ : undefined;
338
+ const bidContract = bidAddress
339
+ ? new FungibleTokenBidContract(bidAddress)
340
+ : undefined;
341
+ const offerContractDeployment = offerAddress
342
+ ? new FungibleTokenOfferContract(offerAddress, tokenId)
343
+ : undefined;
344
+ const bidContractDeployment = bidAddress
345
+ ? new FungibleTokenBidContract(bidAddress)
346
+ : undefined;
347
+ const vk = tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "devnet"].vk;
348
+ if (!vk ||
349
+ !vk.FungibleTokenOfferContract ||
350
+ !vk.FungibleTokenOfferContract.hash ||
351
+ !vk.FungibleTokenOfferContract.data ||
352
+ !vk.FungibleTokenBidContract ||
353
+ !vk.FungibleTokenBidContract.hash ||
354
+ !vk.FungibleTokenBidContract.data ||
355
+ !vk.FungibleTokenAdvancedAdmin ||
356
+ !vk.FungibleTokenAdvancedAdmin.hash ||
357
+ !vk.FungibleTokenAdvancedAdmin.data ||
358
+ !vk.FungibleTokenBondingCurveAdmin ||
359
+ !vk.FungibleTokenBondingCurveAdmin.hash ||
360
+ !vk.FungibleTokenBondingCurveAdmin.data ||
361
+ !vk.FungibleTokenAdmin ||
362
+ !vk.FungibleTokenAdmin.hash ||
363
+ !vk.FungibleTokenAdmin.data ||
364
+ !vk.AdvancedFungibleToken ||
365
+ !vk.AdvancedFungibleToken.hash ||
366
+ !vk.AdvancedFungibleToken.data ||
367
+ !vk.FungibleToken ||
368
+ !vk.FungibleToken.hash ||
369
+ !vk.FungibleToken.data)
370
+ throw new Error("Cannot get verification key from vk");
371
+ const offerVerificationKey = FungibleTokenOfferContract._verificationKey ?? {
372
+ hash: Field(vk.FungibleTokenOfferContract.hash),
373
+ data: vk.FungibleTokenOfferContract.data,
374
+ };
375
+ const bidVerificationKey = FungibleTokenBidContract._verificationKey ?? {
376
+ hash: Field(vk.FungibleTokenBidContract.hash),
377
+ data: vk.FungibleTokenBidContract.data,
378
+ };
379
+ const isNewBidOfferAccount = txType === "token:offer:create" && offerAddress
380
+ ? !Mina.hasAccount(offerAddress, tokenId)
381
+ : txType === "token:bid:create" && bidAddress
382
+ ? !Mina.hasAccount(bidAddress)
383
+ : false;
384
+ const isNewBuyAccount = txType === "token:offer:buy" ? !Mina.hasAccount(sender, tokenId) : false;
385
+ let isNewSellAccount = false;
386
+ if (txType === "token:bid:sell") {
387
+ if (!bidAddress || !bidContract)
388
+ throw new Error("Bid address is required");
389
+ await fetchMinaAccount({
390
+ publicKey: bidAddress,
391
+ force: true,
392
+ });
393
+ const buyer = bidContract.buyer.get();
394
+ await fetchMinaAccount({
395
+ publicKey: buyer,
396
+ tokenId,
397
+ force: false,
398
+ });
399
+ isNewSellAccount = !Mina.hasAccount(buyer, tokenId);
400
+ }
401
+ if (txType === "token:burn") {
402
+ await fetchMinaAccount({
403
+ publicKey: sender,
404
+ force: true,
405
+ });
406
+ await fetchMinaAccount({
407
+ publicKey: sender,
408
+ tokenId,
409
+ force: false,
410
+ });
411
+ if (!Mina.hasAccount(sender, tokenId))
412
+ throw new Error("Sender does not have tokens to burn");
413
+ }
414
+ const isNewTransferMintAccount = (txType === "token:transfer" ||
415
+ txType === "token:airdrop" ||
416
+ txType === "token:mint") &&
417
+ to
418
+ ? !Mina.hasAccount(to, tokenId)
419
+ : false;
420
+ const accountCreationFee = (isNewBidOfferAccount ? 1_000_000_000 : 0) +
421
+ (isNewBuyAccount ? 1_000_000_000 : 0) +
422
+ (isNewSellAccount ? 1_000_000_000 : 0) +
423
+ (isNewTransferMintAccount ? 1_000_000_000 : 0) +
424
+ (isToNewAccount &&
425
+ txType === "token:mint" &&
426
+ adminType === "advanced" &&
427
+ advancedAdminContract.whitelist.get().isSome().toBoolean()
428
+ ? 1_000_000_000
429
+ : 0);
430
+ console.log("accountCreationFee", accountCreationFee / 1_000_000_000);
431
+ switch (txType) {
432
+ case "token:offer:buy":
433
+ case "token:offer:withdraw":
434
+ case "token:offer:whitelist":
435
+ if (offerContract === undefined)
436
+ throw new Error("Offer contract is required");
437
+ if (Mina.getAccount(offerContract.address, tokenId).zkapp?.verificationKey?.hash.toJSON() !==
438
+ vk.FungibleTokenOfferContract.hash)
439
+ throw new Error("Invalid offer verification key, offer contract has to be upgraded");
440
+ break;
441
+ }
442
+ switch (txType) {
443
+ case "token:bid:sell":
444
+ case "token:bid:withdraw":
445
+ case "token:bid:whitelist":
446
+ if (bidContract === undefined)
447
+ throw new Error("Bid contract is required");
448
+ if (Mina.getAccount(bidContract.address).zkapp?.verificationKey?.hash.toJSON() !==
449
+ vk.FungibleTokenBidContract.hash)
450
+ throw new Error("Invalid bid verification key, bid contract has to be upgraded");
451
+ break;
452
+ }
453
+ switch (txType) {
454
+ case "token:mint":
455
+ case "token:burn":
456
+ case "token:redeem":
457
+ case "token:transfer":
458
+ case "token:airdrop":
459
+ case "token:offer:create":
460
+ case "token:bid:create":
461
+ case "token:offer:buy":
462
+ case "token:offer:withdraw":
463
+ case "token:bid:sell":
464
+ if (Mina.getAccount(zkToken.address).zkapp?.verificationKey?.hash.toJSON() !== vk.FungibleToken.hash)
465
+ throw new Error("Invalid token verification key, token contract has to be upgraded");
466
+ break;
467
+ }
468
+ const tx = await Mina.transaction({ sender, fee, memo, nonce }, async () => {
469
+ if (adminType !== "bondingCurve" ||
470
+ (txType !== "token:mint" && txType !== "token:redeem")) {
471
+ const feeAccountUpdate = AccountUpdate.createSigned(sender);
472
+ if (accountCreationFee > 0) {
473
+ feeAccountUpdate.balance.subInPlace(accountCreationFee);
474
+ }
475
+ if (provingKey && provingFee)
476
+ feeAccountUpdate.send({
477
+ to: provingKey,
478
+ amount: provingFee,
479
+ });
480
+ if (developerAddress && developerFee) {
481
+ feeAccountUpdate.send({
482
+ to: developerAddress,
483
+ amount: developerFee,
484
+ });
485
+ }
486
+ }
487
+ switch (txType) {
488
+ case "token:mint":
489
+ if (amount === undefined)
490
+ throw new Error("Error: Amount is required");
491
+ if (to === undefined)
492
+ throw new Error("Error: To address is required");
493
+ if (adminType === "bondingCurve") {
494
+ if (price === undefined)
495
+ throw new Error("Error: Price is required for bonding curve mint");
496
+ await bondingCurveAdminContract.mint(to, amount, price);
497
+ }
498
+ else {
499
+ await zkToken.mint(to, amount);
500
+ }
501
+ break;
502
+ case "token:redeem":
503
+ if (adminType !== "bondingCurve")
504
+ throw new Error("Error: Invalid admin type for redeem");
505
+ if (amount === undefined)
506
+ throw new Error("Error: Amount is required");
507
+ if (price === undefined)
508
+ throw new Error("Error: Price is required");
509
+ if (slippage === undefined)
510
+ throw new Error("Error: Slippage is required");
511
+ await bondingCurveAdminContract.redeem(amount, price, slippage);
512
+ break;
513
+ case "token:transfer":
514
+ if (amount === undefined)
515
+ throw new Error("Error: Amount is required");
516
+ if (to === undefined)
517
+ throw new Error("Error: From address is required");
518
+ await zkToken.transfer(sender, to, amount);
519
+ break;
520
+ case "token:burn":
521
+ if (amount === undefined)
522
+ throw new Error("Error: Amount is required");
523
+ if (from === undefined)
524
+ throw new Error("Error: From address is required");
525
+ await zkToken.burn(from, amount);
526
+ break;
527
+ case "token:offer:create":
528
+ if (price === undefined)
529
+ throw new Error("Error: Price is required");
530
+ if (amount === undefined)
531
+ throw new Error("Error: Amount is required");
532
+ if (offerContract === undefined)
533
+ throw new Error("Error: Offer address is required");
534
+ if (offerContractDeployment === undefined)
535
+ throw new Error("Error: Offer address is required");
536
+ if (isNewBidOfferAccount) {
537
+ await offerContractDeployment.deploy({
538
+ verificationKey: offerVerificationKey,
539
+ whitelist: whitelist ?? Whitelist.empty(),
540
+ });
541
+ offerContract.account.zkappUri.set(`Offer for ${symbol}`);
542
+ await offerContract.initialize(sender, tokenAddress, amount, price);
543
+ await zkToken.approveAccountUpdates([
544
+ offerContractDeployment.self,
545
+ offerContract.self,
546
+ ]);
547
+ }
548
+ else {
549
+ await offerContract.offer(amount, price);
550
+ await zkToken.approveAccountUpdate(offerContract.self);
551
+ }
552
+ break;
553
+ case "token:offer:buy":
554
+ if (amount === undefined)
555
+ throw new Error("Error: Amount is required");
556
+ if (offerContract === undefined)
557
+ throw new Error("Error: Offer address is required");
558
+ await offerContract.buy(amount);
559
+ await zkToken.approveAccountUpdate(offerContract.self);
560
+ break;
561
+ case "token:offer:withdraw":
562
+ if (amount === undefined)
563
+ throw new Error("Error: Amount is required");
564
+ if (offerContract === undefined)
565
+ throw new Error("Error: Offer address is required");
566
+ await offerContract.withdraw(amount);
567
+ await zkToken.approveAccountUpdate(offerContract.self);
568
+ break;
569
+ case "token:bid:create":
570
+ if (price === undefined)
571
+ throw new Error("Error: Price is required");
572
+ if (amount === undefined)
573
+ throw new Error("Error: Amount is required");
574
+ if (bidContract === undefined)
575
+ throw new Error("Error: Bid address is required");
576
+ if (bidContractDeployment === undefined)
577
+ throw new Error("Error: Bid address is required");
578
+ if (isNewBidOfferAccount) {
579
+ await bidContractDeployment.deploy({
580
+ verificationKey: bidVerificationKey,
581
+ whitelist: whitelist ?? Whitelist.empty(),
582
+ });
583
+ bidContract.account.zkappUri.set(`Bid for ${symbol}`);
584
+ await bidContract.initialize(tokenAddress, amount, price);
585
+ await zkToken.approveAccountUpdates([
586
+ bidContractDeployment.self,
587
+ bidContract.self,
588
+ ]);
589
+ }
590
+ else {
591
+ await bidContract.bid(amount, price);
592
+ await zkToken.approveAccountUpdate(bidContract.self);
593
+ }
594
+ break;
595
+ case "token:bid:sell":
596
+ if (amount === undefined)
597
+ throw new Error("Error: Amount is required");
598
+ if (bidContract === undefined)
599
+ throw new Error("Error: Bid address is required");
600
+ await bidContract.sell(amount);
601
+ await zkToken.approveAccountUpdate(bidContract.self);
602
+ break;
603
+ case "token:bid:withdraw":
604
+ if (amount === undefined)
605
+ throw new Error("Error: Amount is required");
606
+ if (bidContract === undefined)
607
+ throw new Error("Error: Bid address is required");
608
+ await bidContract.withdraw(amount);
609
+ //await zkToken.approveAccountUpdate(bidContract.self);
610
+ break;
611
+ case "token:admin:whitelist":
612
+ if (adminType !== "advanced")
613
+ throw new Error("Invalid admin type for updateAdminWhitelist");
614
+ await advancedAdminContract.updateWhitelist(whitelist);
615
+ break;
616
+ case "token:bid:whitelist":
617
+ if (bidContract === undefined)
618
+ throw new Error("Error: Bid address is required");
619
+ await bidContract.updateWhitelist(whitelist);
620
+ break;
621
+ case "token:offer:whitelist":
622
+ if (offerContract === undefined)
623
+ throw new Error("Error: Offer address is required");
624
+ await offerContract.updateWhitelist(whitelist);
625
+ break;
626
+ default:
627
+ throw new Error(`Unknown transaction type: ${txType}`);
628
+ }
629
+ });
630
+ return {
631
+ request: txType === "token:offer:create" ||
632
+ txType === "token:bid:create" ||
633
+ txType === "token:offer:whitelist" ||
634
+ txType === "token:bid:whitelist" ||
635
+ txType === "token:admin:whitelist"
636
+ ? {
637
+ ...args,
638
+ whitelist: whitelist?.toString(),
639
+ }
640
+ : args,
641
+ tx,
642
+ adminType,
643
+ adminContractAddress,
644
+ adminAddress,
645
+ symbol,
646
+ verificationKeyHashes,
647
+ };
648
+ }
649
+ export async function getTokenSymbolAndAdmin(params) {
650
+ const { txType, tokenAddress, chain, to, offerAddress, bidAddress } = params;
651
+ const vk = tokenVerificationKeys[chain === "mainnet" ? "mainnet" : "devnet"].vk;
652
+ let verificationKeyHashes = [];
653
+ if (bidAddress) {
654
+ verificationKeyHashes.push(vk.FungibleTokenBidContract.hash);
655
+ }
656
+ if (offerAddress) {
657
+ verificationKeyHashes.push(vk.FungibleTokenOfferContract.hash);
658
+ }
659
+ class FungibleTokenState extends Struct({
660
+ decimals: UInt8,
661
+ admin: PublicKey,
662
+ paused: Bool,
663
+ }) {
664
+ }
665
+ const FungibleTokenStateSize = FungibleTokenState.sizeInFields();
666
+ class FungibleTokenAdminState extends Struct({
667
+ adminPublicKey: PublicKey,
668
+ }) {
669
+ }
670
+ const FungibleTokenAdminStateSize = FungibleTokenAdminState.sizeInFields();
671
+ await fetchMinaAccount({ publicKey: tokenAddress, force: true });
672
+ if (!Mina.hasAccount(tokenAddress)) {
673
+ throw new Error("Token contract account not found");
674
+ }
675
+ const tokenId = TokenId.derive(tokenAddress);
676
+ await fetchMinaAccount({ publicKey: tokenAddress, tokenId, force: true });
677
+ if (!Mina.hasAccount(tokenAddress, tokenId)) {
678
+ throw new Error("Token contract totalSupply account not found");
679
+ }
680
+ const account = Mina.getAccount(tokenAddress);
681
+ const verificationKey = account.zkapp?.verificationKey;
682
+ if (!verificationKey) {
683
+ throw new Error("Token contract verification key not found");
684
+ }
685
+ if (!verificationKeyHashes.includes(verificationKey.hash.toJSON())) {
686
+ verificationKeyHashes.push(verificationKey.hash.toJSON());
687
+ }
688
+ if (account.zkapp?.appState === undefined) {
689
+ throw new Error("Token contract state not found");
690
+ }
691
+ const state = FungibleTokenState.fromFields(account.zkapp?.appState.slice(0, FungibleTokenStateSize));
692
+ const symbol = account.tokenSymbol;
693
+ const adminContractPublicKey = state.admin;
694
+ await fetchMinaAccount({
695
+ publicKey: adminContractPublicKey,
696
+ force: true,
697
+ });
698
+ if (!Mina.hasAccount(adminContractPublicKey)) {
699
+ throw new Error("Admin contract account not found");
700
+ }
701
+ const adminContract = Mina.getAccount(adminContractPublicKey);
702
+ const adminVerificationKey = adminContract.zkapp?.verificationKey;
703
+ if (!adminVerificationKey) {
704
+ throw new Error("Admin verification key not found");
705
+ }
706
+ if (!verificationKeyHashes.includes(adminVerificationKey.hash.toJSON())) {
707
+ verificationKeyHashes.push(adminVerificationKey.hash.toJSON());
708
+ }
709
+ let adminType = "unknown";
710
+ if (vk.FungibleTokenAdvancedAdmin.hash === adminVerificationKey.hash.toJSON() &&
711
+ vk.FungibleTokenAdvancedAdmin.data === adminVerificationKey.data) {
712
+ adminType = "advanced";
713
+ }
714
+ else if (vk.FungibleTokenAdmin.hash === adminVerificationKey.hash.toJSON() &&
715
+ vk.FungibleTokenAdmin.data === adminVerificationKey.data) {
716
+ adminType = "standard";
717
+ }
718
+ else if (vk.FungibleTokenBondingCurveAdmin.hash ===
719
+ adminVerificationKey.hash.toJSON() &&
720
+ vk.FungibleTokenBondingCurveAdmin.data === adminVerificationKey.data) {
721
+ adminType = "bondingCurve";
722
+ }
723
+ else {
724
+ console.error("Unknown admin verification key", {
725
+ hash: adminVerificationKey.hash.toJSON(),
726
+ symbol,
727
+ address: adminContractPublicKey.toBase58(),
728
+ });
729
+ }
730
+ let isToNewAccount = undefined;
731
+ if (to) {
732
+ if (adminType === "advanced") {
733
+ const adminTokenId = TokenId.derive(adminContractPublicKey);
734
+ await fetchMinaAccount({
735
+ publicKey: to,
736
+ tokenId: adminTokenId,
737
+ force: false,
738
+ });
739
+ isToNewAccount = !Mina.hasAccount(to, adminTokenId);
740
+ }
741
+ if (adminType === "bondingCurve") {
742
+ const adminTokenId = TokenId.derive(adminContractPublicKey);
743
+ await fetchMinaAccount({
744
+ publicKey: adminContractPublicKey,
745
+ tokenId: adminTokenId,
746
+ force: true,
747
+ });
748
+ }
749
+ }
750
+ const adminAddress0 = adminContract.zkapp?.appState[0];
751
+ const adminAddress1 = adminContract.zkapp?.appState[1];
752
+ if (adminAddress0 === undefined || adminAddress1 === undefined) {
753
+ throw new Error("Cannot fetch admin address from admin contract");
754
+ }
755
+ const adminAddress = PublicKey.fromFields([adminAddress0, adminAddress1]);
756
+ for (const hash of verificationKeyHashes) {
757
+ const found = Object.values(vk).some((key) => key.hash === hash);
758
+ if (!found) {
759
+ console.error(`Final check: unknown verification key hash: ${hash}`);
760
+ verificationKeyHashes = verificationKeyHashes.filter((h) => h !== hash);
761
+ }
762
+ }
763
+ // Sort verification key hashes by type: upgrade -> admin -> token -> user
764
+ verificationKeyHashes.sort((a, b) => {
765
+ const typeA = Object.values(vk).find((key) => key.hash === a)?.type;
766
+ const typeB = Object.values(vk).find((key) => key.hash === b)?.type;
767
+ if (typeA === undefined || typeB === undefined) {
768
+ throw new Error("Unknown verification key hash");
769
+ }
770
+ const typeOrder = {
771
+ upgrade: 0,
772
+ nft: 1,
773
+ admin: 2,
774
+ collection: 3,
775
+ token: 4,
776
+ user: 5,
777
+ };
778
+ return typeOrder[typeA] - typeOrder[typeB];
779
+ });
780
+ return {
781
+ adminContractAddress: adminContractPublicKey,
782
+ adminAddress: adminAddress,
783
+ symbol,
784
+ adminType,
785
+ isToNewAccount,
786
+ verificationKeyHashes,
787
+ };
788
+ }
789
+ //# sourceMappingURL=build.js.map