-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exchange.test.ts
494 lines (387 loc) · 16.1 KB
/
Exchange.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import { AccountUpdate, Encoding, MerkleTree, Mina, PrivateKey, PublicKey, Signature, UInt64 } from 'o1js'
import { Vault } from './Vault'
import { Token } from './Token'
import { Exchange, PAIRS_HEIGHT } from './Exchange'
import { Database } from './common/database'
const proofsEnabled = false
describe('token vault test', () => {
const Local = Mina.LocalBlockchain({ proofsEnabled })
Mina.setActiveInstance(Local)
const database = new Database()
const czPrivkey = Local.testAccounts[0].privateKey
const czPubkey = Local.testAccounts[0].publicKey
const satoshiPrivkey = Local.testAccounts[1].privateKey
const satoshiPubkey = Local.testAccounts[1].publicKey
const fedPrivkey = Local.testAccounts[2].privateKey
const fedPubkey = Local.testAccounts[2].publicKey
const exchangePrivkey = PrivateKey.random()
const exchangePubkey = exchangePrivkey.toPublicKey()
const exchangeZkapp = new Exchange(exchangePubkey)
const btcTokenPrivkey = PrivateKey.random()
const btcTokenPubkey = btcTokenPrivkey.toPublicKey()
const btcTokenZkapp = new Token(btcTokenPubkey)
const usdTokenPrivkey = PrivateKey.random()
const usdTokenPubkey = usdTokenPrivkey.toPublicKey()
const usdTokenZkapp = new Token(usdTokenPubkey)
const btcVaultZkapp = new Vault(exchangePubkey, btcTokenZkapp.token.id)
const usdVaultZkapp = new Vault(exchangePubkey, usdTokenZkapp.token.id)
const BTC_SYMBOL = Encoding.stringToFields('BTC')[0]
const BTC_DECIMALS = UInt64.from(6)
const BTC_MAX_SUPPLY = UInt64.from(21_000_000_000_000n)
const BTC_MINT_AMOUNT = UInt64.from(10_000_000_000_000n)
const USD_SYMBOL = Encoding.stringToFields('USD')[0]
const USD_DECIMALS = UInt64.from(2)
const USD_MAX_SUPPLY = UInt64.from(999_000_000_000_000_000n)
const USD_MINT_AMOUNT = UInt64.from(999_000_000_000_000_000n)
const BUY_ORDER_AMOUNT = UInt64.from(1_000_000n)
const BUY_ORDER_PRICE = UInt64.from(69_000_00n)
const SELL_ORDER_AMOUNT = UInt64.from(2_000_000n)
const SELL_ORDER_PRICE = UInt64.from(69_500_00n)
beforeAll(async () => {
if (proofsEnabled) {
await Exchange.compile()
await Vault.compile()
await Token.compile()
}
})
it('can deploy and initialize exchange', async () => {
const tx = await Mina.transaction(czPubkey, () => {
AccountUpdate.fundNewAccount(czPubkey)
exchangeZkapp.deploy()
exchangeZkapp.initialize(czPubkey)
})
await tx.prove()
tx.sign([czPrivkey, exchangePrivkey])
await tx.send()
exchangeZkapp.authority.getAndRequireEquals().assertEquals(czPubkey)
exchangeZkapp.root.getAndRequireEquals().assertEquals(new MerkleTree(PAIRS_HEIGHT).getRoot())
})
it('can deploy and initialize BTC tokens', async () => {
const tx = await Mina.transaction(satoshiPubkey, () => {
AccountUpdate.fundNewAccount(satoshiPubkey)
btcTokenZkapp.deploy()
btcTokenZkapp.initialize(BTC_SYMBOL, BTC_DECIMALS, BTC_MAX_SUPPLY)
})
await tx.prove()
tx.sign([satoshiPrivkey, btcTokenPrivkey])
await tx.send()
btcTokenZkapp.symbol.getAndRequireEquals().assertEquals(BTC_SYMBOL)
btcTokenZkapp.decimals.getAndRequireEquals().assertEquals(BTC_DECIMALS)
btcTokenZkapp.maxSupply.getAndRequireEquals().assertEquals(BTC_MAX_SUPPLY)
btcTokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(UInt64.zero)
})
it('can deploy and initialize USD tokens', async () => {
const tx = await Mina.transaction(fedPubkey, () => {
AccountUpdate.fundNewAccount(fedPubkey)
usdTokenZkapp.deploy()
usdTokenZkapp.initialize(USD_SYMBOL, USD_DECIMALS, USD_MAX_SUPPLY)
})
await tx.prove()
tx.sign([fedPrivkey, usdTokenPrivkey])
await tx.send()
usdTokenZkapp.symbol.getAndRequireEquals().assertEquals(USD_SYMBOL)
usdTokenZkapp.decimals.getAndRequireEquals().assertEquals(USD_DECIMALS)
usdTokenZkapp.maxSupply.getAndRequireEquals().assertEquals(USD_MAX_SUPPLY)
usdTokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(UInt64.zero)
})
it('can mint BTC tokens', async () => {
const tx = await Mina.transaction(satoshiPubkey, () => {
AccountUpdate.fundNewAccount(satoshiPubkey)
btcTokenZkapp.mint(satoshiPubkey, BTC_MINT_AMOUNT)
})
await tx.prove()
tx.sign([satoshiPrivkey])
await tx.send()
Mina.getBalance(satoshiPubkey, btcTokenZkapp.token.id).assertEquals(BTC_MINT_AMOUNT)
btcTokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(BTC_MINT_AMOUNT)
})
it('can mint USD tokens', async () => {
const tx = await Mina.transaction(fedPubkey, () => {
AccountUpdate.fundNewAccount(fedPubkey)
usdTokenZkapp.mint(fedPubkey, USD_MINT_AMOUNT)
})
await tx.prove()
tx.sign([fedPrivkey])
await tx.send()
Mina.getBalance(fedPubkey, usdTokenZkapp.token.id).assertEquals(USD_MINT_AMOUNT)
usdTokenZkapp.circulatingSupply.getAndRequireEquals().assertEquals(USD_MINT_AMOUNT)
})
// todo: make vault deployment possible without needing exchange's private key to sign
it('can deploy BTC vault', async () => {
const tx = await Mina.transaction(satoshiPubkey, () => {
AccountUpdate.fundNewAccount(satoshiPubkey)
btcVaultZkapp.deploy()
btcTokenZkapp.approveUpdate(btcVaultZkapp.self)
})
await tx.prove()
tx.sign([satoshiPrivkey, exchangePrivkey])
await tx.send()
})
it('can deploy USD vault', async () => {
const tx = await Mina.transaction(fedPubkey, () => {
AccountUpdate.fundNewAccount(fedPubkey)
usdVaultZkapp.deploy()
usdTokenZkapp.approveUpdate(usdVaultZkapp.self)
})
await tx.prove()
tx.sign([fedPrivkey, exchangePrivkey])
await tx.send()
})
it('can create BTC/USD pair', async () => {
database.addPair({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const pairWitness = database.getPairWitness({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const signature = Signature.create(czPrivkey, [
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...pairWitness.toFields(),
])
const tx = await Mina.transaction(satoshiPubkey, () => {
exchangeZkapp.createPair(btcTokenPubkey, usdTokenPubkey, pairWitness, signature)
})
await tx.prove()
tx.sign([satoshiPrivkey])
await tx.send()
})
it('can place orders', async () => {
const pairWitness = database.getPairWitness({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const orderIndex = database.addOrder({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
amount: BUY_ORDER_AMOUNT.toBigInt(),
price: BUY_ORDER_PRICE.toBigInt(),
maker: fedPubkey.toBase58(),
})
const buyOrderWitness = database.getOrderWitness({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex,
})
const sellOrdersRoot = database.getOrdersRoot({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const signature = Signature.create(czPrivkey, [
...BUY_ORDER_AMOUNT.toFields(),
...BUY_ORDER_PRICE.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])
const tx = await Mina.transaction(fedPubkey, () => {
exchangeZkapp.placeBuyOrder(
BUY_ORDER_AMOUNT,
BUY_ORDER_PRICE,
btcTokenPubkey,
usdTokenPubkey,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
signature
)
})
await tx.prove()
tx.sign([fedPrivkey])
await tx.send()
Mina.getBalance(fedPubkey, usdTokenZkapp.token.id).assertEquals(
USD_MINT_AMOUNT.sub(BUY_ORDER_AMOUNT.mul(BUY_ORDER_PRICE))
)
Mina.getBalance(exchangePubkey, usdTokenZkapp.token.id).assertEquals(BUY_ORDER_AMOUNT.mul(BUY_ORDER_PRICE))
})
it('can cancel orders', async () => {
const pairWitness = database.getPairWitness({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const buyOrderWitness = database.getOrderWitness({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex: 0,
})
database.removeOrder({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex: 0,
})
const sellOrdersRoot = database.getOrdersRoot({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const signature = Signature.create(czPrivkey, [
...BUY_ORDER_AMOUNT.toFields(),
...BUY_ORDER_PRICE.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...sellOrdersRoot.toFields(),
...buyOrderWitness.toFields(),
...pairWitness.toFields(),
])
const tx = await Mina.transaction(fedPubkey, () => {
exchangeZkapp.cancelBuyOrder(
BUY_ORDER_AMOUNT,
BUY_ORDER_PRICE,
btcTokenPubkey,
usdTokenPubkey,
buyOrderWitness,
sellOrdersRoot,
pairWitness,
signature
)
})
await tx.prove()
tx.sign([fedPrivkey])
await tx.send()
Mina.getBalance(fedPubkey, usdTokenZkapp.token.id).assertEquals(USD_MINT_AMOUNT)
Mina.getBalance(exchangePubkey, usdTokenZkapp.token.id).assertEquals(UInt64.zero)
})
it('can place orders', async () => {
const pairWitness = database.getPairWitness({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const orderIndex = database.addOrder({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
amount: SELL_ORDER_AMOUNT.toBigInt(),
price: SELL_ORDER_PRICE.toBigInt(),
maker: satoshiPubkey.toBase58(),
})
const buyOrdersRoot = database.getOrdersRoot({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const sellOrderWitness = database.getOrderWitness({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex,
})
const signature = Signature.create(czPrivkey, [
...SELL_ORDER_AMOUNT.toFields(),
...SELL_ORDER_PRICE.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...buyOrdersRoot.toFields(),
...sellOrderWitness.toFields(),
...pairWitness.toFields(),
])
const tx1 = await Mina.transaction(satoshiPubkey, () => {
AccountUpdate.fundNewAccount(satoshiPubkey)
const update = AccountUpdate.createSigned(satoshiPubkey, usdTokenZkapp.token.id)
usdTokenZkapp.approveUpdate(update)
})
await tx1.prove()
tx1.sign([satoshiPrivkey])
await tx1.send()
const tx2 = await Mina.transaction(satoshiPubkey, () => {
exchangeZkapp.placeSellOrder(
SELL_ORDER_AMOUNT,
SELL_ORDER_PRICE,
btcTokenPubkey,
usdTokenPubkey,
buyOrdersRoot,
sellOrderWitness,
pairWitness,
signature
)
})
await tx2.prove()
tx2.sign([satoshiPrivkey])
await tx2.send()
Mina.getBalance(satoshiPubkey, btcTokenZkapp.token.id).assertEquals(BTC_MINT_AMOUNT.sub(SELL_ORDER_AMOUNT))
Mina.getBalance(exchangePubkey, btcTokenZkapp.token.id).assertEquals(SELL_ORDER_AMOUNT)
})
it('can execute orders', async () => {
const pairWitness = database.getPairWitness({
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const orderToExecute = database.getOrder({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex: 0,
})
const buyOrdersRoot = database.getOrdersRoot({
side: 'BUY',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
})
const sellOrderWitness = database.getOrderWitness({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex: 0,
})
database.removeOrder({
side: 'SELL',
baseCurrency: btcTokenPubkey.toBase58(),
quoteCurrency: usdTokenPubkey.toBase58(),
orderIndex: 0,
})
const maker = PublicKey.fromBase58(orderToExecute.maker)
const amount = UInt64.from(orderToExecute.amount)
const price = UInt64.from(orderToExecute.price)
const signature = Signature.create(czPrivkey, [
...maker.toFields(),
...amount.toFields(),
...price.toFields(),
...btcTokenPubkey.toFields(),
...usdTokenPubkey.toFields(),
...buyOrdersRoot.toFields(),
...sellOrderWitness.toFields(),
...pairWitness.toFields(),
])
const tx1 = await Mina.transaction(fedPubkey, () => {
AccountUpdate.fundNewAccount(fedPubkey)
const update = AccountUpdate.createSigned(fedPubkey, btcTokenZkapp.token.id)
btcTokenZkapp.approveUpdate(update)
})
await tx1.prove()
tx1.sign([fedPrivkey])
await tx1.send()
const tx2 = await Mina.transaction(fedPubkey, () => {
exchangeZkapp.executeSellOrder(
maker,
amount,
price,
btcTokenPubkey,
usdTokenPubkey,
buyOrdersRoot,
sellOrderWitness,
pairWitness,
signature
)
})
await tx2.prove()
tx2.sign([fedPrivkey])
await tx2.send()
console.log(tx2.transaction.accountUpdates.length)
Mina.getBalance(satoshiPubkey, btcTokenZkapp.token.id).assertEquals(BTC_MINT_AMOUNT.sub(SELL_ORDER_AMOUNT))
Mina.getBalance(exchangePubkey, btcTokenZkapp.token.id).assertEquals(UInt64.zero)
Mina.getBalance(fedPubkey, btcTokenZkapp.token.id).assertEquals(SELL_ORDER_AMOUNT)
Mina.getBalance(satoshiPubkey, usdTokenZkapp.token.id).assertEquals(SELL_ORDER_AMOUNT.mul(SELL_ORDER_PRICE))
Mina.getBalance(exchangePubkey, usdTokenZkapp.token.id).assertEquals(UInt64.zero)
Mina.getBalance(fedPubkey, usdTokenZkapp.token.id).assertEquals(
USD_MINT_AMOUNT.sub(SELL_ORDER_AMOUNT.mul(SELL_ORDER_PRICE))
)
})
})