forked from tact-lang/tact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-jetton.tact
334 lines (284 loc) · 10.4 KB
/
sample-jetton.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
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
import "@stdlib/ownable";
message Mint {
amount: Int;
receiver: Address;
}
struct JettonData {
totalSupply: Int;
mintable: Bool;
owner: Address;
content: Cell;
walletCode: Cell;
}
contract SampleJetton with Jetton {
totalSupply: Int as coins;
max_supply: Int as coins;
owner: Address;
content: Cell;
mintable: Bool;
init(owner: Address, content: Cell, max_supply: Int) {
self.totalSupply = 0;
self.max_supply = max_supply;
self.owner = owner;
self.mintable = true;
self.content = content;
}
receive(msg: Mint) {
let ctx: Context = context();
require(ctx.sender == self.owner, "Not Owner");
require(self.mintable, "Can't Mint Anymore");
self.mint(msg.receiver, msg.amount, self.owner); //(to, amount, response_destination)
}
receive("Mint: 100") { // Public Minting
let ctx: Context = context();
require(self.mintable, "Can't Mint Anymore");
self.mint(ctx.sender, 100, self.owner);
}
receive("Owner: MintClose") {
let ctx: Context = context();
require(ctx.sender == self.owner, "Not Owner");
self.mintable = false;
}
}
// ============================================================================================================ //
@interface("org.ton.jetton.master")
trait Jetton with Ownable {
totalSupply: Int; // Already set initially
max_supply: Int;
mintable: Bool;
owner: Address;
content: Cell;
receive(msg: TokenUpdateContent) {
self.requireOwner(); // Allow changing content only by owner
self.content = msg.content; // Update content
}
receive(msg: TokenBurnNotification) {
self.requireWallet(msg.owner); // Check wallet
self.totalSupply = self.totalSupply - msg.amount; // Update supply
if (msg.response_destination != null) { // Cashback
send(SendParameters{
to: msg.response_destination!!,
value: 0,
bounce: false,
mode: SendRemainingValue | SendIgnoreErrors,
body: TokenExcesses{
queryId: msg.queryId
}.toCell()
});
}
}
// Private Methods //
// @to The Address receive the Jetton token after minting
// @amount The amount of Jetton token being minted
// @response_destination The previous owner address
fun mint(to: Address, amount: Int, response_destination: Address) {
require(self.totalSupply + amount <= self.max_supply, "The total supply will be overlapping.");
self.totalSupply = self.totalSupply + amount; // Update total supply
let wInit: StateInit = self.getJettonWalletInit(to); // Create message
send(SendParameters{
to: contractAddress(wInit),
value: 0,
bounce: false,
mode: SendRemainingValue,
body: TokenTransferInternal{
queryId: 0,
amount: amount,
from: myAddress(),
response_destination: response_destination,
forward_ton_amount: 0,
forward_payload: emptySlice()
}.toCell(),
code: wInit.code,
data: wInit.data
});
}
fun requireWallet(owner: Address) {
let ctx: Context = context();
let wInit: StateInit = self.getJettonWalletInit(owner);
require(contractAddress(wInit) == ctx.sender, "Invalid sender");
}
virtual fun getJettonWalletInit(address: Address): StateInit {
return initOf JettonDefaultWallet(myAddress(), address);
}
// Get Methods //
get fun get_jetton_data(): JettonData {
let code: Cell = self.getJettonWalletInit(myAddress()).code;
return JettonData{
totalSupply: self.totalSupply,
mintable: self.mintable,
owner: self.owner,
content: self.content,
walletCode: code
};
}
get fun get_wallet_address(owner: Address): Address {
let wInit: StateInit = self.getJettonWalletInit(owner);
return contractAddress(wInit);
}
}
message(0xf8a7ea5) TokenTransfer {
queryId: Int as uint64;
amount: Int as coins;
destination: Address;
response_destination: Address?;
custom_payload: Cell?;
forward_ton_amount: Int as coins;
forward_payload: Slice as remaining;
}
message(0x178d4519) TokenTransferInternal {
queryId: Int as uint64;
amount: Int as coins;
from: Address;
response_destination: Address?;
forward_ton_amount: Int as coins;
forward_payload: Slice as remaining;
}
message(0x7362d09c) TokenNotification {
queryId: Int as uint64;
amount: Int as coins;
from: Address;
forward_payload: Slice as remaining; // Comment Text message when Transfer the jetton
}
message(0x595f07bc) TokenBurn {
queryId: Int as uint64;
amount: Int as coins;
owner: Address;
response_destination: Address;
}
message(0x7bdd97de) TokenBurnNotification {
queryId: Int as uint64;
amount: Int as coins;
owner: Address;
response_destination: Address?;
}
message(0xd53276db) TokenExcesses {
queryId: Int as uint64;
}
message TokenUpdateContent {
content: Cell;
}
// ============================================================ //
@interface("org.ton.jetton.wallet")
contract JettonDefaultWallet {
const minTonsForStorage: Int = ton("0.01");
const gasConsumption: Int = ton("0.01");
balance: Int;
owner: Address;
master: Address;
init(master: Address, owner: Address) {
self.balance = 0;
self.owner = owner;
self.master = master;
}
receive(msg: TokenTransfer) { // 0xf8a7ea5
let ctx: Context = context(); // Check sender
require(ctx.sender == self.owner, "Invalid sender");
let fwdFee: Int = ctx.readForwardFee() + ctx.readForwardFee(); // Gas checks
let final: Int = 2 * self.gasConsumption + self.minTonsForStorage + fwdFee;
require(ctx.value > min(final, ton("0.01")), "Invalid value!!");
self.balance = self.balance - msg.amount; // Update balance
require(self.balance >= 0, "Invalid balance");
let init: StateInit = initOf JettonDefaultWallet(self.master, msg.destination);
let walletAddress: Address = contractAddress(init);
send(SendParameters{
to: walletAddress,
value: 0,
mode: SendRemainingValue,
bounce: false,
body: TokenTransferInternal{
queryId: msg.queryId,
amount: msg.amount,
from: self.owner,
response_destination: msg.response_destination,
forward_ton_amount: msg.forward_ton_amount,
forward_payload: msg.forward_payload
}.toCell(),
code: init.code,
data: init.data
});
}
receive(msg: TokenTransferInternal) { // 0x178d4519
let ctx: Context = context();
if (ctx.sender != self.master) {
let sInit: StateInit = initOf JettonDefaultWallet(self.master, msg.from);
require(contractAddress(sInit) == ctx.sender, "Invalid sender!");
}
self.balance = self.balance + msg.amount;
require(self.balance >= 0, "Invalid balance"); // Update balance
if (msg.forward_ton_amount > 0) {
send(SendParameters{
to: self.owner,
value: msg.forward_ton_amount,
bounce: false,
body: TokenNotification { // 0x7362d09c - notify new owner
queryId: msg.queryId,
amount: msg.amount,
from: msg.from,
forward_payload: msg.forward_payload
}.toCell()
});
}
let msgValue: Int = self.msgValue(ctx.value); // Get value for gas
let fwdFee: Int = ctx.readForwardFee();
msgValue = msgValue - msg.forward_ton_amount - fwdFee;
// msgValue = msgValue - msg.forward_ton_amount - min(fwdFee, ton("0.01"));
if (msg.response_destination != null) { // Cashback
send(SendParameters {
to: msg.response_destination!!,
value: msgValue,
bounce: false,
body: TokenExcesses { // 0xd53276db
queryId: msg.queryId
}.toCell(),
mode: SendIgnoreErrors
});
}
}
get fun msgValue(value: Int): Int {
let tonBalanceBeforeMsg: Int = myBalance() - value;
let storageFee: Int = self.minTonsForStorage - min(tonBalanceBeforeMsg, self.minTonsForStorage);
value -= storageFee + self.gasConsumption;
return value;
}
receive(msg: TokenBurn) {
let ctx: Context = context();
require(ctx.sender == self.owner, "Invalid sender"); // Check sender
self.balance = self.balance - msg.amount; // Update balance
require(self.balance >= 0, "Invalid balance");
let fwdFee: Int = ctx.readForwardFee(); // Gas checks
require(ctx.value > fwdFee + 2 * self.gasConsumption + self.minTonsForStorage, "Invalid value - Burn");
send(SendParameters{ // Burn tokens
to: self.master,
value: 0,
mode: SendRemainingValue,
bounce: true,
body: TokenBurnNotification{
queryId: msg.queryId,
amount: msg.amount,
owner: self.owner,
response_destination: self.owner
}.toCell()
});
}
bounced(msg: Slice) {
let op: Int = msg.loadUint(32);
let queryId: Int = msg.loadUint(64);
let jettonAmount: Int = msg.loadCoins();
require(op == 0x178d4519 || op == 0x7bdd97de, "Invalid bounced message");
self.balance = self.balance + jettonAmount; // Update balance
}
get fun get_wallet_data(): JettonWalletData {
return JettonWalletData{
balance: self.balance,
owner: self.owner,
master: self.master,
walletCode: (initOf JettonDefaultWallet(self.master, self.owner)).code
};
}
}
struct JettonWalletData {
balance: Int;
owner: Address;
master: Address;
walletCode: Cell;
}