@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,40 @@
1
+ import { Cache, Field } from "o1js";
2
+ import {
3
+ FungibleToken,
4
+ AdvancedFungibleToken,
5
+ BondingCurveFungibleToken,
6
+ FungibleTokenAdmin,
7
+ FungibleTokenAdvancedAdmin,
8
+ FungibleTokenBondingCurveAdmin,
9
+ FungibleTokenBidContract,
10
+ FungibleTokenOfferContract,
11
+ FungibleTokenClaimContract,
12
+ } from "@silvana-one/token";
13
+
14
+ export type Compilable = {
15
+ compile({ cache }?: { cache: Cache }): Promise<{
16
+ verificationKey: {
17
+ data: string;
18
+ hash: Field;
19
+ };
20
+ }>;
21
+ };
22
+
23
+ export const tokenContracts: Record<string, Compilable> = {
24
+ FungibleToken: FungibleToken,
25
+ FungibleTokenAdmin: FungibleTokenAdmin,
26
+ AdvancedFungibleToken: AdvancedFungibleToken,
27
+ BondingCurveFungibleToken: BondingCurveFungibleToken,
28
+ FungibleTokenAdvancedAdmin: FungibleTokenAdvancedAdmin,
29
+ FungibleTokenBondingCurveAdmin: FungibleTokenBondingCurveAdmin,
30
+ FungibleTokenBidContract: FungibleTokenBidContract,
31
+ FungibleTokenOfferContract: FungibleTokenOfferContract,
32
+ FungibleTokenClaimContract: FungibleTokenClaimContract,
33
+ };
34
+
35
+ export type CompileDependencies = Record<
36
+ /** Transaction type */
37
+ string,
38
+ /** List of contract names */
39
+ string[]
40
+ >;
@@ -0,0 +1,3 @@
1
+ export * from "./build.js";
2
+ export * from "./contracts.js";
3
+ export * from "./info.js";
@@ -0,0 +1,600 @@
1
+ import { Mina, PublicKey, Bool, TokenId, Struct, UInt8, Field } from "o1js";
2
+ import { fetchMinaAccount } from "../fetch.js";
3
+ import {
4
+ TokenState,
5
+ TokenInfoRequestParams,
6
+ BalanceRequestParams,
7
+ BalanceResponse,
8
+ } from "@silvana-one/api";
9
+ import { checkAddress } from "../info/address.js";
10
+ import {
11
+ FungibleToken,
12
+ AdvancedFungibleToken,
13
+ FungibleTokenAdmin,
14
+ FungibleTokenAdvancedAdmin,
15
+ FungibleTokenBidContract,
16
+ FungibleTokenOfferContract,
17
+ FungibleTokenClaimContract,
18
+ } from "@silvana-one/token";
19
+ import { ContractInfo } from "@silvana-one/api";
20
+ import { tokenVerificationKeys } from "../vk/vk.js";
21
+
22
+ export async function getContractInfo(params: {
23
+ address: string | PublicKey;
24
+ tokenId?: string | Field;
25
+ parentTokenId?: Field;
26
+ decimals?: number;
27
+ chain: "mainnet" | "devnet";
28
+ }): Promise<ContractInfo[]> {
29
+ const { address, chain, parentTokenId, decimals } = params;
30
+ const vk = tokenVerificationKeys[chain].vk;
31
+ const info: ContractInfo[] = [];
32
+ if (typeof address === "string" && !checkAddress(address)) {
33
+ throw new Error("Invalid address");
34
+ }
35
+ const publicKey =
36
+ typeof address === "string" ? PublicKey.fromBase58(address) : address;
37
+ const tokenId = params.tokenId
38
+ ? typeof params.tokenId === "string"
39
+ ? TokenId.fromBase58(params.tokenId)
40
+ : params.tokenId
41
+ : undefined;
42
+ await fetchMinaAccount({ publicKey, tokenId, force: false });
43
+ if (!Mina.hasAccount(publicKey, tokenId)) {
44
+ throw new Error("Account does not exist");
45
+ }
46
+ const account = Mina.getAccount(publicKey, tokenId);
47
+ if (account.zkapp?.appState === undefined) {
48
+ throw new Error("The account is not a zkApp");
49
+ }
50
+
51
+ const tokenSymbol = account.tokenSymbol;
52
+ const uri = account.zkapp?.zkappUri;
53
+ const verificationKey = account.zkapp?.verificationKey?.data;
54
+ const verificationKeyHash = account.zkapp?.verificationKey?.hash.toJSON();
55
+
56
+ const versionData = account.zkapp?.zkappVersion;
57
+ const version = Number(versionData.toBigint());
58
+ const name = Object.keys(vk).find(
59
+ (key) => vk[key].hash === verificationKeyHash
60
+ );
61
+ const derivedTokenId = TokenId.derive(publicKey, tokenId);
62
+ const info0: ContractInfo = {
63
+ name: { type: "name", value: name ?? "unknown" },
64
+ address: { type: "address", value: publicKey.toBase58() },
65
+ tokenId: {
66
+ type: "tokenId",
67
+ value: TokenId.toBase58(tokenId ?? Field.from(1)),
68
+ },
69
+ derivedTokenId: {
70
+ type: "tokenId",
71
+ value: TokenId.toBase58(derivedTokenId),
72
+ },
73
+ symbol: { type: "symbol", value: tokenSymbol },
74
+ uri: { type: "uri", value: uri },
75
+ verificationKey: { type: "verificationKey", value: verificationKey ?? "" },
76
+ verificationKeyHash: {
77
+ type: "verificationKeyHash",
78
+ value: verificationKeyHash ?? "",
79
+ },
80
+ zkappVersion: { type: "zkappVersion", value: version.toString() },
81
+ };
82
+
83
+ switch (name) {
84
+ case "FungibleToken":
85
+ case "AdvancedFungibleToken":
86
+ {
87
+ const zkApp = new FungibleToken(publicKey, tokenId);
88
+ const admin = zkApp.admin.get();
89
+ const decimals = zkApp.decimals.get();
90
+ const paused = zkApp.paused.get();
91
+ const zkAppTokenId = zkApp.deriveTokenId();
92
+ if (TokenId.toBase58(zkAppTokenId) !== info0.derivedTokenId.value) {
93
+ throw new Error("Derived tokenId does not match");
94
+ }
95
+ await fetchMinaAccount({
96
+ publicKey,
97
+ tokenId: zkAppTokenId,
98
+ force: false,
99
+ });
100
+ const totalSupply = Mina.getBalance(publicKey, zkAppTokenId).toBigInt();
101
+ info0.admin = { type: "address", value: admin.toBase58() };
102
+ info0.decimals = {
103
+ type: "number",
104
+ value: decimals.toNumber().toString(),
105
+ };
106
+ info0.paused = {
107
+ type: "boolean",
108
+ value: paused.toBoolean().toString(),
109
+ };
110
+ info0.totalSupply = {
111
+ type: "bigint",
112
+ value: totalSupply.toString(),
113
+ presentation: formatBalanceInternal(
114
+ Number(totalSupply / BigInt(10 ** decimals.toNumber()))
115
+ ),
116
+ };
117
+ const info1: ContractInfo[] = await getContractInfo({
118
+ address: admin,
119
+ parentTokenId: zkAppTokenId,
120
+ decimals: decimals.toNumber(),
121
+ chain,
122
+ });
123
+ info.push(...info1);
124
+ }
125
+ break;
126
+
127
+ case "FungibleTokenAdmin":
128
+ {
129
+ const adminAddress0 = account.zkapp?.appState[0];
130
+ const adminAddress1 = account.zkapp?.appState[1];
131
+ if (adminAddress0 === undefined || adminAddress1 === undefined) {
132
+ throw new Error("Cannot fetch admin address from admin contract");
133
+ }
134
+ const adminAddress = PublicKey.fromFields([
135
+ adminAddress0,
136
+ adminAddress1,
137
+ ]);
138
+ let adminTokenBalance: bigint = 0n;
139
+ if (parentTokenId) {
140
+ try {
141
+ await fetchMinaAccount({
142
+ publicKey: adminAddress,
143
+ tokenId: parentTokenId,
144
+ force: false,
145
+ });
146
+ adminTokenBalance = Mina.getBalance(
147
+ adminAddress,
148
+ parentTokenId
149
+ ).toBigInt();
150
+ } catch (error) {}
151
+ }
152
+ info0.admin = { type: "address", value: adminAddress.toBase58() };
153
+ info0.adminTokenBalance = {
154
+ type: "bigint",
155
+ value: adminTokenBalance.toString(),
156
+ presentation: formatBalanceInternal(
157
+ Number(adminTokenBalance / BigInt(1 << (decimals ?? 9)))
158
+ ),
159
+ };
160
+ }
161
+ break;
162
+ }
163
+ info.push(info0);
164
+ return info;
165
+ }
166
+
167
+ function formatBalanceInternal(num: number): string {
168
+ const fixed = num.toFixed(2);
169
+ return fixed.endsWith(".00") ? fixed.slice(0, -3) : fixed;
170
+ }
171
+
172
+ export async function tokenBalance(
173
+ params: BalanceRequestParams
174
+ ): Promise<BalanceResponse> {
175
+ const { tokenAddress, address } = params;
176
+
177
+ if (!address || !checkAddress(address)) {
178
+ throw new Error("Invalid address");
179
+ }
180
+
181
+ if (tokenAddress && !checkAddress(tokenAddress)) {
182
+ throw new Error("Invalid token address");
183
+ }
184
+
185
+ const tokenContractPublicKey = tokenAddress
186
+ ? PublicKey.fromBase58(tokenAddress)
187
+ : undefined;
188
+ const publicKey = PublicKey.fromBase58(address);
189
+ const tokenIdDerived = tokenContractPublicKey
190
+ ? TokenId.derive(tokenContractPublicKey)
191
+ : undefined;
192
+
193
+ if (
194
+ tokenIdDerived &&
195
+ params.tokenId &&
196
+ TokenId.toBase58(tokenIdDerived) !== params.tokenId
197
+ ) {
198
+ throw new Error("TokenId does not match tokenAddress");
199
+ }
200
+ const tokenId =
201
+ tokenIdDerived ??
202
+ (params.tokenId ? TokenId.fromBase58(params.tokenId) : undefined);
203
+
204
+ try {
205
+ await fetchMinaAccount({
206
+ publicKey,
207
+ tokenId,
208
+ force: false,
209
+ });
210
+ return {
211
+ tokenAddress,
212
+ address,
213
+ tokenId: tokenId ? TokenId.toBase58(tokenId) : undefined,
214
+ balance: Mina.hasAccount(publicKey, tokenId)
215
+ ? Number(Mina.getAccount(publicKey, tokenId).balance.toBigInt())
216
+ : null,
217
+ };
218
+ } catch (error) {
219
+ console.error("Cannot fetch account balance", params, error);
220
+
221
+ return {
222
+ tokenAddress,
223
+ address,
224
+ tokenId: tokenId ? TokenId.toBase58(tokenId) : undefined,
225
+ balance: null,
226
+ };
227
+ }
228
+ }
229
+
230
+ // export async function offerInfo(
231
+ // params: OfferInfoRequest,
232
+ // apiKeyAddress: string
233
+ // ): Promise<ApiResponse<OfferInfo>> {
234
+ // const { tokenAddress, offerAddress } = params;
235
+
236
+ // try {
237
+ // await initBlockchain();
238
+
239
+ // if (!offerAddress || !checkAddress(offerAddress)) {
240
+ // return {
241
+ // status: 400,
242
+ // json: { error: "Invalid offer address" },
243
+ // };
244
+ // }
245
+
246
+ // if (!tokenAddress || !checkAddress(tokenAddress)) {
247
+ // return {
248
+ // status: 400,
249
+ // json: { error: "Invalid token address" },
250
+ // };
251
+ // }
252
+
253
+ // const tokenContractPublicKey = PublicKey.fromBase58(tokenAddress);
254
+ // const offerPublicKey = PublicKey.fromBase58(offerAddress);
255
+ // const tokenId = TokenId.derive(tokenContractPublicKey);
256
+
257
+ // await fetchMinaAccount({
258
+ // publicKey: offerPublicKey,
259
+ // tokenId,
260
+ // force: false,
261
+ // });
262
+ // if (!Mina.hasAccount(offerPublicKey, tokenId)) {
263
+ // return {
264
+ // status: 400,
265
+ // json: { error: "Offer account not found" },
266
+ // };
267
+ // }
268
+ // const offer = new FungibleTokenOfferContract(offerPublicKey, tokenId);
269
+ // const price = Number(offer.price.get().toBigInt());
270
+ // const amount = Number(
271
+ // Mina.getAccount(offerPublicKey, tokenId).balance.toBigInt()
272
+ // );
273
+ // const ownerAddress = offer.seller.get().toBase58();
274
+ // const offerInfo = await getOffer({ tokenAddress, offerAddress });
275
+ // if (
276
+ // offerInfo === null ||
277
+ // amount !== Number(offerInfo?.amount) ||
278
+ // price !== Number(offerInfo?.price) ||
279
+ // ownerAddress !== offerInfo?.ownerAddress
280
+ // ) {
281
+ // writeOffer({
282
+ // tokenAddress,
283
+ // offerAddress,
284
+ // amount,
285
+ // price,
286
+ // ownerAddress,
287
+ // });
288
+ // }
289
+
290
+ // return {
291
+ // status: 200,
292
+ // json: {
293
+ // tokenAddress,
294
+ // offerAddress,
295
+ // ownerAddress,
296
+ // amount,
297
+ // price,
298
+ // },
299
+ // };
300
+ // } catch (error) {
301
+ // console.error("Cannot fetch offer info", params, error);
302
+ // return {
303
+ // status: 500,
304
+ // json: { error: "Failed to get offer info" },
305
+ // };
306
+ // }
307
+ // }
308
+
309
+ // export async function bidInfo(
310
+ // params: BidInfoRequest,
311
+ // apiKeyAddress: string
312
+ // ): Promise<ApiResponse<BidInfo>> {
313
+ // const { tokenAddress, bidAddress } = params;
314
+
315
+ // try {
316
+ // await initBlockchain();
317
+
318
+ // if (!bidAddress || !checkAddress(bidAddress)) {
319
+ // return {
320
+ // status: 400,
321
+ // json: { error: "Invalid bid address" },
322
+ // };
323
+ // }
324
+
325
+ // if (!tokenAddress || !checkAddress(tokenAddress)) {
326
+ // return {
327
+ // status: 400,
328
+ // json: { error: "Invalid token address" },
329
+ // };
330
+ // }
331
+
332
+ // const tokenContractPublicKey = PublicKey.fromBase58(tokenAddress);
333
+ // const bidPublicKey = PublicKey.fromBase58(bidAddress);
334
+ // const tokenId = TokenId.derive(tokenContractPublicKey);
335
+
336
+ // await fetchMinaAccount({
337
+ // publicKey: bidPublicKey,
338
+ // force: false,
339
+ // });
340
+ // if (!Mina.hasAccount(bidPublicKey)) {
341
+ // return {
342
+ // status: 400,
343
+ // json: { error: "Bid account not found" },
344
+ // };
345
+ // }
346
+ // const bid = new FungibleTokenBidContract(bidPublicKey);
347
+ // const price = Number(bid.price.get().toBigInt());
348
+ // const amount = Number(Mina.getAccount(bidPublicKey).balance.toBigInt());
349
+ // const ownerAddress = bid.buyer.get().toBase58();
350
+ // const bidInfo = await getBid({ tokenAddress, bidAddress });
351
+ // if (
352
+ // bidInfo === null ||
353
+ // amount !== Number(bidInfo?.amount) ||
354
+ // price !== Number(bidInfo?.price) ||
355
+ // ownerAddress !== bidInfo?.ownerAddress
356
+ // ) {
357
+ // writeBid({
358
+ // tokenAddress,
359
+ // bidAddress,
360
+ // amount,
361
+ // price,
362
+ // ownerAddress,
363
+ // });
364
+ // }
365
+
366
+ // return {
367
+ // status: 200,
368
+ // json: {
369
+ // tokenAddress,
370
+ // bidAddress,
371
+ // ownerAddress,
372
+ // amount,
373
+ // price,
374
+ // },
375
+ // };
376
+ // } catch (error) {
377
+ // console.error("Cannot fetch bid info", params, error);
378
+ // return {
379
+ // status: 500,
380
+ // json: { error: "Failed to get bid info" },
381
+ // };
382
+ // }
383
+ // }
384
+
385
+ // class FungibleTokenState extends Struct({
386
+ // decimals: UInt8,
387
+ // admin: PublicKey,
388
+ // paused: Bool,
389
+ // }) {}
390
+
391
+ // const FungibleTokenStateSize = FungibleTokenState.sizeInFields();
392
+
393
+ // class FungibleTokenAdminState extends Struct({
394
+ // adminPublicKey: PublicKey,
395
+ // }) {}
396
+
397
+ // const FungibleTokenAdminStateSize = FungibleTokenAdminState.sizeInFields();
398
+
399
+ // export async function getTokenState(props: {
400
+ // params: TokenInfoRequestParams;
401
+ // name: ApiName;
402
+ // apiKeyAddress: string;
403
+ // }): Promise<ApiResponse<TokenState>> {
404
+ // const { params, name, apiKeyAddress } = props;
405
+ // const { tokenAddress } = params;
406
+ // try {
407
+ // await initBlockchain();
408
+ // if (!checkAddress(tokenAddress)) {
409
+ // return {
410
+ // status: 400,
411
+ // json: { error: "Invalid token address" },
412
+ // };
413
+ // }
414
+ // const tokenContractPublicKey = PublicKey.fromBase58(tokenAddress);
415
+
416
+ // await fetchMinaAccount({ publicKey: tokenContractPublicKey, force: false });
417
+ // if (!Mina.hasAccount(tokenContractPublicKey)) {
418
+ // return {
419
+ // status: 400,
420
+ // json: { error: "Token contract account not found" },
421
+ // };
422
+ // }
423
+ // const tokenId = TokenId.derive(tokenContractPublicKey);
424
+ // await fetchMinaAccount({
425
+ // publicKey: tokenContractPublicKey,
426
+ // tokenId,
427
+ // force: false,
428
+ // });
429
+ // if (!Mina.hasAccount(tokenContractPublicKey, tokenId)) {
430
+ // console.error(
431
+ // "getTokenState: Token contract totalSupply account not found",
432
+ // {
433
+ // tokenAddress,
434
+ // }
435
+ // );
436
+ // return {
437
+ // status: 400,
438
+ // json: { error: "Token contract totalSupply account not found" },
439
+ // };
440
+ // }
441
+ // const account = Mina.getAccount(tokenContractPublicKey);
442
+ // if (account.zkapp?.appState === undefined) {
443
+ // console.error("getTokenState: Token contract state not found", {
444
+ // tokenAddress,
445
+ // });
446
+ // return {
447
+ // status: 400,
448
+ // json: { error: "Token contract state not found" },
449
+ // };
450
+ // }
451
+ // const state = FungibleTokenState.fromFields(
452
+ // account.zkapp?.appState.slice(0, FungibleTokenStateSize)
453
+ // );
454
+ // const adminContractPublicKey = state.admin;
455
+ // const decimals = state.decimals.toNumber();
456
+ // const isPaused = state.paused.toBoolean();
457
+ // const totalSupply =
458
+ // Number(Mina.getBalance(tokenContractPublicKey, tokenId).toBigInt()) /
459
+ // 1_000_000_000;
460
+ // const tokenSymbol = account.tokenSymbol;
461
+ // const uri = account.zkapp?.zkappUri;
462
+
463
+ // if (uri === undefined) {
464
+ // console.error("getTokenState: Token uri not found", {
465
+ // tokenAddress,
466
+ // });
467
+ // return {
468
+ // status: 400,
469
+ // json: { error: "Token uri not found" },
470
+ // };
471
+ // }
472
+ // const verificationKeyHash = account.zkapp?.verificationKey?.hash.toJSON();
473
+ // if (verificationKeyHash === undefined) {
474
+ // console.error("getTokenState: Token verification key hash not found", {
475
+ // tokenAddress,
476
+ // });
477
+ // return {
478
+ // status: 400,
479
+ // json: { error: "Token verification key hash not found" },
480
+ // };
481
+ // }
482
+ // const versionData = account.zkapp?.zkappVersion;
483
+ // if (versionData === undefined) {
484
+ // console.error("getTokenState: Token contract version not found", {
485
+ // tokenAddress,
486
+ // });
487
+ // return {
488
+ // status: 400,
489
+ // json: { error: "Token contract version not found" },
490
+ // };
491
+ // }
492
+ // const version = Number(versionData.toBigint());
493
+
494
+ // await fetchMinaAccount({ publicKey: adminContractPublicKey, force: false });
495
+ // if (!Mina.hasAccount(adminContractPublicKey)) {
496
+ // console.error("getTokenState: Admin contract account not found", {
497
+ // tokenAddress,
498
+ // });
499
+ // return {
500
+ // status: 400,
501
+ // json: { error: "Admin contract account not found" },
502
+ // };
503
+ // }
504
+
505
+ // const adminContract = Mina.getAccount(adminContractPublicKey);
506
+ // const adminTokenSymbol = adminContract.tokenSymbol;
507
+ // const adminUri = adminContract.zkapp?.zkappUri;
508
+
509
+ // const adminVerificationKeyHash =
510
+ // adminContract.zkapp?.verificationKey?.hash.toJSON();
511
+ // if (adminVerificationKeyHash === undefined) {
512
+ // console.error(
513
+ // "getTokenState: Admin contract verification key hash not found",
514
+ // {
515
+ // adminContractPublicKey: adminContractPublicKey.toBase58(),
516
+ // }
517
+ // );
518
+ // return {
519
+ // status: 400,
520
+ // json: { error: "Admin contract verification key hash not found" },
521
+ // };
522
+ // }
523
+ // const adminVersionData = adminContract.zkapp?.zkappVersion;
524
+ // if (adminVersionData === undefined) {
525
+ // console.error("getTokenState: Admin contract version not found", {
526
+ // adminContractPublicKey: adminContractPublicKey.toBase58(),
527
+ // });
528
+ // return {
529
+ // status: 400,
530
+ // json: { error: "Admin contract version not found" },
531
+ // };
532
+ // }
533
+ // const adminVersion = Number(adminVersionData.toBigint());
534
+ // const adminAddress0 = adminContract.zkapp?.appState[0];
535
+ // const adminAddress1 = adminContract.zkapp?.appState[1];
536
+ // if (adminAddress0 === undefined || adminAddress1 === undefined) {
537
+ // console.error("Cannot fetch admin address from admin contract");
538
+ // return {
539
+ // status: 400,
540
+ // json: { error: "Cannot fetch admin address from admin contract" },
541
+ // };
542
+ // }
543
+ // const adminAddress = PublicKey.fromFields([adminAddress0, adminAddress1]);
544
+ // let adminTokenBalance = 0;
545
+ // try {
546
+ // await fetchMinaAccount({
547
+ // publicKey: adminAddress,
548
+ // tokenId,
549
+ // force: false,
550
+ // });
551
+ // adminTokenBalance = Number(
552
+ // Mina.getBalance(adminAddress, tokenId).toBigInt()
553
+ // );
554
+ // } catch (error) {}
555
+
556
+ // const tokenState: TokenState = {
557
+ // tokenAddress: tokenContractPublicKey.toBase58(),
558
+ // tokenId: TokenId.toBase58(tokenId),
559
+ // adminContractAddress: adminContractPublicKey.toBase58(),
560
+ // adminAddress: adminAddress.toBase58(),
561
+ // adminTokenBalance,
562
+ // totalSupply,
563
+ // isPaused,
564
+ // decimals,
565
+ // tokenSymbol,
566
+ // verificationKeyHash,
567
+ // uri,
568
+ // version,
569
+ // adminTokenSymbol,
570
+ // adminUri: adminUri ?? "",
571
+ // adminVerificationKeyHash,
572
+ // adminVersion,
573
+ // };
574
+
575
+ // const updated = await updateTokenInfo({
576
+ // tokenAddress,
577
+ // tokenState,
578
+ // });
579
+ // if (updated) {
580
+ // console.log("getTokenState: Updated token info", {
581
+ // tokenAddress,
582
+ // symbol: tokenState.tokenSymbol,
583
+ // });
584
+ // }
585
+ // return {
586
+ // status: 200,
587
+ // json: tokenState,
588
+ // };
589
+ // } catch (error: any) {
590
+ // console.error("getTokenState catch", error);
591
+ // return {
592
+ // status: 503,
593
+ // json: {
594
+ // error:
595
+ // "getTokenState error:" +
596
+ // (error?.message ?? (error ? String(error) : "unknown error")),
597
+ // },
598
+ // };
599
+ // }
600
+ // }
package/src/types.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * blockchain is the type for the chain ID.
3
+ */
4
+ export type blockchain = "local" | "devnet" | "lightnet" | "mainnet" | "zeko";