-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathJettonWallet.spec.ts
927 lines (834 loc) · 47.6 KB
/
JettonWallet.spec.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
import { Blockchain, SandboxContract, TreasuryContract, internal, BlockchainSnapshot } from '@ton/sandbox';
import { Cell, toNano, beginCell, Address } from '@ton/core';
import { JettonWallet } from '../wrappers/JettonWallet';
import { JettonMinter, jettonContentToCell } from '../wrappers/JettonMinter';
import '@ton/test-utils';
import { compile } from '@ton/blueprint';
import { randomAddress, getRandomTon, differentAddress, getRandomInt, testJettonTransfer, testJettonInternalTransfer, testJettonNotification, testJettonBurnNotification } from './utils';
import { Op, Errors } from '../wrappers/JettonConstants';
/*
These tests check compliance with the TEP-74 and TEP-89,
but also checks some implementation details.
If you want to keep only TEP-74 and TEP-89 compliance tests,
you need to remove/modify the following tests:
mint tests (since minting is not covered by standard)
exit_codes
prove pathway
*/
//jetton params
let fwd_fee = 1804014n, gas_consumption = 15000000n, min_tons_for_storage = 10000000n;
//let fwd_fee = 1804014n, gas_consumption = 14000000n, min_tons_for_storage = 10000000n;
describe('JettonWallet', () => {
let jwallet_code = new Cell();
let minter_code = new Cell();
let blockchain: Blockchain;
let deployer:SandboxContract<TreasuryContract>;
let notDeployer:SandboxContract<TreasuryContract>;
let jettonMinter:SandboxContract<JettonMinter>;
let userWallet:any;
let defaultContent:Cell;
beforeAll(async () => {
jwallet_code = await compile('JettonWallet');
minter_code = await compile('JettonMinter');
blockchain = await Blockchain.create();
deployer = await blockchain.treasury('deployer');
notDeployer = await blockchain.treasury('notDeployer');
defaultContent = jettonContentToCell({type: 1, uri: "https://testjetton.org/content.json"});
jettonMinter = blockchain.openContract(
JettonMinter.createFromConfig(
{
admin: deployer.address,
content: defaultContent,
wallet_code: jwallet_code,
},
minter_code));
userWallet = async (address:Address) => blockchain.openContract(
JettonWallet.createFromAddress(
await jettonMinter.getWalletAddress(address)
)
);
});
// implementation detail
it('should deploy', async () => {
const deployResult = await jettonMinter.sendDeploy(deployer.getSender(), toNano('100'));
expect(deployResult.transactions).toHaveTransaction({
from: deployer.address,
to: jettonMinter.address,
deploy: true,
});
});
// implementation detail
it('minter admin should be able to mint jettons', async () => {
// can mint from deployer
let initialTotalSupply = await jettonMinter.getTotalSupply();
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = toNano('1000.23');
const mintResult = await jettonMinter.sendMint(deployer.getSender(), deployer.address, initialJettonBalance, toNano('0.05'), toNano('1'));
expect(mintResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployerJettonWallet.address,
deploy: true,
});
expect(mintResult.transactions).toHaveTransaction({ // excesses
from: deployerJettonWallet.address,
to: jettonMinter.address
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply + initialJettonBalance);
initialTotalSupply += initialJettonBalance;
// can mint from deployer again
let additionalJettonBalance = toNano('2.31');
await jettonMinter.sendMint(deployer.getSender(), deployer.address, additionalJettonBalance, toNano('0.05'), toNano('1'));
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance + additionalJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply + additionalJettonBalance);
initialTotalSupply += additionalJettonBalance;
// can mint to other address
let otherJettonBalance = toNano('3.12');
await jettonMinter.sendMint(deployer.getSender(), notDeployer.address, otherJettonBalance, toNano('0.05'), toNano('1'));
const notDeployerJettonWallet = await userWallet(notDeployer.address);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(otherJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply + otherJettonBalance);
});
// implementation detail
it('not a minter admin should not be able to mint jettons', async () => {
let initialTotalSupply = await jettonMinter.getTotalSupply();
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const unAuthMintResult = await jettonMinter.sendMint(notDeployer.getSender(), deployer.address, toNano('777'), toNano('0.05'), toNano('1'));
expect(unAuthMintResult.transactions).toHaveTransaction({
from: notDeployer.address,
to: jettonMinter.address,
aborted: true,
exitCode: Errors.not_admin, // error::unauthorized_mint_request
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply);
});
// Implementation detail
it('minter admin can change admin', async () => {
const adminBefore = await jettonMinter.getAdminAddress();
expect(adminBefore).toEqualAddress(deployer.address);
let res = await jettonMinter.sendChangeAdmin(deployer.getSender(), notDeployer.address);
expect(res.transactions).toHaveTransaction({
from: deployer.address,
on: jettonMinter.address,
success: true
});
const adminAfter = await jettonMinter.getAdminAddress();
expect(adminAfter).toEqualAddress(notDeployer.address);
await jettonMinter.sendChangeAdmin(notDeployer.getSender(), deployer.address);
expect((await jettonMinter.getAdminAddress()).equals(deployer.address)).toBe(true);
});
it('not a minter admin can not change admin', async () => {
const adminBefore = await jettonMinter.getAdminAddress();
expect(adminBefore).toEqualAddress(deployer.address);
let changeAdmin = await jettonMinter.sendChangeAdmin(notDeployer.getSender(), notDeployer.address);
expect((await jettonMinter.getAdminAddress()).equals(deployer.address)).toBe(true);
expect(changeAdmin.transactions).toHaveTransaction({
from: notDeployer.address,
on: jettonMinter.address,
aborted: true,
exitCode: Errors.not_admin, // error::unauthorized_change_admin_request
});
});
it('minter admin can change content', async () => {
let newContent = jettonContentToCell({type: 1, uri: "https://totally_new_jetton.org/content.json"})
expect((await jettonMinter.getContent()).equals(defaultContent)).toBe(true);
let changeContent = await jettonMinter.sendChangeContent(deployer.getSender(), newContent);
expect((await jettonMinter.getContent()).equals(newContent)).toBe(true);
changeContent = await jettonMinter.sendChangeContent(deployer.getSender(), defaultContent);
expect((await jettonMinter.getContent()).equals(defaultContent)).toBe(true);
});
it('not a minter admin can not change content', async () => {
let newContent = beginCell().storeUint(1,1).endCell();
let changeContent = await jettonMinter.sendChangeContent(notDeployer.getSender(), newContent);
expect((await jettonMinter.getContent()).equals(defaultContent)).toBe(true);
expect(changeContent.transactions).toHaveTransaction({
from: notDeployer.address,
to: jettonMinter.address,
aborted: true,
exitCode: Errors.not_admin, // error::unauthorized_change_content_request
});
});
it('wallet owner should be able to send jettons', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = toNano('0.5');
let forwardAmount = toNano('0.05');
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, null);
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: notDeployerJettonWallet.address,
to: deployer.address,
});
expect(sendResult.transactions).toHaveTransaction({ //notification
from: notDeployerJettonWallet.address,
to: notDeployer.address,
value: forwardAmount
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - sentAmount);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2 + sentAmount);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply);
});
it('not wallet owner should not be able to send jettons', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = toNano('0.5');
const sendResult = await deployerJettonWallet.sendTransfer(notDeployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, toNano('0.05'), null);
expect(sendResult.transactions).toHaveTransaction({
from: notDeployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_owner, //error::unauthorized_transfer
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply);
});
it('impossible to send too much jettons', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = initialJettonBalance + 1n;
let forwardAmount = toNano('0.05');
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, null);
expect(sendResult.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.balance_error, //error::not_enough_jettons
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2);
});
it.skip('malformed forward payload', async() => {
const deployerJettonWallet = await userWallet(deployer.address);
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let sentAmount = toNano('0.5');
let forwardAmount = getRandomTon(0.01, 0.05); // toNano('0.05');
let forwardPayload = beginCell().storeUint(0x1234567890abcdefn, 128).endCell();
let msgPayload = beginCell().storeUint(0xf8a7ea5, 32).storeUint(0, 64) // op, queryId
.storeCoins(sentAmount).storeAddress(notDeployer.address)
.storeAddress(deployer.address)
.storeMaybeRef(null)
.storeCoins(toNano('0.05')) // No forward payload indication
.endCell();
const res = await blockchain.sendMessage(internal({
from: deployer.address,
to: deployerJettonWallet.address,
body: msgPayload,
value: toNano('0.2')
}));
expect(res.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: 708
});
});
it('correctly sends forward_payload', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = toNano('0.5');
let forwardAmount = toNano('0.05');
let forwardPayload = beginCell().storeUint(0x1234567890abcdefn, 128).endCell();
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, forwardPayload);
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: notDeployerJettonWallet.address,
to: deployer.address,
});
/*
transfer_notification#7362d09c query_id:uint64 amount:(VarUInteger 16)
sender:MsgAddress forward_payload:(Either Cell ^Cell)
= InternalMsgBody;
*/
expect(sendResult.transactions).toHaveTransaction({ //notification
from: notDeployerJettonWallet.address,
to: notDeployer.address,
value: forwardAmount,
body: beginCell().storeUint(Op.transfer_notification, 32).storeUint(0, 64) //default queryId
.storeCoins(sentAmount)
.storeAddress(deployer.address)
.storeUint(1, 1)
.storeRef(forwardPayload)
.endCell()
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - sentAmount);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2 + sentAmount);
});
it('no forward_ton_amount - no forward', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = toNano('0.5');
let forwardAmount = 0n;
let forwardPayload = beginCell().storeUint(0x1234567890abcdefn, 128).endCell();
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, forwardPayload);
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: notDeployerJettonWallet.address,
to: deployer.address,
});
expect(sendResult.transactions).not.toHaveTransaction({ //no notification
from: notDeployerJettonWallet.address,
to: notDeployer.address
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - sentAmount);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2 + sentAmount);
});
it('check revert on not enough tons for forward', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
await deployer.send({value:toNano('1'), bounce:false, to: deployerJettonWallet.address});
let sentAmount = toNano('0.1');
let forwardAmount = toNano('0.3');
let forwardPayload = beginCell().storeUint(0x1234567890abcdefn, 128).endCell();
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), forwardAmount, // not enough tons, no tons for gas
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, forwardPayload);
expect(sendResult.transactions).toHaveTransaction({
from: deployer.address,
on: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_enough_ton, //error::not_enough_tons
});
// Make sure value bounced
expect(sendResult.transactions).toHaveTransaction({
from: deployerJettonWallet.address,
on: deployer.address,
inMessageBounced: true,
success: true
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
});
// implementation detail
it('works with minimal ton amount', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const someAddress = Address.parse("EQD__________________________________________0vo");
const someJettonWallet = await userWallet(someAddress);
let initialJettonBalance2 = await someJettonWallet.getJettonBalance();
await deployer.send({value:toNano('1'), bounce:false, to: deployerJettonWallet.address});
let forwardAmount = toNano('0.3');
/*
forward_ton_amount +
fwd_count * fwd_fee +
(2 * gas_consumption + min_tons_for_storage));
*/
let minimalFee = 2n* fwd_fee + 2n*gas_consumption + min_tons_for_storage;
let sentAmount = forwardAmount + minimalFee; // not enough, need >
let forwardPayload = null;
let tonBalance =(await blockchain.getContract(deployerJettonWallet.address)).balance;
let tonBalance2 = (await blockchain.getContract(someJettonWallet.address)).balance;
let sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), sentAmount,
sentAmount, someAddress,
deployer.address, null, forwardAmount, forwardPayload);
expect(sendResult.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_enough_ton, //error::not_enough_tons
});
sentAmount += 1n; // now enough
sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), sentAmount,
sentAmount, someAddress,
deployer.address, null, forwardAmount, forwardPayload);
expect(sendResult.transactions).not.toHaveTransaction({ //no excesses
from: someJettonWallet.address,
to: deployer.address,
});
/*
transfer_notification#7362d09c query_id:uint64 amount:(VarUInteger 16)
sender:MsgAddress forward_payload:(Either Cell ^Cell)
= InternalMsgBody;
*/
expect(sendResult.transactions).toHaveTransaction({ //notification
from: someJettonWallet.address,
to: someAddress,
value: forwardAmount,
body: beginCell().storeUint(Op.transfer_notification, 32).storeUint(0, 64) //default queryId
.storeCoins(sentAmount)
.storeAddress(deployer.address)
.storeUint(0, 1)
.endCell()
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - sentAmount);
expect(await someJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2 + sentAmount);
tonBalance =(await blockchain.getContract(deployerJettonWallet.address)).balance;
expect((await blockchain.getContract(someJettonWallet.address)).balance).toBeGreaterThan(min_tons_for_storage);
});
// implementation detail
it('wallet does not accept internal_transfer not from wallet', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
/*
internal_transfer query_id:uint64 amount:(VarUInteger 16) from:MsgAddress
response_address:MsgAddress
forward_ton_amount:(VarUInteger 16)
forward_payload:(Either Cell ^Cell)
= InternalMsgBody;
*/
let internalTransfer = beginCell().storeUint(0x178d4519, 32).storeUint(0, 64) //default queryId
.storeCoins(toNano('0.01'))
.storeAddress(deployer.address)
.storeAddress(deployer.address)
.storeCoins(toNano('0.05'))
.storeUint(0, 1)
.endCell();
const sendResult = await blockchain.sendMessage(internal({
from: notDeployer.address,
to: deployerJettonWallet.address,
body: internalTransfer,
value:toNano('0.3')
}));
expect(sendResult.transactions).toHaveTransaction({
from: notDeployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_valid_wallet, //error::unauthorized_incoming_transfer
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
});
it('wallet owner should be able to burn jettons', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
let burnAmount = toNano('0.01');
const sendResult = await deployerJettonWallet.sendBurn(deployer.getSender(), toNano('0.1'), // ton amount
burnAmount, deployer.address, null); // amount, response address, custom payload
expect(sendResult.transactions).toHaveTransaction({ //burn notification
from: deployerJettonWallet.address,
to: jettonMinter.address
});
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: jettonMinter.address,
to: deployer.address
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - burnAmount);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply - burnAmount);
});
it('not wallet owner should not be able to burn jettons', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
let burnAmount = toNano('0.01');
const sendResult = await deployerJettonWallet.sendBurn(notDeployer.getSender(), toNano('0.1'), // ton amount
burnAmount, deployer.address, null); // amount, response address, custom payload
expect(sendResult.transactions).toHaveTransaction({
from: notDeployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_owner, //error::unauthorized_transfer
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply);
});
it('wallet owner can not burn more jettons than it has', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
let burnAmount = initialJettonBalance + 1n;
const sendResult = await deployerJettonWallet.sendBurn(deployer.getSender(), toNano('0.1'), // ton amount
burnAmount, deployer.address, null); // amount, response address, custom payload
expect(sendResult.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.balance_error, //error::not_enough_jettons
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply);
});
it('minimal burn message fee', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialTotalSupply = await jettonMinter.getTotalSupply();
let burnAmount = toNano('0.01');
let fwd_fee = 1492012n /*1500012n*/, gas_consumption = 15000000n;
let minimalFee = fwd_fee + 2n*gas_consumption;
const sendLow = await deployerJettonWallet.sendBurn(deployer.getSender(), minimalFee, // ton amount
burnAmount, deployer.address, null); // amount, response address, custom payload
expect(sendLow.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.not_enough_gas, //error::burn_fee_not_matched
});
const sendExcess = await deployerJettonWallet.sendBurn(deployer.getSender(), minimalFee + 1n,
burnAmount, deployer.address, null);
expect(sendExcess.transactions).toHaveTransaction({
from: deployer.address,
to: deployerJettonWallet.address,
success: true
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - burnAmount);
expect(await jettonMinter.getTotalSupply()).toEqual(initialTotalSupply - burnAmount);
});
it('minter should only accept burn messages from jetton wallets', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
const burnAmount = toNano('1');
const burnNotification = (amount: bigint, addr: Address) => {
return beginCell()
.storeUint(Op.burn_notification, 32)
.storeUint(0, 64)
.storeCoins(amount)
.storeAddress(addr)
.storeAddress(deployer.address)
.endCell();
}
let res = await blockchain.sendMessage(internal({
from: deployerJettonWallet.address,
to: jettonMinter.address,
body: burnNotification(burnAmount, randomAddress(0)),
value: toNano('0.1')
}));
expect(res.transactions).toHaveTransaction({
from: deployerJettonWallet.address,
to: jettonMinter.address,
aborted: true,
exitCode: Errors.unouthorized_burn // Unauthorized burn
});
res = await blockchain.sendMessage(internal({
from: deployerJettonWallet.address,
to: jettonMinter.address,
body: burnNotification(burnAmount, deployer.address),
value: toNano('0.1')
}));
expect(res.transactions).toHaveTransaction({
from: deployerJettonWallet.address,
to: jettonMinter.address,
success: true
});
});
// TEP-89
it('report correct discovery address', async () => {
let discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(), deployer.address, true);
/*
take_wallet_address#d1735400 query_id:uint64 wallet_address:MsgAddress owner_address:(Maybe ^MsgAddress) = InternalMsgBody;
*/
const deployerJettonWallet = await userWallet(deployer.address);
expect(discoveryResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployer.address,
body: beginCell().storeUint(Op.take_wallet_address, 32).storeUint(0, 64)
.storeAddress(deployerJettonWallet.address)
.storeUint(1, 1)
.storeRef(beginCell().storeAddress(deployer.address).endCell())
.endCell()
});
discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(), notDeployer.address, true);
const notDeployerJettonWallet = await userWallet(notDeployer.address);
expect(discoveryResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployer.address,
body: beginCell().storeUint(Op.take_wallet_address, 32).storeUint(0, 64)
.storeAddress(notDeployerJettonWallet.address)
.storeUint(1, 1)
.storeRef(beginCell().storeAddress(notDeployer.address).endCell())
.endCell()
});
// do not include owner address
discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(), notDeployer.address, false);
expect(discoveryResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployer.address,
body: beginCell().storeUint(Op.take_wallet_address, 32).storeUint(0, 64)
.storeAddress(notDeployerJettonWallet.address)
.storeUint(0, 1)
.endCell()
});
});
it('Minimal discovery fee', async () => {
// 5000 gas-units + msg_forward_prices.lump_price + msg_forward_prices.cell_price = 0.0061
const fwdFee = 1464012n;
const minimalFee = fwdFee + 10000000n; // toNano('0.0061');
let discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(),
notDeployer.address,
false,
minimalFee);
expect(discoveryResult.transactions).toHaveTransaction({
from: deployer.address,
to: jettonMinter.address,
aborted: true,
exitCode: Errors.discovery_fee_not_matched // discovery_fee_not_matched
});
/*
* Might be helpfull to have logical OR in expect lookup
* Because here is what is stated in standard:
* and either throw an exception if amount of incoming value is not enough to calculate wallet address
* or response with message (sent with mode 64)
* https://github.com/ton-blockchain/TEPs/blob/master/text/0089-jetton-wallet-discovery.md
* At least something like
* expect(discoveryResult.hasTransaction({such and such}) ||
* discoveryResult.hasTransaction({yada yada})).toBeTruethy()
*/
discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(),
notDeployer.address,
false,
minimalFee + 1n);
expect(discoveryResult.transactions).toHaveTransaction({
from: deployer.address,
to: jettonMinter.address,
success: true
});
});
it('Correctly handles not valid address in discovery', async () =>{
const badAddr = randomAddress(-1);
let discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(),
badAddr,
false);
expect(discoveryResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployer.address,
body: beginCell().storeUint(Op.take_wallet_address, 32).storeUint(0, 64)
.storeUint(0, 2) // addr_none
.storeUint(0, 1)
.endCell()
});
// Include address should still be available
discoveryResult = await jettonMinter.sendDiscovery(deployer.getSender(),
badAddr,
true); // Include addr
expect(discoveryResult.transactions).toHaveTransaction({
from: jettonMinter.address,
to: deployer.address,
body: beginCell().storeUint(Op.take_wallet_address, 32).storeUint(0, 64)
.storeUint(0, 2) // addr_none
.storeUint(1, 1)
.storeRef(beginCell().storeAddress(badAddr).endCell())
.endCell()
});
});
// This test consume a lot of time: 18 sec
// and is needed only for measuring ton accruing
/*it('jettonWallet can process 250 transfer', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
const notDeployerJettonWallet = await userWallet(notDeployer.address);
let initialJettonBalance2 = await notDeployerJettonWallet.getJettonBalance();
let sentAmount = 1n, count = 250n;
let forwardAmount = toNano('0.05');
let sendResult: any;
let payload = beginCell()
.storeUint(0x12345678, 32).storeUint(0x87654321, 32)
.storeRef(beginCell().storeUint(0x12345678, 32).storeUint(0x87654321, 108).endCell())
.storeRef(beginCell().storeUint(0x12345671, 32).storeUint(0x87654321, 240).endCell())
.storeRef(beginCell().storeUint(0x12345672, 32).storeUint(0x87654321, 77)
.storeRef(beginCell().endCell())
.storeRef(beginCell().storeUint(0x1245671, 91).storeUint(0x87654321, 32).endCell())
.storeRef(beginCell().storeUint(0x2245671, 180).storeUint(0x87654321, 32).endCell())
.storeRef(beginCell().storeUint(0x8245671, 255).storeUint(0x87654321, 32).endCell())
.endCell())
.endCell();
let initialBalance =(await blockchain.getContract(deployerJettonWallet.address)).balance;
let initialBalance2 = (await blockchain.getContract(notDeployerJettonWallet.address)).balance;
for(let i = 0; i < count; i++) {
sendResult = await deployerJettonWallet.sendTransferMessage(deployer.getSender(), toNano('0.1'), //tons
sentAmount, notDeployer.address,
deployer.address, null, forwardAmount, payload);
}
// last chain was successful
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: notDeployerJettonWallet.address,
to: deployer.address,
});
expect(sendResult.transactions).toHaveTransaction({ //notification
from: notDeployerJettonWallet.address,
to: notDeployer.address,
value: forwardAmount
});
expect(await deployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance - sentAmount*count);
expect(await notDeployerJettonWallet.getJettonBalance()).toEqual(initialJettonBalance2 + sentAmount*count);
let finalBalance =(await blockchain.getContract(deployerJettonWallet.address)).balance;
let finalBalance2 = (await blockchain.getContract(notDeployerJettonWallet.address)).balance;
// if it is not true, it's ok but gas_consumption constant is too high
// and excesses of TONs will be accrued on wallet
expect(finalBalance).toBeLessThan(initialBalance + toNano('0.001'));
expect(finalBalance2).toBeLessThan(initialBalance2 + toNano('0.001'));
expect(finalBalance).toBeGreaterThan(initialBalance - toNano('0.001'));
expect(finalBalance2).toBeGreaterThan(initialBalance2 - toNano('0.001'));
});
*/
// implementation detail
it('can not send to masterchain', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let sentAmount = toNano('0.5');
let forwardAmount = toNano('0.05');
const sendResult = await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, Address.parse("Ef8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAU"),
deployer.address, null, forwardAmount, null);
expect(sendResult.transactions).toHaveTransaction({ //excesses
from: deployer.address,
to: deployerJettonWallet.address,
aborted: true,
exitCode: Errors.wrong_workchain //error::wrong_workchain
});
});
describe('Bounces', () => {
// This is borrowed from stablecoin, and is not implemented here.
// Should it be implemented?
it.skip('minter should restore supply on internal_transfer bounce', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
const mintAmount = BigInt(getRandomInt(1000, 2000));
const mintMsg = JettonMinter.mintMessage(jettonMinter.address, deployer.address, mintAmount,toNano('0.1'), toNano('0.1'));
const supplyBefore = await jettonMinter.getTotalSupply();
const minterSmc = await blockchain.getContract(jettonMinter.address);
// Sending message but only processing first step of tx chain
let res = minterSmc.receiveMessage(internal({
from: deployer.address,
to: jettonMinter.address,
body: mintMsg,
value: toNano('1')
}));
expect(res.outMessagesCount).toEqual(1);
const outMsgSc = res.outMessages.get(0)!.body.beginParse();
expect(outMsgSc.preloadUint(32)).toEqual(Op.internal_transfer);
expect(await jettonMinter.getTotalSupply()).toEqual(supplyBefore + mintAmount);
minterSmc.receiveMessage(internal({
from: deployerJettonWallet.address,
to: jettonMinter.address,
bounced: true,
body: beginCell().storeUint(0xFFFFFFFF, 32).storeSlice(outMsgSc).endCell(),
value: toNano('0.95')
}));
// Supply should change back
expect(await jettonMinter.getTotalSupply()).toEqual(supplyBefore);
});
it('wallet should restore balance on internal_transfer bounce', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
const notDeployerJettonWallet = await userWallet(notDeployer.address);
const balanceBefore = await deployerJettonWallet.getJettonBalance();
const txAmount = BigInt(getRandomInt(100, 200));
const transferMsg = JettonWallet.transferMessage(txAmount, notDeployer.address, deployer.address, null, 0n, null);
const walletSmc = await blockchain.getContract(deployerJettonWallet.address);
const res = walletSmc.receiveMessage(internal({
from: deployer.address,
to: deployerJettonWallet.address,
body: transferMsg,
value: toNano('1')
}));
expect(res.outMessagesCount).toEqual(1);
const outMsgSc = res.outMessages.get(0)!.body.beginParse();
expect(outMsgSc.preloadUint(32)).toEqual(Op.internal_transfer);
expect(await deployerJettonWallet.getJettonBalance()).toEqual(balanceBefore - txAmount);
walletSmc.receiveMessage(internal({
from: notDeployerJettonWallet.address,
to: walletSmc.address,
bounced: true,
body: beginCell().storeUint(0xFFFFFFFF, 32).storeSlice(outMsgSc).endCell(),
value: toNano('0.95')
}));
// Balance should roll back
expect(await deployerJettonWallet.getJettonBalance()).toEqual(balanceBefore);
});
it('wallet should restore balance on burn_notification bounce', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
const balanceBefore = await deployerJettonWallet.getJettonBalance();
const burnAmount = BigInt(getRandomInt(100, 200));
const burnMsg = JettonWallet.burnMessage(burnAmount, deployer.address, null);
const walletSmc = await blockchain.getContract(deployerJettonWallet.address);
const res = walletSmc.receiveMessage(internal({
from: deployer.address,
to: deployerJettonWallet.address,
body: burnMsg,
value: toNano('1')
}));
expect(res.outMessagesCount).toEqual(1);
const outMsgSc = res.outMessages.get(0)!.body.beginParse();
expect(outMsgSc.preloadUint(32)).toEqual(Op.burn_notification);
expect(await deployerJettonWallet.getJettonBalance()).toEqual(balanceBefore - burnAmount);
walletSmc.receiveMessage(internal({
from: jettonMinter.address,
to: walletSmc.address,
bounced: true,
body: beginCell().storeUint(0xFFFFFFFF, 32).storeSlice(outMsgSc).endCell(),
value: toNano('0.95')
}));
// Balance should roll back
expect(await deployerJettonWallet.getJettonBalance()).toEqual(balanceBefore);
});
});
// Current wallet version doesn't support those operations
// implementation detail
it.skip('owner can withdraw excesses', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
await deployer.send({value:toNano('1'), bounce:false, to: deployerJettonWallet.address});
let initialBalance = (await blockchain.getContract(deployer.address)).balance;
const withdrawResult = await deployerJettonWallet.sendWithdrawTons(deployer.getSender());
expect(withdrawResult.transactions).toHaveTransaction({ //excesses
from: deployerJettonWallet.address,
to: deployer.address
});
let finalBalance = (await blockchain.getContract(deployer.address)).balance;
let finalWalletBalance = (await blockchain.getContract(deployerJettonWallet.address)).balance;
expect(finalWalletBalance).toEqual(min_tons_for_storage);
expect(finalBalance - initialBalance).toBeGreaterThan(toNano('0.99'));
});
// implementation detail
it.skip('not owner can not withdraw excesses', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
await deployer.send({value:toNano('1'), bounce:false, to: deployerJettonWallet.address});
let initialBalance = (await blockchain.getContract(deployer.address)).balance;
const withdrawResult = await deployerJettonWallet.sendWithdrawTons(notDeployer.getSender());
expect(withdrawResult.transactions).not.toHaveTransaction({ //excesses
from: deployerJettonWallet.address,
to: deployer.address
});
let finalBalance = (await blockchain.getContract(deployer.address)).balance;
let finalWalletBalance = (await blockchain.getContract(deployerJettonWallet.address)).balance;
expect(finalWalletBalance).toBeGreaterThan(toNano('1'));
expect(finalBalance - initialBalance).toBeLessThan(toNano('0.1'));
});
// implementation detail
it.skip('owner can withdraw jettons owned by JettonWallet', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let sentAmount = toNano('0.5');
let forwardAmount = toNano('0.05');
await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, deployerJettonWallet.address,
deployer.address, null, forwardAmount, null);
const childJettonWallet = await userWallet(deployerJettonWallet.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialChildJettonBalance = await childJettonWallet.getJettonBalance();
expect(initialChildJettonBalance).toEqual(toNano('0.5'));
let withdrawResult = await deployerJettonWallet.sendWithdrawJettons(deployer.getSender(), childJettonWallet.address, toNano('0.4'));
expect(await deployerJettonWallet.getJettonBalance() - initialJettonBalance).toEqual(toNano('0.4'));
expect(await childJettonWallet.getJettonBalance()).toEqual(toNano('0.1'));
//withdraw the rest
await deployerJettonWallet.sendWithdrawJettons(deployer.getSender(), childJettonWallet.address, toNano('0.1'));
});
// implementation detail
it.skip('not owner can not withdraw jettons owned by JettonWallet', async () => {
const deployerJettonWallet = await userWallet(deployer.address);
let sentAmount = toNano('0.5');
let forwardAmount = toNano('0.05');
await deployerJettonWallet.sendTransfer(deployer.getSender(), toNano('0.1'), //tons
sentAmount, deployerJettonWallet.address,
deployer.address, null, forwardAmount, null);
const childJettonWallet = await userWallet(deployerJettonWallet.address);
let initialJettonBalance = await deployerJettonWallet.getJettonBalance();
let initialChildJettonBalance = await childJettonWallet.getJettonBalance();
expect(initialChildJettonBalance).toEqual(toNano('0.5'));
let withdrawResult = await deployerJettonWallet.sendWithdrawJettons(notDeployer.getSender(), childJettonWallet.address, toNano('0.4'));
expect(await deployerJettonWallet.getJettonBalance() - initialJettonBalance).toEqual(toNano('0.0'));
expect(await childJettonWallet.getJettonBalance()).toEqual(toNano('0.5'));
});
});