@@ -11,6 +11,7 @@ import (
1111 "strings"
1212 "time"
1313
14+ "github.com/btcsuite/btcd/btcutil/base58"
1415 "github.com/go-telegram/bot"
1516 "github.com/go-telegram/bot/models"
1617 "github.com/shopspring/decimal"
@@ -21,6 +22,11 @@ import (
2122 "github.com/v03413/bepusdt/app/log"
2223 "github.com/v03413/bepusdt/app/model"
2324 "github.com/v03413/go-cache"
25+ api2 "github.com/v03413/tronprotocol/api"
26+ "github.com/v03413/tronprotocol/core"
27+ "google.golang.org/grpc"
28+ "google.golang.org/grpc/backoff"
29+ "google.golang.org/grpc/credentials/insecure"
2430 "gorm.io/gorm"
2531)
2632
@@ -390,54 +396,76 @@ func dbMarkOrderSuccAction(ctx context.Context, b *bot.Bot, u *models.Update) {
390396}
391397
392398func getTronWalletInfo (address string ) string {
393- var client = http.Client {Timeout : time .Second * 5 }
394- resp , err := client .Get ("https://apilist.tronscanapi.com/api/accountv2?address=" + address )
399+ var grpcParams = grpc.ConnectParams {
400+ Backoff : backoff.Config {BaseDelay : 1 * time .Second , MaxDelay : 30 * time .Second , Multiplier : 1.5 },
401+ MinConnectTimeout : 1 * time .Minute ,
402+ }
403+ conn , err := grpc .NewClient (conf .GetTronGrpcNode (), grpc .WithConnectParams (grpcParams ), grpc .WithTransportCredentials (insecure .NewCredentials ()))
395404 if err != nil {
396- log .Error ( "GetWalletInfoByAddress client.Get(url) " , err )
405+ log .Warn ( "getTronWalletInfo Error NewClient: " , err )
397406
398- return ""
407+ return "地址信息获取失败! "
399408 }
400409
401- defer resp .Body .Close ()
402- if resp .StatusCode != 200 {
403- log .Error ("GetWalletInfoByAddress resp.StatusCode != 200" , resp .StatusCode , err )
410+ defer conn .Close ()
404411
405- return ""
412+ var client = api2 .NewWalletClient (conn )
413+ var ctx , cancel = context .WithTimeout (context .Background (), time .Second * 5 )
414+ defer cancel ()
415+
416+ info , err2 := client .GetAccount (ctx , & core.Account {Address : base58 .Decode (address )[:21 ]})
417+ if err2 != nil {
418+ log .Warn ("getTronWalletInfo Error GetAccount:" , err2 )
419+
420+ return "地址信息获取失败!"
406421 }
407422
408- all , err := io .ReadAll (resp .Body )
423+ var dateCreated = time .UnixMilli (info .CreateTime )
424+ var latestOperationTime = time .UnixMilli (info .LatestOprationTime )
425+ var text = "```" + `
426+ 💰TRX余额:` + decimal .NewFromBigInt (new (big.Int ).SetInt64 (info .Balance ), - 6 ).RoundFloor (2 ).String () + ` TRX
427+ 💲USDT余额:` + getTronUsdtBalance (address ) + ` USDT
428+ ⏰创建时间:` + help .Ec (dateCreated .Format (time .DateTime )) + `
429+ ⏰最后活动:` + help .Ec (latestOperationTime .Format (time .DateTime )) + `
430+ ☘️查询地址:` + address + "\n ```"
431+
432+ return text
433+ }
434+
435+ func getTronUsdtBalance (address string ) string {
436+ var grpcParams = grpc.ConnectParams {
437+ Backoff : backoff.Config {BaseDelay : 1 * time .Second , MaxDelay : 30 * time .Second , Multiplier : 1.5 },
438+ MinConnectTimeout : 1 * time .Minute ,
439+ }
440+ conn , err := grpc .NewClient (conf .GetTronGrpcNode (), grpc .WithConnectParams (grpcParams ), grpc .WithTransportCredentials (insecure .NewCredentials ()))
409441 if err != nil {
410- log .Error ("GetWalletInfoByAddress io.ReadAll(resp.Body)" , err )
411-
412- return ""
413- }
414- result := gjson .ParseBytes (all )
415-
416- var dateCreated = time .UnixMilli (result .Get ("date_created" ).Int ())
417- var latestOperationTime = time .UnixMilli (result .Get ("latest_operation_time" ).Int ())
418- var netRemaining = result .Get ("bandwidth.netRemaining" ).Int () + result .Get ("bandwidth.freeNetRemaining" ).Int ()
419- var netLimit = result .Get ("bandwidth.netLimit" ).Int () + result .Get ("bandwidth.freeNetLimit" ).Int ()
420- var text = `
421- >💰 TRX余额:0.00 TRX
422- >💲 USDT余额:0.00 USDT
423- >📬 交易数量:` + result .Get ("totalTransactionCount" ).String () + `
424- >📈 转账数量:↑ ` + result .Get ("transactions_out" ).String () + ` ↓ ` + result .Get ("transactions_in" ).String () + `
425- >📡 宽带资源:` + fmt .Sprintf ("%v" , netRemaining ) + ` / ` + fmt .Sprintf ("%v" , netLimit ) + `
426- >🔋 能量资源:` + result .Get ("bandwidth.energyRemaining" ).String () + ` / ` + result .Get ("bandwidth.energyLimit" ).String () + `
427- >⏰ 创建时间:` + help .Ec (dateCreated .Format (time .DateTime )) + `
428- >⏰ 最后活动:` + help .Ec (latestOperationTime .Format (time .DateTime )) + `
429- >☘️ 查询地址:` + address
430-
431- for _ , v := range result .Get ("withPriceTokens" ).Array () {
432- if v .Get ("tokenName" ).String () == "trx" {
433- text = strings .Replace (text , "0.00 TRX" , help .Ec (fmt .Sprintf ("%.2f TRX" , v .Get ("balance" ).Float ()/ 1000000 )), 1 )
434- }
435- if v .Get ("tokenName" ).String () == "Tether USD" {
436- text = strings .Replace (text , "0.00 USDT" , help .Ec (fmt .Sprintf ("%.2f USDT" , v .Get ("balance" ).Float ()/ 1000000 )), 1 )
437- }
442+ log .Warn ("getTronUsdtBalance Error NewClient:" , err )
443+
444+ return "0.00"
438445 }
439446
440- return text
447+ defer conn .Close ()
448+
449+ var client = api2 .NewWalletClient (conn )
450+ var ctx , cancel = context .WithTimeout (context .Background (), time .Second * 5 )
451+ defer cancel ()
452+
453+ var addr = base58 .Decode (address )
454+ info , err2 := client .TriggerConstantContract (ctx , & core.TriggerSmartContract {
455+ OwnerAddress : addr ,
456+ ContractAddress : []byte {0x41 , 0xa6 , 0x14 , 0xf8 , 0x03 , 0xb6 , 0xfd , 0x78 , 0x09 , 0x86 , 0xa4 , 0x2c , 0x78 , 0xec , 0x9c , 0x7f , 0x77 , 0xe6 , 0xde , 0xd1 , 0x3c },
457+ Data : append ([]byte {0x70 , 0xa0 , 0x82 , 0x31 }, append (make ([]byte , 12 ), addr [1 :]... )... ),
458+ })
459+ if err2 != nil {
460+ log .Warn ("getTronUsdtBalance Error TriggerConstantContract:" , err2 )
461+
462+ return "0.00"
463+ }
464+
465+ var data = new (big.Int )
466+ data .SetBytes (info .ConstantResult [0 ])
467+
468+ return decimal .NewFromBigInt (data , - 6 ).String ()
441469}
442470
443471func getAptosWalletInfo (wa model.WalletAddress ) string {
0 commit comments