Skip to content

Commit

Permalink
Merge pull request #31 from v03413/block-scan
Browse files Browse the repository at this point in the history
Block scan merge
  • Loading branch information
v03413 authored Sep 23, 2024
2 parents d773866 + f99c025 commit 9f72d69
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 449 deletions.
38 changes: 15 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,21 @@ Bepusdt 所有参数都是以传递环境变量的方式进行配置,大部分

### 参数列表

| 参数名称 | 默认值 | 用法说明 |
|----------------------|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| EXPIRE_TIME | `600` | 订单有效期,单位秒 |
| USDT_RATE || USDT汇率,默认留空则获取Okx交易所的汇率(每分钟同步一次),支持多种写法,如:`7.4` 表示固定7.4、`~1.02`表示最新汇率上浮2%、`~0.97`表示最新汇率下浮3%、`+0.3`表示最新加0.3、`-0.2`表示最新减0.2,以此类推;如参数错误则使用固定值6.4 |
| USDT_ATOM | `0.01` | `0.01`表示支付数额保留两位小数,相同金额时递增颗粒度为`0.01`,依次类推,如无特殊需求不建议修改。 |
| AUTH_TOKEN | `123234` | 认证Token,对接会用到这个参数 |
| LISTEN | `:8080` | 服务器HTTP监听地址 |
| TRADE_IS_CONFIRMED | `0` | 是否需要网络确认,禁用可以提高回调速度,启用则可以防止交易失败 |
| APP_URI || 应用访问地址,留空则系统自动获取,前端收银台会用到,建议设置,例如:https://token-pay.example.com |
| WALLET_ADDRESS || 启动时需要添加的钱包地址,多个请用半角符逗号`,`分开;当然,同样也支持通过机器人添加。 |
| TG_BOT_TOKEN || Telegram Bot Token,**必须设置**,否则无法使用 |
| TG_BOT_ADMIN_ID || Telegram Bot 管理员ID,**必须设置**,否则无法使用 |
| TG_BOT_GROUP_ID || Telegram 群组ID,设置之后机器人会将交易消息会推送到此群 |
| TRON_SERVER_API | `TRON_SCAN` | 可选`TRON_SCAN`,`TRON_GRID`,推荐`TRON_GRID``TRON_GRID_API_KEY`搭配使用,*更准更强更及时* |
| TRON_SCAN_API_KEY || TRONSCAN API KEY,如果收款地址较多推荐设置,可避免被官方QOS |
| TRON_GRID_API_KEY || TRONGRID API KEY,如果收款地址较多推荐设置,可避免被官方QOS |
| PAYMENT_AMOUNT_RANGE | `0.01,99999` | 支付监控的允许数额范围(闭区间),设置合理数值可避免一些诱导式诈骗交易提醒 |
| 参数名称 | 默认值 | 用法说明 |
|----------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| EXPIRE_TIME | `600` | 订单有效期,单位秒 |
| USDT_RATE || USDT汇率,默认留空则获取Okx交易所的汇率(每分钟同步一次),支持多种写法,如:`7.4` 表示固定7.4、`~1.02`表示最新汇率上浮2%、`~0.97`表示最新汇率下浮3%、`+0.3`表示最新加0.3、`-0.2`表示最新减0.2,以此类推;如参数错误则使用固定值6.4 |
| USDT_ATOM | `0.01` | `0.01`表示支付数额保留两位小数,相同金额时递增颗粒度为`0.01`,依次类推,如无特殊需求不建议修改。 |
| AUTH_TOKEN | `123234` | 认证Token,对接会用到这个参数 |
| LISTEN | `:8080` | 服务器HTTP监听地址 |
| TRADE_IS_CONFIRMED | `0` | 是否需要网络确认,禁用可以提高回调速度,启用则可以防止交易失败 |
| APP_URI || 应用访问地址,留空则系统自动获取,前端收银台会用到,建议设置,例如:https://token-pay.example.com |
| WALLET_ADDRESS || 启动时需要添加的钱包地址,多个请用半角符逗号`,`分开;当然,同样也支持通过机器人添加。 |
| TG_BOT_TOKEN || Telegram Bot Token,**必须设置**,否则无法使用 |
| TG_BOT_ADMIN_ID || Telegram Bot 管理员ID,**必须设置**,否则无法使用 |
| TG_BOT_GROUP_ID || Telegram 群组ID,设置之后机器人会将交易消息会推送到此群 |
| PAYMENT_AMOUNT_RANGE | `0.01,99999` | 支付监控的允许数额范围(闭区间),设置合理数值可避免一些诱导式诈骗交易提醒 |
| TRON_GRPC_NODE | `18.141.79.38:50051` | Tron区块网络的GRPC节点,可选列表:https://developers.tron.network/docs/networks#public-node |

**Ps:所以综上所述,必须设置的参数有`TG_BOT_TOKEN TG_BOT_ADMIN_ID`,否则无法使用!**

Expand All @@ -70,12 +68,6 @@ Bepusdt 所有参数都是以传递环境变量的方式进行配置,大部分

Telegram 搜索`@userinfobot`机器人并启用,返回的ID就是`TG_BOT_ADMIN_ID`

### 如何申请`TronScan``TronGrid`的ApiKey

目前[TronScan](https://tronscan.org/)[TronGrid](https://www.trongrid.io/)
都可以通过邮箱注册,登录之后在用户中心创建一个ApiKey即可;默认免费套餐都是每天10W请求,对于个人收款绰绰有余。
**❗️最近发现TronScan接口不稳定且数据不及时,可以有条件的话都推荐使用TronGrid。**

## ⚠️ 特别注意

- 订单交易强依赖时间,请确保服务器时间准确性,否则可能导致订单异常!
Expand Down
50 changes: 11 additions & 39 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ const defaultExpireTime = 600 // 订单默认有效期 10分钟
const defaultUsdtRate = 6.4 // 默认汇率
const defaultAuthToken = "123234" // 默认授权码
const defaultListen = ":8080" // 默认监听地址
const TronServerApiScan = "TRON_SCAN"
const TronServerApiGrid = "TRON_GRID"
const defaultPaymentMinAmount = 0.01
const defaultPaymentMaxAmount = 99999
const defaultAtomicity = "0.01" // 原子精度
const defaultAtomicity = "0.01" // 原子精度
const defaultTronGrpcNode = "18.141.79.38:50051" // 默认GRPC节点

var runPath string

Expand All @@ -35,6 +34,15 @@ func init() {
runPath = filepath.Dir(execPath)
}

func GetTronGrpcNode() string {
if data := help.GetEnv("TRON_GRPC_NODE"); data != "" {

return strings.TrimSpace(data)
}

return defaultTronGrpcNode
}

func GetAtomicity() (decimal.Decimal, int) {
var _defaultAtom, _ = decimal.NewFromString(defaultAtomicity)
var _defaultExp = cast.ToInt(math.Abs(float64(_defaultAtom.Exponent())))
Expand Down Expand Up @@ -126,42 +134,6 @@ func GetUsdtRateRaw() string {
return ""
}

func GetTronServerApi() string {
if data := help.GetEnv("TRON_SERVER_API"); data != "" {

return strings.TrimSpace(data)
}

return ""
}

func GetTronScanApiKey() string {
if data := help.GetEnv("TRON_SCAN_API_KEY"); data != "" {

return strings.TrimSpace(data)
}

return ""
}

func GetTronGridApiKey() string {
if data := help.GetEnv("TRON_GRID_API_KEY"); data != "" {

return strings.TrimSpace(data)
}

return ""
}

func IsTronScanApi() bool {
if GetTronServerApi() == TronServerApiScan {

return true
}

return GetTronServerApi() != TronServerApiGrid
}

func GetUsdtRate() (string, decimal.Decimal, float64) {
if data := help.GetEnv("USDT_RATE"); data != "" {
data = strings.TrimSpace(data)
Expand Down
27 changes: 27 additions & 0 deletions app/model/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package model

type Config struct {
K string `gorm:"column:k;type:varchar(32);primaryKey"`
V string `gorm:"column:v;type:varchar(255)"`
}

func (c Config) TableName() string {

return "config"
}

func SetK(k, v string) {
DB.Exec("REPLACE INTO config (k, v) VALUES (?, ?)", k, v)
}

func GetK(k string) string {
var row Config

var tx = DB.Where("k = ?", k).First(&row)
if tx.Error == nil {

return row.V
}

return ""
}
2 changes: 1 addition & 1 deletion app/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func Init() error {

func AutoMigrate() error {

return DB.AutoMigrate(&WalletAddress{}, &TradeOrders{}, &NotifyRecord{})
return DB.AutoMigrate(&WalletAddress{}, &TradeOrders{}, &NotifyRecord{}, &Config{})
}
14 changes: 11 additions & 3 deletions app/model/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type TradeOrders struct {
NotifyUrl string `gorm:"type:varchar(255);not null;default:'';comment:异步地址"`
NotifyNum int `gorm:"type:int(11);not null;default:0;comment:回调次数"`
NotifyState int `gorm:"type:tinyint(1);not null;default:0;comment:回调状态 1:成功 0:失败"`
RefBlockNum int64 `gorm:"type:bigint(20);not null;default:0;comment:交易所在区块"`
ExpiredAt time.Time `gorm:"type:timestamp;not null;comment:订单失效时间"`
CreatedAt time.Time `gorm:"autoCreateTime;type:timestamp;not null;comment:创建时间"`
UpdatedAt time.Time `gorm:"autoUpdateTime;type:timestamp;not null;comment:更新时间"`
Expand All @@ -44,12 +45,19 @@ func (o *TradeOrders) OrderSetExpired() error {
return DB.Save(o).Error
}

func (o *TradeOrders) OrderSetSucc(fromAddress, tradeHash string, confirmedAt time.Time) error {
// 订单标记交易成功
o.Status = OrderStatusSuccess
func (o *TradeOrders) OrderUpdateTxInfo(refBlockNum int64, fromAddress, tradeHash string, confirmedAt time.Time) error {
o.FromAddress = fromAddress
o.ConfirmedAt = confirmedAt
o.TradeHash = tradeHash
o.RefBlockNum = refBlockNum
r := DB.Save(o)

return r.Error
}

func (o *TradeOrders) OrderSetSucc() error {
o.Status = OrderStatusSuccess // 标记成功

r := DB.Save(o)

return r.Error
Expand Down
Loading

0 comments on commit 9f72d69

Please sign in to comment.