|
| 1 | + |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace BinanceBot.Market.Domain; |
| 5 | + |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Market Contract type |
| 9 | +/// </summary> |
| 10 | +public enum ContractType |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Spot market |
| 14 | + /// </summary> |
| 15 | + Spot, |
| 16 | + /// <summary> |
| 17 | + /// Futures contract |
| 18 | + /// </summary> |
| 19 | + Futures |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// A market symbol representation based on a Base and Quote asset and Contract type |
| 25 | +/// </summary> |
| 26 | +public record MarketSymbol |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="MarketSymbol"/> class. |
| 30 | + /// </summary> |
| 31 | + /// <param name="baseAsset"></param> |
| 32 | + /// <param name="quoteAsset"></param> |
| 33 | + /// <param name="contractType"></param> |
| 34 | + /// <exception cref="ArgumentNullException">Thrown when baseAsset or quoteAsset is null.</exception> |
| 35 | + public MarketSymbol(string baseAsset, string quoteAsset, ContractType contractType) |
| 36 | + { |
| 37 | + BaseAsset = baseAsset ?? throw new ArgumentNullException(nameof(baseAsset)); |
| 38 | + QuoteAsset = quoteAsset ?? throw new ArgumentNullException(nameof(quoteAsset)); |
| 39 | + ContractType = contractType; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Initializes a new instance of the <see cref="MarketSymbol"/> class from a pair string. |
| 44 | + /// </summary> |
| 45 | + /// <param name="pair">The trading pair in the format "BASE/QUOTE".</param> |
| 46 | + /// <param name="contractType">The contract type (Spot or Futures). Defaults to Spot.</param> |
| 47 | + /// <exception cref="ArgumentException">Thrown when the pair format is invalid.</exception> |
| 48 | + public MarketSymbol(string pair, ContractType contractType = ContractType.Spot) |
| 49 | + { |
| 50 | + if (string.IsNullOrWhiteSpace(pair)) |
| 51 | + throw new ArgumentException("Pair cannot be null or empty", nameof(pair)); |
| 52 | + |
| 53 | + var assets = pair.Split('/', StringSplitOptions.RemoveEmptyEntries); |
| 54 | + if (assets.Length != 2) |
| 55 | + throw new ArgumentException("Pair must be in the format 'BASE/QUOTE'", nameof(pair)); |
| 56 | + |
| 57 | + BaseAsset = assets[0]; |
| 58 | + QuoteAsset = assets[1]; |
| 59 | + ContractType = contractType; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// The base asset of the symbol |
| 64 | + /// </summary> |
| 65 | + public string BaseAsset { get; init; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// The quote asset of the symbol |
| 69 | + /// </summary> |
| 70 | + public string QuoteAsset { get; init; } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// The symbol name, can be used to overwrite the default formatted name |
| 74 | + /// </summary> |
| 75 | + public string FullName => $"{BaseAsset}{QuoteAsset}"; |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// The Contract type of the symbol |
| 79 | + /// </summary> |
| 80 | + public ContractType ContractType { get; init; } |
| 81 | + |
| 82 | + override public string ToString() => $"{BaseAsset}/{QuoteAsset} ({ContractType})"; |
| 83 | +} |
0 commit comments