Skip to content

Commit 33089a0

Browse files
Merge branch 'jetton_score'
1 parent c471dec commit 33089a0

10 files changed

+140
-29
lines changed

api/openapi.json

+8
Original file line numberDiff line numberDiff line change
@@ -3822,6 +3822,10 @@
38223822
"example": "https://cache.tonapi.io/images/jetton.jpg",
38233823
"type": "string"
38243824
},
3825+
"score": {
3826+
"format": "int32",
3827+
"type": "integer"
3828+
},
38253829
"total_supply": {
38263830
"example": "5887105890579978",
38273831
"type": "string",
@@ -3962,6 +3966,10 @@
39623966
"example": "Wrapped TON",
39633967
"type": "string"
39643968
},
3969+
"score": {
3970+
"format": "int32",
3971+
"type": "integer"
3972+
},
39653973
"symbol": {
39663974
"example": "WTON",
39673975
"type": "string"

api/openapi.yml

+6
Original file line numberDiff line numberDiff line change
@@ -5436,6 +5436,9 @@ components:
54365436
$ref: '#/components/schemas/JettonVerificationType'
54375437
custom_payload_api_uri: # todo: maybe remove
54385438
type: string
5439+
score:
5440+
type: integer
5441+
format: int32
54395442
JettonBalance:
54405443
type: object
54415444
required:
@@ -7041,6 +7044,9 @@ components:
70417044
type: integer
70427045
format: int32
70437046
example: 2000
7047+
score:
7048+
type: integer
7049+
format: int32
70447050
JettonHolders:
70457051
type: object
70467052
required:

pkg/api/event_converters.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func (h *Handler) convertRisk(ctx context.Context, risk wallet.Risk, walletAddre
7474
}
7575
jettonWallet := jettonWallets[0]
7676
meta := h.GetJettonNormalizedMetadata(ctx, jettonWallet.JettonAddress)
77-
preview := jettonPreview(jettonWallet.JettonAddress, meta)
77+
score, _ := h.score.GetJettonScore(jettonWallet.JettonAddress)
78+
preview := jettonPreview(jettonWallet.JettonAddress, meta, score)
7879
jettonQuantity := oas.JettonQuantity{
7980
Quantity: quantity.String(),
8081
WalletAddress: convertAccountAddress(jettonWallet.Address, h.addressBook),
@@ -203,7 +204,8 @@ func (h *Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLang
203204

204205
func (h *Handler) convertActionJettonTransfer(ctx context.Context, t *bath.JettonTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptJettonTransferAction, oas.ActionSimplePreview) {
205206
meta := h.GetJettonNormalizedMetadata(ctx, t.Jetton)
206-
preview := jettonPreview(t.Jetton, meta)
207+
score, _ := h.score.GetJettonScore(t.Jetton)
208+
preview := jettonPreview(t.Jetton, meta, score)
207209
var action oas.OptJettonTransferAction
208210
action.SetTo(oas.JettonTransferAction{
209211
Amount: g.Pointer(big.Int(t.Amount)).String(),
@@ -241,7 +243,8 @@ func (h *Handler) convertActionJettonTransfer(ctx context.Context, t *bath.Jetto
241243

242244
func (h *Handler) convertActionJettonMint(ctx context.Context, m *bath.JettonMintAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptJettonMintAction, oas.ActionSimplePreview) {
243245
meta := h.GetJettonNormalizedMetadata(ctx, m.Jetton)
244-
preview := jettonPreview(m.Jetton, meta)
246+
score, _ := h.score.GetJettonScore(m.Jetton)
247+
preview := jettonPreview(m.Jetton, meta, score)
245248
var action oas.OptJettonMintAction
246249
action.SetTo(oas.JettonMintAction{
247250
Amount: g.Pointer(big.Int(m.Amount)).String(),
@@ -465,7 +468,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
465468
action.JettonMint, action.SimplePreview = h.convertActionJettonMint(ctx, a.JettonMint, acceptLanguage.Value, viewer)
466469
case bath.JettonBurn:
467470
meta := h.GetJettonNormalizedMetadata(ctx, a.JettonBurn.Jetton)
468-
preview := jettonPreview(a.JettonBurn.Jetton, meta)
471+
score, _ := h.score.GetJettonScore(a.JettonBurn.Jetton)
472+
preview := jettonPreview(a.JettonBurn.Jetton, meta, score)
469473
action.JettonBurn.SetTo(oas.JettonBurnAction{
470474
Amount: g.Pointer(big.Int(a.JettonBurn.Amount)).String(),
471475
Sender: convertAccountAddress(a.JettonBurn.Sender, h.addressBook),
@@ -640,7 +644,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
640644
} else {
641645
swapAction.AmountIn = a.JettonSwap.In.Amount.String()
642646
jettonInMeta := h.GetJettonNormalizedMetadata(ctx, a.JettonSwap.In.JettonMaster)
643-
preview := jettonPreview(a.JettonSwap.In.JettonMaster, jettonInMeta)
647+
score, _ := h.score.GetJettonScore(a.JettonSwap.In.JettonMaster)
648+
preview := jettonPreview(a.JettonSwap.In.JettonMaster, jettonInMeta, score)
644649
swapAction.JettonMasterIn.SetTo(preview)
645650
simplePreviewData["JettonIn"] = preview.GetSymbol()
646651
simplePreviewData["AmountIn"] = ScaleJettons(a.JettonSwap.In.Amount, jettonInMeta.Decimals).String()
@@ -652,7 +657,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
652657
} else {
653658
swapAction.AmountOut = a.JettonSwap.Out.Amount.String()
654659
jettonOutMeta := h.GetJettonNormalizedMetadata(ctx, a.JettonSwap.Out.JettonMaster)
655-
preview := jettonPreview(a.JettonSwap.Out.JettonMaster, jettonOutMeta)
660+
score, _ := h.score.GetJettonScore(a.JettonSwap.Out.JettonMaster)
661+
preview := jettonPreview(a.JettonSwap.Out.JettonMaster, jettonOutMeta, score)
656662
swapAction.JettonMasterOut.SetTo(preview)
657663
simplePreviewData["JettonOut"] = preview.GetSymbol()
658664
simplePreviewData["AmountOut"] = ScaleJettons(a.JettonSwap.Out.Amount, jettonOutMeta.Decimals).String()
@@ -806,7 +812,8 @@ func (h *Handler) toEvent(ctx context.Context, trace *core.Trace, result *bath.A
806812
continue
807813
}
808814
meta := h.GetJettonNormalizedMetadata(ctx, jettonMaster)
809-
previews[jettonMaster] = jettonPreview(jettonMaster, meta)
815+
score, _ := h.score.GetJettonScore(jettonMaster)
816+
previews[jettonMaster] = jettonPreview(jettonMaster, meta, score)
810817
}
811818
}
812819
for accountID, flow := range result.ValueFlow.Accounts {

pkg/api/handler.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/tonkeeper/opentonapi/pkg/chainstate"
1010
"github.com/tonkeeper/opentonapi/pkg/core"
1111
"github.com/tonkeeper/opentonapi/pkg/rates"
12+
"github.com/tonkeeper/opentonapi/pkg/score"
1213
"github.com/tonkeeper/opentonapi/pkg/verifier"
1314
"github.com/tonkeeper/tongo"
1415
"github.com/tonkeeper/tongo/contract/dns"
@@ -41,6 +42,7 @@ type Handler struct {
4142
limits Limits
4243
spamFilter SpamFilter
4344
ratesSource ratesSource
45+
score scoreSource
4446
metaCache metadataCache
4547
tonConnect *tonconnect.Server
4648
verifierSource verifierSource
@@ -83,6 +85,7 @@ type Options struct {
8385
ctxToDetails ctxToDetails
8486
gasless Gasless
8587
verifier verifierSource
88+
score scoreSource
8689
}
8790

8891
type Option func(o *Options)
@@ -158,6 +161,12 @@ func WithVerifier(verifier verifierSource) Option {
158161
}
159162
}
160163

164+
func WithScore(score scoreSource) Option {
165+
return func(o *Options) {
166+
o.score = score
167+
}
168+
}
169+
161170
func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
162171
options := &Options{}
163172
for _, o := range opts {
@@ -206,6 +215,9 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
206215
if err != nil {
207216
return nil, fmt.Errorf("failed to init tonconnect")
208217
}
218+
if options.score == nil {
219+
options.score = score.NewScore()
220+
}
209221
return &Handler{
210222
logger: logger,
211223
storage: options.storage,
@@ -217,8 +229,9 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
217229
spamFilter: options.spamFilter,
218230
ctxToDetails: options.ctxToDetails,
219231
gasless: options.gasless,
220-
verifierSource: options.verifier,
232+
score: options.score,
221233
ratesSource: rates.InitCalculator(options.ratesSource),
234+
verifierSource: options.verifier,
222235
metaCache: metadataCache{
223236
collectionsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "nft_metadata_cache"),
224237
jettonsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "jetton_metadata_cache"),

pkg/api/interfaces.go

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ type ratesSource interface {
179179
GetMarketsTonPrice() ([]rates.Market, error)
180180
}
181181

182+
type scoreSource interface {
183+
GetJettonScore(masterID ton.AccountID) (int32, error)
184+
}
185+
182186
type SpamFilter interface {
183187
CheckActions(actions []oas.Action, viewer *ton.AccountID, initiator ton.AccountID) bool
184188
JettonTrust(address tongo.AccountID, symbol, name, image string) core.TrustType

pkg/api/jetton_converters.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/tonkeeper/tongo/ton"
1616
)
1717

18-
func jettonPreview(master ton.AccountID, meta NormalizedMetadata) oas.JettonPreview {
18+
func jettonPreview(master ton.AccountID, meta NormalizedMetadata, score int32) oas.JettonPreview {
1919
preview := oas.JettonPreview{
2020
Address: master.ToRaw(),
2121
Name: meta.Name,
@@ -27,6 +27,9 @@ func jettonPreview(master ton.AccountID, meta NormalizedMetadata) oas.JettonPrev
2727
if meta.CustomPayloadApiUri != "" {
2828
preview.CustomPayloadAPIURI = oas.NewOptString(meta.CustomPayloadApiUri)
2929
}
30+
if score != 0 {
31+
preview.Score = oas.NewOptInt32(score)
32+
}
3033
return preview
3134
}
3235

@@ -163,15 +166,16 @@ func (h *Handler) convertJettonBalance(ctx context.Context, wallet core.JettonWa
163166
}
164167
normalizedMetadata = NormalizeMetadata(meta, nil, trust)
165168
}
166-
jettonBalance.Jetton = jettonPreview(wallet.JettonAddress, normalizedMetadata)
169+
score, _ := h.score.GetJettonScore(wallet.JettonAddress)
170+
jettonBalance.Jetton = jettonPreview(wallet.JettonAddress, normalizedMetadata, score)
167171

168172
return jettonBalance, nil
169173
}
170174

171-
func (h *Handler) convertJettonInfo(ctx context.Context, master core.JettonMaster, holders map[tongo.AccountID]int32) oas.JettonInfo {
175+
func (h *Handler) convertJettonInfo(ctx context.Context, master core.JettonMaster, holders map[tongo.AccountID]int32, score int32) oas.JettonInfo {
172176
meta := h.GetJettonNormalizedMetadata(ctx, master.Address)
173177
metadata := jettonMetadata(master.Address, meta)
174-
return oas.JettonInfo{
178+
info := oas.JettonInfo{
175179
Mintable: master.Mintable,
176180
TotalSupply: master.TotalSupply.String(),
177181
Metadata: metadata,
@@ -180,4 +184,8 @@ func (h *Handler) convertJettonInfo(ctx context.Context, master core.JettonMaste
180184
Admin: convertOptAccountAddress(master.Admin, h.addressBook),
181185
Preview: meta.PreviewImage,
182186
}
187+
if score != 0 {
188+
info.Score = oas.NewOptInt32(score)
189+
}
190+
return info
183191
}

pkg/api/jetton_handlers.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,20 @@ func (h *Handler) GetJettonInfo(ctx context.Context, params oas.GetJettonInfoPar
7171
if err != nil {
7272
return nil, toError(http.StatusBadRequest, err)
7373
}
74-
meta := h.GetJettonNormalizedMetadata(ctx, account.ID)
75-
metadata := jettonMetadata(account.ID, meta)
76-
data, err := h.storage.GetJettonMasterData(ctx, account.ID)
74+
master, err := h.storage.GetJettonMasterData(ctx, account.ID)
7775
if errors.Is(err, core.ErrEntityNotFound) {
7876
return nil, toError(http.StatusNotFound, err)
7977
}
8078
if err != nil {
8179
return nil, toError(http.StatusInternalServerError, err)
8280
}
83-
holdersCount, err := h.storage.GetJettonsHoldersCount(ctx, []tongo.AccountID{account.ID})
81+
holders, err := h.storage.GetJettonsHoldersCount(ctx, []tongo.AccountID{account.ID})
8482
if err != nil {
8583
return nil, toError(http.StatusInternalServerError, err)
8684
}
87-
return &oas.JettonInfo{
88-
Mintable: data.Mintable,
89-
TotalSupply: data.TotalSupply.String(),
90-
Metadata: metadata,
91-
Verification: oas.JettonVerificationType(meta.Verification),
92-
HoldersCount: holdersCount[account.ID],
93-
Admin: convertOptAccountAddress(data.Admin, h.addressBook),
94-
Preview: meta.PreviewImage,
95-
}, nil
85+
score, _ := h.score.GetJettonScore(account.ID)
86+
converted := h.convertJettonInfo(ctx, master, holders, score)
87+
return &converted, nil
9688
}
9789

9890
func (h *Handler) GetAccountJettonsHistory(ctx context.Context, params oas.GetAccountJettonsHistoryParams) (*oas.AccountEvents, error) {
@@ -163,7 +155,8 @@ func (h *Handler) GetJettons(ctx context.Context, params oas.GetJettonsParams) (
163155
}
164156
results := make([]oas.JettonInfo, len(jettons))
165157
for idx, master := range jettons {
166-
results[idx] = h.convertJettonInfo(ctx, master, holders)
158+
score, _ := h.score.GetJettonScore(master.Address)
159+
results[idx] = h.convertJettonInfo(ctx, master, holders, score)
167160
}
168161
return &oas.Jettons{Jettons: results}, nil
169162
}
@@ -294,7 +287,8 @@ func (h *Handler) GetJettonInfosByAddresses(ctx context.Context, request oas.Opt
294287
}
295288
results := make([]oas.JettonInfo, len(jettons))
296289
for idx, master := range jettons {
297-
results[idx] = h.convertJettonInfo(ctx, master, jettonsHolders)
290+
score, _ := h.score.GetJettonScore(master.Address)
291+
results[idx] = h.convertJettonInfo(ctx, master, jettonsHolders, score)
298292
}
299293

300294
return &oas.Jettons{Jettons: results}, nil

pkg/oas/oas_json_gen.go

+36-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)