Skip to content

Commit 7c61a13

Browse files
author
v03413
committed
feat: 优化完善 solana aptos usdt 余额获取
1 parent 428150b commit 7c61a13

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

app/bot/callback.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/v03413/go-cache"
1717
"gorm.io/gorm"
1818
"io"
19+
"math/big"
1920
"net/http"
2021
"net/url"
2122
"strings"
@@ -120,13 +121,19 @@ func cbAddressAction(ctx context.Context, b *bot.Bot, u *models.Update) {
120121
otherTextLabel = "🔴已禁用 非订单交易监控通知"
121122
}
122123

123-
var text = fmt.Sprintf("> %s", wa.Address)
124+
var text = fmt.Sprintf(">`%s`", wa.Address)
124125
if help.IsValidTronAddress(wa.Address) {
125126
text = getTronWalletInfo(wa.Address)
126127
}
127128
if help.IsValidEvmAddress(wa.Address) {
128129
text = getEvmWalletInfo(wa)
129130
}
131+
if help.IsValidAptosAddress(wa.Address) {
132+
text = getAptosWalletInfo(wa.Address)
133+
}
134+
if help.IsValidSolanaAddress(wa.Address) {
135+
text = getSolanaWalletInfo(wa.Address)
136+
}
130137

131138
EditMessageText(ctx, b, &bot.EditMessageTextParams{
132139
ChatID: u.CallbackQuery.Message.Message.Chat.ID,
@@ -394,6 +401,74 @@ func getTronWalletInfo(address string) string {
394401
return text
395402
}
396403

404+
func getAptosWalletInfo(address string) string {
405+
var text = fmt.Sprintf(">`%s`", address)
406+
var client = http.Client{Timeout: time.Second * 5}
407+
resp, err := client.Get(fmt.Sprintf("%sv1/accounts/%s/balance/%s", conf.GetAptosRpcNode(), address, conf.UsdtAptos))
408+
if err != nil {
409+
log.Error("getAptosWalletInfo client.Get(url)", err)
410+
411+
return text
412+
}
413+
414+
defer resp.Body.Close()
415+
if resp.StatusCode != 200 {
416+
log.Error("getAptosWalletInfo resp.StatusCode != 200", resp.StatusCode, err)
417+
418+
return text
419+
}
420+
421+
all, err := io.ReadAll(resp.Body)
422+
if err != nil {
423+
log.Error("getAptosWalletInfo io.ReadAll(resp.Body)", err)
424+
425+
return text
426+
}
427+
428+
result, _ := new(big.Int).SetString(string(all), 10)
429+
balance := decimal.NewFromBigInt(result, conf.UsdtAptosDecimals)
430+
431+
return fmt.Sprintf(">💲余额:%s\\(%s\\)\n>☘️地址:`%s`", help.Ec(balance.String()), help.Ec(model.OrderTradeTypeUsdtAptos), address)
432+
}
433+
434+
func getSolanaWalletInfo(address string) string {
435+
var text = fmt.Sprintf(">`%s`", address)
436+
var jsonData = []byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"getTokenAccountsByOwner","params":["%s",{"programId":"%s"},{"commitment":"finalized","encoding":"jsonParsed"}]}`,
437+
address, conf.SolSplToken))
438+
var client = &http.Client{Timeout: time.Second * 5}
439+
resp, err := client.Post(conf.GetSolanaRpcEndpoint(), "application/json", bytes.NewBuffer(jsonData))
440+
if err != nil {
441+
log.Warn("Error Post response:", err)
442+
443+
return text
444+
}
445+
446+
defer resp.Body.Close()
447+
if resp.StatusCode != 200 {
448+
log.Error("getAptosWalletInfo resp.StatusCode != 200", resp.StatusCode, err)
449+
450+
return text
451+
}
452+
453+
all, err := io.ReadAll(resp.Body)
454+
if err != nil {
455+
log.Error("getAptosWalletInfo io.ReadAll(resp.Body)", err)
456+
457+
return text
458+
}
459+
460+
for _, v := range gjson.GetBytes(all, "result.value").Array() {
461+
if v.Get("account.data.parsed.info.mint").String() == conf.UsdtSolana {
462+
return fmt.Sprintf(">💲余额:%s\\(%s\\)\n>☘️地址:`%s`",
463+
help.Ec(v.Get("account.data.parsed.info.tokenAmount.uiAmountString").String()),
464+
help.Ec(model.OrderTradeTypeUsdtAptos),
465+
address)
466+
}
467+
}
468+
469+
return text
470+
}
471+
397472
func getEvmWalletInfo(wa model.WalletAddress) string {
398473

399474
return fmt.Sprintf(">💲余额:%s\\(%s\\)\n>☘️地址:`%s`", help.Ec(evmUSDTBalanceOf(wa)), help.Ec(wa.TradeType), wa.Address)

0 commit comments

Comments
 (0)