第十阶段 stage 2(一):交易对改为 X/CRC 池,任意币互换经 CRC 两跳路由,币价表改为 CRC 价格

This commit is contained in:
ZHU MINCHAO committed 2026-11-05 07:09:05 +08:00
1 parent 75fb6d6b2a
commit 3758049dcb
14 files changed
+196 -48

No files matched your search

+41 -3
View File
@@ -4,6 +4,14 @@ using System.Linq;
namespace Decentralized.Domain
{
/// <summary>一次互换的报价:得到数量、经过几个池、相对中间价的损失(手续费加滑点)。</summary>
public sealed class SwapQuote
{
public string From, To;
public double In, Out, Impact;
public int Hops;
}
/// <summary>
/// 影响行情的游戏事件种类。接入层把游戏的新闻事件与派系行动映射到这里(例如全站停电、电力短缺 → Blackout)。
/// </summary>
@@ -64,14 +72,43 @@ namespace Decentralized.Domain
var m = new Market();
double depth = Tuning.PoolDepth(day), p = Tuning.PscStartPrice;
m.Pools.Add(Pool.Create(Venue.Exchange, Assets.PSC, Assets.CRC, p, depth, Tuning.ExchangeFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.PSC, Assets.CRT, p, depth * SideDepthShare, Tuning.DexFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.PSC, Assets.CRA, p, depth * SideDepthShare, Tuning.DexFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.PSC, Assets.CRC, p, depth * SideDepthShare, Tuning.DexFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.CRT, Assets.CRC, 1, depth * SideDepthShare, Tuning.DexFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.CRA, Assets.CRC, 1, depth * SideDepthShare, Tuning.DexFee));
m.Pools.Add(Pool.Create(Venue.Dex, Assets.SPARK, Assets.CRC, Tuning.SparkStart, depth * Tuning.SparkDepthShare, Tuning.DexFee));
return m;
}
public Pool Main => Pools[0];
/// <summary>DEX 上 X/CRC 的池(所有币都以 CRC 计价)。</summary>
public Pool DexPool(string asset) => Find(Venue.Dex, asset, Assets.CRC);
/// <summary>
/// 任意两币互换的报价:CRC 在一端走一个池,否则 X → CRC → Y 两跳(两次手续费、滑点叠加)。
/// 不改池子;没有路由(同币、没有池)返回 null。
/// </summary>
public SwapQuote Quote(string from, string to, double amount)
{
if (from == to || amount <= 0) return null;
var a = from == Assets.CRC ? null : DexPool(from);
var b = to == Assets.CRC ? null : DexPool(to);
if ((from != Assets.CRC && a == null) || (to != Assets.CRC && b == null)) return null;
double crc = a == null ? amount : a.QuoteOut(amount);
double mid = a == null ? amount : amount * a.Price;
double outAmt = b == null ? crc : b.BaseOut(crc);
double ideal = b == null ? mid : mid / b.Price;
return new SwapQuote { From = from, To = to, In = amount, Out = outAmt, Hops = (a != null ? 1 : 0) + (b != null ? 1 : 0), Impact = ideal > 0 ? Math.Max(0, 1 - outAmt / ideal) : 0 };
}
/// <summary>成交:按 <see cref="Quote"/> 的路由改动池子,返回得到的数量(没有路由返回 0)。</summary>
public double Swap(string from, string to, double amount)
{
if (Quote(from, to, amount) == null) return 0;
double crc = from == Assets.CRC ? amount : DexPool(from).SellBase(amount);
return to == Assets.CRC ? crc : DexPool(to).BuyBase(crc);
}
public Pool Find(Venue venue, string baseAsset, string quote) =>
Pools.FirstOrDefault(x => x.Venue == venue && x.Base == baseAsset && x.Quote == quote);
@@ -152,7 +189,8 @@ namespace Decentralized.Domain
double baseCredits = pool.Base == Assets.PSC ? p : stablePrice(pool.Base);
double quoteCredits = pool.Quote == Assets.PSC ? p : stablePrice(pool.Quote);
if (quoteCredits <= 0) continue;
pool.Revert(baseCredits / quoteCredits, ArbRate, depth * SideDepthShare / quoteCredits);
double share = pool.Base == Assets.SPARK ? Tuning.SparkDepthShare : SideDepthShare;
pool.Revert(baseCredits / quoteCredits, ArbRate, depth * share / quoteCredits);
}
}
}
+1 -1
View File
@@ -97,7 +97,7 @@ namespace Decentralized.Domain
/// <summary>外观折价(占价值的比例):叶片积灰 / 缺角,金手指发暗 / 氧化,挡板划痕,封条缺失。</summary>
public const double CardFanDust = 0.04, CardFanChipped = 0.12, CardFingersDark = 0.04, CardFingersOxidized = 0.12, CardBracketScratch = 0.02, CardSealMissing = 0.06;
/// <summary>非主交易对每晚被拉回参考价的比例,及其深度占主交易对的比例。</summary>
public const double SideArbRate = 0.75, SideDepthShare = 0.3;
public const double SideArbRate = 0.75, SideDepthShare = 0.3, SparkDepthShare = 0.15;
/// <summary>Web3 黑市:开放所需黑市声望,中 / 高档货所需声望,现货每周刷新的天数与基础件数(每高 BmStockPerRep 声望多一件),现货溢价。</summary>
public const int BmOpenRep = 10, BmMidRep = 40, BmHighRep = 80, BmRefreshDays = 7, BmStockBase = 4;
public const double BmStockPerRep = 40, BmPremium = 0.30;
+1
View File
@@ -324,6 +324,7 @@ namespace Decentralized.Domain
["SentimentVol"] = Tuning.SentimentVol,
["SideArbRate"] = Tuning.SideArbRate,
["SideDepthShare"] = Tuning.SideDepthShare,
["SparkDepthShare"] = Tuning.SparkDepthShare,
["SparkFee"] = Tuning.SparkFee,
["SparkMaxMult"] = Tuning.SparkMaxMult,
["SparkMinMult"] = Tuning.SparkMinMult,