fix coin seller stock deduction delta

This commit is contained in:
local 2026-06-23 23:52:06 +08:00
parent ac767d1c7e
commit 3cf62395e0
2 changed files with 8 additions and 1 deletions

View File

@ -57,7 +57,7 @@ func (r *Repository) AdminCreditCoinSellerStock(ctx context.Context, command led
if availableDelta < 0 && account.AvailableAmount < command.CoinAmount {
return ledger.CoinSellerStockCreditReceipt{}, xerr.New(xerr.InsufficientBalance, "insufficient balance")
}
balanceAfter, err := checkedAdd(account.AvailableAmount, availableDelta)
balanceAfter, err := checkedAddDelta(account.AvailableAmount, availableDelta)
if err != nil {
return ledger.CoinSellerStockCreditReceipt{}, err
}

View File

@ -49,6 +49,13 @@ func checkedAdd(left int64, right int64) (int64, error) {
return left + right, nil
}
func checkedAddDelta(left int64, right int64) (int64, error) {
if (right > 0 && left > math.MaxInt64-right) || (right < 0 && left < math.MinInt64-right) {
return 0, xerr.New(xerr.CoinSellerStockAmountInvalid, "coin amount overflow")
}
return left + right, nil
}
func stableHash(value string) string {
sum := sha256.Sum256([]byte(value))
return hex.EncodeToString(sum[:])