forked from magicblock-labs/Solana.Unity-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenQuantity.cs
More file actions
101 lines (86 loc) · 3.41 KB
/
Copy pathTokenQuantity.cs
File metadata and controls
101 lines (86 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using Solana.Unity.Extensions.Models.TokenMint;
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Solana.Unity.Extensions
{
/// <summary>
/// Represents a token quantity of a known mint with a known number of decimal places.
/// </summary>
public class TokenQuantity
{
/// <summary>
/// Constructs a TokenQuantity instance.
/// </summary>
/// <param name="tokenDef">A TokenDef instance that describes this token.</param>
/// <param name="balanceDecimal">Token balance in decimal.</param>
/// <param name="balanceRaw">Token balance in raw ulong.</param>
internal TokenQuantity(TokenDef tokenDef,
decimal balanceDecimal,
ulong balanceRaw)
{
TokenDef = tokenDef ?? throw new ArgumentNullException(nameof(tokenDef));
Symbol = tokenDef.Symbol;
TokenName = tokenDef.TokenName;
TokenMint = tokenDef.TokenMint;
DecimalPlaces = tokenDef.DecimalPlaces;
QuantityDecimal = balanceDecimal;
QuantityRaw = balanceRaw;
}
/// <summary>
/// The origin TokenDef instance
/// </summary>
public TokenDef TokenDef { get; init; }
/// <summary>
/// The token mint public key address.
/// </summary>
public string TokenMint { get; init; }
/// <summary>
/// The symbol this token uses.
/// </summary>
public string Symbol { get; init; }
/// <summary>
/// The name of this token.
/// </summary>
public string TokenName { get; init; }
/// <summary>
/// The number of decimal places this token uses.
/// </summary>
public int DecimalPlaces { get; init; }
/// <summary>
/// Token balance in decimal.
/// </summary>
public decimal QuantityDecimal { get; init; }
/// <summary>
/// Token balance in raw ulong.
/// </summary>
public ulong QuantityRaw { get; init; }
/// <summary>
/// Provide a friendly to read balance with symbol and name.
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (Symbol == TokenName)
return $"{QuantityDecimal.ToString(CultureInfo.InvariantCulture)} {Symbol}";
else
return $"{QuantityDecimal.ToString(CultureInfo.InvariantCulture)} {Symbol} ({TokenName})";
}
/// <summary>
/// Add the value of another TokenQuantity to this TokenQuantity.
/// </summary>
/// <param name="valueDecimal">Number of tokens as decimal to add to this TokenQuantity.</param>
/// <param name="valueRaw">Number of tokens as ulong to add to this TokenQuantity.</param>
/// <returns>A new instance with this TokenQuantity added to the accumulators.</returns>
internal TokenQuantity AddQuantity(decimal valueDecimal,
ulong valueRaw)
{
return new TokenQuantity(this.TokenDef,
QuantityDecimal + valueDecimal,
QuantityRaw + valueRaw);
}
}
}