-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.spec.ts
87 lines (70 loc) · 3.09 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
import { toNano } from "@ton/core";
import { Blockchain } from "@ton/sandbox";
import "@ton/test-utils";
import { JettonDex, AddJetton } from "./output/sample_JettonDex"; // Ensure correct imports
import { findErrorCodeByMessage } from './utils/error';
describe("JettonDex Contract", () => {
it("should deploy correctly and handle operations", async () => {
// Create a new blockchain sandbox
let system = await Blockchain.create();
// Create treasury wallets for the owner and another user
let owner = await system.treasury("owner");
let nonOwner = await system.treasury("non-owner");
// Initialize contract parameters
let jettonAAddress = owner.address; // Use owner.address as a placeholder for testing
let jettonBAddress = nonOwner.address;
console.log(`Owner : ${jettonAAddress}`);
console.log(`Receiver : ${jettonBAddress}`);
// Deploy the JettonDex contract
let contract = system.openContract(await JettonDex.fromInit(
owner.address,
jettonAAddress,
jettonBAddress
));
const deployResult = await contract.send(owner.getSender(), {
value: toNano(1)
}, {
$$type: "Deploy",
queryId: 0n
});
// Verify successful deployment
expect(deployResult.transactions).toHaveTransaction({
from: owner.address,
to: contract.address,
deploy: true,
success: true,
});
console.log("********* BEFORE ADD ***********");
console.log(`Jetton balance ${await contract.getGetJettonABalance()}`);
console.log("Add 10 Jetton")
// Check initial Jetton A balance
expect(await contract.getGetJettonABalance()).toEqual(0n);
// Simulate the owner adding Jettons using the AddJetton message type
const addJettonMessage: AddJetton = {
$$type: "AddJetton",
amount: 10n // Use bigint notation for the amount
};
await contract.send(owner.getSender(), {
value: toNano(1)
}, addJettonMessage);
console.log("********* AFTER ADD ************");
console.log(`After add : Jetton balance ${await contract.getGetJettonABalance()}`);
// Verify Jetton balance update
expect(await contract.getGetJettonABalance()).toBeGreaterThan(0n);
// Non-owner tries to add Jettons - should fail
const nonOwnerResult = await contract.send(nonOwner.getSender(), {
value: toNano(1)
}, addJettonMessage);
// Find the error code for invalid sender and ensure it's a number
const errorCodeForInvalidSender = findErrorCodeByMessage(
contract.abi.errors,
"Invalid sender"
) ?? 0; // Fallback to a default error code 0 or adjust as needed
expect(nonOwnerResult.transactions).toHaveTransaction({
from: nonOwner.address,
to: contract.address,
success: false,
exitCode: errorCodeForInvalidSender
});
});
});