Skip to content

Commit 270ddbb

Browse files
committed
ordering fix
1 parent aa92a88 commit 270ddbb

File tree

3 files changed

+67
-68
lines changed

3 files changed

+67
-68
lines changed

pkg/api/interfaces.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ type storage interface {
7575
GetJettonHolders(ctx context.Context, jettonMaster tongo.AccountID, limit, offset int) ([]core.JettonHolder, error)
7676
GetJettonMasterMetadata(ctx context.Context, master tongo.AccountID) (tongo.JettonMetadata, error)
7777
GetJettonMasterData(ctx context.Context, master tongo.AccountID) (core.JettonMaster, error)
78-
GetAccountJettonsHistory(ctx context.Context, address tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) ([]core.JettonOperation, error)
79-
GetAccountJettonHistoryByID(ctx context.Context, address, jettonMaster tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) ([]core.JettonOperation, error)
78+
GetAccountJettonsHistory(ctx context.Context, address tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) (map[core.TraceID][]core.JettonOperation, error)
79+
GetAccountJettonHistoryByID(ctx context.Context, address, jettonMaster tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) (map[core.TraceID][]core.JettonOperation, error)
8080
GetJettonTransferPayload(ctx context.Context, accountID, jettonMaster ton.AccountID) (*core.JettonTransferPayload, error)
8181

8282
GetAllAuctions(ctx context.Context) ([]core.Auction, error)

pkg/api/jetton_converters.go

+63-64
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/tonkeeper/tongo/abi"
99
"github.com/tonkeeper/tongo/tlb"
1010
"net/http"
11+
"sort"
1112
"strings"
1213

1314
"github.com/tonkeeper/opentonapi/pkg/bath"
@@ -54,85 +55,83 @@ func jettonMetadata(account ton.AccountID, meta NormalizedMetadata) oas.JettonMe
5455
return metadata
5556
}
5657

57-
func (h *Handler) convertJettonHistory(ctx context.Context, account ton.AccountID, master *ton.AccountID, history []core.JettonOperation, acceptLanguage oas.OptString) ([]oas.AccountEvent, int64, error) {
58+
func (h *Handler) convertJettonHistory(ctx context.Context, account ton.AccountID, master *ton.AccountID, history map[core.TraceID][]core.JettonOperation, acceptLanguage oas.OptString) ([]oas.AccountEvent, int64, error) {
5859
var lastLT uint64
5960
var events []oas.AccountEvent
60-
res := make(map[core.TraceID]oas.AccountEvent)
6161

62-
for _, op := range history {
63-
event, ok := res[op.TraceID]
64-
if !ok {
65-
event = oas.AccountEvent{
66-
EventID: op.TraceID.Hash.Hex(),
67-
Account: convertAccountAddress(account, h.addressBook),
68-
Timestamp: op.TraceID.UTime,
69-
IsScam: false,
70-
Lt: int64(op.TraceID.Lt),
71-
Extra: 0,
72-
}
62+
for id, ops := range history {
63+
event := oas.AccountEvent{
64+
EventID: id.Hash.Hex(),
65+
Account: convertAccountAddress(account, h.addressBook),
66+
Timestamp: id.UTime, // TODO: or first/last op Utime
67+
IsScam: false,
68+
Lt: int64(id.Lt), // TODO: or first/last op Lt
69+
Extra: 0,
7370
}
74-
75-
var action bath.Action
76-
switch op.Operation {
77-
case core.TransferJettonOperation:
78-
transferAction := bath.JettonTransferAction{
79-
Jetton: op.JettonMaster,
80-
Recipient: op.Destination,
81-
Sender: op.Source,
82-
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
71+
for _, op := range ops {
72+
var action bath.Action
73+
switch op.Operation {
74+
case core.TransferJettonOperation:
75+
transferAction := bath.JettonTransferAction{
76+
Jetton: op.JettonMaster,
77+
Recipient: op.Destination,
78+
Sender: op.Source,
79+
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
80+
}
81+
action.Type = "JettonTransfer"
82+
action.JettonTransfer = &transferAction
83+
var payload abi.JettonPayload
84+
err := json.Unmarshal([]byte(op.ForwardPayload), &payload)
85+
if err != nil {
86+
break
87+
}
88+
switch p := payload.Value.(type) {
89+
case abi.TextCommentJettonPayload:
90+
comment := string(p.Text)
91+
action.JettonTransfer.Comment = &comment
92+
case abi.EncryptedTextCommentJettonPayload:
93+
action.JettonTransfer.EncryptedComment = &bath.EncryptedComment{EncryptionType: "simple", CipherText: p.CipherText}
94+
}
95+
case core.MintJettonOperation:
96+
mintAction := bath.JettonMintAction{
97+
Jetton: op.JettonMaster,
98+
Recipient: *op.Destination,
99+
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
100+
}
101+
action.Type = "JettonMint"
102+
action.JettonMint = &mintAction
103+
case core.BurnJettonOperation:
104+
burnAction := bath.JettonBurnAction{
105+
Jetton: op.JettonMaster,
106+
Sender: *op.Source,
107+
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
108+
}
109+
action.Type = "JettonTransfer"
110+
action.JettonBurn = &burnAction
111+
default:
112+
continue
83113
}
84-
action.Type = "JettonTransfer"
85-
action.JettonTransfer = &transferAction
86-
var payload abi.JettonPayload
87-
err := json.Unmarshal([]byte(op.ForwardPayload), &payload)
114+
convertedAction, err := h.convertAction(ctx, &account, action, acceptLanguage)
88115
if err != nil {
89-
break
90-
}
91-
switch p := payload.Value.(type) {
92-
case abi.TextCommentJettonPayload:
93-
comment := string(p.Text)
94-
action.JettonTransfer.Comment = &comment
95-
case abi.EncryptedTextCommentJettonPayload:
96-
action.JettonTransfer.EncryptedComment = &bath.EncryptedComment{EncryptionType: "simple", CipherText: p.CipherText}
116+
return nil, 0, err
97117
}
98-
case core.MintJettonOperation:
99-
mintAction := bath.JettonMintAction{
100-
Jetton: op.JettonMaster,
101-
Recipient: *op.Destination,
102-
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
118+
event.Actions = append(event.Actions, convertedAction)
119+
if lastLT == 0 {
120+
lastLT = op.Lt
103121
}
104-
action.Type = "JettonMint"
105-
action.JettonMint = &mintAction
106-
case core.BurnJettonOperation:
107-
burnAction := bath.JettonBurnAction{
108-
Jetton: op.JettonMaster,
109-
Sender: *op.Source,
110-
Amount: tlb.VarUInteger16(*op.Amount.BigInt()),
122+
if op.Lt < lastLT {
123+
lastLT = op.Lt
111124
}
112-
action.Type = "JettonTransfer"
113-
action.JettonBurn = &burnAction
114-
default:
115-
continue
116-
}
117-
convertedAction, err := h.convertAction(ctx, &account, action, acceptLanguage)
118-
if err != nil {
119-
return nil, 0, err
120125
}
121-
event.Actions = append(event.Actions, convertedAction)
122-
if op.Lt > lastLT {
123-
lastLT = op.Lt
124-
}
125-
res[op.TraceID] = event
126-
}
127-
128-
for _, event := range res {
129-
event.IsScam = h.spamFilter.CheckActions(event.Actions, &account, nil)
130126
if len(event.Actions) == 0 {
131127
continue
132128
}
129+
event.IsScam = h.spamFilter.CheckActions(event.Actions, &account, nil)
133130
events = append(events, event)
134131
}
135-
132+
sort.Slice(events, func(i, j int) bool {
133+
return events[i].Lt > events[j].Lt
134+
})
136135
return events, int64(lastLT), nil
137136
}
138137

pkg/litestorage/jetton.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ func (s *LiteStorage) GetJettonMasterData(ctx context.Context, master tongo.Acco
117117
return jettonMaster, nil
118118
}
119119

120-
func (s *LiteStorage) GetAccountJettonsHistory(ctx context.Context, address tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) ([]core.JettonOperation, error) {
120+
func (s *LiteStorage) GetAccountJettonsHistory(ctx context.Context, address tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) (map[core.TraceID][]core.JettonOperation, error) {
121121
return nil, nil
122122
}
123123

124-
func (s *LiteStorage) GetAccountJettonHistoryByID(ctx context.Context, address, jettonMaster tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) ([]core.JettonOperation, error) {
124+
func (s *LiteStorage) GetAccountJettonHistoryByID(ctx context.Context, address, jettonMaster tongo.AccountID, limit int, beforeLT, startTime, endTime *int64) (map[core.TraceID][]core.JettonOperation, error) {
125125
return nil, nil
126126
}
127127

0 commit comments

Comments
 (0)