Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update parse address #234

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions pkg/addressbook/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/opentonapi/pkg/references"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/ton"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -231,12 +230,12 @@ func (b *Book) refreshAddresses(logger *zap.Logger, addressPath string) {
b.mu.Lock()
defer b.mu.Unlock()
for _, item := range addresses {
accountID, err := tongo.ParseAccountID(item.Address)
account, err := tongo.ParseAddress(item.Address)
if err != nil {
continue
}
item.Address = accountID.ToRaw()
b.addresses[accountID] = item
item.Address = account.ID.ToRaw()
b.addresses[account.ID] = item
}
}

Expand All @@ -249,12 +248,12 @@ func (b *Book) refreshJettons(logger *zap.Logger, jettonPath string) {
b.mu.Lock()
defer b.mu.Unlock()
for _, item := range jettons {
accountID, err := tongo.ParseAccountID(item.Address)
account, err := tongo.ParseAddress(item.Address)
if err != nil {
continue
}
item.Address = accountID.ToRaw()
b.jettons[accountID] = item
item.Address = account.ID.ToRaw()
b.jettons[account.ID] = item
}
}

Expand All @@ -275,21 +274,21 @@ func (b *Book) refreshCollections(logger *zap.Logger, collectionPath string) {
defer b.mu.Unlock()
for _, item := range collections {
// TODO: remove items that were previously added but aren't present in the current list.
accountID, err := tongo.ParseAccountID(item.Address)
account, err := tongo.ParseAddress(item.Address)
if err != nil {
continue
}
currentCollection, ok := b.collections[accountID]
currentCollection, ok := b.collections[account.ID]
if !ok {
// this is a new item, so we only add tonkeeper as approver.
item.Address = accountID.ToRaw()
item.Address = account.ID.ToRaw()
item.Approvers = unique(append(item.Approvers, oas.NftApprovedByItemTonkeeper))
b.collections[accountID] = item
b.collections[account.ID] = item
continue
}
// this is an existing item, so we merge approvers and remove duplicates adding tonkeeper.
item.Approvers = unique(append(append(currentCollection.Approvers, item.Approvers...), oas.NftApprovedByItemTonkeeper))
b.collections[accountID] = item
b.collections[account.ID] = item
}
}

Expand All @@ -298,13 +297,13 @@ func (b *Book) refreshTfPools(logger *zap.Logger) {
defer b.mu.Unlock()

for _, pool := range getPools(logger) {
accountID, err := tongo.ParseAccountID(pool.Address)
account, err := tongo.ParseAddress(pool.Address)
if err != nil {
logger.Error("failed to parse account in pools", zap.Error(err))
continue
}
pool.Address = accountID.ToRaw()
b.tfPools[accountID] = pool
pool.Address = account.ID.ToRaw()
b.tfPools[account.ID] = pool
}
}

Expand Down Expand Up @@ -381,11 +380,11 @@ func FetchGetGemsVerifiedCollections() ([]tongo.AccountID, error) {
}
accountIDs := make([]tongo.AccountID, 0, len(q.GetAddressesOfVerifiedCollections))
for _, collection := range q.GetAddressesOfVerifiedCollections {
accountID, err := ton.ParseAccountID(string(collection))
account, err := tongo.ParseAddress(string(collection))
if err != nil {
return nil, err
}
accountIDs = append(accountIDs, accountID)
accountIDs = append(accountIDs, account.ID)
}
return accountIDs, nil
}
78 changes: 39 additions & 39 deletions pkg/api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
)

func (h *Handler) GetBlockchainRawAccount(ctx context.Context, params oas.GetBlockchainRawAccountParams) (*oas.BlockchainRawAccount, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
rawAccount, err := h.storage.GetRawAccount(ctx, accountID)
rawAccount, err := h.storage.GetRawAccount(ctx, account.ID)
if errors.Is(err, core.ErrEntityNotFound) {
return nil, toError(http.StatusNotFound, err)
}
Expand All @@ -41,26 +41,26 @@ func (h *Handler) GetBlockchainRawAccount(ctx context.Context, params oas.GetBlo
}

func (h *Handler) GetAccount(ctx context.Context, params oas.GetAccountParams) (*oas.Account, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
account, err := h.storage.GetRawAccount(ctx, accountID)
rawAccount, err := h.storage.GetRawAccount(ctx, account.ID)
if errors.Is(err, core.ErrEntityNotFound) {
return &oas.Account{
Address: accountID.ToRaw(),
Address: account.ID.ToRaw(),
Status: string(tlb.AccountNone),
}, nil
}
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
ab, found := h.addressBook.GetAddressInfoByAddress(accountID)
ab, found := h.addressBook.GetAddressInfoByAddress(account.ID)
var res oas.Account
if found {
res = convertToAccount(account, &ab, h.state)
res = convertToAccount(rawAccount, &ab, h.state)
} else {
res = convertToAccount(account, nil, h.state)
res = convertToAccount(rawAccount, nil, h.state)
}
return &res, nil
}
Expand All @@ -75,12 +75,12 @@ func (h *Handler) GetAccounts(ctx context.Context, request oas.OptGetAccountsReq
var ids []tongo.AccountID
allAccountIDs := make(map[tongo.AccountID]struct{}, len(request.Value.AccountIds))
for _, str := range request.Value.AccountIds {
accountID, err := tongo.ParseAccountID(str)
account, err := tongo.ParseAddress(str)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
ids = append(ids, accountID)
allAccountIDs[accountID] = struct{}{}
ids = append(ids, account.ID)
allAccountIDs[account.ID] = struct{}{}
}
accounts, err := h.storage.GetRawAccounts(ctx, ids)
if err != nil {
Expand Down Expand Up @@ -110,14 +110,14 @@ func (h *Handler) GetAccounts(ctx context.Context, request oas.OptGetAccountsReq
}

func (h *Handler) GetBlockchainAccountTransactions(ctx context.Context, params oas.GetBlockchainAccountTransactionsParams) (*oas.Transactions, error) {
accountID, err := tongo.ParseAddress(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
if params.BeforeLt.Value == 0 {
params.BeforeLt.Value = 1 << 62
}
txs, err := h.storage.GetAccountTransactions(ctx, accountID.ID, int(params.Limit.Value), uint64(params.BeforeLt.Value), uint64(params.AfterLt.Value))
txs, err := h.storage.GetAccountTransactions(ctx, account.ID, int(params.Limit.Value), uint64(params.BeforeLt.Value), uint64(params.AfterLt.Value))
if errors.Is(err, core.ErrEntityNotFound) {
return &oas.Transactions{}, nil
}
Expand All @@ -134,7 +134,7 @@ func (h *Handler) GetBlockchainAccountTransactions(ctx context.Context, params o
}

func (h *Handler) ExecGetMethodForBlockchainAccount(ctx context.Context, params oas.ExecGetMethodForBlockchainAccountParams) (*oas.MethodExecutionResult, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
Expand All @@ -146,7 +146,7 @@ func (h *Handler) ExecGetMethodForBlockchainAccount(ctx context.Context, params
}
stack = append(stack, r)
}
exitCode, stack, err := h.executor.RunSmcMethodByID(ctx, accountID, utils.MethodIdFromName(params.MethodName), stack)
exitCode, stack, err := h.executor.RunSmcMethodByID(ctx, account.ID, utils.MethodIdFromName(params.MethodName), stack)
if err != nil {
if errors.Is(err, core.ErrEntityNotFound) {
return nil, toError(http.StatusNotFound, err)
Expand Down Expand Up @@ -186,16 +186,16 @@ func (h *Handler) SearchAccounts(ctx context.Context, params oas.SearchAccountsP
mapOfFoundAccounts = make(map[tongo.AccountID]addressbook.AttachedAccount)
)
for _, account := range accounts {
accountID, err := tongo.ParseAccountID(account.Wallet)
accountID, err := tongo.ParseAddress(account.Wallet)
if err != nil {
continue
}
if account.Symbol != "" {
if h.spamFilter.IsJettonBlacklisted(accountID, account.Symbol) {
if h.spamFilter.IsJettonBlacklisted(accountID.ID, account.Symbol) {
continue
}
}
mapOfFoundAccounts[accountID] = account
mapOfFoundAccounts[accountID.ID] = account
}
for _, account := range mapOfFoundAccounts {
response.Addresses = append(response.Addresses, oas.FoundAccountsAddressesItem{
Expand All @@ -210,26 +210,26 @@ func (h *Handler) SearchAccounts(ctx context.Context, params oas.SearchAccountsP

// ReindexAccount updates internal cache for a particular account.
func (h *Handler) ReindexAccount(ctx context.Context, params oas.ReindexAccountParams) error {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return toError(http.StatusBadRequest, err)
}
if err = h.storage.ReindexAccount(ctx, accountID); err != nil {
if err = h.storage.ReindexAccount(ctx, account.ID); err != nil {
return toError(http.StatusInternalServerError, err)
}
return nil
}

func (h *Handler) GetAccountDnsExpiring(ctx context.Context, params oas.GetAccountDnsExpiringParams) (*oas.DnsExpiring, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var period *int
if params.Period.Set {
period = &params.Period.Value
}
dnsExpiring, err := h.storage.GetDnsExpiring(ctx, accountID, period)
dnsExpiring, err := h.storage.GetDnsExpiring(ctx, account.ID, period)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand Down Expand Up @@ -270,13 +270,13 @@ func (h *Handler) GetAccountDnsExpiring(ctx context.Context, params oas.GetAccou
}

func (h *Handler) GetAccountPublicKey(ctx context.Context, params oas.GetAccountPublicKeyParams) (*oas.GetAccountPublicKeyOK, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
pubKey, err := h.storage.GetWalletPubKey(ctx, accountID)
pubKey, err := h.storage.GetWalletPubKey(ctx, account.ID)
if err != nil {
state, err := h.storage.GetRawAccount(ctx, accountID)
state, err := h.storage.GetRawAccount(ctx, account.ID)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand All @@ -289,11 +289,11 @@ func (h *Handler) GetAccountPublicKey(ctx context.Context, params oas.GetAccount
}

func (h *Handler) GetAccountSubscriptions(ctx context.Context, params oas.GetAccountSubscriptionsParams) (*oas.Subscriptions, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
subscriptions, err := h.storage.GetSubscriptions(ctx, accountID)
subscriptions, err := h.storage.GetSubscriptions(ctx, account.ID)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand All @@ -317,11 +317,11 @@ func (h *Handler) GetAccountSubscriptions(ctx context.Context, params oas.GetAcc
}

func (h *Handler) GetAccountTraces(ctx context.Context, params oas.GetAccountTracesParams) (*oas.TraceIDs, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
traceIDs, err := h.storage.SearchTraces(ctx, accountID, params.Limit.Value, nil, nil, nil, false)
traceIDs, err := h.storage.SearchTraces(ctx, account.ID, params.Limit.Value, nil, nil, nil, false)
if err != nil && !errors.Is(err, core.ErrEntityNotFound) {
return nil, toError(http.StatusInternalServerError, err)
}
Expand All @@ -340,43 +340,43 @@ func (h *Handler) GetAccountTraces(ctx context.Context, params oas.GetAccountTra
}

func (h *Handler) GetAccountDiff(ctx context.Context, params oas.GetAccountDiffParams) (*oas.GetAccountDiffOK, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
balanceChange, err := h.storage.GetAccountDiff(ctx, accountID, params.StartDate, params.EndDate)
balanceChange, err := h.storage.GetAccountDiff(ctx, account.ID, params.StartDate, params.EndDate)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
return &oas.GetAccountDiffOK{BalanceChange: balanceChange}, nil
}

func (h *Handler) GetAccountNftHistory(ctx context.Context, params oas.GetAccountNftHistoryParams) (*oas.AccountEvents, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
traceIDs, err := h.storage.GetAccountNftsHistory(ctx, accountID, params.Limit, optIntToPointer(params.BeforeLt), optIntToPointer(params.StartDate), optIntToPointer(params.EndDate))
traceIDs, err := h.storage.GetAccountNftsHistory(ctx, account.ID, params.Limit, optIntToPointer(params.BeforeLt), optIntToPointer(params.StartDate), optIntToPointer(params.EndDate))
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
events, lastLT, err := h.convertNftHistory(ctx, accountID, traceIDs, params.AcceptLanguage)
events, lastLT, err := h.convertNftHistory(ctx, account.ID, traceIDs, params.AcceptLanguage)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
return &oas.AccountEvents{Events: events, NextFrom: lastLT}, nil
}

func (h *Handler) BlockchainAccountInspect(ctx context.Context, params oas.BlockchainAccountInspectParams) (*oas.BlockchainAccountInspect, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
account, err := h.storage.GetRawAccount(ctx, accountID)
rawAccount, err := h.storage.GetRawAccount(ctx, account.ID)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
cells, err := boc.DeserializeBoc(account.Code)
cells, err := boc.DeserializeBoc(rawAccount.Code)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand All @@ -387,12 +387,12 @@ func (h *Handler) BlockchainAccountInspect(ctx context.Context, params oas.Block
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
methods, err := code.ParseContractMethods(account.Code)
methods, err := code.ParseContractMethods(rawAccount.Code)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
resp := oas.BlockchainAccountInspect{
Code: hex.EncodeToString(account.Code),
Code: hex.EncodeToString(rawAccount.Code),
CodeHash: hex.EncodeToString(codeHash),
Compiler: oas.NewOptBlockchainAccountInspectCompiler(oas.BlockchainAccountInspectCompilerFunc),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func stringToTVMStackRecord(s string) (tlb.VmStackValue, error) {
if s == "Null" {
return tlb.VmStackValue{SumType: "VmStkNull"}, nil
}
a, err := tongo.ParseAccountID(s)
account, err := tongo.ParseAddress(s)
if err == nil {
return tlb.TlbStructToVmCellSlice(a.ToMsgAddress())
return tlb.TlbStructToVmCellSlice(account.ID.ToMsgAddress())
}
if strings.HasPrefix(s, "0x") {
b, err := hex.DecodeString(s[2:])
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/dns_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (h *Handler) dnsResolver(ctx context.Context) (*dns.DNS, error) {
}

func (h *Handler) AccountDnsBackResolve(ctx context.Context, params oas.AccountDnsBackResolveParams) (*oas.DomainNames, error) {
accountID, err := tongo.ParseAccountID(params.AccountID)
account, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
domains, err := h.storage.FindAllDomainsResolvedToAddress(ctx, accountID, references.DomainSuffixes)
domains, err := h.storage.FindAllDomainsResolvedToAddress(ctx, account.ID, references.DomainSuffixes)
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}
Expand All @@ -68,7 +68,7 @@ func (h *Handler) AccountDnsBackResolve(ctx context.Context, params oas.AccountD
if err != nil || w == nil {
break
}
if *w != accountID {
if *w != account.ID {
break
}
found = true
Expand Down
Loading