feat: add block offset confirmation feature
resolved: https://github.com/v03413/BEpusdt/issues/160
This commit is contained in:
@@ -148,6 +148,7 @@ func ConfInit() {
|
||||
RpcEndpointPlasma: "https://rpc.plasma.to/",
|
||||
NotifyMaxRetry: "10",
|
||||
BlockHeightMaxDiff: "1000",
|
||||
BlockOffsetConfirm: "0",
|
||||
PaymentTimeout: "1200", // 20分钟
|
||||
PaymentStaticPath: "",
|
||||
PaymentMatchMode: string(Classic),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
+15
-7
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -11,6 +11,17 @@
|
||||
<a-input v-model="form.block_height_max_diff" placeholder="推荐 1000" />
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item
|
||||
field="block_offset_confirm"
|
||||
label="区块偏移确认"
|
||||
extra="启用后可进一步提高交易的安全性,但会增加交易确认的等待时间"
|
||||
>
|
||||
<a-select v-model="form.block_offset_confirm" placeholder="请选择">
|
||||
<a-option value="0">关闭</a-option>
|
||||
<a-option value="1">开启</a-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item
|
||||
field="notify_max_retry"
|
||||
label="回调最大重试"
|
||||
@@ -72,6 +83,7 @@ const data = defineModel() as any;
|
||||
const form = ref({
|
||||
payment_timeout: "",
|
||||
block_height_max_diff: "",
|
||||
block_offset_confirm: "0",
|
||||
notify_max_retry: "",
|
||||
payment_max_amount: "",
|
||||
payment_min_amount: "",
|
||||
@@ -86,6 +98,12 @@ const rules = {
|
||||
message: "区块高度最大差值不能为空"
|
||||
}
|
||||
],
|
||||
block_offset_confirm: [
|
||||
{
|
||||
required: true,
|
||||
message: "区块偏移确认不能为空"
|
||||
}
|
||||
],
|
||||
notify_max_retry: [
|
||||
{
|
||||
required: true,
|
||||
@@ -133,6 +151,7 @@ const onSubmit = async ({ errors }: ArcoDesign.ArcoSubmit) => {
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user