refactor: 重新适配钱包添加逻辑

This commit is contained in:
zhiqiang.wang
2025-06-14 22:40:32 +08:00
parent b1a06b8e17
commit f7bb776e99
10 changed files with 88 additions and 64 deletions
+1
View File
@@ -1,3 +1,4 @@
tmp
.DS_Store
.idea
conf.toml
+1
View File
@@ -53,6 +53,7 @@ func Start() {
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbWallet, bot.MatchTypePrefix, cbWalletAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddress, bot.MatchTypePrefix, cbAddressAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddressAdd, bot.MatchTypePrefix, cbAddressAddAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddressType, bot.MatchTypePrefix, cbAddressTypeAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddressDelete, bot.MatchTypePrefix, cbAddressDelAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddressEnable, bot.MatchTypePrefix, cbAddressEnableAction)
api.RegisterHandler(bot.HandlerTypeCallbackQueryData, cbAddressDisable, bot.MatchTypePrefix, cbAddressDisableAction)
+30 -6
View File
@@ -12,6 +12,7 @@ import (
"github.com/v03413/bepusdt/app/help"
"github.com/v03413/bepusdt/app/log"
"github.com/v03413/bepusdt/app/model"
"github.com/v03413/go-cache"
"gorm.io/gorm"
"io"
"math/big"
@@ -24,6 +25,7 @@ import (
const cbWallet = "wallet"
const cbAddress = "address_act"
const cbAddressAdd = "address_add"
const cbAddressType = "address_type"
const cbAddressEnable = "address_enable"
const cbAddressDisable = "address_disable"
const cbAddressDelete = "address_del"
@@ -39,12 +41,12 @@ func cbWalletAction(ctx context.Context, b *bot.Bot, u *models.Update) {
if strings.HasPrefix(address, "T") {
text = getTronWalletInfo(address)
}
if help.IsValidPolygonAddress(address) {
if help.IsValidEvmAddress(address) {
text = getPolygonWalletInfo(address)
}
var uri = "https://tronscan.org/#/address/" + address
if help.IsValidPolygonAddress(address) {
if help.IsValidEvmAddress(address) {
uri = "https://polygonscan.com/address/" + address
}
@@ -69,17 +71,39 @@ func cbWalletAction(ctx context.Context, b *bot.Bot, u *models.Update) {
}
func cbAddressAddAction(ctx context.Context, b *bot.Bot, u *models.Update) {
var params = &bot.SendMessageParams{
var tradeType = ctx.Value("args").([]string)[1]
var k = fmt.Sprintf("%s_%d_trade_type", cbAddressAdd, u.CallbackQuery.Message.Message.Chat.ID)
cache.Set(k, tradeType, -1)
SendMessage(&bot.SendMessageParams{
Text: replayAddressText,
ChatID: u.CallbackQuery.Message.Message.Chat.ID,
ReplyMarkup: &models.ForceReply{
ForceReply: true,
Selective: true,
InputFieldPlaceholder: "输入钱包地址",
InputFieldPlaceholder: fmt.Sprintf("钱包地址(%s)", tradeType),
},
})
}
func cbAddressTypeAction(ctx context.Context, b *bot.Bot, u *models.Update) {
var btn [][]models.InlineKeyboardButton
for _, v := range model.SupportTradeTypes {
btn = append(btn, []models.InlineKeyboardButton{
{
Text: fmt.Sprintf("💎 %s", strings.ToUpper(v)),
CallbackData: fmt.Sprintf("%s|%s", cbAddressAdd, v),
},
})
}
SendMessage(params)
SendMessage(&bot.SendMessageParams{
Text: "*🏝️ 请选择添加的钱包地址类型:*",
ChatID: u.CallbackQuery.Message.Message.Chat.ID,
ParseMode: models.ParseModeMarkdown,
ReplyMarkup: models.InlineKeyboardMarkup{InlineKeyboard: btn},
})
}
func cbAddressDelAction(ctx context.Context, b *bot.Bot, u *models.Update) {
@@ -238,7 +262,7 @@ func cbOrderDetailAction(ctx context.Context, b *bot.Bot, u *models.Update) {
📊交易汇率:` + o.TradeRate + `(` + conf.GetUsdtRate() + `)
💲交易数额:` + o.Amount + `
💰交易金额:` + fmt.Sprintf("%.2f", o.Money) + ` CNY
💍交易类别:` + strings.ToUpper(o.TradeType) + fmt.Sprintf("(%s)", o.GetTradeChain()) + `
💍交易类别:` + strings.ToUpper(o.TradeType) + `
🌏商户网站:` + site.String() + `
🔋收款状态:` + o.GetStatusLabel() + `
🍀回调状态:` + notifyStateLabel + `
+2 -2
View File
@@ -19,7 +19,7 @@ const cmdState = "state"
const cmdWallet = "wallet"
const cmdOrder = "order"
const replayAddressText = "🚚 请发送一个合法的钱包地址"
const replayAddressText = "🚚 请发送需要添加的钱包地址"
func cmdGetIdHandle(ctx context.Context, b *bot.Bot, u *models.Update) {
@@ -58,7 +58,7 @@ func cmdStartHandle(ctx context.Context, b *bot.Bot, u *models.Update) {
chatID = u.CallbackQuery.Message.Message.Chat.ID
}
btn = append(btn, []models.InlineKeyboardButton{{Text: "👛 添加新的钱包地址", CallbackData: cbAddressAdd}})
btn = append(btn, []models.InlineKeyboardButton{{Text: "👛 收款地址添加", CallbackData: cbAddressType}})
SendMessage(&bot.SendMessageParams{
ChatID: chatID,
+7 -15
View File
@@ -2,10 +2,12 @@ package bot
import (
"context"
"fmt"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/v03413/bepusdt/app/help"
"github.com/v03413/bepusdt/app/model"
"github.com/v03413/go-cache"
"strings"
)
@@ -23,7 +25,7 @@ func defaultHandle(ctx context.Context, bot *bot.Bot, u *models.Update) {
go queryTronAddressInfo(u.Message)
}
if help.IsValidPolygonAddress(text) {
if help.IsValidEvmAddress(text) {
go queryPolygonAddressInfo(u.Message)
}
}
@@ -31,28 +33,18 @@ func defaultHandle(ctx context.Context, bot *bot.Bot, u *models.Update) {
func addWalletAddress(u *models.Update) {
var address = strings.TrimSpace(u.Message.Text)
if !help.IsValidTronAddress(address) && !help.IsValidPolygonAddress(address) {
if !help.IsValidTronAddress(address) && !help.IsValidEvmAddress(address) {
SendMessage(&bot.SendMessageParams{Text: "钱包地址不合法"})
return
}
var chain = model.WaChainTron
if help.IsValidPolygonAddress(address) {
var tradeType, _ = cache.Get(fmt.Sprintf("%s_%d_trade_type", cbAddressAdd, u.Message.Chat.ID))
chain = model.WaChainPolygon
}
var wa = model.WalletAddress{Chain: chain, Address: address, Status: model.StatusEnable, OtherNotify: model.OtherNotifyEnable}
var wa = model.WalletAddress{TradeType: tradeType.(string), Address: address, Status: model.StatusEnable, OtherNotify: model.OtherNotifyEnable}
var r = model.DB.Create(&wa)
if r.Error != nil {
if r.Error.Error() == "UNIQUE constraint failed: wallet_address.address" {
SendMessage(&bot.SendMessageParams{Text: "❌地址添加失败,地址重复!"})
return
}
SendMessage(&bot.SendMessageParams{Text: "❌地址添加失败,错误信息:" + r.Error.Error()})
SendMessage(&bot.SendMessageParams{Text: "❌地址添加失败," + r.Error.Error()})
return
}
+1 -1
View File
@@ -99,7 +99,7 @@ func IsValidTronAddress(address string) bool {
return match && err == nil
}
func IsValidPolygonAddress(address string) bool {
func IsValidEvmAddress(address string) bool {
if len(address) != 42 || !strings.HasPrefix(address, "0x") {
return false
+26 -26
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/v03413/bepusdt/app/conf"
"github.com/v03413/bepusdt/app/help"
"strings"
"time"
)
@@ -12,39 +13,43 @@ const (
StatusDisable uint8 = 0
OtherNotifyEnable uint8 = 1
OtherNotifyDisable uint8 = 0
WaChainTron = "tron"
WaChainPolygon = "polygon"
WaChainEthereum = "ethereum"
)
var tradeChain = map[string]string{
OrderTradeTypeTronTrx: WaChainTron,
OrderTradeTypeUsdtTrc20: WaChainTron,
OrderTradeTypeUsdtPolygon: WaChainPolygon,
// SupportTradeTypes 目前支持的收款交易类型
var SupportTradeTypes = []string{
OrderTradeTypeTronTrx,
OrderTradeTypeUsdtTrc20,
OrderTradeTypeUsdtErc20,
OrderTradeTypeUsdtBep20,
OrderTradeTypeUsdtXlayer,
OrderTradeTypeUsdtPolygon,
}
type WalletAddress struct {
ID int64 `gorm:"integer;primaryKey;not null;comment:id"`
Status uint8 `gorm:"column:status;type:tinyint(1);not null;default:1;comment:地址状态 1启动 0禁止"`
Chain string `gorm:"column:chain;type:varchar(16);not null;default:'tron';comment:链类型"`
Address string `gorm:"column:address;type:varchar(64);not null;uniqueIndex;comment:钱包地址"`
OtherNotify uint8 `gorm:"column:other_notify;type:tinyint(1);not null;default:0;comment:其它转账通知 1启动 0禁止"`
Status uint8 `gorm:"column:status;type:tinyint(1);not null;default:1;comment:地址状态"`
TradeType string `gorm:"column:trade_type;type:varchar(20);not null;comment:交易类型"`
Address string `gorm:"column:address;type:varchar(64);not null;index;comment:钱包地址"`
OtherNotify uint8 `gorm:"column:other_notify;type:tinyint(1);not null;default:0;comment:其它转账通知"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime;type:timestamp;not null;comment:创建时间"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime;type:timestamp;not null;comment:更新时间"`
}
// 启动时添加初始钱包地址
func addStartWalletAddress() {
for _, address := range conf.GetWalletAddress() {
if !help.IsValidTronAddress(address) && !help.IsValidPolygonAddress(address) {
fmt.Println("❌钱包地址不合法:", address)
for _, itm := range conf.GetWalletAddress() {
var info = strings.Split(itm, ":")
if len(info) != 2 {
continue
}
var chain = WaChainTron
if help.IsValidPolygonAddress(address) {
chain = WaChainPolygon
var address = info[1]
if !help.IsValidTronAddress(address) && !help.IsValidEvmAddress(address) {
fmt.Println("❌钱包地址不合法:", address)
continue
}
var wa WalletAddress
@@ -54,14 +59,14 @@ func addStartWalletAddress() {
continue
}
var err = DB.Create(&WalletAddress{Chain: chain, Address: address, Status: StatusEnable}).Error
var err = DB.Create(&WalletAddress{TradeType: info[0], Address: address, Status: StatusEnable}).Error
if err != nil {
fmt.Println("❌钱包地址添加失败:", err)
continue
}
fmt.Println("✅钱包地址添加成功:", chain, address)
fmt.Println("✅钱包地址添加成功:", info[0], address)
}
}
@@ -87,16 +92,11 @@ func (wa *WalletAddress) Delete() {
func GetAvailableAddress(address, tradeType string) []WalletAddress {
var rows []WalletAddress
var chain = tradeChain[tradeType]
if address == "" {
DB.Where("chain = ? and status = ?", chain, StatusEnable).Find(&rows)
return rows
}
DB.Where("trade_type = ? and status = ?", tradeType, StatusEnable).Find(&rows)
DB.Where("address = ?", address).Find(&rows)
if len(rows) == 0 {
var wa = WalletAddress{Chain: chain, Address: address, Status: StatusEnable, OtherNotify: OtherNotifyDisable}
var wa = WalletAddress{TradeType: tradeType, Address: address, Status: StatusEnable, OtherNotify: OtherNotifyDisable}
DB.Create(&wa)
+17 -8
View File
@@ -3,7 +3,6 @@ package model
import (
"github.com/shopspring/decimal"
"github.com/v03413/bepusdt/app/conf"
"github.com/v03413/bepusdt/app/help"
"strconv"
"sync"
"time"
@@ -19,11 +18,11 @@ const (
OrderStatusWaiting = 1 // 等待支付
OrderTradeTypeBscBnb = "bsc.bnb"
OrderTradeTypeTronTrx = "tron.trx"
OrderTradeTypeEthEth = "ethereum.eth"
OrderTradeTypeXlayerOkb = "xlayer.okb"
OrderTradeTypePolygonPol = "polygon.pol"
OrderTradeTypeTronTrx = "tron.trx"
OrderTradeTypeUsdtTrc20 = "usdt.trc20"
OrderTradeTypeUsdtPolygon = "usdt.polygon"
OrderTradeTypeUsdtErc20 = "usdt.erc20"
@@ -134,17 +133,27 @@ func (o *TradeOrders) GetStatusEmoji() string {
}
func (o *TradeOrders) GetTxDetailUrl() string {
if help.IsValidTronAddress(o.Address) {
if o.TradeType == OrderTradeTypeUsdtErc20 {
return "https://tronscan.org/#/transaction/" + o.TradeHash
return "https://etherscan.io/tx/" + o.TradeHash
}
return "https://polygonscan.com/tx/" + o.TradeHash
}
if o.TradeType == OrderTradeTypeUsdtBep20 {
func (o *TradeOrders) GetTradeChain() string {
return "https://bscscan.com/tx/" + o.TradeHash
}
return tradeChain[o.TradeType]
if o.TradeType == OrderTradeTypeUsdtXlayer {
return "https://web3.okx.com/zh-hans/explorer/x-layer/tx/" + o.TradeHash
}
if o.TradeType == OrderTradeTypeUsdtPolygon {
return "https://polygonscan.com/tx/" + o.TradeHash
}
return "https://tronscan.org/#/transaction/" + o.TradeHash
}
func GetTradeType(trade string) string {
+2 -5
View File
@@ -99,10 +99,6 @@ func notOrderTransferHandle(context.Context) {
model.DB.Where("status = ? and other_notify = ?", model.StatusEnable, model.OtherNotifyEnable).Find(&was)
for _, wa := range was {
if wa.Chain == model.WaChainPolygon {
wa.Address = strings.ToLower(wa.Address)
}
for _, t := range transfers {
if t.RecvAddress != wa.Address && t.FromAddress != wa.Address {
@@ -161,8 +157,9 @@ func notOrderTransferHandle(context.Context) {
func tronResourceHandle(context.Context) {
for resources := range resourceQueue.Out {
var was []model.WalletAddress
var types = []string{model.OrderTradeTypeTronTrx, model.OrderTradeTypeUsdtTrc20}
model.DB.Where("status = ? and other_notify = ? and chain = ?", model.StatusEnable, model.OtherNotifyEnable, model.WaChainTron).Find(&was)
model.DB.Where("status = ? and other_notify = ? and trade_type in (?)", model.StatusEnable, model.OtherNotifyEnable, types).Find(&was)
for _, wa := range was {
for _, t := range resources {
+1 -1
View File
@@ -77,7 +77,7 @@ func createTransaction(ctx *gin.Context) {
}
if v, ok := data["address"]; ok && cast.ToString(v) != "" {
address = cast.ToString(v)
if !help.IsValidTronAddress(address) && !help.IsValidPolygonAddress(address) {
if !help.IsValidTronAddress(address) && !help.IsValidEvmAddress(address) {
ctx.JSON(200, respFailJson(fmt.Sprintf("收款钱包地址(%s)不合法", address)))
return