Skip to content

Commit

Permalink
Merge pull request #190 from tonkeeper/jetton_spam_detector
Browse files Browse the repository at this point in the history
create jetton spam detector
  • Loading branch information
mr-tron authored Sep 12, 2023
2 parents 8e1e629 + 11553e8 commit be69eac
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.8.4
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230911133119-702402af5714
github.com/tonkeeper/tongo v1.2.3-0.20230906163250-fcf4e9b56841
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/metric v1.16.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269 h1:roJPVpmFnUZX6YCUqzoD5+0+Z2ruuuXmxWKS+IH3O2s=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269/go.mod h1:VGp8QednbWkKHcpQVlWyO0XSqAA0cR6d9wEdrDmHbbA=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230911103049-ddd89c5c0c4e h1:rePeUIT9A9FrJH3X6usFgdxvrnRPU+pmJ1R2HqBmhp4=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230911103049-ddd89c5c0c4e/go.mod h1:JS7CStRKXjl+nZtRk+f0PReL+mVityMP4O3HupHfyfg=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230911133119-702402af5714 h1:HwtfwtwZK6OyeqvTCqFTxiY8p7gPSS23pV8nir/tKmk=
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230911133119-702402af5714/go.mod h1:JS7CStRKXjl+nZtRk+f0PReL+mVityMP4O3HupHfyfg=
github.com/tonkeeper/tongo v1.2.3-0.20230906163250-fcf4e9b56841 h1:ls2rVf+DV45opFCm/fLSfztjx3JeeDYza5rrOnh1kBw=
github.com/tonkeeper/tongo v1.2.3-0.20230906163250-fcf4e9b56841/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand Down
3 changes: 2 additions & 1 deletion pkg/addressbook/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type AttachedAccount struct {
Name string `json:"name"`
Preview string `json:"preview"`
Wallet string `json:"wallet"`
Normalized string
Symbol string `json:"-"`
Normalized string `json:"-"`
}

type JettonVerificationType string
Expand Down
10 changes: 10 additions & 0 deletions pkg/api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"sort"

rules "github.com/tonkeeper/scam_backoffice_rules"
"github.com/tonkeeper/tongo/code"
"github.com/tonkeeper/tongo/utils"

Expand Down Expand Up @@ -180,6 +181,15 @@ func (h Handler) SearchAccounts(ctx context.Context, params oas.SearchAccountsPa
accounts := h.addressBook.SearchAttachedAccountsByPrefix(params.Name)
var response oas.FoundAccounts
for _, account := range accounts {
if account.Symbol != "" {
accountID, err := tongo.ParseAccountID(account.Wallet)
if err != nil {
continue
}
if h.spamFilter.CheckJettonAction(accountID, account.Symbol) == rules.Drop {
continue
}
}
response.Addresses = append(response.Addresses, oas.FoundAccountsAddressesItem{
Address: account.Wallet,
Name: account.Name,
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/event_converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func signedValue(value string, viewer *tongo.AccountID, source, destination tong
func (h Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptTonTransferAction, oas.ActionSimplePreview, bool) {
var spamDetected bool
if t.Amount < int64(ton.OneTON) && t.Comment != nil {
if spamAction := rules.CheckAction(h.spamRules(), *t.Comment); spamAction == rules.Drop {
if spamAction := rules.CheckAction(h.spamFilter.GetRules(), *t.Comment); spamAction == rules.Drop {
*t.Comment = ""
spamDetected = true
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func (h Handler) convertActionTonTransfer(t *bath.TonTransferAction, acceptLangu
func (h Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptNftItemTransferAction, oas.ActionSimplePreview, bool) {
var spamDetected bool
if t.Comment != nil {
if spamAction := rules.CheckAction(h.spamRules(), *t.Comment); spamAction == rules.Drop {
if spamAction := rules.CheckAction(h.spamFilter.GetRules(), *t.Comment); spamAction == rules.Drop {
spamDetected = true
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (h Handler) convertActionNftTransfer(t *bath.NftTransferAction, acceptLangu
func (h Handler) convertActionJettonTransfer(ctx context.Context, t *bath.JettonTransferAction, acceptLanguage string, viewer *tongo.AccountID) (oas.OptJettonTransferAction, oas.ActionSimplePreview, bool) {
var spamDetected bool
if t.Comment != nil {
if spamAction := rules.CheckAction(h.spamRules(), *t.Comment); spamAction == rules.Drop {
if spamAction := rules.CheckAction(h.spamFilter.GetRules(), *t.Comment); spamAction == rules.Drop {
spamDetected = true
}
}
Expand Down
22 changes: 9 additions & 13 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"

"github.com/tonkeeper/opentonapi/pkg/chainstate"

"github.com/go-faster/errors"
"github.com/tonkeeper/opentonapi/pkg/chainstate"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/rates"
rules "github.com/tonkeeper/scam_backoffice_rules"
"github.com/tonkeeper/opentonapi/pkg/spam"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/contract/dns"
"github.com/tonkeeper/tongo/tep64"
Expand All @@ -35,7 +34,7 @@ type Handler struct {
executor executor
dns *dns.DNS
limits Limits
spamRules func() rules.Rules
spamFilter spamFilter
ratesSource ratesSource
metaCache metadataCache
mempoolEmulate mempoolEmulate
Expand All @@ -50,7 +49,7 @@ type Options struct {
msgSender messageSender
executor executor
limits Limits
spamRules func() rules.Rules
spamFilter spamFilter
ratesSource ratesSource
tonConnectSecret string
}
Expand Down Expand Up @@ -92,9 +91,9 @@ func WithLimits(limits Limits) Option {
}
}

func WithSpamRules(spamRules func() rules.Rules) Option {
func WithSpamFilter(spamFilter spamFilter) Option {
return func(o *Options) {
o.spamRules = spamRules
o.spamFilter = spamFilter
}
}

Expand Down Expand Up @@ -128,11 +127,8 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
if options.chainState == nil {
options.chainState = chainstate.NewChainState(options.storage)
}
if options.spamRules == nil {
options.spamRules = func() rules.Rules {
defaultRules := rules.GetDefaultRules()
return defaultRules
}
if options.spamFilter == nil {
options.spamFilter = spam.NewSpamFilter()
}
if options.ratesSource == nil {
options.ratesSource = rates.Mock{Storage: options.storage}
Expand All @@ -154,7 +150,7 @@ func NewHandler(logger *zap.Logger, opts ...Option) (*Handler, error) {
executor: options.executor,
dns: dnsClient,
limits: options.limits,
spamRules: options.spamRules,
spamFilter: options.spamFilter,
ratesSource: rates.InitCalculator(options.ratesSource),
metaCache: metadataCache{
collectionsCache: cache.NewLRUCache[tongo.AccountID, tep64.Metadata](10000, "nft_metadata_cache"),
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ed25519"
"sync"

rules "github.com/tonkeeper/scam_backoffice_rules"
"github.com/tonkeeper/tongo/boc"

"github.com/tonkeeper/tongo"
Expand Down Expand Up @@ -148,6 +149,11 @@ type ratesSource interface {
GetRatesChart(token string, currency string, startDate *int64, endDate *int64) ([][]any, error)
}

type spamFilter interface {
GetRules() rules.Rules
CheckJettonAction(address tongo.AccountID, symbol string) rules.TypeOfAction
}

type metadataCache struct {
collectionsCache cache.Cache[tongo.AccountID, tep64.Metadata]
jettonsCache cache.Cache[tongo.AccountID, tep64.Metadata]
Expand Down
5 changes: 3 additions & 2 deletions pkg/core/attached_account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

type AttachedAccount struct {
Name string
Image string
Name string
Symbol string
Image string
}
26 changes: 26 additions & 0 deletions pkg/spam/spam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package spam

import (
rules "github.com/tonkeeper/scam_backoffice_rules"
"github.com/tonkeeper/tongo"
)

type SpamFilter struct {
Rules rules.Rules
JettonEvaluate *rules.JettonEvaluate
}

func NewSpamFilter() *SpamFilter {
return &SpamFilter{
Rules: rules.GetDefaultRules(),
JettonEvaluate: rules.NewJettonEvaluate(),
}
}

func (s *SpamFilter) GetRules() rules.Rules {
return s.Rules
}

func (s *SpamFilter) CheckJettonAction(address tongo.AccountID, symbol string) rules.TypeOfAction {
return s.JettonEvaluate.SearchAction(address, symbol)
}

0 comments on commit be69eac

Please sign in to comment.