forked from ton-blockchain/liquid-staking-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.func
More file actions
115 lines (88 loc) · 4.11 KB
/
messages.func
File metadata and controls
115 lines (88 loc) · 4.11 KB
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
;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool src:MsgAddress -> 011000
const int msgflag::NON_BOUNCEABLE = 0x10;
const int msgflag::BOUNCEABLE = 0x18;
const int sendmode::REGULAR = 0;
const int sendmode::PAY_FEES_SEPARETELY = 1;
const int sendmode::IGNORE_ERRORS = 2;
const int sendmode::DESTROY = 32;
const int sendmode::CARRY_ALL_REMAINING_MESSAGE_VALUE = 64;
const int sendmode::CARRY_ALL_BALANCE = 128;
builder store_msg_flags(builder b, int msg_flag) inline { return b.store_uint(msg_flag, 6); }
{-
Helpers below fill in default/overwritten values of message layout:
Relevant part of TL-B schema:
... other:ExtraCurrencyCollection ihr_fee:Grams fwd_fee:Grams created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
bits 1 4 4 64 32
... init:(Maybe (Either StateInit ^StateInit)) body:(Either X ^X) = Message X;
bits 1 1(if prev is true) 1
-}
builder store_msgbody_prefix_stateinit(builder b, cell state_init, cell ref) inline {
return b.store_uint(4 + 2 + 1, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1).store_ref(state_init).store_ref(ref);
}
builder store_msgbody_prefix_stateinit_slice(builder b, cell state_init) inline {
return b.store_uint(4 + 2 + 0, 1 + 4 + 4 + 64 + 32 + 1 + 1 + 1).store_ref(state_init);
}
builder store_msgbody_prefix_slice(builder b) inline {
return b.store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1);
}
builder store_msgbody_prefix_ref(builder b, cell ref) inline {
return b.store_uint(1, 1 + 4 + 4 + 64 + 32 + 1 + 1).store_ref(ref);
}
{-
addr_std$10 anycast:(Maybe Anycast)
workchain_id:int8 address:bits256 = MsgAddressInt;
-}
builder store_masterchain_address(builder b, int address_hash) inline {
return b.store_uint(4, 3).store_workchain(MASTERCHAIN).store_uint(address_hash, 256);
}
() send_msg(slice to_address, int amount, cell payload, int flags, int send_mode) impure inline_ref {
int has_payload = ~ cell_null?(payload);
builder msg = begin_cell()
.store_msg_flags(flags)
.store_slice(to_address)
.store_coins(amount);
if (has_payload) {
msg = msg.store_msgbody_prefix_ref(payload);
} else {
msg = msg.store_msgbody_prefix_slice();
}
send_raw_message(msg.end_cell(), send_mode);
}
() send_msg_builder(slice to_address, int amount, builder payload, int flags, int send_mode) impure inline_ref {
int payload_length = payload.null?() ? 0 : payload.builder_bits();
builder msg = begin_cell()
.store_msg_flags(flags)
.store_slice(to_address)
.store_coins(amount);
if (payload_length + msg.builder_bits() > 1023 - (1 + 4 + 4 + 64 + 32 + 1 + 1)) {
msg = msg.store_msgbody_prefix_ref(begin_cell().store_builder(payload).end_cell());
} else {
msg = msg.store_msgbody_prefix_slice()
.store_builder(payload);
}
send_raw_message(msg.end_cell(), send_mode);
}
() send_excesses(slice sender_address) impure inline_ref {
send_msg(
sender_address,
0, ;; value
null(),
msgflag::NON_BOUNCEABLE,
sendmode::CARRY_ALL_REMAINING_MESSAGE_VALUE | sendmode::IGNORE_ERRORS); ;; non-bouneable, remaining inbound message amount, fee deducted from amount, ignore errors
}
() emit_log (int topic, builder data) impure inline {
;; 1023 - (4+2+9+256+64+32+2) = 654 bit free
var msg = begin_cell()
.store_uint (12, 4) ;; ext_out_msg_info$11 src:MsgAddressInt ()
.store_uint (1, 2) ;; addr_extern$01
.store_uint (256, 9) ;; len:(## 9)
.store_uint(topic, 256); ;; external_address:(bits len)
if (data.builder_bits() > 1023 - (4 + 2 + 9 + 256 + 64 + 32 + 2) ) {
msg = msg.store_uint(1, 64 + 32 + 2) ;; created_lt, created_at, init:Maybe, body:Either
.store_ref(begin_cell().store_builder(data).end_cell());
} else {
msg = msg.store_uint(0, 64 + 32 + 2) ;; created_lt, created_at, init:Maybe, body:Either
.store_builder(data);
}
send_raw_message(msg.end_cell(), sendmode::REGULAR);
}