1临时提交 生成订单
Some checks failed
Build / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
build / Build (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitee (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitlab (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
Some checks failed
Build / build (push) Has been cancelled
CodeQL / Analyze (go) (push) Has been cancelled
build / Build (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitee (push) Has been cancelled
GitHub Actions Mirror / mirror_to_gitlab (push) Has been cancelled
Issue Close Require / issue-close-require (push) Has been cancelled
This commit is contained in:
103
app/jobs/trxpayment_job.go
Normal file
103
app/jobs/trxpayment_job.go
Normal file
@ -0,0 +1,103 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"go-admin/app/admin/service"
|
||||
"go-admin/app/admin/service/dto"
|
||||
"go-admin/config"
|
||||
"go-admin/utils/utility"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type TrxPaymentJob struct{}
|
||||
|
||||
const (
|
||||
// tronGridURL = "https://api.trongrid.io" // TronGrid API 地址
|
||||
UsdtContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" //TRX USDT 合约地址
|
||||
)
|
||||
|
||||
// trx 链上支付定时查询
|
||||
func (j *TrxPaymentJob) Exec(args interface{}) error {
|
||||
configService := service.SysConfig{}
|
||||
configService.Orm = GetDb()
|
||||
req := dto.SysConfigByKeyReq{}
|
||||
req.ConfigKey = "trx_receive_address"
|
||||
configData := dto.GetSysConfigByKEYForServiceResp{}
|
||||
configService.GetWithKey(&req, &configData)
|
||||
if configData.ConfigValue == "" {
|
||||
logger.Error("查询地址为空")
|
||||
return nil
|
||||
}
|
||||
|
||||
rechargeService := service.TmRechargeLog{}
|
||||
rechargeService.Orm = GetDb()
|
||||
startTime := time.Now().UnixMilli()
|
||||
endTime := time.Now().Add(-1 * time.Hour).UnixMilli()
|
||||
transfers, err := GetTRC20Transfers(UsdtContractAddress, configData.ConfigValue, endTime, startTime)
|
||||
if err != nil {
|
||||
logger.Error("查询失败", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
logs := make([]dto.TmRechargeCallbackReq, 0)
|
||||
item := dto.TmRechargeCallbackReq{}
|
||||
|
||||
for _, transfer := range transfers {
|
||||
if transfer.TransactionID == "" || transfer.ToAddress != configData.ConfigValue {
|
||||
continue
|
||||
}
|
||||
|
||||
//实际金额
|
||||
payableAmount := utility.StringToDecimal(transfer.Value).Div(decimal.NewFromInt(10).Pow(decimal.NewFromInt(int64(transfer.TokenInfo.Decimals)))).Truncate(6)
|
||||
item.TxHash = transfer.TransactionID
|
||||
item.PayableAmount = payableAmount
|
||||
item.FromAddress = transfer.FromAddress
|
||||
|
||||
logs = append(logs, item)
|
||||
}
|
||||
|
||||
if len(logs) > 0 {
|
||||
err := rechargeService.PayCallBack(&logs)
|
||||
|
||||
if err != nil {
|
||||
logger.Error("执行完毕,err:")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTRC20Transfers 获取指定 TRC20 代币的交易记录
|
||||
func GetTRC20Transfers(contractAddress, accountAddress string, minTimestamp, maxTimestamp int64) ([]dto.TRC20Transfer, error) {
|
||||
url := fmt.Sprintf("%s/v1/accounts/%s/transactions/trc20?contract_address=%s", config.ExtConfig.TrxGridUrl, 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 []dto.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
|
||||
}
|
||||
Reference in New Issue
Block a user