code
This commit is contained in:
		
							
								
								
									
										82
									
								
								app/jobs/trxJobs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								app/jobs/trxJobs.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,82 @@ | ||||
| package jobs | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"github.com/go-admin-team/go-admin-core/logger" | ||||
| 	"github.com/shopspring/decimal" | ||||
| 	"go-admin/pkg/utility" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| type TrxQueryJobs struct { | ||||
| } | ||||
|  | ||||
| const ( | ||||
| 	tronGridURL         = "https://api.trongrid.io" // TronGrid API 地址 | ||||
| 	UsdtContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" | ||||
| ) | ||||
|  | ||||
| // TRC20Transfer 结构体用于解析 TRC20 转账记录 | ||||
| type TRC20Transfer struct { | ||||
| 	TransactionID string `json:"transaction_id"` | ||||
| 	BlockNumber   int64  `json:"block_number"` | ||||
| 	Timestamp     int64  `json:"block_timestamp"` | ||||
| 	FromAddress   string `json:"from"` | ||||
| 	ToAddress     string `json:"to"` | ||||
| 	Value         string `json:"value"` | ||||
| 	TokenInfo     struct { | ||||
| 		Symbol   string `json:"symbol"` | ||||
| 		Address  string `json:"address"` | ||||
| 		Decimals int    `json:"decimals"` | ||||
| 	} `json:"token_info"` | ||||
| } | ||||
|  | ||||
| // GetTRC20Transfers 获取指定 TRC20 代币的交易记录 | ||||
| func GetTRC20Transfers(contractAddress, accountAddress string, minTimestamp, maxTimestamp int64) ([]TRC20Transfer, error) { | ||||
| 	url := fmt.Sprintf("%s/v1/accounts/%s/transactions/trc20?contract_address=%s", tronGridURL, accountAddress, contractAddress) | ||||
| 	if minTimestamp > 0 { | ||||
| 		url += fmt.Sprintf("&min_timestamp=%d", minTimestamp) | ||||
| 	} | ||||
| 	if maxTimestamp > 0 { | ||||
| 		url += fmt.Sprintf("&max_timestamp=%d", maxTimestamp) | ||||
| 	} | ||||
| 	resp, err := http.Get(url) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("failed to send request: %v", err) | ||||
| 	} | ||||
| 	defer resp.Body.Close() | ||||
|  | ||||
| 	body, err := ioutil.ReadAll(resp.Body) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("failed to read response body: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	var result struct { | ||||
| 		Data []TRC20Transfer `json:"data"` | ||||
| 	} | ||||
| 	if err := json.Unmarshal(body, &result); err != nil { | ||||
| 		return nil, fmt.Errorf("failed to unmarshal response: %v", err) | ||||
| 	} | ||||
| 	return result.Data, nil | ||||
| } | ||||
|  | ||||
| func (t TrxQueryJobs) Exec(arg interface{}) error { | ||||
| 	targetAddress := arg.(string) | ||||
| 	if targetAddress == "" { | ||||
| 		logger.Error(fmt.Sprintf("查询地址为空")) | ||||
| 		return nil | ||||
| 	} | ||||
| 	startTime := time.Now().UnixMilli() | ||||
| 	//endTime := time.Now().Add(time.Duration(-2) * time.Hour).UnixMilli() | ||||
| 	endTime := time.Now().AddDate(-2, 0, 0).UnixMilli() | ||||
| 	transfers, _ := GetTRC20Transfers(UsdtContractAddress, targetAddress, endTime, startTime) | ||||
| 	for _, transfer := range transfers { | ||||
| 		//实际金额 | ||||
| 		utility.StringToDecimal(transfer.Value).Div(decimal.NewFromInt(int64(transfer.TokenInfo.Decimals))).Truncate(5) | ||||
|  | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 daichao
					daichao