zkcloudworker 0.18.8 → 0.18.10

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 (69) hide show
  1. package/dist/ts/mina/token/FungibleTokenAdmin.d.ts +1 -6
  2. package/dist/ts/mina/token/FungibleTokenAdmin.js.map +1 -1
  3. package/dist/ts/mina/token/FungibleTokenContract.d.ts +520 -0
  4. package/dist/ts/mina/token/FungibleTokenContract.js +290 -0
  5. package/dist/ts/mina/token/FungibleTokenContract.js.map +1 -0
  6. package/dist/ts/mina/token/FungibleTokenWhitelistedAdmin.d.ts +33 -0
  7. package/dist/ts/mina/token/FungibleTokenWhitelistedAdmin.js +119 -0
  8. package/dist/ts/mina/token/FungibleTokenWhitelistedAdmin.js.map +1 -0
  9. package/dist/ts/mina/token/api.d.ts +2 -2
  10. package/dist/ts/mina/token/api.js.map +1 -1
  11. package/dist/ts/mina/token/bid.d.ts +24 -0
  12. package/dist/ts/mina/token/bid.js +167 -0
  13. package/dist/ts/mina/token/bid.js.map +1 -0
  14. package/dist/ts/mina/token/build.d.ts +64 -0
  15. package/dist/ts/mina/token/build.js +437 -0
  16. package/dist/ts/mina/token/build.js.map +1 -0
  17. package/dist/ts/mina/token/fee.d.ts +2 -0
  18. package/dist/ts/mina/token/fee.js +3 -0
  19. package/dist/ts/mina/token/fee.js.map +1 -0
  20. package/dist/ts/mina/token/index.d.ts +5 -2
  21. package/dist/ts/mina/token/index.js +5 -2
  22. package/dist/ts/mina/token/index.js.map +1 -1
  23. package/dist/ts/mina/token/offer.d.ts +25 -0
  24. package/dist/ts/mina/token/offer.js +175 -0
  25. package/dist/ts/mina/token/offer.js.map +1 -0
  26. package/dist/ts/mina/token/token.d.ts +426 -0
  27. package/dist/ts/mina/token/token.js +87 -0
  28. package/dist/ts/mina/token/token.js.map +1 -0
  29. package/dist/tsconfig.web.tsbuildinfo +1 -1
  30. package/dist/web/mina/token/FungibleTokenAdmin.d.ts +1 -6
  31. package/dist/web/mina/token/FungibleTokenAdmin.js.map +1 -1
  32. package/dist/web/mina/token/FungibleTokenContract.d.ts +520 -0
  33. package/dist/web/mina/token/FungibleTokenContract.js +290 -0
  34. package/dist/web/mina/token/FungibleTokenContract.js.map +1 -0
  35. package/dist/web/mina/token/FungibleTokenWhitelistedAdmin.d.ts +33 -0
  36. package/dist/web/mina/token/FungibleTokenWhitelistedAdmin.js +119 -0
  37. package/dist/web/mina/token/FungibleTokenWhitelistedAdmin.js.map +1 -0
  38. package/dist/web/mina/token/api.d.ts +2 -2
  39. package/dist/web/mina/token/api.js.map +1 -1
  40. package/dist/web/mina/token/bid.d.ts +24 -0
  41. package/dist/web/mina/token/bid.js +167 -0
  42. package/dist/web/mina/token/bid.js.map +1 -0
  43. package/dist/web/mina/token/build.d.ts +64 -0
  44. package/dist/web/mina/token/build.js +437 -0
  45. package/dist/web/mina/token/build.js.map +1 -0
  46. package/dist/web/mina/token/fee.d.ts +2 -0
  47. package/dist/web/mina/token/fee.js +3 -0
  48. package/dist/web/mina/token/fee.js.map +1 -0
  49. package/dist/web/mina/token/index.d.ts +5 -2
  50. package/dist/web/mina/token/index.js +5 -2
  51. package/dist/web/mina/token/index.js.map +1 -1
  52. package/dist/web/mina/token/offer.d.ts +25 -0
  53. package/dist/web/mina/token/offer.js +175 -0
  54. package/dist/web/mina/token/offer.js.map +1 -0
  55. package/dist/web/mina/token/token.d.ts +426 -0
  56. package/dist/web/mina/token/token.js +87 -0
  57. package/dist/web/mina/token/token.js.map +1 -0
  58. package/package.json +1 -1
  59. package/src/mina/token/FungibleTokenAdmin.ts +1 -7
  60. package/src/mina/token/FungibleTokenContract.ts +327 -0
  61. package/src/mina/token/FungibleTokenWhitelistedAdmin.ts +112 -0
  62. package/src/mina/token/api.ts +5 -2
  63. package/src/mina/token/bid.ts +173 -0
  64. package/src/mina/token/build.ts +610 -0
  65. package/src/mina/token/fee.ts +2 -0
  66. package/src/mina/token/index.ts +5 -2
  67. package/src/mina/token/offer.ts +174 -0
  68. package/src/mina/token/token.ts +127 -0
  69. package/src/mina/token/FungibleToken.ts +0 -316
@@ -0,0 +1,327 @@
1
+ import {
2
+ AccountUpdate,
3
+ AccountUpdateForest,
4
+ assert,
5
+ Bool,
6
+ DeployArgs,
7
+ Field,
8
+ Int64,
9
+ method,
10
+ Permissions,
11
+ Provable,
12
+ PublicKey,
13
+ SmartContract,
14
+ State,
15
+ state,
16
+ Struct,
17
+ TokenContract,
18
+ Types,
19
+ UInt64,
20
+ UInt8,
21
+ VerificationKey,
22
+ } from "o1js";
23
+
24
+ export type FungibleTokenAdminBase = SmartContract & {
25
+ canMint(accountUpdate: AccountUpdate): Promise<Bool>;
26
+ canChangeAdmin(admin: PublicKey): Promise<Bool>;
27
+ canPause(): Promise<Bool>;
28
+ canResume(): Promise<Bool>;
29
+ };
30
+
31
+ export type FungibleTokenAdminConstructor = new (
32
+ adminPublicKey: PublicKey
33
+ ) => FungibleTokenAdminBase;
34
+
35
+ export interface FungibleTokenDeployProps
36
+ extends Exclude<DeployArgs, undefined> {
37
+ /** The token symbol. */
38
+ symbol: string;
39
+ /** A source code reference, which is placed within the `zkappUri` of the contract account.
40
+ * Typically a link to a file on github. */
41
+ src: string;
42
+ }
43
+
44
+ export const FungibleTokenErrors = {
45
+ noAdminKey: "could not fetch admin contract key",
46
+ noPermissionToChangeAdmin: "Not allowed to change admin contract",
47
+ tokenPaused: "Token is currently paused",
48
+ noPermissionToMint: "Not allowed to mint tokens",
49
+ noPermissionToPause: "Not allowed to pause token",
50
+ noPermissionToResume: "Not allowed to resume token",
51
+ noTransferFromCirculation: "Can't transfer to/from the circulation account",
52
+ noPermissionChangeAllowed:
53
+ "Can't change permissions for access or receive on token accounts",
54
+ flashMinting:
55
+ "Flash-minting or unbalanced transaction detected. Please make sure that your transaction is balanced, and that your `AccountUpdate`s are ordered properly, so that tokens are not received before they are sent.",
56
+ unbalancedTransaction: "Transaction is unbalanced",
57
+ };
58
+
59
+ export function FungibleTokenContract(
60
+ adminContract: FungibleTokenAdminConstructor
61
+ ) {
62
+ class FungibleToken extends TokenContract {
63
+ @state(UInt8)
64
+ decimals = State<UInt8>();
65
+ @state(PublicKey)
66
+ admin = State<PublicKey>();
67
+ @state(Bool)
68
+ paused = State<Bool>();
69
+
70
+ readonly events = {
71
+ SetAdmin: SetAdminEvent,
72
+ Pause: PauseEvent,
73
+ Mint: MintEvent,
74
+ Burn: BurnEvent,
75
+ BalanceChange: BalanceChangeEvent,
76
+ };
77
+
78
+ async deploy(props: FungibleTokenDeployProps) {
79
+ await super.deploy(props);
80
+ this.paused.set(Bool(true));
81
+ this.account.zkappUri.set(props.src);
82
+ this.account.tokenSymbol.set(props.symbol);
83
+
84
+ this.account.permissions.set({
85
+ ...Permissions.default(),
86
+ setVerificationKey:
87
+ Permissions.VerificationKey.impossibleDuringCurrentVersion(),
88
+ setPermissions: Permissions.impossible(),
89
+ access: Permissions.proof(),
90
+ });
91
+ }
92
+
93
+ /** Update the verification key.
94
+ * Note that because we have set the permissions for setting the verification key to `impossibleDuringCurrentVersion()`, this will only be possible in case of a protocol update that requires an update.
95
+ */
96
+ @method
97
+ async updateVerificationKey(vk: VerificationKey) {
98
+ this.account.verificationKey.set(vk);
99
+ }
100
+
101
+ /** Initializes the account for tracking total circulation.
102
+ * @argument {PublicKey} admin - public key where the admin contract is deployed
103
+ * @argument {UInt8} decimals - number of decimals for the token
104
+ * @argument {Bool} startPaused - if set to `Bool(true), the contract will start in a mode where token minting and transfers are paused. This should be used for non-atomic deployments
105
+ */
106
+ @method
107
+ async initialize(admin: PublicKey, decimals: UInt8, startPaused: Bool) {
108
+ this.account.provedState.requireEquals(Bool(false));
109
+
110
+ this.admin.set(admin);
111
+ this.decimals.set(decimals);
112
+ this.paused.set(Bool(false));
113
+
114
+ this.paused.set(startPaused);
115
+
116
+ const accountUpdate = AccountUpdate.createSigned(
117
+ this.address,
118
+ this.deriveTokenId()
119
+ );
120
+ let permissions = Permissions.default();
121
+ // This is necessary in order to allow token holders to burn.
122
+ permissions.send = Permissions.none();
123
+ permissions.setPermissions = Permissions.impossible();
124
+ accountUpdate.account.permissions.set(permissions);
125
+ }
126
+
127
+ public async getAdminContract(): Promise<FungibleTokenAdminBase> {
128
+ const admin = await Provable.witnessAsync(PublicKey, async () => {
129
+ let pk = await this.admin.fetch();
130
+ assert(pk !== undefined, FungibleTokenErrors.noAdminKey);
131
+ return pk;
132
+ });
133
+ this.admin.requireEquals(admin);
134
+ return new adminContract(admin);
135
+ }
136
+
137
+ @method
138
+ async setAdmin(admin: PublicKey) {
139
+ const adminContract = await this.getAdminContract();
140
+ const canChangeAdmin = await adminContract.canChangeAdmin(admin);
141
+ canChangeAdmin.assertTrue(FungibleTokenErrors.noPermissionToChangeAdmin);
142
+ this.admin.set(admin);
143
+ this.emitEvent("SetAdmin", new SetAdminEvent({ adminKey: admin }));
144
+ }
145
+
146
+ @method.returns(AccountUpdate)
147
+ async mint(recipient: PublicKey, amount: UInt64): Promise<AccountUpdate> {
148
+ this.paused
149
+ .getAndRequireEquals()
150
+ .assertFalse(FungibleTokenErrors.tokenPaused);
151
+ const accountUpdate = this.internal.mint({ address: recipient, amount });
152
+ const adminContract = await this.getAdminContract();
153
+ const canMint = await adminContract.canMint(accountUpdate);
154
+ canMint.assertTrue(FungibleTokenErrors.noPermissionToMint);
155
+ recipient
156
+ .equals(this.address)
157
+ .assertFalse(FungibleTokenErrors.noTransferFromCirculation);
158
+ this.approve(accountUpdate);
159
+ this.emitEvent("Mint", new MintEvent({ recipient, amount }));
160
+ const circulationUpdate = AccountUpdate.create(
161
+ this.address,
162
+ this.deriveTokenId()
163
+ );
164
+ circulationUpdate.balanceChange = Int64.fromUnsigned(amount);
165
+ return accountUpdate;
166
+ }
167
+
168
+ @method.returns(AccountUpdate)
169
+ async burn(from: PublicKey, amount: UInt64): Promise<AccountUpdate> {
170
+ this.paused
171
+ .getAndRequireEquals()
172
+ .assertFalse(FungibleTokenErrors.tokenPaused);
173
+ const accountUpdate = this.internal.burn({ address: from, amount });
174
+ const circulationUpdate = AccountUpdate.create(
175
+ this.address,
176
+ this.deriveTokenId()
177
+ );
178
+ from
179
+ .equals(this.address)
180
+ .assertFalse(FungibleTokenErrors.noTransferFromCirculation);
181
+ circulationUpdate.balanceChange = Int64.fromUnsigned(amount).neg();
182
+ this.emitEvent("Burn", new BurnEvent({ from, amount }));
183
+ return accountUpdate;
184
+ }
185
+
186
+ @method
187
+ async pause() {
188
+ const adminContract = await this.getAdminContract();
189
+ const canPause = await adminContract.canPause();
190
+ canPause.assertTrue(FungibleTokenErrors.noPermissionToPause);
191
+ this.paused.set(Bool(true));
192
+ this.emitEvent("Pause", new PauseEvent({ isPaused: Bool(true) }));
193
+ }
194
+
195
+ @method
196
+ async resume() {
197
+ const adminContract = await this.getAdminContract();
198
+ const canResume = await adminContract.canResume();
199
+ canResume.assertTrue(FungibleTokenErrors.noPermissionToResume);
200
+ this.paused.set(Bool(false));
201
+ this.emitEvent("Pause", new PauseEvent({ isPaused: Bool(false) }));
202
+ }
203
+
204
+ @method
205
+ async transfer(from: PublicKey, to: PublicKey, amount: UInt64) {
206
+ this.paused
207
+ .getAndRequireEquals()
208
+ .assertFalse(FungibleTokenErrors.tokenPaused);
209
+ from
210
+ .equals(this.address)
211
+ .assertFalse(FungibleTokenErrors.noTransferFromCirculation);
212
+ to.equals(this.address).assertFalse(
213
+ FungibleTokenErrors.noTransferFromCirculation
214
+ );
215
+ this.internal.send({ from, to, amount });
216
+ }
217
+
218
+ checkPermissionsUpdate(update: AccountUpdate) {
219
+ let permissions = update.update.permissions;
220
+
221
+ let { access, receive } = permissions.value;
222
+ let accessIsNone = Provable.equal(
223
+ Types.AuthRequired,
224
+ access,
225
+ Permissions.none()
226
+ );
227
+ let receiveIsNone = Provable.equal(
228
+ Types.AuthRequired,
229
+ receive,
230
+ Permissions.none()
231
+ );
232
+ let updateAllowed = accessIsNone.and(receiveIsNone);
233
+
234
+ assert(
235
+ updateAllowed.or(permissions.isSome.not()),
236
+ FungibleTokenErrors.noPermissionChangeAllowed
237
+ );
238
+ }
239
+
240
+ /** Approve `AccountUpdate`s that have been created outside of the token contract.
241
+ *
242
+ * @argument {AccountUpdateForest} updates - The `AccountUpdate`s to approve. Note that the forest size is limited by the base token contract, @see TokenContract.MAX_ACCOUNT_UPDATES The current limit is 9.
243
+ */
244
+ @method
245
+ async approveBase(updates: AccountUpdateForest): Promise<void> {
246
+ this.paused
247
+ .getAndRequireEquals()
248
+ .assertFalse(FungibleTokenErrors.tokenPaused);
249
+ let totalBalance = Int64.from(0);
250
+ this.forEachUpdate(updates, (update, usesToken) => {
251
+ // Make sure that the account permissions are not changed
252
+ this.checkPermissionsUpdate(update);
253
+ this.emitEventIf(
254
+ usesToken,
255
+ "BalanceChange",
256
+ new BalanceChangeEvent({
257
+ address: update.publicKey,
258
+ amount: update.balanceChange,
259
+ })
260
+ );
261
+ // Don't allow transfers to/from the account that's tracking circulation
262
+ update.publicKey
263
+ .equals(this.address)
264
+ .and(usesToken)
265
+ .assertFalse(FungibleTokenErrors.noTransferFromCirculation);
266
+ totalBalance = Provable.if(
267
+ usesToken,
268
+ totalBalance.add(update.balanceChange),
269
+ totalBalance
270
+ );
271
+ totalBalance.isPositive().assertFalse(FungibleTokenErrors.flashMinting);
272
+ });
273
+ totalBalance.assertEquals(
274
+ Int64.zero,
275
+ FungibleTokenErrors.unbalancedTransaction
276
+ );
277
+ }
278
+
279
+ @method.returns(UInt64)
280
+ async getBalanceOf(address: PublicKey): Promise<UInt64> {
281
+ const account = AccountUpdate.create(
282
+ address,
283
+ this.deriveTokenId()
284
+ ).account;
285
+ const balance = account.balance.get();
286
+ account.balance.requireEquals(balance);
287
+ return balance;
288
+ }
289
+
290
+ /** Reports the current circulating supply
291
+ * This does take into account currently unreduced actions.
292
+ */
293
+ async getCirculating(): Promise<UInt64> {
294
+ let circulating = await this.getBalanceOf(this.address);
295
+ return circulating;
296
+ }
297
+
298
+ @method.returns(UInt8)
299
+ async getDecimals(): Promise<UInt8> {
300
+ return this.decimals.getAndRequireEquals();
301
+ }
302
+ }
303
+ return FungibleToken;
304
+ }
305
+
306
+ export class SetAdminEvent extends Struct({
307
+ adminKey: PublicKey,
308
+ }) {}
309
+
310
+ export class PauseEvent extends Struct({
311
+ isPaused: Bool,
312
+ }) {}
313
+
314
+ export class MintEvent extends Struct({
315
+ recipient: PublicKey,
316
+ amount: UInt64,
317
+ }) {}
318
+
319
+ export class BurnEvent extends Struct({
320
+ from: PublicKey,
321
+ amount: UInt64,
322
+ }) {}
323
+
324
+ export class BalanceChangeEvent extends Struct({
325
+ address: PublicKey,
326
+ amount: Int64,
327
+ }) {}
@@ -0,0 +1,112 @@
1
+ import {
2
+ AccountUpdate,
3
+ assert,
4
+ Bool,
5
+ DeployArgs,
6
+ method,
7
+ Permissions,
8
+ Provable,
9
+ PublicKey,
10
+ SmartContract,
11
+ State,
12
+ state,
13
+ UInt64,
14
+ VerificationKey,
15
+ } from "o1js";
16
+ import { Whitelist } from "./whitelist.js";
17
+ import { FungibleTokenAdminBase } from "./FungibleTokenContract.js";
18
+
19
+ export interface FungibleTokenWhitelistedAdminDeployProps
20
+ extends Exclude<DeployArgs, undefined> {
21
+ adminPublicKey: PublicKey;
22
+ whitelist: Whitelist;
23
+ }
24
+
25
+ /** A contract that grants permissions for administrative actions on a token.
26
+ *
27
+ * We separate this out into a dedicated contract. That way, when issuing a token, a user can
28
+ * specify their own rules for administrative actions, without changing the token contract itself.
29
+ *
30
+ * The advantage is that third party applications that only use the token in a non-privileged way
31
+ * can integrate against the unchanged token contract.
32
+ */
33
+ export class FungibleTokenWhitelistedAdmin
34
+ extends SmartContract
35
+ implements FungibleTokenAdminBase
36
+ {
37
+ @state(PublicKey) adminPublicKey = State<PublicKey>();
38
+ @state(Whitelist) whitelist = State<Whitelist>();
39
+
40
+ async deploy(props: FungibleTokenWhitelistedAdminDeployProps) {
41
+ await super.deploy(props);
42
+ this.adminPublicKey.set(props.adminPublicKey);
43
+ this.whitelist.set(props.whitelist);
44
+ this.account.permissions.set({
45
+ ...Permissions.default(),
46
+ setVerificationKey:
47
+ Permissions.VerificationKey.impossibleDuringCurrentVersion(),
48
+ setPermissions: Permissions.impossible(),
49
+ });
50
+ }
51
+
52
+ events = { updateWhitelist: Whitelist };
53
+
54
+ /** Update the verification key.
55
+ * Note that because we have set the permissions for setting the verification key to `impossibleDuringCurrentVersion()`, this will only be possible in case of a protocol update that requires an update.
56
+ */
57
+ @method
58
+ async updateVerificationKey(vk: VerificationKey) {
59
+ this.account.verificationKey.set(vk);
60
+ }
61
+
62
+ private async ensureAdminSignature() {
63
+ const admin = await Provable.witnessAsync(PublicKey, async () => {
64
+ let pk = await this.adminPublicKey.fetch();
65
+ assert(pk !== undefined, "could not fetch admin public key");
66
+ return pk;
67
+ });
68
+ this.adminPublicKey.requireEquals(admin);
69
+ return AccountUpdate.createSigned(admin);
70
+ }
71
+
72
+ @method.returns(Bool)
73
+ public async canMint(_accountUpdate: AccountUpdate) {
74
+ const address = _accountUpdate.body.publicKey;
75
+ const balanceChange = _accountUpdate.body.balanceChange;
76
+ balanceChange.isPositive().assertTrue();
77
+ const whitelist = this.whitelist.getAndRequireEquals();
78
+ const whitelistedAmount = await whitelist.getWhitelistedAmount(address);
79
+ return balanceChange.magnitude.lessThanOrEqual(
80
+ whitelistedAmount.orElse(UInt64.from(0)) // here can be a minimum amount allowed by travel rule instead of 0
81
+ );
82
+ }
83
+
84
+ @method.returns(Bool)
85
+ public async canChangeAdmin(_admin: PublicKey) {
86
+ await this.ensureAdminSignature();
87
+ return Bool(true);
88
+ }
89
+
90
+ @method.returns(Bool)
91
+ public async canPause(): Promise<Bool> {
92
+ await this.ensureAdminSignature();
93
+ return Bool(true);
94
+ }
95
+
96
+ @method.returns(Bool)
97
+ public async canResume(): Promise<Bool> {
98
+ await this.ensureAdminSignature();
99
+ return Bool(true);
100
+ }
101
+
102
+ @method async updateWhitelist(whitelist: Whitelist) {
103
+ const admin = this.adminPublicKey.getAndRequireEquals();
104
+ const sender = this.sender.getUnconstrained();
105
+ const senderUpdate = AccountUpdate.createSigned(sender);
106
+ senderUpdate.body.useFullCommitment = Bool(true);
107
+ admin.assertEquals(sender);
108
+
109
+ this.whitelist.set(whitelist);
110
+ this.emitEvent("updateWhitelist", whitelist);
111
+ }
112
+ }
@@ -26,7 +26,10 @@ export type FungibleTokenTransactionType =
26
26
  | "buy"
27
27
  | "sell"
28
28
  | "withdrawBid"
29
- | "withdrawOffer";
29
+ | "withdrawOffer"
30
+ | "whitelistBid"
31
+ | "whitelistOffer"
32
+ | "whitelistAdmin";
30
33
  export interface FungibleTokenTransactionParams {
31
34
  txType: FungibleTokenTransactionType;
32
35
  tokenAddress: string;
@@ -35,7 +38,7 @@ export interface FungibleTokenTransactionParams {
35
38
  signedData: string;
36
39
  from: string;
37
40
  to: string;
38
- amount: number;
41
+ amount?: number;
39
42
  price?: number;
40
43
  whitelist?: { address: string; amount?: number }[];
41
44
  sendTransaction: boolean;
@@ -0,0 +1,173 @@
1
+ import {
2
+ AccountUpdate,
3
+ DeployArgs,
4
+ method,
5
+ Permissions,
6
+ PublicKey,
7
+ State,
8
+ state,
9
+ UInt64,
10
+ SmartContract,
11
+ Bool,
12
+ Field,
13
+ assert,
14
+ Mina,
15
+ Struct,
16
+ } from "o1js";
17
+ import { Whitelist } from "./whitelist.js";
18
+ import { FungibleToken, tokenVerificationKeys } from "./token.js";
19
+
20
+ export interface FungibleTokenBidContractDeployProps
21
+ extends Exclude<DeployArgs, undefined> {
22
+ /** The whitelist. */
23
+ whitelist: Whitelist;
24
+ }
25
+ export class FungibleTokenBidContract extends SmartContract {
26
+ @state(UInt64) price = State<UInt64>();
27
+ @state(PublicKey) buyer = State<PublicKey>();
28
+ @state(PublicKey) token = State<PublicKey>();
29
+ @state(Whitelist) whitelist = State<Whitelist>();
30
+
31
+ async deploy(args: FungibleTokenBidContractDeployProps) {
32
+ await super.deploy(args);
33
+ const verificationKey =
34
+ args?.verificationKey ?? FungibleTokenBidContract._verificationKey;
35
+ assert(verificationKey !== undefined);
36
+ const hash =
37
+ typeof verificationKey.hash === "string"
38
+ ? verificationKey.hash
39
+ : verificationKey.hash.toJSON();
40
+ const networkId = Mina.getNetworkId();
41
+ assert(networkId === "mainnet" || networkId === "testnet");
42
+ assert(
43
+ hash === tokenVerificationKeys[networkId].vk.FungibleTokenBidContract.hash
44
+ );
45
+ assert(
46
+ verificationKey.data ===
47
+ tokenVerificationKeys[networkId].vk.FungibleTokenBidContract.data
48
+ );
49
+ this.whitelist.set(args.whitelist);
50
+ this.account.permissions.set({
51
+ ...Permissions.default(),
52
+ send: Permissions.proof(),
53
+ setVerificationKey:
54
+ Permissions.VerificationKey.impossibleDuringCurrentVersion(),
55
+ setPermissions: Permissions.impossible(),
56
+ });
57
+ }
58
+
59
+ events = {
60
+ bid: UInt64,
61
+ withdraw: UInt64,
62
+ sell: UInt64,
63
+ updateWhitelist: Whitelist,
64
+ };
65
+
66
+ @method async initialize(token: PublicKey, amount: UInt64, price: UInt64) {
67
+ this.account.provedState.requireEquals(Bool(false));
68
+ amount.equals(UInt64.from(0)).assertFalse();
69
+
70
+ const totalPriceField = price.value
71
+ .mul(amount.value)
72
+ .div(Field(1_000_000_000));
73
+ totalPriceField.assertLessThan(
74
+ UInt64.MAXINT().value,
75
+ "totalPrice overflow"
76
+ );
77
+ const totalPrice = UInt64.Unsafe.fromField(totalPriceField);
78
+
79
+ const buyer = this.sender.getUnconstrained();
80
+ const buyerUpdate = AccountUpdate.createSigned(buyer);
81
+ buyerUpdate.send({ to: this.address, amount: totalPrice });
82
+ buyerUpdate.body.useFullCommitment = Bool(true);
83
+
84
+ this.buyer.set(buyer);
85
+ this.price.set(price);
86
+ this.token.set(token);
87
+ this.emitEvent("bid", amount);
88
+ }
89
+
90
+ @method async bid(amount: UInt64, price: UInt64) {
91
+ amount.equals(UInt64.from(0)).assertFalse();
92
+
93
+ const balance = this.account.balance.getAndRequireEquals();
94
+ const oldPrice = this.price.getAndRequireEquals();
95
+ // Price can be changed only when the balance is 0
96
+ price
97
+ .equals(oldPrice)
98
+ .or(balance.equals(UInt64.from(0)))
99
+ .assertTrue();
100
+ this.price.set(price);
101
+
102
+ const totalPriceField = price.value
103
+ .mul(amount.value)
104
+ .div(Field(1_000_000_000));
105
+ totalPriceField.assertLessThan(
106
+ UInt64.MAXINT().value,
107
+ "totalPrice overflow"
108
+ );
109
+ const totalPrice = UInt64.Unsafe.fromField(totalPriceField);
110
+
111
+ const sender = this.sender.getUnconstrained();
112
+ const buyer = this.buyer.getAndRequireEquals();
113
+ sender.assertEquals(buyer);
114
+ const buyerUpdate = AccountUpdate.createSigned(buyer);
115
+ buyerUpdate.send({ to: this.address, amount: totalPrice });
116
+ buyerUpdate.body.useFullCommitment = Bool(true);
117
+
118
+ this.price.set(price);
119
+ this.emitEvent("bid", amount);
120
+ }
121
+
122
+ @method async withdraw(amountInMina: UInt64) {
123
+ amountInMina.equals(UInt64.from(0)).assertFalse();
124
+ this.account.balance.requireBetween(amountInMina, UInt64.MAXINT());
125
+
126
+ const buyer = this.buyer.getAndRequireEquals();
127
+ const sender = this.sender.getUnconstrained();
128
+ const senderUpdate = AccountUpdate.createSigned(sender);
129
+ senderUpdate.body.useFullCommitment = Bool(true);
130
+ sender.assertEquals(buyer);
131
+
132
+ let bidUpdate = this.send({ to: senderUpdate, amount: amountInMina });
133
+ bidUpdate.body.useFullCommitment = Bool(true);
134
+ this.emitEvent("withdraw", amountInMina);
135
+ }
136
+
137
+ @method async sell(amount: UInt64) {
138
+ amount.equals(UInt64.from(0)).assertFalse();
139
+ const price = this.price.getAndRequireEquals();
140
+ const totalPriceField = price.value
141
+ .mul(amount.value)
142
+ .div(Field(1_000_000_000));
143
+ totalPriceField.assertLessThan(
144
+ UInt64.MAXINT().value,
145
+ "totalPrice overflow"
146
+ );
147
+ const totalPrice = UInt64.Unsafe.fromField(totalPriceField);
148
+
149
+ this.account.balance.requireBetween(totalPrice, UInt64.MAXINT());
150
+ const buyer = this.buyer.getAndRequireEquals();
151
+ const token = this.token.getAndRequireEquals();
152
+
153
+ const seller = this.sender.getUnconstrained();
154
+ const sellerUpdate = this.send({ to: seller, amount: totalPrice });
155
+ sellerUpdate.body.useFullCommitment = Bool(true);
156
+ sellerUpdate.requireSignature();
157
+
158
+ const tokenContract = new FungibleToken(token);
159
+ await tokenContract.transfer(seller, buyer, amount);
160
+ this.emitEvent("sell", amount);
161
+ }
162
+
163
+ @method async updateWhitelist(whitelist: Whitelist) {
164
+ const buyer = this.buyer.getAndRequireEquals();
165
+ const sender = this.sender.getUnconstrained();
166
+ const senderUpdate = AccountUpdate.createSigned(sender);
167
+ senderUpdate.body.useFullCommitment = Bool(true);
168
+ sender.assertEquals(buyer);
169
+
170
+ this.whitelist.set(whitelist);
171
+ this.emitEvent("updateWhitelist", whitelist);
172
+ }
173
+ }