-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptoAccount.gs
245 lines (192 loc) · 6.58 KB
/
CryptoAccount.gs
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* Cryptocurrency account.
* Calculation are done in integer amounts of subunits to avoid computational rounding errors.
*/
var CryptoAccount = class CryptoAccount {
/**
* Sets the cryptocurrency currency and initializes an empty array to contain the crytocurrency lots.
* @param {string} ticker - the cryptocurrency currency ticker.
*/
constructor(ticker) {
/**
* The cryptocurrency currency ticker.
* @type {string}
*/
this.ticker = ticker;
/**
* The crytocurrency lots.
* @type {Array<Lot>}
*/
this.lots = [];
}
get ticker() {
return this._ticker;
}
set ticker(ticker) {
this._ticker = ticker;
this._currencySubunits = Currency.subunits(ticker);
}
/**
* The balance in the account in subunits.
* @type {number}
*/
get subunits() {
let subunits = 0;
for (let lot of this.lots) {
subunits += lot.subunits; //adding two integers - no need to round
}
return subunits;
}
/**
* The balance in the account.
* @type {number}
*/
get balance() {
return this.subunits / this._currencySubunits;
}
/**
* Deposits a single or multiple lots of cryptocurrency into the account.
* @param {(Lot|Lot[])} lots - The single lot or array of lots to deposit into the account.
*/
deposit(lots) {
if (Array.isArray(lots)) {
this.lots = this.lots.concat(lots);
}
else {
this.lots.push(lots);
}
}
/**
* Withdraws an amount of cryptocurrency from the account.
* If necessary the last lot to be withdrawn is split.
* The fee is assigned to the withdrawn lots in proportion to their size.
* Throws an error if the amount requested is greater than the balance in the account.
* @param {number} amount - The amount of cryptocurrency to withdraw.
* @param {number} fee - The fee which is also withdrawn from the account.
* @param {string} lotMatching - The lot matching method used to determine the order in which lots are withdrawn.
* FIFO First in first out.
* LIFO Last in first out.
* HIFO Highest cost first out.
* LOFO Lowest cost first out.
* @param {number} rowIndex - The index of the row in the ledger sheet used to set the current cell in case of an error.
* @return {Lot[]} The collection of lots withdrawn.
*/
withdraw(amount, fee, lotMatching, rowIndex) {
let amountSubunits = Math.round(amount * this._currencySubunits);
let feeSubunits = Math.round(fee * this._currencySubunits);
let neededSubunits = amountSubunits + feeSubunits;
if (neededSubunits > this.subunits) {
throw new CryptoAccountError(`Attempted to withdraw ${this.ticker} ${amount} + fee ${fee} from balance of ${this.ticker} ${this.balance}`, rowIndex);
}
this.lots.sort(this.lotComparator(lotMatching));
let keepLots = [];
let withdrawLots = [];
for (let lot of this.lots) {
if (neededSubunits > 0) { //need more
if (lot.subunits <= neededSubunits) { //want full lot
withdrawLots.push(lot);
neededSubunits -= lot.subunits;
}
else { //need to split lot
let splitLots = lot.split(neededSubunits);
withdrawLots.push(splitLots[0]);
keepLots.push(splitLots[1]);
neededSubunits = 0;
}
}
else { //keep the remaining lots
keepLots.push(lot);
}
}
//apportion the fee to withdrawal lots
this.apportionFeeSubunits(feeSubunits, withdrawLots);
this.lots = keepLots;
return withdrawLots;
}
/**
* Apportions fee subunits equitably between lots.
* The fee subunits are assigned to the lots in proportion to each lot's subunits.
* Throws an error if the fee subunits are greater than the total lots' subunits.
* @param {number} fee subunit - The fee subunits to assign to the lots.
* @param {Lot[]} lots - The collection of lots.
*/
apportionFeeSubunits(feeSubunits, lots) {
let lotSubunits = [];
for (let lot of lots) {
lotSubunits.push(lot.subunits);
}
let apportionedFeeSubunits = CryptoTracker.apportionInteger(feeSubunits, lotSubunits);
let index = 0;
for (let lot of lots) {
lot.creditFeeSubunits += apportionedFeeSubunits[index++];
}
}
/**
* Apportions fee equitably between the lots of the account.
* The fee is assigned to the lots in proportion to the lot size.
* Throws an error if the fee is greater than the balance in the account.
* @param {number} fee - The fee to assign to the lots of this account.
* @param {number} rowIndex - The index of the row in the ledger sheet used to set the current cell in case of an error.
*/
apportionFee(fee, rowIndex) {
let feeSubunits = Math.round(fee * this._currencySubunits);
if (feeSubunits > this.subunits) {
throw new CryptoAccountError(`Attempted to withdraw fee ${fee} from balance of ${this.ticker} ${this.balance}`, rowIndex);
}
this.apportionFeeSubunits(feeSubunits, this.lots);
}
/**
* Removes any lots with zero subunits.
* Used when misc fee sets lot subunits to zero.
* @return {Lot[]} The collection of lots with zero subunits.
*/
removeZeroSubunitLots() {
let keepLots = [];
let withdrawLots = [];
for (let lot of this.lots) {
if (lot.subunits > 0) {
keepLots.push(lot);
}
else {
withdrawLots.push(lot);
}
}
this.lots = keepLots;
return withdrawLots;
}
/**
* Given a lot matching method string returns a comparator function used to sort lots.
* @param {string} lotMatching - The lot matching method used to determine the order in which lots are withdrawn.
* FIFO First in first out.
* LIFO Last in first out.
* HIFO Highest cost first out.
* LOFO Lowest cost first out.
* Throw an error with any other input.
* @return {function} The comparator function used to sort lots.
*/
lotComparator(lotMatching) {
if (lotMatching === 'FIFO') {
return function (lot1, lot2) {
return lot1.date - lot2.date;
};
}
else if (lotMatching === 'LIFO') {
return function (lot1, lot2) {
return lot2.date - lot1.date;
};
}
else if (lotMatching === 'LOFO') {
return function (lot1, lot2) {
return (lot1.costBasisSubunits / lot1.subunits) - (lot2.costBasisSubunits / lot2.subunits);
};
}
else if (lotMatching === 'HIFO') {
return function (lot1, lot2) {
return (lot2.costBasisSubunits / lot2.subunits) - (lot1.costBasisSubunits / lot1.subunits);
};
}
else {
throw Error(`Lot Matching Method (${lotMatching}) not recognized.`);
}
}
};