@@ -17,6 +17,7 @@ import (
17
17
"github.com/tonkeeper/tongo"
18
18
"github.com/tonkeeper/tongo/abi"
19
19
"github.com/tonkeeper/tongo/tlb"
20
+ "github.com/tonkeeper/tongo/ton"
20
21
"go.uber.org/zap"
21
22
"golang.org/x/exp/maps"
22
23
"golang.org/x/exp/slices"
@@ -282,42 +283,62 @@ func (m *manualAddresser) SearchAttachedAccounts(prefix string) []AttachedAccoun
282
283
}
283
284
284
285
// refreshAddresses updates the list of known addresses
285
- func (m * manualAddresser ) refreshAddresses (addressPath string ) error {
286
+ func (m * manualAddresser ) refreshAddresses (addressPath , jettonPath string ) error {
286
287
addresses , err := downloadJson [KnownAddress ](addressPath )
287
288
if err != nil {
288
289
return err
289
290
}
290
- knownAccounts := make (map [tongo.AccountID ]KnownAddress , len (addresses ))
291
- attachedAccounts := make ([]AttachedAccount , 0 , len (addresses )* 3 )
292
- for _ , item := range addresses {
293
- account , err := tongo .ParseAddress (item .Address )
294
- if err != nil {
295
- continue
296
- }
297
- item .Address = account .ID .ToRaw ()
298
- knownAccounts [account .ID ] = item
291
+ jettons , err := downloadJson [KnownJetton ](jettonPath )
292
+ if err != nil {
293
+ return err
294
+ }
295
+ knownAccounts := make (map [tongo.AccountID ]KnownAddress )
296
+ var attachedAccounts []AttachedAccount
297
+ process := func (accountID tongo.AccountID , name , image string , accountType AttachedAccountType ) error {
299
298
// Generate name variants for the account
300
- slugs := GenerateSlugVariants (item . Name )
299
+ slugs := GenerateSlugVariants (name )
301
300
for _ , slug := range slugs {
302
301
weight := KnownAccountWeight
303
302
// Convert known account to attached account
304
- attachedAccount , err := ConvertAttachedAccount (item . Name , slug , item . Image , account . ID , weight , core .TrustWhitelist , ManualAccountType )
303
+ attachedAccount , err := ConvertAttachedAccount (name , slug , image , accountID , weight , core .TrustWhitelist , accountType )
305
304
if err != nil {
306
305
continue
307
306
}
308
307
attachedAccounts = append (attachedAccounts , attachedAccount )
309
308
}
309
+ return nil
310
310
}
311
+ for _ , item := range addresses {
312
+ accountID , err := ton .ParseAccountID (item .Address )
313
+ if err != nil {
314
+ return err
315
+ }
316
+ item .Address = accountID .ToRaw ()
317
+ knownAccounts [accountID ] = item
318
+ err = process (accountID , item .Name , item .Image , ManualAccountType )
319
+ if err != nil {
320
+ continue
321
+ }
322
+ }
323
+ for _ , jetton := range jettons {
324
+ accountID , err := ton .ParseAccountID (jetton .Address )
325
+ if err != nil {
326
+ return err
327
+ }
328
+ err = process (accountID , jetton .Name , jetton .Image , JettonNameAccountType )
329
+ if err != nil {
330
+ continue
331
+ }
332
+ }
333
+ // Sort the attached accounts by their normalized names
334
+ sort .Slice (attachedAccounts , func (i , j int ) bool {
335
+ return attachedAccounts [i ].Normalized < attachedAccounts [j ].Normalized
336
+ })
311
337
312
338
m .mu .Lock ()
313
- defer m .mu .Unlock ()
314
339
m .addresses = knownAccounts
315
- sorted := m .sorted
316
- sorted = append (sorted , attachedAccounts ... )
317
- sort .Slice (sorted , func (i , j int ) bool {
318
- return sorted [i ].Normalized < sorted [j ].Normalized
319
- })
320
- m .sorted = sorted
340
+ m .sorted = attachedAccounts
341
+ m .mu .Unlock ()
321
342
322
343
return nil
323
344
}
@@ -346,8 +367,8 @@ func NewAddressBook(logger *zap.Logger, addressPath, jettonPath, collectionPath
346
367
}
347
368
// Start background refreshers
348
369
go Refresher ("gg whitelist" , time .Hour , 5 * time .Minute , logger , book .getGGWhitelist )
349
- go Refresher ("addresses" , time .Minute * 15 , 5 * time .Minute , logger , func () error { return manual .refreshAddresses (addressPath ) })
350
- go Refresher ("jettons" , time .Minute * 15 , 5 * time .Minute , logger , func () error { return book .refreshJettons (manual , jettonPath ) })
370
+ go Refresher ("addresses" , time .Minute * 15 , 5 * time .Minute , logger , func () error { return manual .refreshAddresses (addressPath , jettonPath ) })
371
+ go Refresher ("jettons" , time .Minute * 15 , 5 * time .Minute , logger , func () error { return book .refreshJettons (jettonPath ) })
351
372
go Refresher ("collections" , time .Minute * 15 , 5 * time .Minute , logger , func () error { return book .refreshCollections (collectionPath ) })
352
373
book .refreshTfPools (logger ) // Refresh tfPools once on initialization as it doesn't need periodic updates
353
374
@@ -369,47 +390,26 @@ func Refresher(name string, interval, errorInterval time.Duration, logger *zap.L
369
390
}
370
391
371
392
// refreshJettons fetches and updates the jetton data from the provided URL
372
- func (b * Book ) refreshJettons (addresser * manualAddresser , jettonPath string ) error {
393
+ func (b * Book ) refreshJettons (jettonPath string ) error {
373
394
// Download jettons data
374
395
jettons , err := downloadJson [KnownJetton ](jettonPath )
375
396
if err != nil {
376
397
return err
377
398
}
378
399
knownJettons := make (map [tongo.AccountID ]KnownJetton , len (jettons ))
379
- var attachedAccounts []AttachedAccount
380
400
for _ , item := range jettons {
381
- account , err := tongo . ParseAddress (item .Address )
401
+ accountID , err := ton . ParseAccountID (item .Address )
382
402
if err != nil {
383
403
continue
384
404
}
385
- item .Address = account .ID .ToRaw ()
386
- knownJettons [account .ID ] = item
387
- // Generate name variants for the jetton
388
- slugs := GenerateSlugVariants (item .Name )
389
- for _ , slug := range slugs {
390
- weight := KnownAccountWeight
391
- // Convert known account to attached account
392
- attachedAccount , err := ConvertAttachedAccount (item .Name , slug , item .Image , account .ID , weight , core .TrustWhitelist , JettonNameAccountType )
393
- if err != nil {
394
- continue
395
- }
396
- attachedAccounts = append (attachedAccounts , attachedAccount )
397
- }
405
+ item .Address = accountID .ToRaw ()
406
+ knownJettons [accountID ] = item
398
407
}
399
408
400
409
b .mu .Lock ()
401
410
b .jettons = knownJettons
402
411
b .mu .Unlock ()
403
412
404
- addresser .mu .Lock ()
405
- defer addresser .mu .Unlock ()
406
- sorted := addresser .sorted
407
- sorted = append (sorted , attachedAccounts ... )
408
- sort .Slice (sorted , func (i , j int ) bool {
409
- return sorted [i ].Normalized < sorted [j ].Normalized
410
- })
411
- addresser .sorted = sorted
412
-
413
413
return nil
414
414
}
415
415
0 commit comments