-
Notifications
You must be signed in to change notification settings - Fork 10
/
contract.tact
85 lines (74 loc) · 2.83 KB
/
contract.tact
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
import "@stdlib/deploy";
// Define messages for interacting with the contract
message SwapRequest {
amount: Int as uint32;
fromJetton: Address;
}
message AddJetton {
amount: Int as uint32;
}
// Main contract: JettonDex
contract JettonDex with Deployable {
owner: Address;
jettonABalance: Int as uint32;
jettonBBalance: Int as uint32;
jettonAAddress: Address;
jettonBAddress: Address;
// Initialize contract with owner's address and initial jetton addresses
init(owner: Address, jettonAAddress: Address, jettonBAddress: Address) {
self.owner = owner;
self.jettonABalance = 0;
self.jettonBBalance = 0;
self.jettonAAddress = jettonAAddress;
self.jettonBAddress = jettonBAddress;
}
// Function to handle adding Jettons by owner
fun addJetton(v: Int, jetton: Address) {
// Check if sender is the owner
let ctx: Context = context();
require(ctx.sender == self.owner, "Invalid sender");
// Update appropriate balance
if (jetton == self.jettonAAddress) {
self.jettonABalance += v;
} else if (jetton == self.jettonBAddress) {
self.jettonBBalance += v;
} else {
require(false, "Unsupported jetton address");
}
}
// Function to execute a jetton swap
fun executeSwap(amount: Int, fromJetton: Address) {
// Check if the swap is for supported jettons
if (fromJetton == self.jettonAAddress) {
require(amount <= self.jettonABalance, "Insufficient Jetton A balance");
let amountToTransfer = (self.jettonBBalance * amount) / self.jettonABalance;
require(amountToTransfer <= self.jettonBBalance, "Insufficient Jetton B for swap");
self.jettonABalance -= amount;
self.jettonBBalance += amountToTransfer;
} else if (fromJetton == self.jettonBAddress) {
require(amount <= self.jettonBBalance, "Insufficient Jetton B balance");
let amountToTransfer = (self.jettonABalance * amount) / self.jettonBBalance;
require(amountToTransfer <= self.jettonABalance, "Insufficient Jetton A for swap");
self.jettonBBalance -= amount;
self.jettonABalance += amountToTransfer;
} else {
require(false, "Unsupported jetton address for swap");
}
}
// Receive method for adding jettons
receive(msg: AddJetton) {
self.addJetton(msg.amount, context().sender);
}
// Receive method for executing a swap
receive(msg: SwapRequest) {
self.executeSwap(msg.amount, msg.fromJetton);
}
// Getter for Jetton A balance
get fun getJettonABalance(): Int {
return self.jettonABalance;
}
// Getter for Jetton B balance
get fun getJettonBBalance(): Int {
return self.jettonBBalance;
}
}