zkcloudworker 0.18.28 → 0.19.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.
@@ -1,112 +0,0 @@
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
- }
@@ -1,173 +0,0 @@
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
- }