Domain: market, stablecoins, compliant cash-out with AML

Market: PSCoin reference price with sentiment, anchor shifts from game
events (blackout, crackdown), daily volatility, NPC whales, a day-average
mark price, and side pairs pulled toward the reference by arbitrage.
Stablecoins: CRC depeg and haircut, CRT depeg and freeze chance by black
market reputation, CRA staking subsidy, warnings and the three-day spiral.
Exchange: CRC cash-out at market price, AML hit chance from size, 7-day
total, taint ratio and mixer/flag; light hits confiscate the tainted part,
heavy hits also report it as fencing; tax only on gains.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZHU MINCHAOandClaude Opus 5.5 committed 2026-11-03 08:55:31 +08:00
1 parent ce3bed1467
commit efc58888f3
6 files changed
+422

No files matched your search

+118
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Decentralized.Domain;
@@ -158,6 +159,123 @@ internal static class Program
$"PMD 20% / 76% / 95%:{q[0]:P0} / {q[1]:P0} / {q[2]:P0}");
});
Test("市场:价格锚受停电、镇压影响;夜间向锚回归并限制在 0.3–3 倍", () =>
{
var m = Market.CreateDefault(1);
var none = new MarketEvent[0];
Check(Near(m.Anchor(none), 100, 1e-9) && Near(m.Anchor(new[] { MarketEvent.Blackout }), 110, 1e-9)
&& Near(m.Anchor(new[] { MarketEvent.Crackdown }), 92, 1e-9), "锚:基准 100,停电 ×1.1,镇压 ×0.92");
Check(Market.Volatility(new[] { MarketEvent.GangWar }) == Tuning.PscVol * 2, "火并时波动 ×2");
m.Main.SetPrice(60, Tuning.PoolDepth(1));
var rng = new Rng(5);
double sum = 0;
for (int i = 0; i < 400; i++)
{
var mm = Market.CreateDefault(1);
mm.Main.SetPrice(60, Tuning.PoolDepth(1));
mm.Night(none, new Rng((ulong)i), 1, a => 1);
sum += Math.Log(mm.Price);
}
double expect = Math.Log(60) + 0.15 * (Math.Log(100) - Math.Log(60));
Check(Near(sum / 400, expect, 0.01), $"从 60 起一晚平均回到 {Math.Exp(expect):0.0} 附近:{Math.Exp(sum / 400):0.0}");
var hi = Market.CreateDefault(1);
hi.Main.SetPrice(1000, Tuning.PoolDepth(1));
hi.Night(none, rng, 1, a => 1);
Check(hi.Price <= 300 + 1e-9, $"上限 3 倍:{hi.Price:0.0}");
});
Test("市场:其他交易对每晚被套利拉向参考价,三角价差收敛", () =>
{
var m = Market.CreateDefault(1);
var crt = m.Find(Venue.Dex, Assets.PSC, Assets.CRT);
crt.SetPrice(130, crt.QuoteReserve);
for (int d = 1; d <= 5; d++) m.Night(new MarketEvent[0], new Rng((ulong)d), 1, a => 1);
double gap = Math.Abs(crt.Price / m.Price - 1);
Check(gap < 0.03, $"五晚后 PSC/CRT 与参考价相差 {gap:P1}");
var crtCrc = m.Find(Venue.Dex, Assets.CRT, Assets.CRC);
m.Night(new MarketEvent[0], new Rng(9), 1, a => a == Assets.CRT ? 0.9 : 1);
Check(crtCrc.Price < 0.98, $"CRT 脱锚时 CRT/CRC 向 0.9 靠拢:{crtCrc.Price:0.000}");
});
Test("稳定币:CRC 脱锚与减记,CRT 脱锚与冻结概率", () =>
{
int depegs = 0, haircuts = 0;
for (int i = 0; i < 2000; i++)
{
var s = new Stablecoins();
var rng = new Rng((ulong)i);
s.OnEventStart(MarketEvent.RevRaidUpper, 10, rng);
if (s.Crc < 1)
{
depegs++;
Check(s.Crc >= 0.88 && s.Crc <= 0.93 && s.CrcDepegEnd >= 12 && s.CrcDepegEnd <= 13, "CRC 脱锚深度与持续");
double f = 1;
for (int d = 10; d <= s.CrcDepegEnd; d++) f *= s.Night(d, rng, 0);
if (f < 1) haircuts++;
Check(s.Crc == 1, "到期恢复");
}
}
Check(Near(depegs / 2000.0, 0.30, 0.03), $"革命军袭击上层时 30% 脱锚:{depegs / 2000.0:P0}");
Check(Near(haircuts / (double)depegs, 0.15, 0.04), $"其中 15% 减记:{haircuts / (double)depegs:P0}");
Check(Near(Stablecoins.CrtFreezeChance(0), 0.40, 1e-9) && Stablecoins.CrtFreezeChance(50) == 0 && Near(Stablecoins.CrtFreezeChance(25), 0.2, 1e-9),
"CRT 冻结概率:声望 0 为 40%,25 为 20%,50 为 0");
});
Test("稳定币:CRA 补贴池告急即为预兆;螺旋三天 0.85 / 0.30 / 0.02", () =>
{
var s = new Stablecoins { CraSubsidy = Tuning.CraSubsidy0 * 0.19 };
Check(s.CraWarning(1) && s.CraRate == Tuning.CraRateLow, "补贴池低于 20%:预兆出现、利率降到 0.2%");
var t = new Stablecoins { CraSubsidy = 1 };
var rng = new Rng(3);
t.Night(1, rng, 100000);
Check(t.CraWarnDay == 1 && t.CraSpiralDay >= 5 && t.CraSpiralDay <= 8, $"补贴池吃空触发螺旋,提前 4–7 天:第 {t.CraSpiralDay} 天开始");
var path = new List<double>();
for (int d = 2; d <= t.CraSpiralDay + 3; d++) { t.Night(d, rng, 0); if (d >= t.CraSpiralDay) path.Add(t.Cra); }
Check(path.Take(3).SequenceEqual(new[] { 0.85, 0.30, 0.02 }), $"螺旋路径:{string.Join(" / ", path)}");
Check(!t.CraWarning(t.CraSpiralDay + 1), "螺旋开始后不再算预兆");
});
Test("合规出金:干净小额不查;污点高时重度命中记罪证;轻度命中只没收污点部分;只对收益计税", () =>
{
Func<string, double> px = a => 1;
Check(Exchange.HitChance(500, 0, 0, 200, false) == 0, "干净、低于 5 倍房租:不查");
Check(Near(Exchange.HitChance(2000, 0, 0, 200, false), 0.2, 1e-9), "单笔为大额阈值 2 倍:20%");
Check(Near(Exchange.HitChance(1000, 500, 0, 200, false), 0.75, 1e-9), "污点 50%:75%");
Check(Exchange.HitChance(100000, 100000, 1e6, 200, true) == Tuning.AmlMaxChance, "上限 90%");
int heavy = 0;
double crime = 0;
for (int i = 0; i < 1000; i++)
{
var w = new Wallet { Taint = 500, Basis = 1000 };
w.Add(Assets.CRC, 1000);
var r = new Exchange().Cashout(w, 1000, px, 1, 200, false, new Rng((ulong)i));
if (r.Outcome == AmlOutcome.Heavy) { heavy++; crime = r.Crime; Check(Near(r.Received, 998 - 500, 1e-6), "重度:没收污点部分"); }
else Check(Near(r.Received, 998, 1e-6) && r.Tax == 0, "未命中:全额到账;本金不计税");
}
Check(Near(heavy / 1000.0, 0.75, 0.04) && crime == 500, $"污点 50% 时约 75% 重度命中,罪证 = 污点额:{heavy / 1000.0:P0}");
var light = new Wallet { Taint = 150 };
light.Add(Assets.CRC, 1000);
CashoutResult lr = null;
for (ulong i = 0; lr == null || lr.Outcome == AmlOutcome.Clear; i++)
{
var w = new Wallet { Taint = 150 };
w.Add(Assets.CRC, 1000);
lr = new Exchange().Cashout(w, 1000, px, 1, 200, false, new Rng(i));
}
Check(lr.Outcome == AmlOutcome.Light && lr.Crime == 0 && Near(lr.Confiscated, 150, 1e-6), "污点 15%:轻度命中,没收 150、不记罪证");
Check(Near(lr.Tax, (998 - 150) * Tuning.TaxRate, 1e-6), "成本为 0 时到账部分全额计税");
var ex = new Exchange();
var big = new Wallet();
big.Add(Assets.CRC, 4000);
ex.Cashout(big, 2000, px, 1, 200, false, new Rng(1));
Check(Near(Exchange.HitChance(2000, 0, ex.History.Sum(h => h.Value), 200, false), 0.2 + 0.2 * 1000 / 3000.0, 1e-9),
"7 天内累计超过 15 倍房租也会提高命中率");
var dep = new Wallet { Basis = 1000 };
dep.Add(Assets.CRC, 1000);
var dr = new Exchange().Cashout(dep, 1000, a => 0.9, 1, 200, false, new Rng(1));
Check(Near(dr.Received, 900 * (1 - Tuning.ExchangeFee), 1e-6), "CRC 脱锚时按市价兑出");
});
Console.WriteLine($"\n{_passed} 通过,{_failed} 失败");
return _failed == 0 ? 0 : 1;
}