第十阶段 stage 2(一):交易对改为 X/CRC 池,任意币互换经 CRC 两跳路由,币价表改为 CRC 价格
This commit is contained in:
1 parent
75fb6d6b2a
commit
3758049dcb
14 files changed
+196
-48
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user