Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Jun 5, 2023
1 parent fb2bdfb commit 50817e2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 152 deletions.
141 changes: 3 additions & 138 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,20 @@ package tongo
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"strings"

"github.com/snksoft/crc"

"github.com/tonkeeper/tongo/account"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/utils"
)

// Deprecated
type AccountID struct {
Workchain int32
Address [32]byte
}
type AccountID = account.ID // deprecated
type AccountInfo = account.Info // deprecated

func NewAccountId(id int32, addr [32]byte) *AccountID {
return &AccountID{Workchain: id, Address: addr}
}

func (id AccountID) String() string {
return id.ToRaw()
}

func (id AccountID) IsZero() bool {
for i := range id.Address {
if id.Address[i] != 0 {
return false
}
}
return true
}

func (id AccountID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.ToRaw())
}

func (id *AccountID) UnmarshalJSON(data []byte) error {
a, err := ParseAccountID(strings.Trim(string(data), "\"\n "))
if err != nil {
return err
}
id.Workchain = a.Workchain
id.Address = a.Address
return nil
}

func (id AccountID) ToRaw() string {
return fmt.Sprintf("%v:%x", id.Workchain, id.Address)
}

func (id AccountID) ToHuman(bounce, testnet bool) string {
prefix := byte(0b00010001)
if testnet {
prefix |= 0b10000000
}
if !bounce {
prefix |= 0b01000000
}
buf := make([]byte, 36)
buf[0] = prefix
buf[1] = byte(id.Workchain)
copy(buf[2:34], id.Address[:])
binary.BigEndian.PutUint16(buf[34:36], utils.Crc16(buf[:34]))
return base64.URLEncoding.EncodeToString(buf)
}

func (id AccountID) MarshalTL() ([]byte, error) {
payload := make([]byte, 36)
binary.LittleEndian.PutUint32(payload[:4], uint32(id.Workchain))
copy(payload[4:36], id.Address[:])
return payload, nil
}

func (id *AccountID) UnmarshalTL(r io.Reader) error {
var b [4]byte
_, err := io.ReadFull(r, b[:])
if err != nil {
return err
}
id.Workchain = int32(binary.LittleEndian.Uint32(b[:]))
_, err = io.ReadFull(r, id.Address[:])
return err
}

func (id *AccountID) ToMsgAddress() tlb.MsgAddress {
if id == nil {
return tlb.MsgAddress{
SumType: "AddrNone",
}
}
return tlb.MsgAddress{
SumType: "AddrStd",
AddrStd: struct {
Anycast tlb.Maybe[tlb.Anycast]
WorkchainId int8
Address tlb.Bits256
}{
WorkchainId: int8(id.Workchain),
Address: id.Address,
},
}
}

type AccountInfo struct {
Status tlb.AccountStatus
Balance uint64
Data []byte
Code []byte
FrozenHash Bits256
LastTransactionLt uint64
}

func AccountIDFromBase64Url(s string) (AccountID, error) {
var aa AccountID
b, err := base64.URLEncoding.DecodeString(s)
Expand Down Expand Up @@ -172,41 +72,6 @@ func MustParseAccountID(s string) AccountID {
return aa
}

func GetAccountInfo(a tlb.Account) (AccountInfo, error) {
if a.SumType == "AccountNone" {
return AccountInfo{Status: tlb.AccountNone}, nil
}
res := AccountInfo{
Balance: uint64(a.Account.Storage.Balance.Grams),
LastTransactionLt: a.Account.Storage.LastTransLt,
}
if a.Account.Storage.State.SumType == "AccountUninit" {
res.Status = tlb.AccountUninit
return res, nil
}
if a.Account.Storage.State.SumType == "AccountFrozen" {
res.FrozenHash = Bits256(a.Account.Storage.State.AccountFrozen.StateHash)
res.Status = tlb.AccountFrozen
return res, nil
}
res.Status = tlb.AccountActive
if a.Account.Storage.State.AccountActive.StateInit.Data.Exists {
data, err := a.Account.Storage.State.AccountActive.StateInit.Data.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
}
res.Data = data
}
if a.Account.Storage.State.AccountActive.StateInit.Code.Exists {
code, err := a.Account.Storage.State.AccountActive.StateInit.Code.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
}
res.Code = code
}
return res, nil
}

// TODO: replace pointer with nullable type
func AccountIDFromTlb(a tlb.MsgAddress) (*AccountID, error) {
switch a.SumType {
Expand Down
17 changes: 8 additions & 9 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/snksoft/crc"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/utils"
)
Expand Down Expand Up @@ -107,12 +106,12 @@ func (id *ID) ToMsgAddress() tlb.MsgAddress {
}
}

type AccountInfo struct {
type Info struct {
Status tlb.AccountStatus
Balance uint64
Data []byte
Code []byte
FrozenHash tongo.Bits256
FrozenHash tlb.Bits256
LastTransactionLt uint64
}

Expand Down Expand Up @@ -171,11 +170,11 @@ func MustParseAccountID(s string) ID {
return aa
}

func GetAccountInfo(a tlb.Account) (AccountInfo, error) {
func GetAccountInfo(a tlb.Account) (Info, error) {
if a.SumType == "AccountNone" {
return AccountInfo{Status: tlb.AccountNone}, nil
return Info{Status: tlb.AccountNone}, nil
}
res := AccountInfo{
res := Info{
Balance: uint64(a.Account.Storage.Balance.Grams),
LastTransactionLt: a.Account.Storage.LastTransLt,
}
Expand All @@ -184,22 +183,22 @@ func GetAccountInfo(a tlb.Account) (AccountInfo, error) {
return res, nil
}
if a.Account.Storage.State.SumType == "AccountFrozen" {
res.FrozenHash = tongo.Bits256(a.Account.Storage.State.AccountFrozen.StateHash)
res.FrozenHash = a.Account.Storage.State.AccountFrozen.StateHash
res.Status = tlb.AccountFrozen
return res, nil
}
res.Status = tlb.AccountActive
if a.Account.Storage.State.AccountActive.StateInit.Data.Exists {
data, err := a.Account.Storage.State.AccountActive.StateInit.Data.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
return Info{}, err
}
res.Data = data
}
if a.Account.Storage.State.AccountActive.StateInit.Code.Exists {
code, err := a.Account.Storage.State.AccountActive.StateInit.Code.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
return Info{}, err
}
res.Code = code
}
Expand Down
12 changes: 7 additions & 5 deletions account/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
"fmt"

"github.com/snksoft/crc"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/contract/dns"
"github.com/tonkeeper/tongo/liteapi"
"github.com/tonkeeper/tongo/tlb"
)

type executor interface {
RunSmcMethodByID(context.Context, ID, int, tlb.VmStack) (uint32, tlb.VmStack, error)
}

type parser struct {
root ID
executor abi.Executor
executor executor
}

type Address struct {
Expand All @@ -30,7 +32,7 @@ func (p *parser) Root(root ID) *parser {
return p
}

func (p *parser) Executor(executor abi.Executor) *parser {
func (p *parser) Executor(executor executor) *parser {
p.executor = executor
return p
}
Expand Down Expand Up @@ -77,7 +79,7 @@ func (p *parser) ParseAddress(ctx context.Context, address string) (Address, err
return Address{}, err
}
}
newDns := dns.NewDNS(tongo.AccountID(p.root), p.executor)
newDns := dns.NewDNS(p.root, p.executor) // import cycle in package/dns.go
result, err := newDns.Resolve(ctx, address)
if err != nil {
return Address{}, err
Expand Down

0 comments on commit 50817e2

Please sign in to comment.