feat: 实现钱包地址能量代理回收机器人通知

This commit is contained in:
zhiqiang.wang
2024-09-26 22:42:07 +08:00
parent 90e4a2a3c8
commit ed9645ecd0
+90 -27
View File
@@ -8,6 +8,7 @@ import (
"github.com/btcsuite/btcd/btcutil/base58"
"github.com/btcsuite/btcd/chaincfg/chainhash"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/spf13/cast"
"github.com/v03413/bepusdt/app/config"
"github.com/v03413/bepusdt/app/help"
"github.com/v03413/bepusdt/app/log"
@@ -31,12 +32,13 @@ var usdtTrc20ContractAddress = []byte{0x41, 0xa6, 0x14, 0xf8, 0x03, 0xb6, 0xfd,
var currentBlockHeight int64
type resource struct {
ID string
Type core.ResourceCode
Balance int64
FromAddress string
RecvAddress string
Timestamp time.Time
ID string
Type core.Transaction_Contract_ContractType
Balance int64
FromAddress string
RecvAddress string
Timestamp time.Time
ResourceCode core.ResourceCode
}
type transfer struct {
@@ -112,8 +114,7 @@ func BlockScanStart() {
func parseBlockTrans(block *api.BlockExtention, nowHeight int64) {
currentBlockHeight = nowHeight
var unDelegateResources = make([]resource, 0)
var delegateResources = make([]resource, 0)
var resources = make([]resource, 0)
var transfers = make([]transfer, 0)
var timestamp = time.UnixMilli(block.GetBlockHeader().GetRawData().GetTimestamp())
for _, v := range block.GetTransactions() {
@@ -134,13 +135,14 @@ func parseBlockTrans(block *api.BlockExtention, nowHeight int64) {
continue
}
delegateResources = append(delegateResources, resource{
ID: id,
Type: foo.Resource,
Balance: foo.Balance,
FromAddress: base58CheckEncode(foo.OwnerAddress),
RecvAddress: base58CheckEncode(foo.ReceiverAddress),
Timestamp: timestamp,
resources = append(resources, resource{
ID: id,
Type: core.Transaction_Contract_DelegateResourceContract,
Balance: foo.Balance,
ResourceCode: foo.Resource,
FromAddress: base58CheckEncode(foo.OwnerAddress),
RecvAddress: base58CheckEncode(foo.ReceiverAddress),
Timestamp: timestamp,
})
}
@@ -153,13 +155,14 @@ func parseBlockTrans(block *api.BlockExtention, nowHeight int64) {
continue
}
unDelegateResources = append(unDelegateResources, resource{
ID: id,
Type: foo.Resource,
Balance: foo.Balance,
FromAddress: base58CheckEncode(foo.OwnerAddress),
RecvAddress: base58CheckEncode(foo.ReceiverAddress),
Timestamp: timestamp,
resources = append(resources, resource{
ID: id,
Type: core.Transaction_Contract_UnDelegateResourceContract,
Balance: foo.Balance,
ResourceCode: foo.Resource,
FromAddress: base58CheckEncode(foo.OwnerAddress),
RecvAddress: base58CheckEncode(foo.ReceiverAddress),
Timestamp: timestamp,
})
}
@@ -221,11 +224,8 @@ func parseBlockTrans(block *api.BlockExtention, nowHeight int64) {
handleOtherNotify(transfers)
}
if len(unDelegateResources) > 0 {
}
if len(delegateResources) > 0 {
if len(resources) > 0 {
handleResourceNotify(resources)
}
log.Info("区块扫描完成:", nowHeight)
@@ -408,6 +408,69 @@ func handleOtherNotify(items []transfer) {
}
}
func handleResourceNotify(items []resource) {
var ads []model.WalletAddress
var tx = model.DB.Where("status = ? and other_notify = ?", model.StatusEnable, model.OtherNotifyEnable).Find(&ads)
if tx.RowsAffected <= 0 {
return
}
for _, wa := range ads {
for _, trans := range items {
if trans.RecvAddress != wa.Address && trans.FromAddress != wa.Address {
continue
}
if trans.ResourceCode != core.ResourceCode_ENERGY {
continue
}
var detailUrl = "https://tronscan.org/#/transaction/" + trans.ID
if !model.IsNeedNotifyByTxid(trans.ID) {
// 不需要额外通知
continue
}
var title = "代理"
if trans.Type == core.Transaction_Contract_UnDelegateResourceContract {
title = "回收"
}
var text = fmt.Sprintf(
"#资源动态 #能量"+title+"\n---\n```\n🔋质押数量:"+cast.ToString(trans.Balance/1000000)+"\n⏱️交易时间:%v\n✅操作地址:%v\n🅾️资源来源:%v```\n",
trans.Timestamp.Format(time.DateTime),
help.MaskAddress(trans.RecvAddress),
help.MaskAddress(trans.FromAddress),
)
var chatId, err = strconv.ParseInt(config.GetTgBotNotifyTarget(), 10, 64)
if err != nil {
continue
}
var msg = tgbotapi.NewMessage(chatId, text)
msg.ParseMode = tgbotapi.ModeMarkdown
msg.ReplyMarkup = tgbotapi.InlineKeyboardMarkup{
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{
{
tgbotapi.NewInlineKeyboardButtonURL("📝查看交易明细", detailUrl),
},
},
}
var _record = model.NotifyRecord{Txid: trans.ID}
model.DB.Create(&_record)
go telegram.SendMsg(msg)
}
}
}
func base58CheckEncode(input []byte) string {
checksum := chainhash.DoubleHashB(input)
checksum = checksum[:4]