-
Notifications
You must be signed in to change notification settings - Fork 22
/
Coin.ts
212 lines (186 loc) 路 5.17 KB
/
Coin.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { JSONSerializable } from '../util/json';
import { Denom } from './Denom';
import { Dec, Int, Numeric } from './numeric';
import { Coin as Coin_pb } from '@terra-money/terra.proto/cosmos/base/v1beta1/coin';
/**
* Captures `sdk.Coin` and `sdk.DecCoin` from Cosmos SDK. A composite value that combines
* a denomination with an amount value. Coins are immutable once created, and operations
* that return Coin will return a new Coin. See [[Coins]] for a collection of Coin objects.
*/
export class Coin
extends JSONSerializable<Coin.Amino, Coin.Data, Coin.Proto>
implements Numeric<Coin>
{
public readonly amount: Numeric.Output;
/**
* Creates a new coin. Depending on the type of amount, it will be converted to an
* integer coin or decimal coin.
*
* @param denom denomination
* @param amount coin's amount
*/
constructor(public readonly denom: Denom, amount: Numeric.Input) {
super();
this.amount = Numeric.parse(amount);
}
/**
* Checks whether the Coin is an Integer coin.
*/
public isIntCoin(): boolean {
// TODO: convert into typeguard
return this.amount instanceof Int;
}
/**
* Checks whether the Coin is a Decimal coin.
*/
public isDecCoin(): boolean {
return this.amount instanceof Dec;
}
/**
* Turns the Coin into an Integer coin.
*/
public toIntCoin(): Coin {
return new Coin(this.denom, new Int(this.amount));
}
/**
* Turns the Coin into an Integer coin with ceiling the amount.
*/
public toIntCeilCoin(): Coin {
return new Coin(this.denom, new Int(this.amount.ceil()));
}
/**
* Turns the Coin into a Decimal coin.
*/
public toDecCoin(): Coin {
return new Coin(this.denom, new Dec(this.amount));
}
/**
* Outputs `<amount><denom>`.
*
* Eg: `Coin('uluna', 1500) -> 1500uluna`
*/
public toString(): string {
const amount = this.amount.toFixed();
if (this.isDecCoin() && amount.indexOf('.') === -1) {
return `${amount}.0${this.denom}`;
}
return `${amount}${this.denom}`;
}
public static fromString(str: string): Coin {
const m = str.match(/^(-?[0-9]+(\.[0-9]+)?)([0-9a-zA-Z/]+)$/);
if (m === null) {
throw new Error(`failed to parse to Coin: ${str}`);
}
const amount = m[1];
const denom = m[3];
return new Coin(denom, amount);
}
/**
* Creates a new Coin adding to the current value.
*
* @param other
*/
public add(other: Numeric.Input | Coin): Coin {
let otherAmount;
if (other instanceof Coin) {
if (other.denom !== this.denom) {
throw new Coin.ArithmeticError(
`cannot add two Coins of different denoms: ${this.denom} and ${other.denom}`
);
}
otherAmount = other.amount;
} else {
otherAmount = other;
}
otherAmount = Numeric.parse(otherAmount);
return new Coin(this.denom, this.amount.add(otherAmount));
}
/**
* Creates a new Coin subtracting from the current value.
* @param other
*/
public sub(other: Numeric.Input | Coin): Coin {
let otherAmount;
if (other instanceof Coin) {
if (other.denom !== this.denom) {
throw new Coin.ArithmeticError(
`cannot subtract two Coins of different denoms: ${this.denom} and ${other.denom}`
);
}
otherAmount = other.amount;
} else {
otherAmount = other;
}
otherAmount = Numeric.parse(otherAmount);
return new Coin(this.denom, this.amount.sub(otherAmount));
}
/**
* Multiplies the current value with an amount.
* @param other
*/
public mul(other: Numeric.Input): Coin {
const otherAmount = Numeric.parse(other);
return new Coin(this.denom, this.amount.mul(otherAmount));
}
/**
* Divides the current value with an amount.
* @param other
*/
public div(other: Numeric.Input): Coin {
const otherAmount = Numeric.parse(other);
return new Coin(this.denom, this.amount.div(otherAmount));
}
/**
* Modulo the current value with an amount.
* @param other
*/
public mod(other: Numeric.Input): Coin {
const otherAmount = Numeric.parse(other);
return new Coin(this.denom, this.amount.mod(otherAmount));
}
public static fromAmino(data: Coin.Amino): Coin {
const { denom, amount } = data;
return new Coin(denom, amount);
}
public toAmino(): Coin.Amino {
const { denom, amount } = this;
return {
denom,
amount: amount.toString(),
};
}
public static fromData(data: Coin.Data): Coin {
const { denom, amount } = data;
return new Coin(denom, amount);
}
public toData(): Coin.Data {
const { denom, amount } = this;
return {
denom,
amount: amount.toString(),
};
}
public static fromProto(proto: Coin.Proto): Coin {
return new Coin(proto.denom, Numeric.parse(proto.amount));
}
public toProto(): Coin.Proto {
return Coin_pb.fromPartial({
denom: this.denom,
amount: this.amount.toString(),
});
}
}
export namespace Coin {
export interface Amino {
denom: Denom;
amount: string;
}
export interface Data {
denom: Denom;
amount: string;
}
export class ArithmeticError {
constructor(public readonly message: string) {}
}
export type Proto = Coin_pb;
}