增加主动查询 api
This commit is contained in:
@@ -41,6 +41,16 @@ type USDTMan struct {
|
||||
httpClient *http.Client // HTTP 客户端
|
||||
}
|
||||
|
||||
// QueryFilter 查询过滤条件
|
||||
type QueryFilter struct {
|
||||
Address string // 地址(必填)
|
||||
StartTime int64 // 开始时间戳(毫秒),0 表示不限制
|
||||
EndTime int64 // 结束时间戳(毫秒),0 表示不限制
|
||||
MinAmount *big.Int // 最小金额(micro USDT),nil 表示不限制
|
||||
MinConfirmations int64 // 最小确认数,0 表示不限制
|
||||
Limit int // 查询数量限制,0 使用默认值
|
||||
}
|
||||
|
||||
// USDTPayment USDT 收款信息
|
||||
type USDTPayment struct {
|
||||
Address string
|
||||
@@ -351,6 +361,43 @@ func (m *USDTMan) getUSDTTransactions(address string) ([]TronGridTransaction, er
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// getUSDTTransactionsWithLimit 获取指定数量的交易
|
||||
func (m *USDTMan) getUSDTTransactionsWithLimit(address string, limit int) ([]TronGridTransaction, error) {
|
||||
url := fmt.Sprintf("%s/v1/accounts/%s/transactions/trc20?limit=%d&contract_address=%s&only_to=true",
|
||||
TronGridAPI, address, limit, USDTContractAddress)
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if m.apiKey != "" {
|
||||
req.Header.Set("TRON-PRO-API-KEY", m.apiKey)
|
||||
}
|
||||
|
||||
resp, err := m.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("TronGrid API error: %d %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result TronGridAccountTransactions
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// getTransactionBlock 获取交易的区块号
|
||||
func (m *USDTMan) getTransactionBlock(txID string) (int64, error) {
|
||||
url := fmt.Sprintf("%s/wallet/gettransactioninfobyid?value=%s", TronGridAPI, txID)
|
||||
@@ -466,3 +513,85 @@ func (m *USDTMan) RemoveAddress(address string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// QueryTransactions 主动查询交易记录(带过滤条件)
|
||||
func (m *USDTMan) QueryTransactions(filter QueryFilter) ([]*USDTPayment, error) {
|
||||
if filter.Address == "" {
|
||||
return nil, fmt.Errorf("address is required")
|
||||
}
|
||||
|
||||
// 设置默认 limit
|
||||
limit := filter.Limit
|
||||
if limit == 0 {
|
||||
limit = m.maxHistoryTxns
|
||||
}
|
||||
|
||||
// 获取交易列表
|
||||
transactions, err := m.getUSDTTransactionsWithLimit(filter.Address, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取当前区块(用于计算确认数)
|
||||
currentBlock, err := m.getCurrentBlock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var results []*USDTPayment
|
||||
|
||||
for _, txn := range transactions {
|
||||
// 时间过滤
|
||||
if filter.StartTime > 0 && txn.BlockTimestamp < filter.StartTime {
|
||||
continue
|
||||
}
|
||||
if filter.EndTime > 0 && txn.BlockTimestamp > filter.EndTime {
|
||||
continue
|
||||
}
|
||||
|
||||
// 只处理转入该地址的交易
|
||||
if !strings.EqualFold(txn.To, filter.Address) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 解析金额
|
||||
amount := new(big.Int)
|
||||
amount, ok := amount.SetString(txn.Value, 10)
|
||||
if !ok || amount.Sign() <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 金额过滤
|
||||
if filter.MinAmount != nil && amount.Cmp(filter.MinAmount) < 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取区块号
|
||||
blockNumber, err := m.getTransactionBlock(txn.TransactionID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 计算确认数
|
||||
confirmations := currentBlock - blockNumber
|
||||
|
||||
// 确认数过滤
|
||||
if filter.MinConfirmations > 0 && confirmations < filter.MinConfirmations {
|
||||
continue
|
||||
}
|
||||
|
||||
payment := &USDTPayment{
|
||||
Address: filter.Address,
|
||||
Amount: amount,
|
||||
TxID: txn.TransactionID,
|
||||
BlockNumber: blockNumber,
|
||||
Timestamp: txn.BlockTimestamp,
|
||||
From: txn.From,
|
||||
Confirmations: confirmations,
|
||||
}
|
||||
|
||||
results = append(results, payment)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user