From 0ed85860fcd3a18dfd60d5479648c659baad55be Mon Sep 17 00:00:00 2001 From: v03413 Date: Sun, 22 Feb 2026 22:11:38 +0800 Subject: [PATCH] feat: add block offset confirmation feature resolved: https://github.com/v03413/BEpusdt/issues/160 --- app/model/conf.go | 1 + app/model/const.go | 1 + app/task/aptos.go | 9 +++++++++ app/task/evm.go | 10 ++++++++++ app/task/solana.go | 9 +++++++++ app/task/tron.go | 22 +++++++++++++++------- web/src/views/system/base/base.vue | 1 + web/src/views/system/base/info.vue | 20 ++++++++++++++++++++ 8 files changed, 66 insertions(+), 7 deletions(-) diff --git a/app/model/conf.go b/app/model/conf.go index 8bf7f5b..daee6f1 100644 --- a/app/model/conf.go +++ b/app/model/conf.go @@ -148,6 +148,7 @@ func ConfInit() { RpcEndpointPlasma: "https://rpc.plasma.to/", NotifyMaxRetry: "10", BlockHeightMaxDiff: "1000", + BlockOffsetConfirm: "0", PaymentTimeout: "1200", // 20分钟 PaymentStaticPath: "", PaymentMatchMode: string(Classic), diff --git a/app/model/const.go b/app/model/const.go index fb4fd7d..4e2e09e 100644 --- a/app/model/const.go +++ b/app/model/const.go @@ -67,6 +67,7 @@ const ( RateSyncInterval ConfKey = "rate_sync_interval" // 汇率同步间隔,单位秒 NotifyMaxRetry ConfKey = "notify_max_retry" // 最大重试次数,订单回调失败 BlockHeightMaxDiff ConfKey = "block_height_max_diff" // 区块高度最大差值,超过此值则以当前区块高度为准,重新开始扫描 + BlockOffsetConfirm ConfKey = "block_offset_confirm" // 区块偏移确认数,扫描时以当前区块高度减去此偏移量为准,避免重链导致的订单回调失败 NotifierParams ConfKey = "notifier_params" // 通知参数 (token, chat_id, email NotifierChannel ConfKey = "notifier_channel" // 通知渠道 (telegram, wechat, email diff --git a/app/task/aptos.go b/app/task/aptos.go index 1df641c..6e161aa 100644 --- a/app/task/aptos.go +++ b/app/task/aptos.go @@ -408,6 +408,15 @@ func (a *aptos) tradeConfirmHandle(ctx context.Context) { var wg sync.WaitGroup var handle = func(o model.Order) { + if model.GetC(model.BlockOffsetConfirm) == "1" { + if a.lastVersion == 0 { + return + } + if a.lastVersion-o.RefBlockNum < a.versionConfirmedOffset { + return + } + } + req, _ := http.NewRequestWithContext(ctx, "GET", model.Endpoint(conf.Aptos)+"v1/transactions/by_hash/"+o.RefHash, nil) resp, err := a.client.Do(req) if err != nil { diff --git a/app/task/evm.go b/app/task/evm.go index 185996d..2a8f52e 100644 --- a/app/task/evm.go +++ b/app/task/evm.go @@ -399,6 +399,16 @@ func (e *evm) tradeConfirmHandle(ctx context.Context) { var wg sync.WaitGroup var handle = func(o model.Order) { + if model.GetC(model.BlockOffsetConfirm) == "1" { + last, ok := chainBlockNum.Load(e.Network) + if !ok { + return + } + if last.(int64)-o.RefBlockNum < e.Block.ConfirmedOffset { + return + } + } + post := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["%s"],"id":1}`, o.RefHash)) req, err := http.NewRequestWithContext(ctx, "POST", e.rpcEndpoint(), bytes.NewBuffer(post)) if err != nil { diff --git a/app/task/solana.go b/app/task/solana.go index 5aae0e0..9cd7161 100644 --- a/app/task/solana.go +++ b/app/task/solana.go @@ -361,6 +361,15 @@ func (s *solana) tradeConfirmHandle(ctx context.Context) { var wg sync.WaitGroup var handle = func(o model.Order) { + if model.GetC(model.BlockOffsetConfirm) == "1" { + if s.lastSlotNum == 0 { + return + } + if s.lastSlotNum-o.RefBlockNum < s.slotConfirmedOffset { + return + } + } + post := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"getSignatureStatuses","params":[["%s"],{"searchTransactionHistory":true}]}`, o.RefHash)) req, _ := http.NewRequestWithContext(ctx, "POST", model.Endpoint(conf.Solana), bytes.NewBuffer(post)) resp, err := s.client.Do(req) diff --git a/app/task/tron.go b/app/task/tron.go index 7471291..7036bc4 100644 --- a/app/task/tron.go +++ b/app/task/tron.go @@ -32,10 +32,11 @@ var usdtTrc20ContractAddress = []byte{0x41, 0xa6, 0x14, 0xf8, 0x03, 0xb6, 0xfd, var usdcTrc20ContractAddress = []byte{0x41, 0x34, 0x87, 0xb6, 0x3d, 0x30, 0xb5, 0xb2, 0xc8, 0x7f, 0xb7, 0xff, 0xa8, 0xbc, 0xfa, 0xde, 0x38, 0xea, 0xac, 0x1a, 0xbe} type tron struct { - lastBlockNum int64 - blockScanQueue *chanx.UnboundedChan[int64] - conn map[string]*grpc.ClientConn - connMu sync.RWMutex + lastBlockNum int64 + blockConfirmedOffset int64 + blockScanQueue *chanx.UnboundedChan[int64] + conn map[string]*grpc.ClientConn + connMu sync.RWMutex } var tr tron @@ -49,9 +50,10 @@ func init() { func newTron() tron { return tron{ - lastBlockNum: 0, - blockScanQueue: chanx.NewUnboundedChan[int64](context.Background(), 30), - conn: make(map[string]*grpc.ClientConn), + lastBlockNum: 0, + blockConfirmedOffset: 30, + blockScanQueue: chanx.NewUnboundedChan[int64](context.Background(), 30), + conn: make(map[string]*grpc.ClientConn), } } @@ -395,6 +397,12 @@ func (t *tron) tradeConfirmHandle(ctx context.Context) { var wg sync.WaitGroup var handle = func(o model.Order) { + if model.GetC(model.BlockOffsetConfirm) == "1" { + if t.lastBlockNum == 0 || t.lastBlockNum-o.RefBlockNum < t.blockConfirmedOffset { + return + } + } + conn, err := t.client() if err != nil { log.Task.Error("grpc.NewClient", err) diff --git a/web/src/views/system/base/base.vue b/web/src/views/system/base/base.vue index 5914306..cee55bb 100644 --- a/web/src/views/system/base/base.vue +++ b/web/src/views/system/base/base.vue @@ -88,6 +88,7 @@ const getConf = async () => { "admin_username", "admin_secure", "block_height_max_diff", + "block_offset_confirm", "admin_login_at", "admin_login_ip", "notify_max_retry", diff --git a/web/src/views/system/base/info.vue b/web/src/views/system/base/info.vue index 45f18b2..53bfa32 100644 --- a/web/src/views/system/base/info.vue +++ b/web/src/views/system/base/info.vue @@ -11,6 +11,17 @@ + + + 关闭 + 开启 + + + { await setsConfAPI([ { key: "block_height_max_diff", value: form.value.block_height_max_diff }, + { key: "block_offset_confirm", value: form.value.block_offset_confirm }, { key: "notify_max_retry", value: form.value.notify_max_retry }, { key: "payment_max_amount", value: form.value.payment_max_amount }, { key: "payment_min_amount", value: form.value.payment_min_amount }, @@ -149,6 +168,7 @@ watch( () => data.value, () => { form.value.block_height_max_diff = data.value.block_height_max_diff; + form.value.block_offset_confirm = data.value.block_offset_confirm || "0"; form.value.notify_max_retry = data.value.notify_max_retry; form.value.payment_max_amount = data.value.payment_max_amount; form.value.payment_min_amount = data.value.payment_min_amount;