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

Fix bug: allow multiple jettons to share the same symbol #6

Merged
merged 1 commit into from
Oct 30, 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
31 changes: 18 additions & 13 deletions jetton_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const jettonPath = "https://raw.githubusercontent.com/tonkeeper/ton-assets/main/
type JettonVerifier struct {
// mu protects Jettons
mu sync.RWMutex
jettons map[string]jetton
jettons map[string]map[tongo.AccountID]jetton
}

type jetton struct {
Expand All @@ -31,7 +31,8 @@ type jetton struct {

func NewJettonVerifier() *JettonVerifier {
verifier := JettonVerifier{
jettons: map[string]jetton{},
// we have valid jettons sharing the same symbol
jettons: map[string]map[tongo.AccountID]jetton{},
}
go verifier.run()
return &verifier
Expand All @@ -54,12 +55,14 @@ func (verifier *JettonVerifier) run() {
}

func (verifier *JettonVerifier) updateJettons(knownJettons []jetton) {
jettons := make(map[string]jetton, len(knownJettons))
jettons := make(map[string]map[tongo.AccountID]jetton, len(knownJettons))
for _, item := range knownJettons {
normalized := normalizeString(item.Symbol)
jettons[normalized] = item
if _, ok := jettons[normalized]; !ok {
jettons[normalized] = make(map[tongo.AccountID]jetton)
}
jettons[normalized][item.Address] = item
}

verifier.mu.Lock()
defer verifier.mu.Unlock()
verifier.jettons = jettons
Expand All @@ -73,15 +76,17 @@ func (verifier *JettonVerifier) IsBlacklisted(address tongo.AccountID, symbol st
}
verifier.mu.RLock()
defer verifier.mu.RUnlock()
for normalizedSymbol, jetton := range verifier.jettons {
if jetton.Address == address && normalizedSymbol == symbol {
return false
}
if normalizedSymbol == symbol {
return true
}

jettons, ok := verifier.jettons[symbol]
if !ok {
// no jettons with such symbol
return false
}
if _, ok := jettons[address]; ok {
// this jetton is in our list of well-known jettons
return false
}
return false
return true
}

func downloadJettons() ([]jetton, error) {
Expand Down
11 changes: 11 additions & 0 deletions jetton_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var (
Symbol: "jUSDT",
Address: tongo.MustParseAccountID("0:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2"),
},
{
Name: "Valid duplicate of jUSDT",
Symbol: "jUSDT",
Address: tongo.MustParseAccountID("-1:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2"),
},
{
Name: "Ambra",
Symbol: "AMBR",
Expand Down Expand Up @@ -61,6 +66,12 @@ func TestJettonVerifier_IsSimilarToWellKnownSymbol(t *testing.T) {
address: ton.MustParseAccountID("0:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2"),
wantBlacklisted: false,
},
{
name: "valid duplicate of jUSDT",
symbol: "jUSDT",
address: ton.MustParseAccountID("-1:729c13b6df2c07cbf0a06ab63d34af454f3d320ec1bcd8fb5c6d24d0806a17c2"),
wantBlacklisted: false,
},
{
name: "jUSDT but with different address",
symbol: "jUSDT",
Expand Down