-
Notifications
You must be signed in to change notification settings - Fork 16
/
helpers.go
314 lines (287 loc) · 6.56 KB
/
helpers.go
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
package tontest
import (
"crypto/rand"
"fmt"
"time"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
)
type accountBuilder struct {
state *tlb.AccountStatus
balance *tlb.Grams
code *boc.Cell
data *boc.Cell
lastLT *uint64
lastHash *tlb.Bits256
workchain *int32
address *ton.AccountID
frozenHash *tlb.Bits256
}
func Account() accountBuilder {
return accountBuilder{}
}
func (b accountBuilder) Address(a ton.AccountID) accountBuilder {
b.address = &a
return b
}
func (b accountBuilder) Balance(grams tlb.Grams) accountBuilder {
b.balance = &grams
return b
}
func (b accountBuilder) State(state tlb.AccountStatus) accountBuilder {
b.state = &state
return b
}
func (b accountBuilder) StateInit(code, data *boc.Cell) accountBuilder {
b.code = code
b.data = data
return b
}
func (b accountBuilder) Last(lt uint64, hash tlb.Bits256) accountBuilder {
b.lastLT = <
b.lastHash = &hash
return b
}
func (b accountBuilder) MustShardAccount() tlb.ShardAccount {
a, err := b.ShardAccount()
if err != nil {
panic(err)
}
return a
}
func (b accountBuilder) ShardAccount() (tlb.ShardAccount, error) {
// todo: conflicts checks
// frozen without hash
// frozen + data/code
// uninit + data/code
//etc
if (b.state == nil || *b.state == tlb.AccountNone) &&
b.balance == nil &&
b.code == nil &&
b.data == nil &&
b.lastHash == nil &&
b.lastLT == nil {
return tlb.ShardAccount{
Account: tlb.Account{
SumType: "AccountNone",
},
}, nil
}
var address *ton.AccountID
if b.address != nil {
address = b.address
}
if address == nil && (b.code != nil || b.data != nil) {
var workchain int32
if b.workchain != nil {
workchain = *b.workchain
}
var err error
address, err = initToAddress(workchain, b.code, b.data)
if err != nil {
return tlb.ShardAccount{}, err
}
}
if address == nil {
return tlb.ShardAccount{}, fmt.Errorf("account should have address or stateInit")
}
var balance tlb.Grams
if b.balance != nil {
balance = *b.balance
}
lastLt := uint64(100500)
if b.lastLT != nil {
lastLt = *b.lastLT
}
state := tlb.AccountState{
SumType: "AccountUninit",
}
if b.state != nil {
if *b.state == tlb.AccountActive {
state.SumType = "AccountActive"
if b.code != nil {
state.AccountActive.StateInit.Code.Exists = true
state.AccountActive.StateInit.Code.Value.Value = *b.code
}
if b.data != nil {
state.AccountActive.StateInit.Data.Exists = true
state.AccountActive.StateInit.Data.Value.Value = *b.data
}
}
//todo: frozen
}
account := tlb.ShardAccount{
Account: tlb.Account{
SumType: "Account",
Account: tlb.ExistedAccount{
Addr: address.ToMsgAddress(),
StorageStat: tlb.StorageInfo{
LastPaid: uint32(time.Now().Unix()),
},
Storage: tlb.AccountStorage{
LastTransLt: lastLt,
Balance: tlb.CurrencyCollection{
Grams: balance,
},
State: state,
},
},
},
LastTransLt: lastLt - 1, //todo: investigate lt number
}
if b.lastHash != nil {
account.LastTransHash = *b.lastHash
} else {
rand.Read(account.LastTransHash[:])
}
return account, nil
}
func MustAnyToCell(a any) *boc.Cell {
c, err := AnyToCell(a)
if err != nil {
panic(err)
}
return c
}
func AnyToCell(a any) (*boc.Cell, error) {
cell := boc.NewCell()
var err error
switch c := a.(type) {
case []byte:
cells, err := boc.DeserializeBoc(c)
if err != nil {
return nil, err
}
return cells[0], nil
case string:
return boc.DeserializeSinglRootBase64(c)
default:
err = tlb.Marshal(cell, a)
return cell, err
}
}
type MsgType int
const External MsgType = 0
const Internal MsgType = 1
type messageBuilder struct {
msgType MsgType
to *ton.AccountID
from ton.AccountID
value tlb.Grams
bounce bool
bounced bool
body, data, code *boc.Cell
}
func NewMessage() messageBuilder {
return messageBuilder{}
}
func (b messageBuilder) To(a ton.AccountID) messageBuilder {
b.to = &a
return b
}
func (b messageBuilder) Internal(from ton.AccountID, value tlb.Grams) messageBuilder {
b.msgType = Internal
b.value = value
b.from = from
return b
}
func (b messageBuilder) WithBody(body *boc.Cell) messageBuilder {
b.body = body
return b
}
func (b messageBuilder) WithInit(code, data *boc.Cell) messageBuilder {
b.code = code
b.data = data
return b
}
func (b messageBuilder) MustMessage() tlb.Message {
m, err := b.Message()
if err != nil {
panic(err)
}
return m
}
func (b messageBuilder) Message() (tlb.Message, error) {
var m tlb.Message
if b.code != nil {
m.Init.Exists = true
m.Init.Value.Value.Code.Exists = true
m.Init.Value.Value.Code.Value.Value = *b.code
}
if b.data != nil {
m.Init.Exists = true
m.Init.Value.Value.Data.Exists = true
m.Init.Value.Value.Data.Value.Value = *b.data
}
if b.to == nil && !m.Init.Exists {
return tlb.Message{}, fmt.Errorf("can't determine address for message")
} else if b.to == nil && m.Init.Exists {
var err error
b.to, err = initToAddress(0, b.code, b.data)
if err != nil {
return tlb.Message{}, err
}
}
switch b.msgType {
case External:
m.Info.SumType = "ExtInMsgInfo"
m.Info.ExtInMsgInfo = &struct {
Src, Dest tlb.MsgAddress
ImportFee tlb.Grams
}{Src: tlb.MsgAddress{SumType: "AddrNone"}, Dest: b.to.ToMsgAddress()}
case Internal:
m.Info.SumType = "IntMsgInfo"
m.Info.IntMsgInfo = &struct {
IhrDisabled bool
Bounce bool
Bounced bool
Src tlb.MsgAddress
Dest tlb.MsgAddress
Value tlb.CurrencyCollection
IhrFee tlb.Grams
FwdFee tlb.Grams
CreatedLt uint64
CreatedAt uint32
}{
IhrDisabled: true,
Bounce: b.bounce,
Bounced: b.bounced,
Src: b.from.ToMsgAddress(),
Dest: b.to.ToMsgAddress(),
Value: tlb.CurrencyCollection{Grams: b.value},
IhrFee: 0,
FwdFee: 0,
CreatedLt: 100500,
CreatedAt: uint32(time.Now().Unix())}
}
if b.body != nil {
m.Body.Value = tlb.Any(*b.body)
}
return m, nil
}
func initToAddress(workchain int32, code, data *boc.Cell) (*ton.AccountID, error) {
initState := boc.NewCell()
s := tlb.StateInit{}
if code != nil {
s.Code.Exists = true
s.Code.Value.Value = *code
}
if data != nil {
s.Data.Exists = true
s.Data.Value.Value = *data
}
err := tlb.Marshal(initState, s)
if err != nil {
return nil, err
}
hash, err := initState.Hash()
if err != nil {
return nil, err
}
a := ton.AccountID{Workchain: workchain}
copy(a.Address[:], hash)
code.ResetCounters()
data.ResetCounters()
return &a, nil
}