Skip to content

Commit

Permalink
create a score field for jetton info
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Nov 11, 2024
1 parent 63d4d18 commit 2b80bc5
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 15 deletions.
8 changes: 8 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3710,6 +3710,10 @@
"example": true,
"type": "boolean"
},
"score": {
"format": "int32",
"type": "integer"
},
"total_supply": {
"example": "5887105890579978",
"type": "string",
Expand Down Expand Up @@ -3848,6 +3852,10 @@
"example": "Wrapped TON",
"type": "string"
},
"score": {
"format": "int32",
"type": "integer"
},
"symbol": {
"example": "WTON",
"type": "string"
Expand Down
6 changes: 6 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5417,6 +5417,9 @@ components:
$ref: '#/components/schemas/JettonVerificationType'
custom_payload_api_uri: # todo: maybe remove
type: string
score:
type: integer
format: int32
JettonBalance:
type: object
required:
Expand Down Expand Up @@ -6969,6 +6972,9 @@ components:
type: integer
format: int32
example: 2000
score:
type: integer
format: int32
JettonHolders:
type: object
required:
Expand Down
21 changes: 14 additions & 7 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func (h *Handler) convertRisk(ctx context.Context, risk wallet.Risk, walletAddre
}
jettonWallet := jettonWallets[0]
meta := h.GetJettonNormalizedMetadata(ctx, jettonWallet.JettonAddress)
preview := jettonPreview(jettonWallet.JettonAddress, meta)
score, _ := h.score.GetJettonScore(jettonWallet.JettonAddress)
preview := jettonPreview(jettonWallet.JettonAddress, meta, score)
jettonQuantity := oas.JettonQuantity{
Quantity: quantity.String(),
WalletAddress: convertAccountAddress(jettonWallet.Address, h.addressBook),
Expand Down Expand Up @@ -176,7 +177,8 @@ func (h *Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLang

func (h *Handler) convertActionJettonTransfer(ctx context.Context, t *bath.JettonTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptJettonTransferAction, oas.ActionSimplePreview) {
meta := h.GetJettonNormalizedMetadata(ctx, t.Jetton)
preview := jettonPreview(t.Jetton, meta)
score, _ := h.score.GetJettonScore(t.Jetton)
preview := jettonPreview(t.Jetton, meta, score)
var action oas.OptJettonTransferAction
action.SetTo(oas.JettonTransferAction{
Amount: g.Pointer(big.Int(t.Amount)).String(),
Expand Down Expand Up @@ -214,7 +216,8 @@ func (h *Handler) convertActionJettonTransfer(ctx context.Context, t *bath.Jetto

func (h *Handler) convertActionJettonMint(ctx context.Context, m *bath.JettonMintAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptJettonMintAction, oas.ActionSimplePreview) {
meta := h.GetJettonNormalizedMetadata(ctx, m.Jetton)
preview := jettonPreview(m.Jetton, meta)
score, _ := h.score.GetJettonScore(m.Jetton)
preview := jettonPreview(m.Jetton, meta, score)
var action oas.OptJettonMintAction
action.SetTo(oas.JettonMintAction{
Amount: g.Pointer(big.Int(m.Amount)).String(),
Expand Down Expand Up @@ -436,7 +439,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
action.JettonMint, action.SimplePreview = h.convertActionJettonMint(ctx, a.JettonMint, acceptLanguage.Value, viewer)
case bath.JettonBurn:
meta := h.GetJettonNormalizedMetadata(ctx, a.JettonBurn.Jetton)
preview := jettonPreview(a.JettonBurn.Jetton, meta)
score, _ := h.score.GetJettonScore(a.JettonBurn.Jetton)
preview := jettonPreview(a.JettonBurn.Jetton, meta, score)
action.JettonBurn.SetTo(oas.JettonBurnAction{
Amount: g.Pointer(big.Int(a.JettonBurn.Amount)).String(),
Sender: convertAccountAddress(a.JettonBurn.Sender, h.addressBook),
Expand Down Expand Up @@ -611,7 +615,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
} else {
swapAction.AmountIn = a.JettonSwap.In.Amount.String()
jettonInMeta := h.GetJettonNormalizedMetadata(ctx, a.JettonSwap.In.JettonMaster)
preview := jettonPreview(a.JettonSwap.In.JettonMaster, jettonInMeta)
score, _ := h.score.GetJettonScore(a.JettonSwap.In.JettonMaster)
preview := jettonPreview(a.JettonSwap.In.JettonMaster, jettonInMeta, score)
swapAction.JettonMasterIn.SetTo(preview)
simplePreviewData["JettonIn"] = preview.GetSymbol()
simplePreviewData["AmountIn"] = ScaleJettons(a.JettonSwap.In.Amount, jettonInMeta.Decimals).String()
Expand All @@ -623,7 +628,8 @@ func (h *Handler) convertAction(ctx context.Context, viewer *tongo.AccountID, a
} else {
swapAction.AmountOut = a.JettonSwap.Out.Amount.String()
jettonOutMeta := h.GetJettonNormalizedMetadata(ctx, a.JettonSwap.Out.JettonMaster)
preview := jettonPreview(a.JettonSwap.Out.JettonMaster, jettonOutMeta)
score, _ := h.score.GetJettonScore(a.JettonSwap.Out.JettonMaster)
preview := jettonPreview(a.JettonSwap.Out.JettonMaster, jettonOutMeta, score)
swapAction.JettonMasterOut.SetTo(preview)
simplePreviewData["JettonOut"] = preview.GetSymbol()
simplePreviewData["AmountOut"] = ScaleJettons(a.JettonSwap.Out.Amount, jettonOutMeta.Decimals).String()
Expand Down Expand Up @@ -777,7 +783,8 @@ func (h *Handler) toEvent(ctx context.Context, trace *core.Trace, result *bath.A
continue
}
meta := h.GetJettonNormalizedMetadata(ctx, jettonMaster)
previews[jettonMaster] = jettonPreview(jettonMaster, meta)
score, _ := h.score.GetJettonScore(jettonMaster)
previews[jettonMaster] = jettonPreview(jettonMaster, meta, score)
}
}
for accountID, flow := range result.ValueFlow.Accounts {
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/tonkeeper/opentonapi/pkg/chainstate"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/rates"
"github.com/tonkeeper/opentonapi/pkg/score"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/contract/dns"
"github.com/tonkeeper/tongo/tep64"
Expand Down Expand Up @@ -40,6 +41,7 @@ type Handler struct {
limits Limits
spamFilter SpamFilter
ratesSource ratesSource
score scoreSource
metaCache metadataCache
tonConnect *tonconnect.Server

Expand Down Expand Up @@ -80,6 +82,7 @@ type Options struct {
tonConnectSecret string
ctxToDetails ctxToDetails
gasless Gasless
score scoreSource
}

type Option func(o *Options)
Expand Down Expand Up @@ -149,6 +152,12 @@ func WithGasless(gasless Gasless) Option {
}
}

func WithScore(score scoreSource) Option {
return func(o *Options) {
o.score = score
}
}

func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
options := &Options{}
for _, o := range opts {
Expand Down Expand Up @@ -194,6 +203,9 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
if err != nil {
return nil, fmt.Errorf("failed to init tonconnect")
}
if options.score == nil {
options.score = score.NewScore()
}
return &Handler{
logger: logger,
storage: options.storage,
Expand All @@ -205,6 +217,7 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
spamFilter: options.spamFilter,
ctxToDetails: options.ctxToDetails,
gasless: options.gasless,
score: options.score,
ratesSource: rates.InitCalculator(options.ratesSource),
metaCache: metadataCache{
collectionsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "nft_metadata_cache"),
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ type ratesSource interface {
GetMarketsTonPrice() ([]rates.Market, error)
}

type scoreSource interface {
GetJettonScore(masterID ton.AccountID) (int32, error)
}

type SpamFilter interface {
CheckActions(actions []oas.Action, viewer *ton.AccountID) bool
JettonTrust(address tongo.AccountID, symbol, name, image string) core.TrustType
Expand Down
16 changes: 12 additions & 4 deletions pkg/api/jetton_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/tonkeeper/tongo/ton"
)

func jettonPreview(master ton.AccountID, meta NormalizedMetadata) oas.JettonPreview {
func jettonPreview(master ton.AccountID, meta NormalizedMetadata, score int32) oas.JettonPreview {
preview := oas.JettonPreview{
Address: master.ToRaw(),
Name: meta.Name,
Expand All @@ -27,6 +27,9 @@ func jettonPreview(master ton.AccountID, meta NormalizedMetadata) oas.JettonPrev
if meta.CustomPayloadApiUri != "" {
preview.CustomPayloadAPIURI = oas.NewOptString(meta.CustomPayloadApiUri)
}
if score != 0 {
preview.Score = oas.NewOptInt32(score)
}
return preview
}

Expand Down Expand Up @@ -163,20 +166,25 @@ func (h *Handler) convertJettonBalance(ctx context.Context, wallet core.JettonWa
}
normalizedMetadata = NormalizeMetadata(meta, nil, trust)
}
jettonBalance.Jetton = jettonPreview(wallet.JettonAddress, normalizedMetadata)
score, _ := h.score.GetJettonScore(wallet.JettonAddress)
jettonBalance.Jetton = jettonPreview(wallet.JettonAddress, normalizedMetadata, score)

return jettonBalance, nil
}

func (h *Handler) convertJettonInfo(ctx context.Context, master core.JettonMaster, holders map[tongo.AccountID]int32) oas.JettonInfo {
func (h *Handler) convertJettonInfo(ctx context.Context, master core.JettonMaster, holders map[tongo.AccountID]int32, score int32) oas.JettonInfo {
meta := h.GetJettonNormalizedMetadata(ctx, master.Address)
metadata := jettonMetadata(master.Address, meta)
return oas.JettonInfo{
info := oas.JettonInfo{
Mintable: master.Mintable,
TotalSupply: master.TotalSupply.String(),
Metadata: metadata,
Verification: oas.JettonVerificationType(meta.Verification),
HoldersCount: holders[master.Address],
Admin: convertOptAccountAddress(master.Admin, h.addressBook),
}
if score != 0 {
info.Score = oas.NewOptInt32(score)
}
return info
}
6 changes: 4 additions & 2 deletions pkg/api/jetton_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ func (h *Handler) GetJettons(ctx context.Context, params oas.GetJettonsParams) (
}
results := make([]oas.JettonInfo, len(jettons))
for idx, master := range jettons {
results[idx] = h.convertJettonInfo(ctx, master, holders)
score, _ := h.score.GetJettonScore(master.Address)
results[idx] = h.convertJettonInfo(ctx, master, holders, score)
}
return &oas.Jettons{Jettons: results}, nil
}
Expand Down Expand Up @@ -293,7 +294,8 @@ func (h *Handler) GetJettonInfosByAddresses(ctx context.Context, request oas.Opt
}
results := make([]oas.JettonInfo, len(jettons))
for idx, master := range jettons {
results[idx] = h.convertJettonInfo(ctx, master, jettonsHolders)
score, _ := h.score.GetJettonScore(master.Address)
results[idx] = h.convertJettonInfo(ctx, master, jettonsHolders, score)
}

return &oas.Jettons{Jettons: results}, nil
Expand Down
38 changes: 36 additions & 2 deletions pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2b80bc5

Please sign in to comment.