-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathToken.test.ts
103 lines (91 loc) · 4.01 KB
/
Token.test.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
import { AccountUpdate, Encoding, Field, Mina, PrivateKey, UInt64 } from 'o1js'
import { Token } from './Token'
const proofsEnabled = false
describe('token test', () => {
const Local = Mina.LocalBlockchain({ proofsEnabled })
Mina.setActiveInstance(Local)
const userPrivkey = Local.testAccounts[0].privateKey
const userPubkey = Local.testAccounts[0].publicKey
const tokenPrivkey = PrivateKey.random()
const tokenPubkey = tokenPrivkey.toPublicKey()
const tokenZkapp = new Token(tokenPubkey)
const SYMBOL = Encoding.stringToFields('BTC')[0]
const DECIMALS = UInt64.from(9)
const SUPPLY_MAX = UInt64.from(21_000_000_000_000_000n)
const AMOUNT_MINT = UInt64.from(18_000_000_000_000_000n)
const AMOUNT_TRANSFER = UInt64.from(7_000_000_000_000_000n)
beforeAll(async () => {
if (proofsEnabled) {
await Token.compile()
}
})
it('can deploy tokens', async () => {
const tx = await Mina.transaction(userPubkey, () => {
AccountUpdate.fundNewAccount(userPubkey)
tokenZkapp.deploy()
})
await tx.prove()
tx.sign([userPrivkey, tokenPrivkey])
await tx.send()
tokenZkapp.symbol.getAndRequireEquals().assertEquals(Field(0))
tokenZkapp.decimals.getAndRequireEquals().assertEquals(UInt64.zero)
tokenZkapp.maxSupply.getAndRequireEquals().assertEquals(UInt64.zero)
tokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(UInt64.zero)
})
it('can initialize tokens', async () => {
const tx = await Mina.transaction(userPubkey, () => {
tokenZkapp.initialize(SYMBOL, DECIMALS, SUPPLY_MAX)
})
await tx.prove()
tx.sign([userPrivkey, tokenPrivkey])
await tx.send()
tokenZkapp.symbol.getAndRequireEquals().assertEquals(SYMBOL)
tokenZkapp.decimals.getAndRequireEquals().assertEquals(DECIMALS)
tokenZkapp.maxSupply.getAndRequireEquals().assertEquals(SUPPLY_MAX)
tokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(UInt64.from(0))
})
it('can mint tokens', async () => {
const tx = await Mina.transaction(userPubkey, () => {
AccountUpdate.fundNewAccount(userPubkey)
tokenZkapp.mint(userPubkey, AMOUNT_MINT)
})
await tx.prove()
tx.sign([userPrivkey, tokenPrivkey])
await tx.send()
Mina.getBalance(userPubkey, tokenZkapp.token.id).assertEquals(AMOUNT_MINT)
tokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(AMOUNT_MINT)
})
it('can transfer tokens', async () => {
const receiver = PrivateKey.random().toPublicKey()
const tx = await Mina.transaction(userPubkey, () => {
AccountUpdate.fundNewAccount(userPubkey)
tokenZkapp.transfer(userPubkey, receiver, AMOUNT_TRANSFER)
})
await tx.prove()
tx.sign([userPrivkey])
await tx.send()
Mina.getBalance(userPubkey, tokenZkapp.token.id).assertEquals(AMOUNT_MINT.sub(AMOUNT_TRANSFER))
Mina.getBalance(receiver, tokenZkapp.token.id).assertEquals(AMOUNT_TRANSFER)
})
it("can't transfer tokens when signature is not valid", async () => {
const receiver = PrivateKey.random().toPublicKey()
const anyOtherPrivateKey = PrivateKey.random()
const tx = await Mina.transaction(userPubkey, () => {
AccountUpdate.fundNewAccount(userPubkey)
tokenZkapp.transfer(userPubkey, receiver, AMOUNT_TRANSFER)
})
await tx.prove()
tx.sign([anyOtherPrivateKey])
expect(tx.send()).rejects.toThrow()
})
it("can't transfer tokens when balance is not enough", async () => {
const receiver = PrivateKey.random().toPublicKey()
const tx = await Mina.transaction(userPubkey, () => {
AccountUpdate.fundNewAccount(userPubkey)
tokenZkapp.transfer(userPubkey, receiver, SUPPLY_MAX)
})
await tx.prove()
tx.sign([userPrivkey])
expect(tx.send()).rejects.toThrow()
})
})