feat: refactor payment system with multi-currency support and TRX native transfer detection
- Add multi-currency exchange rate API integration (GetRateForCoin) - Implement native TRX transfer detection alongside TRC20 USDT transfers - Replace deprecated Tronscan API with Trongrid API for better reliability - Add TRON Grid API key configuration support - Refactor order service to support multiple tokens and currencies - Update wallet address locking/unlocking to track token type - Add base58 and gjson dependencies for crypto operations - Optimize SQLite connection with WAL mode and busy timeout - Remove deprecated USDT rate job (replaced by dynamic rate API) - Update order data model to include currency and receive address fields - Enhance TRC20 callback with improved error handling and logging
This commit is contained in:
@@ -3,16 +3,19 @@ package data
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/assimon/luuu/model/dao"
|
||||
"github.com/assimon/luuu/model/mdb"
|
||||
"github.com/assimon/luuu/model/request"
|
||||
"github.com/assimon/luuu/util/log"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
CacheWalletAddressWithAmountToTradeIdKey = "wallet:%s_%v" // 钱包_待支付金额 : 交易号
|
||||
CacheWalletAddressWithAmountToTradeIdKey = "wallet:%s_%s_%v" // 钱包_币种_待支付金额 : 交易号
|
||||
)
|
||||
|
||||
// GetOrderInfoByOrderId 通过客户订单号查询订单
|
||||
@@ -78,10 +81,10 @@ func UpdateOrderIsExpirationById(id uint64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetTradeIdByWalletAddressAndAmount 通过钱包地址,支付金额获取交易号
|
||||
func GetTradeIdByWalletAddressAndAmount(token string, amount float64) (string, error) {
|
||||
// GetTradeIdByWalletAddressAndAmountAndToken 通过钱包地址、币种、支付金额获取交易号
|
||||
func GetTradeIdByWalletAddressAndAmountAndToken(address string, token string, amount float64) (string, error) {
|
||||
ctx := context.Background()
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, token, amount)
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, address, strings.ToUpper(token), amount)
|
||||
result, err := dao.Rdb.Get(ctx, cacheKey).Result()
|
||||
if err == redis.Nil {
|
||||
return "", nil
|
||||
@@ -93,17 +96,18 @@ func GetTradeIdByWalletAddressAndAmount(token string, amount float64) (string, e
|
||||
}
|
||||
|
||||
// LockTransaction 锁定交易
|
||||
func LockTransaction(token, tradeId string, amount float64, expirationTime time.Duration) error {
|
||||
func LockTransaction(address, token, tradeId string, amount float64, expirationTime time.Duration) error {
|
||||
ctx := context.Background()
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, token, amount)
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, address, strings.ToUpper(token), amount)
|
||||
log.Sugar.Infof("LockTransaction: cacheKey=%s, tradeId=%s, expirationTime=%v", cacheKey, tradeId, expirationTime)
|
||||
err := dao.Rdb.Set(ctx, cacheKey, tradeId, expirationTime).Err()
|
||||
return err
|
||||
}
|
||||
|
||||
// UnLockTransaction 解锁交易
|
||||
func UnLockTransaction(token string, amount float64) error {
|
||||
func UnLockTransaction(address string, token string, amount float64) error {
|
||||
ctx := context.Background()
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, token, amount)
|
||||
cacheKey := fmt.Sprintf(CacheWalletAddressWithAmountToTradeIdKey, address, strings.ToUpper(token), amount)
|
||||
err := dao.Rdb.Del(ctx, cacheKey).Err()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
// AddWalletAddress 创建钱包
|
||||
func AddWalletAddress(token string) (*mdb.WalletAddress, error) {
|
||||
exist, err := GetWalletAddressByToken(token)
|
||||
func AddWalletAddress(address string) (*mdb.WalletAddress, error) {
|
||||
exist, err := GetWalletAddressByToken(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -16,17 +16,17 @@ func AddWalletAddress(token string) (*mdb.WalletAddress, error) {
|
||||
return nil, constant.WalletAddressAlreadyExists
|
||||
}
|
||||
walletAddress := &mdb.WalletAddress{
|
||||
Token: token,
|
||||
Status: mdb.TokenStatusEnable,
|
||||
Address: address,
|
||||
Status: mdb.TokenStatusEnable,
|
||||
}
|
||||
err = dao.Mdb.Create(walletAddress).Error
|
||||
return walletAddress, err
|
||||
}
|
||||
|
||||
// GetWalletAddressByToken 通过钱包地址获取token
|
||||
func GetWalletAddressByToken(token string) (*mdb.WalletAddress, error) {
|
||||
// GetWalletAddressByToken 通过钱包地址获取address
|
||||
func GetWalletAddressByToken(address string) (*mdb.WalletAddress, error) {
|
||||
walletAddress := new(mdb.WalletAddress)
|
||||
err := dao.Mdb.Model(walletAddress).Limit(1).Find(walletAddress, "token = ?", token).Error
|
||||
err := dao.Mdb.Model(walletAddress).Limit(1).Find(walletAddress, "address = ?", address).Error
|
||||
return walletAddress, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user