Per review, drop the separate truncate mode and use its logic for has_prefix instead. Order amounts are stored without trailing zeros (1.50 as "1.5"), so the plain string prefix check let the 1.5 order claim a 1.51 payment meant for a concurrent order on the same address. Truncate the received amount to the larger of the atom precision and the order's own decimals, then compare exactly: overpayment below the atom precision still matches, underpayment and overpayment of a full atom no longer do.
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package task
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/shopspring/decimal"
|
|
"github.com/v03413/bepusdt/app/model"
|
|
)
|
|
|
|
func TestAmountMatchHasPrefix(t *testing.T) {
|
|
initTaskTestEnv(t, map[model.ConfKey]string{model.PaymentMatchMode: string(model.HasPrefix)})
|
|
|
|
cases := []struct {
|
|
paid, order string
|
|
want bool
|
|
}{
|
|
{"1.5", "1.5", true},
|
|
{"1.50123", "1.5", true},
|
|
{"1.509", "1.5", true},
|
|
{"1.51", "1.5", false},
|
|
{"1.4999", "1.5", false},
|
|
{"2", "2", true},
|
|
{"2.009", "2", true},
|
|
{"2.01", "2", false},
|
|
{"2.5", "2", false},
|
|
{"10.379", "10.37", true},
|
|
{"10.38", "10.37", false},
|
|
{"20", "2", false},
|
|
{"100.5", "100", false},
|
|
{"123.456789", "123.45", true},
|
|
// 订单金额小数位多于原子精度时按订单金额的小数位截断
|
|
{"1.5059", "1.505", true},
|
|
{"1.506", "1.505", false},
|
|
}
|
|
for _, c := range cases {
|
|
got := amountMatch(decimal.RequireFromString(c.paid), c.order, string(model.UsdtTrc20))
|
|
if got != c.want {
|
|
t.Errorf("paid %s order %s: got %v, want %v", c.paid, c.order, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 同一地址并发订单按原子精度递增金额,截断后每笔付款最多对应其中一笔
|
|
func TestAmountMatchHasPrefixConcurrentOrders(t *testing.T) {
|
|
initTaskTestEnv(t, map[model.ConfKey]string{model.PaymentMatchMode: string(model.HasPrefix)})
|
|
|
|
orders := []string{"1.5", "1.51", "1.52"}
|
|
payments := map[string]string{"1.5": "1.5", "1.50123": "1.5", "1.51": "1.51", "1.519": "1.51", "1.52": "1.52"}
|
|
for paid, want := range payments {
|
|
var matched []string
|
|
for _, o := range orders {
|
|
if amountMatch(decimal.RequireFromString(paid), o, string(model.UsdtTrc20)) {
|
|
matched = append(matched, o)
|
|
}
|
|
}
|
|
if len(matched) != 1 || matched[0] != want {
|
|
t.Errorf("paid %s matched %v, want only %s", paid, matched, want)
|
|
}
|
|
}
|
|
}
|