-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcontract.spec.ts
290 lines (266 loc) · 11.8 KB
/
contract.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
import { ContractSystem } from "@tact-lang/emulator";
import { SampleJetton, Mint, TokenTransfer } from "./output/SampleJetton_SampleJetton";
import { JettonDefaultWallet, TokenBurn } from "./output/SampleJetton_JettonDefaultWallet";
import { buildOnchainMetadata } from "./utils/jetton-helpers";
import { Blockchain, SandboxContract, TreasuryContract } from "@ton-community/sandbox";
import { beginCell, contractAddress, fromNano, StateInit, toNano } from "ton-core";
import "@ton-community/test-utils";
//
// This version of test is based on "@ton-community/sandbox" package
//
describe("contract", () => {
let blockchain: Blockchain;
let token: SandboxContract<SampleJetton>;
let jettonWallet: SandboxContract<JettonDefaultWallet>;
let deployer: SandboxContract<TreasuryContract>;
beforeEach(async () => {
blockchain = await Blockchain.create();
deployer = await blockchain.treasury("deployer");
// Create content Cell
const jettonParams = {
name: "Best Practice",
description: "This is description of Test tact jetton",
symbol: "XXX",
image: "https://play-lh.googleusercontent.com/ahJtMe0vfOlAu1XJVQ6rcaGrQBgtrEZQefHy7SXB7jpijKhu1Kkox90XDuH8RmcBOXNn",
};
let content = buildOnchainMetadata(jettonParams);
let max_supply = toNano(1234766689011); // Set the specific total supply in nano
token = blockchain.openContract(await SampleJetton.fromInit(deployer.address, content, max_supply));
// Send Transaction
const deployResult = await token.send(deployer.getSender(), { value: toNano("10") }, "Mint: 100");
expect(deployResult.transactions).toHaveTransaction({
from: deployer.address,
to: token.address,
deploy: true,
success: true,
});
});
it("should deploy", async () => {
// the check is done inside beforeEach, blockchain and token are ready to use
// console.log((await token.getGetJettonData()).owner);
// console.log((await token.getGetJettonData()).totalSupply);
// console.log((await token.getGetJettonData()).max_supply);
// console.log((await token.getGetJettonData()).content);
});
it("should mint successfully", async () => {
const player = await blockchain.treasury("player");
const totalSupplyBefore = (await token.getGetJettonData()).total_supply;
const mintAmount = toNano(100);
const Mint: Mint = {
$$type: "Mint",
amount: mintAmount,
receiver: player.address,
};
const mintResult = await token.send(deployer.getSender(), { value: toNano("10") }, Mint);
expect(mintResult.transactions).toHaveTransaction({
from: deployer.address,
to: token.address,
success: true,
});
const totalSupplyAfter = (await token.getGetJettonData()).total_supply;
expect(totalSupplyBefore + mintAmount).toEqual(totalSupplyAfter);
const playerWallet = await token.getGetWalletAddress(player.address);
jettonWallet = blockchain.openContract(JettonDefaultWallet.fromAddress(playerWallet));
const walletData = await jettonWallet.getGetWalletData();
expect(walletData.owner).toEqualAddress(player.address);
expect(walletData.balance).toEqual(mintAmount);
});
it("should transfer successfully", async () => {
const sender = await blockchain.treasury("sender");
const receiver = await blockchain.treasury("receiver");
const initMintAmount = toNano(1000);
const transferAmount = toNano(80);
const mintMessage: Mint = {
$$type: "Mint",
amount: initMintAmount,
receiver: sender.address,
};
await token.send(deployer.getSender(), { value: toNano("10") }, mintMessage);
const senderWalletAddress = await token.getGetWalletAddress(sender.address);
const senderWallet = blockchain.openContract(JettonDefaultWallet.fromAddress(senderWalletAddress));
// Transfer tokens from sender's wallet to receiver's wallet
const transferMessage: TokenTransfer = {
$$type: "TokenTransfer",
query_id: 1n,
amount: transferAmount,
destination: receiver.address,
response_destination: sender.address,
custom_payload: null,
forward_ton_amount: 1n,
forward_payload: beginCell().endCell(),
};
const transferResult = await senderWallet.send(sender.getSender(), { value: toNano("10") }, transferMessage);
// console.log(transferResult.transactions);
const receiverWalletAddress = await token.getGetWalletAddress(receiver.address);
const receiverWallet = blockchain.openContract(JettonDefaultWallet.fromAddress(receiverWalletAddress));
const senderWalletDataAfterTransfer = await senderWallet.getGetWalletData();
const receiverWalletDataAfterTransfer = await receiverWallet.getGetWalletData();
expect(senderWalletDataAfterTransfer.balance).toEqual(initMintAmount - transferAmount); // check that the sender transferred the right amount of tokens
expect(receiverWalletDataAfterTransfer.balance).toEqual(transferAmount); // check that the receiver received the right amount of tokens
// const balance1 = (await receiverWallet.getGetWalletData()).balance;
// console.log(fromNano(balance1));
});
it("Mint tokens then Burn tokens", async () => {
// const sender = await blockchain.treasury("sender");
const deployerWalletAddress = await token.getGetWalletAddress(deployer.address);
const deployerWallet = blockchain.openContract(JettonDefaultWallet.fromAddress(deployerWalletAddress));
let deployerBalanceInit = (await deployerWallet.getGetWalletData()).balance;
console.log("deployerBalanceInit = ", deployerBalanceInit);
const initMintAmount = toNano(100);
const mintMessage: Mint = {
$$type: "Mint",
amount: initMintAmount,
receiver: deployer.address,
};
await token.send(deployer.getSender(), { value: toNano("10") }, mintMessage);
let deployerBalance = (await deployerWallet.getGetWalletData()).balance;
expect(deployerBalance).toEqual(deployerBalanceInit + initMintAmount);
let burnAmount = toNano(10);
const burnMessage: TokenBurn = {
$$type: "TokenBurn",
query_id: 0n,
amount: burnAmount,
response_destination: deployer.address,
custom_payload: beginCell().endCell(),
};
const burnResult = await deployerWallet.send(deployer.getSender(), { value: toNano("10") }, burnMessage);
let deployerBalanceAfterBurn = (await deployerWallet.getGetWalletData()).balance;
expect(deployerBalanceAfterBurn).toEqual(deployerBalance - burnAmount);
});
// it("should deploy correctly", async () => {
// // Create ContractSystem and deploy contract
// let system = await ContractSystem.create();
// let owner = system.treasure("owner");
// let nonOwner = system.treasure("non-owner");
// let contract = system.open(await SampleTactContract.fromInit(owner.address));
// system.name(contract.address, "main");
// let track = system.track(contract);
// await contract.send(owner, { value: toNano(1) }, { $$type: "Deploy", queryId: 0n });
// await system.run();
// expect(track.collect()).toMatchInlineSnapshot(`
// [
// {
// "$seq": 0,
// "events": [
// {
// "$type": "deploy",
// },
// {
// "$type": "received",
// "message": {
// "body": {
// "cell": "x{946A98B60000000000000000}",
// "type": "cell",
// },
// "bounce": true,
// "from": "@treasure(owner)",
// "to": "@main",
// "type": "internal",
// "value": 1000000000n,
// },
// },
// {
// "$type": "processed",
// "gasUsed": 8307n,
// },
// {
// "$type": "sent",
// "messages": [
// {
// "body": {
// "cell": "x{AFF90F570000000000000000}",
// "type": "cell",
// },
// "bounce": true,
// "from": "@main",
// "to": "@treasure(owner)",
// "type": "internal",
// "value": 990497000n,
// },
// ],
// },
// ],
// },
// ]
// `);
// // Check counter
// expect(await contract.getCounter()).toEqual(0n);
// // Increment counter
// await contract.send(owner, { value: toNano(1) }, "increment");
// await system.run();
// expect(track.collect()).toMatchInlineSnapshot(`
// [
// {
// "$seq": 1,
// "events": [
// {
// "$type": "received",
// "message": {
// "body": {
// "text": "increment",
// "type": "text",
// },
// "bounce": true,
// "from": "@treasure(owner)",
// "to": "@main",
// "type": "internal",
// "value": 1000000000n,
// },
// },
// {
// "$type": "processed",
// "gasUsed": 5495n,
// },
// ],
// },
// ]
// `);
// // Check counter
// expect(await contract.getCounter()).toEqual(1n);
// // Non-owner
// await contract.send(nonOwner, { value: toNano(1) }, "increment");
// await system.run();
// expect(track.collect()).toMatchInlineSnapshot(`
// [
// {
// "$seq": 2,
// "events": [
// {
// "$type": "received",
// "message": {
// "body": {
// "text": "increment",
// "type": "text",
// },
// "bounce": true,
// "from": "@treasure(non-owner)",
// "to": "@main",
// "type": "internal",
// "value": 1000000000n,
// },
// },
// {
// "$type": "failed",
// "errorCode": 4429,
// "errorMessage": "Invalid sender",
// },
// {
// "$type": "sent-bounced",
// "message": {
// "body": {
// "cell": "x{FFFFFFFF00000000696E6372656D656E74}",
// "type": "cell",
// },
// "bounce": false,
// "from": "@main",
// "to": "@treasure(non-owner)",
// "type": "internal",
// "value": 994873000n,
// },
// },
// ],
// },
// ]
// `);
// });
});