feat: implement automatic margin pricing and margin reverse engineering in sync

This commit is contained in:
SmartUp Developer
2026-07-02 21:22:37 +08:00
parent 9011c68b9d
commit 0e77de31ae
5 changed files with 106 additions and 34 deletions
+10 -13
View File
@@ -402,21 +402,18 @@ def test_list_accounts_pagination_returns_none_when_any_page_fails():
def test_calculate_target_rate_priority_weighted():
# 0.18、0.25 + 50% 应得到约 0.29
assert calculate_target_rate(["0.18", "0.25"], 50, "priority_weighted_plus_percent") == Decimal("0.29")
# 单来源时等同于该来源倍率 + 百分比
assert calculate_target_rate(["0.18"], 50, "priority_weighted_plus_percent") == Decimal("0.27")
# 多来源时最低倍率占 85%,其余平分 15%:
# 0.18 * 0.85 + (0.25 + 0.38) * 0.075 = 0.20025 -> 0.20025 * 1.5 = 0.300375 -> 0.30
assert calculate_target_rate(["0.18", "0.25", "0.38"], 50, "priority_weighted_plus_percent") == Decimal("0.30")
# 0.18、0.25 应当至少满足 min * 1.50 (0.27) 和 max * 1.10 (0.275),向上取整为 0.28,忽略 percent 输入
assert calculate_target_rate(["0.18", "0.25"], 50, "priority_weighted_plus_percent") == Decimal("0.28")
assert calculate_target_rate(["0.18", "0.25"], 0, "priority_weighted_plus_percent") == Decimal("0.28")
# 4 个来源以锁定均分 15% 的权重语义:
# 0.18 * 0.85 + (0.24 + 0.30 + 0.36) * 0.05 = 0.198 -> 0.198 * 1.5 = 0.297 -> 0.30
assert calculate_target_rate(["0.18", "0.24", "0.30", "0.36"], 50, "priority_weighted_plus_percent") == Decimal("0.30")
assert calculate_target_rate(["0.18", "0.24", "0.30", "0.36"], 0, "priority_weighted_plus_percent") == Decimal("0.20")
# 单来源 0.05 自动得到 0.08 (max(0.05 * 1.50, 0.05 * 1.10) = 0.075 -> ROUND_UP -> 0.08)
assert calculate_target_rate(["0.05"], 0, "priority_weighted_plus_percent") == Decimal("0.08")
# 验证保护线与精度保护:最低来源 0.124,应当四舍五入并返回两位小数
assert calculate_target_rate(["0.124"], 0, "priority_weighted_plus_percent") == Decimal("0.12")
# 4 个来源:min=0.18, max=0.36. max(0.27, 0.396) = 0.396 -> ROUND_UP -> 0.40
assert calculate_target_rate(["0.18", "0.24", "0.30", "0.36"], 50, "priority_weighted_plus_percent") == Decimal("0.40")
# 验证保护线与精度保护:最低来源 0.124。max(0.186, 0.1364) = 0.186 -> ROUND_UP -> 0.19
assert calculate_target_rate(["0.124"], 0, "priority_weighted_plus_percent") == Decimal("0.19")
# 无可用正数倍率时报错
with pytest.raises(WebsiteError, match="没有可用的正数上游倍率"):