-
Notifications
You must be signed in to change notification settings - Fork 8
/
currency.ts
162 lines (149 loc) · 5.2 KB
/
currency.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
import Decimal from "decimal.js";
import { expose, validateInput } from "../helpers";
import { CurrencyModel } from "./models";
class TFTUSDConversionService {
// TFT rate: 1 TFT = x USD
/**
* Class representing a service for converting between TFT (ThreeFold Token) and USD (United States Dollar).
* The service provides methods for normalizing currency amounts, converting between TFT and USD, and calculating daily, monthly, and yearly amounts in both currencies.
*/
constructor(private _rate: number, private decimals = 2) {}
get rate() {
return this._rate;
}
/**
* Normalize the currency amount to a string with a fixed number of decimals.
*
* @param options - The currency model containing the amount to normalize.
* @returns A string representation of the normalized currency amount.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
normalizeCurrency(options: CurrencyModel): string {
return new Decimal(options.amount).toFixed(this.decimals);
}
/**
* Convert the given amount from `USD` to `TFT` using the current conversion rate.
*
* @param options - The currency model containing the amount in `USD`.
* @returns A string representation of the converted amount in `TFT`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
convertUSDtoTFT(options: CurrencyModel): string {
const amount = options.amount / this.rate;
return this.normalizeCurrency({ amount });
}
/**
* Convert the given amount from `TFT` to `USD` using the current conversion rate.
*
* @param options - The currency model containing the amount in `TFT`.
* @returns A string representation of the converted amount in `USD`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
convertTFTtoUSD(options: CurrencyModel): string {
const amount = options.amount * this.rate;
return this.normalizeCurrency({ amount });
}
/**
* Changes the rate from hourly to daily.
*
* @param options - The currency model containing the amount in `TFT`.
* @returns A string representation of the converted amount in `TFT`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
dailyTFT(options: CurrencyModel): string {
const hours = options.amount * 24;
return this.normalizeCurrency({ amount: hours });
}
/**
* Changes the rate from hourly to monthly.
*
* @param options - The currency model containing the amount in `TFT`.
* @returns A string representation of the converted amount in `TFT`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
monthlyTFT(options: CurrencyModel): string {
const months = +this.dailyTFT(options) * 30;
return this.normalizeCurrency({ amount: months });
}
/**
* Changes the rate from hourly to yearly.
*
* @param options - The currency model containing the amount in TFT.
* @returns A string representation of the converted amount in TFT.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
yearlyTFT(options: CurrencyModel): string {
const years = +this.monthlyTFT(options) * 12;
return this.normalizeCurrency({ amount: years });
}
/**
* Changes the rate from hourly to daily.
*
* @param options - The currency model containing the amount in USD.
* @returns A string representation of the converted amount in USD.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
dailyUSD(options: CurrencyModel): string {
const hours = options.amount * 24;
return this.normalizeCurrency({ amount: hours });
}
/**
* Changes the rate from hourly to monthly.
*
* @param options - The currency model containing the amount in `USD`.
* @returns A string representation of the converted amount in `USD`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
monthlyUSD(options: CurrencyModel): string {
const months = +this.dailyUSD(options) * 30;
return this.normalizeCurrency({ amount: months });
}
/**
* Changes the rate from hourly to yearly.
*
* @param options - The currency model containing the amount in `USD`.
* @returns A string representation of the converted amount in `USD`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
yearlyUSD(options: CurrencyModel): string {
const years = +this.monthlyUSD(options) * 12;
return this.normalizeCurrency({ amount: years });
}
}
export { TFTUSDConversionService as currency };