修复:优化创建交易流程代码,将金额修改为保留3位小数点

This commit is contained in:
Ashang
2022-04-04 16:18:31 +08:00
parent 20c576b9bc
commit 68f92dd1b1
10 changed files with 144 additions and 168 deletions
+34 -22
View File
@@ -1,20 +1,21 @@
package service
import (
"context"
"fmt"
"github.com/assimon/luuu/model/data"
"github.com/assimon/luuu/model/request"
"github.com/assimon/luuu/mq"
"github.com/assimon/luuu/mq/handle"
"github.com/assimon/luuu/telegram"
"github.com/assimon/luuu/util/http_client"
"github.com/assimon/luuu/util/json"
"github.com/assimon/luuu/util/log"
"github.com/golang-module/carbon/v2"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/stdutil"
"github.com/hibiken/asynq"
"github.com/shopspring/decimal"
"net/http"
"sync"
"time"
)
const UsdtTrc20ApiUri = "https://apilist.tronscan.org/api/token_trc20/transfers"
@@ -90,38 +91,33 @@ func Trc20CallBack(token string, wg *sync.WaitGroup) {
if trc20Resp.Total <= 0 {
return
}
ctx := context.Background()
nowTime := time.Now().Unix()
for _, transfer := range trc20Resp.TokenTransfers {
if transfer.ToAddress != token || transfer.FinalResult != "SUCCESS" {
continue
}
x, _ := decimal.NewFromString(transfer.Quant)
y, _ := decimal.NewFromString("1000000")
quant := x.Div(y).InexactFloat64()
amount := fmt.Sprintf("%.4f", quant)
result, err := data.GetExpirationTimeByAmount(ctx, token, amount)
decimalQuant, err := decimal.NewFromString(transfer.Quant)
if err != nil {
panic(err)
}
if result != "" {
expTime := mathutil.MustInt64(result)
// 但是过期了
if expTime < nowTime {
// 删掉过期
_ = data.ClearPayCache(token, amount)
continue
}
}
// 该钱包下有无匹配金额订单
tradeId, err := data.GetTradeIdByAmount(ctx, token, amount)
decimalDivisor := decimal.NewFromFloat(1000000)
amount := decimalQuant.Div(decimalDivisor).InexactFloat64()
tradeId, err := data.GetTradeIdByWalletAddressAndAmount(token, amount)
if err != nil {
panic(err)
}
if tradeId == "" {
continue
}
// 到这一步就匹配到金额了
order, err := data.GetOrderInfoByTradeId(tradeId)
if err != nil {
panic(err)
}
// 区块的确认时间必须在订单创建时间之后
createTime := order.CreatedAt.TimestampWithMillisecond()
if transfer.BlockTs < createTime {
panic("Orders cannot actually be matched")
}
// 到这一步就完全算是支付成功了
req := &request.OrderProcessingRequest{
Token: token,
TradeId: tradeId,
@@ -132,5 +128,21 @@ func Trc20CallBack(token string, wg *sync.WaitGroup) {
if err != nil {
panic(err)
}
// 回调队列
orderCallbackQueue, _ := handle.NewOrderCallbackQueue(order)
mq.MClient.Enqueue(orderCallbackQueue, asynq.MaxRetry(5))
// 发送机器人消息
msgTpl := `
<b>📢📢有新的交易支付成功!</b>
<pre>交易号:%s</pre>
<pre>订单号:%s</pre>
<pre>请求支付金额:%f cny</pre>
<pre>实际支付金额:%f usdt</pre>
<pre>钱包地址:%s</pre>
<pre>订单创建时间:%s</pre>
<pre>支付成功时间:%s</pre>
`
msg := fmt.Sprintf(msgTpl, order.TradeId, order.OrderId, order.Amount, order.ActualAmount, order.Token, order.CreatedAt.ToDateTimeString(), carbon.Now().ToDateTimeString())
telegram.SendToBot(msg)
}
}