-
Notifications
You must be signed in to change notification settings - Fork 2
/
Token.ts
128 lines (103 loc) · 4.49 KB
/
Token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
AccountUpdate,
DeployArgs,
Experimental,
Field,
Int64,
Permissions,
PublicKey,
SmartContract,
State,
UInt64,
method,
state,
} from 'o1js'
export class Token extends SmartContract {
@state(Field) symbol = State<Field>()
@state(UInt64) decimals = State<UInt64>()
@state(UInt64) maxSupply = State<UInt64>()
@state(UInt64) circulatingSupply = State<UInt64>()
@state(PublicKey) owner = State<PublicKey>()
deploy(args?: DeployArgs) {
super.deploy(args)
this.account.permissions.set({
...Permissions.default(),
access: Permissions.proofOrSignature(),
})
}
@method initialize(symbol: Field, decimals: UInt64, maxSupply: UInt64) {
this.symbol.set(symbol)
this.decimals.set(decimals)
this.maxSupply.set(maxSupply)
this.owner.set(this.sender)
}
@method mint(receiver: PublicKey, amount: UInt64) {
this.owner.getAndRequireEquals().assertEquals(this.sender)
const maxSupply = this.maxSupply.getAndRequireEquals()
const circulatingSupply = this.circulatingSupply.getAndRequireEquals()
const newCirculatingSupply = circulatingSupply.add(amount)
newCirculatingSupply.assertLessThanOrEqual(maxSupply)
this.token.mint({
address: receiver,
amount,
})
this.circulatingSupply.set(newCirculatingSupply)
}
@method burn(burner: PublicKey, amount: UInt64) {
const circulatingSupply = this.circulatingSupply.getAndRequireEquals()
const newCirculatingSupply = circulatingSupply.sub(amount)
this.token.burn({
address: burner,
amount,
})
this.circulatingSupply.set(newCirculatingSupply)
}
@method transfer(sender: PublicKey, receiver: PublicKey, amount: UInt64) {
this.token.send({ from: sender, to: receiver, amount })
}
@method approveCallbackAndTransfer(
sender: PublicKey,
receiver: PublicKey,
amount: UInt64,
callback: Experimental.Callback<any>
) {
const tokenId = this.token.id
const senderAccountUpdate = this.approve(callback, AccountUpdate.Layout.AnyChildren)
senderAccountUpdate.body.tokenId.assertEquals(tokenId)
senderAccountUpdate.body.publicKey.assertEquals(sender)
const negativeAmount = Int64.fromObject(senderAccountUpdate.body.balanceChange)
negativeAmount.assertEquals(Int64.from(amount).neg())
const receiverAccountUpdate = Experimental.createChildAccountUpdate(this.self, receiver, tokenId)
receiverAccountUpdate.balance.addInPlace(amount)
}
@method approveUpdateAndTransfer(zkappUpdate: AccountUpdate, receiver: PublicKey, amount: UInt64) {
// TODO: THIS IS INSECURE. The proper version has a prover error (compile != prove) that must be fixed
this.approve(zkappUpdate, AccountUpdate.Layout.AnyChildren)
// THIS IS HOW IT SHOULD BE DONE:
// // approve a layout of two grandchildren, both of which can't inherit the token permission
// let { StaticChildren, AnyChildren } = AccountUpdate.Layout;
// this.approve(zkappUpdate, StaticChildren(AnyChildren, AnyChildren));
// zkappUpdate.body.mayUseToken.parentsOwnToken.assertTrue();
// let [grandchild1, grandchild2] = zkappUpdate.children.accountUpdates;
// grandchild1.body.mayUseToken.inheritFromParent.assertFalse();
// grandchild2.body.mayUseToken.inheritFromParent.assertFalse();
// see if balance change cancels the amount sent
const balanceChange = Int64.fromObject(zkappUpdate.body.balanceChange)
balanceChange.assertEquals(Int64.from(amount).neg())
const receiverAccountUpdate = Experimental.createChildAccountUpdate(this.self, receiver, this.token.id)
receiverAccountUpdate.balance.addInPlace(amount)
}
@method approveUpdate(zkappUpdate: AccountUpdate) {
this.approve(zkappUpdate)
const balanceChange = Int64.fromObject(zkappUpdate.body.balanceChange)
balanceChange.assertEquals(Int64.from(0))
}
// Instead, use `approveUpdate` method.
// @method deployZkapp(address: PublicKey, verificationKey: VerificationKey) {
// let tokenId = this.token.id
// let zkapp = AccountUpdate.create(address, tokenId)
// zkapp.account.permissions.set(Permissions.default())
// zkapp.account.verificationKey.set(verificationKey)
// zkapp.requireSignature()
// }
}