-
Notifications
You must be signed in to change notification settings - Fork 77
/
account_converters.go
110 lines (105 loc) · 3.42 KB
/
account_converters.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
package api
import (
"fmt"
"sort"
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/opentonapi/pkg/addressbook"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/oas"
)
func convertToRawAccount(account *core.Account) (oas.BlockchainRawAccount, error) {
rawAccount := oas.BlockchainRawAccount{
Address: account.AccountAddress.ToRaw(),
Balance: account.TonBalance,
LastTransactionLt: int64(account.LastTransactionLt),
Status: oas.AccountStatus(account.Status),
Storage: oas.AccountStorageInfo{
UsedCells: account.Storage.UsedCells.Int64(),
UsedBits: account.Storage.UsedBits.Int64(),
UsedPublicCells: account.Storage.UsedPublicCells.Int64(),
LastPaid: int64(account.Storage.LastPaid),
DuePayment: account.Storage.DuePayment,
},
}
if account.LastTransactionHash != [32]byte{} {
rawAccount.LastTransactionHash = oas.NewOptString(account.LastTransactionHash.Hex())
}
if account.FrozenHash != nil {
rawAccount.FrozenHash = oas.NewOptString(account.FrozenHash.Hex())
}
if len(account.Libraries) > 0 {
rawAccount.Libraries = make([]oas.BlockchainRawAccountLibrariesItem, 0, len(account.Libraries))
keys := make([]string, 0, len(account.Libraries))
values := make(map[string]*core.SimpleLib, len(account.Libraries))
for key, value := range account.Libraries {
hex := key.Hex()
keys = append(keys, hex)
values[hex] = value
}
sort.Strings(keys)
for _, key := range keys {
value := values[key]
base64, err := value.Root.ToBocBase64()
if err != nil {
// this should never happen, but anyway
return oas.BlockchainRawAccount{}, err
}
rawAccount.Libraries = append(rawAccount.Libraries, oas.BlockchainRawAccountLibrariesItem{
Public: value.Public,
Root: base64,
})
}
}
if account.ExtraBalances != nil {
balances := make(map[string]string, len(account.ExtraBalances))
for key, value := range account.ExtraBalances {
balances[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", value)
}
rawAccount.ExtraBalance = oas.NewOptBlockchainRawAccountExtraBalance(balances)
}
if account.Code != nil && len(account.Code) != 0 {
rawAccount.Code = oas.NewOptString(fmt.Sprintf("%x", account.Code[:]))
}
if account.Data != nil {
rawAccount.Data = oas.NewOptString(fmt.Sprintf("%x", account.Data[:]))
}
return rawAccount, nil
}
func convertToAccount(account *core.Account, ab *addressbook.KnownAddress, state chainState) oas.Account {
acc := oas.Account{
Address: account.AccountAddress.ToRaw(),
Balance: account.TonBalance,
LastActivity: account.LastActivityTime,
Status: oas.AccountStatus(account.Status),
Interfaces: make([]string, len(account.Interfaces)),
GetMethods: account.GetMethods,
}
for i, iface := range account.Interfaces {
acc.Interfaces[i] = iface.String()
}
if state.CheckIsSuspended(account.AccountAddress) {
acc.IsSuspended.SetTo(true)
}
if account.Status == tlb.AccountUninit || account.Status == tlb.AccountNone {
acc.IsWallet = true
}
for _, i := range account.Interfaces {
if i.Implements(abi.Wallet) {
acc.IsWallet = true
break
}
}
if ab == nil {
return acc
}
acc.IsScam = oas.NewOptBool(ab.IsScam)
if len(ab.Name) > 0 {
acc.Name = oas.NewOptString(ab.Name)
}
if len(ab.Image) > 0 {
acc.Icon = oas.NewOptString(ab.Image)
}
acc.MemoRequired = oas.NewOptBool(ab.RequireMemo)
return acc
}