-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_introduction.html
476 lines (395 loc) · 26.7 KB
/
10_introduction.html
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>Introduction</title><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="css/highlight.css" /><script type="text/javascript" src="js/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script><link rel="stylesheet" type="text/css" href="css/randomseed.css" /></head><body><div id="header"><h2>Generated by <a href="https://github.com/weavejester/codox">Codox</a></h2><h1><a href="index.html"><span class="project-title"><span class="project-name">Bankster</span> <span class="project-version">1.2.16</span></span></a></h1></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Project</span></h3><ul class="index-link"><li class="depth-1 "><a href="index.html"><div class="inner">Index</div></a></li></ul><h3 class="no-link"><span class="inner">Topics</span></h3><ul><li class="depth-1 current"><a href="10_introduction.html"><div class="inner"><span>Introduction</span></div></a></li><li class="depth-1 "><a href="20_data_structures.html"><div class="inner"><span>Data Structures</span></div></a></li></ul><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>io</span></div></div></li><li class="depth-2"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>randomseed</span></div></div></li><li class="depth-3"><a href="io.randomseed.bankster.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>bankster</span></div></a></li><li class="depth-4 branch"><a href="io.randomseed.bankster.config.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>config</span></div></a></li><li class="depth-4 branch"><a href="io.randomseed.bankster.currency.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>currency</span></div></a></li><li class="depth-4"><a href="io.randomseed.bankster.money.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>money</span></div></a></li><li class="depth-5 branch"><a href="io.randomseed.bankster.money.inter-ops.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>inter-ops</span></div></a></li><li class="depth-5"><a href="io.randomseed.bankster.money.ops.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>ops</span></div></a></li><li class="depth-4 branch"><a href="io.randomseed.bankster.registry.html"><div class="inner"><span class="tree" style="top: -83px;"><span class="top" style="height: 92px;"></span><span class="bottom"></span></span><span>registry</span></div></a></li><li class="depth-4 branch"><a href="io.randomseed.bankster.scale.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>scale</span></div></a></li><li class="depth-4"><a href="io.randomseed.bankster.util.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>util</span></div></a></li><li class="depth-5 branch"><a href="io.randomseed.bankster.util.fs.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>fs</span></div></a></li><li class="depth-5 branch"><a href="io.randomseed.bankster.util.importer.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>importer</span></div></a></li><li class="depth-5"><a href="io.randomseed.bankster.util.map.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>map</span></div></a></li></ul></div><div class="document" id="content"><div class="doc"><div class="markdown"><h1><a href="#introduction" id="introduction"></a>Introduction</h1>
<p><strong>Money Creation Made Easy!</strong></p>
<p><a href="https://clojars.org/io.randomseed/bankster"><img src="https://img.shields.io/clojars/v/io.randomseed/bankster.svg" alt="Bankster on Clojars" /></a> <a href="https://cljdoc.org/d/io.randomseed/bankster/CURRENT"><img src="https://cljdoc.org/badge/io.randomseed/bankster" alt="Bankster on cljdoc" /></a> <a href="https://circleci.com/gh/randomseed-io/bankster"><img src="https://circleci.com/gh/randomseed-io/bankster.svg?style=svg" alt="CircleCI" /></a></p>
<p>Clojure library to operate on monetary units with cryptocurrencies and custom currencies support.</p>
<h2><a href="#features" id="features"></a>Features</h2>
<ul>
<li>Pure Clojure implementation based on Java’s <strong>BigDecimal</strong>.</li>
<li>Uses <strong>records</strong> to organize data: <code>Registry</code>, <code>Currency</code>, <code>Money</code>.</li>
<li>Built-in standard <strong>currencies database</strong>, extendable with EDN file.</li>
<li>Ability to create <em>ad hoc</em> currencies (with optional registering).</li>
<li>Different sources of <strong>currency registries</strong> (dynamic, global or local).</li>
<li><strong>Polymorphic</strong> interfaces (<code>Scalable</code>, <code>Monetary</code>, <code>Accountable</code> protocols)</li>
<li>Ability to <strong>cast</strong> and <strong>convert</strong> monetary amounts.</li>
<li>Useful <strong>macros</strong> to express currencies and monetary amounts with various forms.</li>
<li><strong>Namespaced identifiers</strong> for non-ISO currencies (e.g. <code>crypto/ETH</code>).</li>
<li>Common <strong>math operators</strong>.</li>
<li><strong>Auto-rescaling</strong> in math operations to handle non-terminating decimal expansion.</li>
<li>Variants of <strong>variadic math functions</strong> with rescaling after each consecutive operation.</li>
<li>Optional <strong>rescaling</strong> of monetary amounts with keeping track of nominal scales.</li>
<li>Functions for <strong>rounding</strong> to a scale or to the given <strong>interval</strong>.</li>
<li><strong>Tagged literals</strong> for currencies and monetary amounts.</li>
<li><strong>Data readers</strong> for currencies and monetary amounts expressed with EDN.</li>
<li>Customizable currency and money <strong>formatting</strong> with <strong>locale support</strong>.</li>
</ul>
<h2><a href="#installation" id="installation"></a>Installation</h2>
<p>To use Bankster in your project, add the following to dependencies section of <code>project.clj</code> or <code>build.boot</code>:</p>
<pre><code class="language-clojure">[io.randomseed/bankster "1.2.16"]
</code></pre>
<p>For <code>deps.edn</code> add the following as an element of a map under <code>:deps</code> or <code>:extra-deps</code> key:</p>
<pre><code class="language-clojure">io.randomseed/bankster {:mvn/version "1.2.16"}
</code></pre>
<p>Additionally, if you want to utilize specs and generators provided by the Bankster you can use (in your development profile):</p>
<pre><code class="language-clojure">org.clojure/spec.alpha {:mvn/version "0.2.176"}
org.clojure/test.check {:mvn/version "0.10.0-alpha4"}
</code></pre>
<p>You can also download JAR from <a href="https://clojars.org/io.randomseed/bankster">Clojars</a>.</p>
<h2><a href="#design" id="design"></a>Design</h2>
<h3><a href="#registry-of-currencies" id="registry-of-currencies"></a>Registry of currencies</h3>
<p>There is a global, shared <strong>registry</strong> (<code>Registry</code>) of currencies, which is thread-safe and encapsulated in an Atom. It consists of databases (maps) for quickly accessing important currency properties. Most of the functions will use this global registry, unless a registry is explicitly passed as an argument or set as a dynamic variable.</p>
<p>Registry is implemented as a record of maps keeping the following associations:</p>
<ul>
<li>currency ID to currency object;</li>
<li>currency ID to a set of country IDs;</li>
<li>currency numeric ID to currency object;</li>
<li>country ID to currency object;</li>
<li>locale ID to localized properties map;</li>
<li>currency code to a sorted set of currency objects.</li>
</ul>
<p>In most cases you won’t have to worry about the internals of a registry. However, when working with multiple data sources or data processing engines (like currency exchange platforms), you may find it useful to have <strong>different registries</strong> (with the same currencies but of different scales). Most of the functions operating on monetary units and currencies will accept a registry object as their additional argument. The exceptions are math operations (especially variadic) for which the only way to use different registry is to set a dynamic variable <code>io.randomseed.bankster.registry/*default*</code> (which can be done using the macro <code>io.randomseed.bankster.currency/with-registry</code>).</p>
<p>When the library loads, its predefined configuration is read from a default EDN file and its contents populates the default, global registry. This registry can be modified too.</p>
<h3><a href="#currency" id="currency"></a>Currency</h3>
<p>Each <strong>currency</strong> (<code>Currency</code>) is a record having the following fields, reflecting its properties:</p>
<ul>
<li><code>id</code> – a keyword <strong>identifying</strong> a currency unit (e.g. <code>:EUR</code> or <code>:crypto/ETH</code>);</li>
<li><code>numeric</code> – a long value being a <strong>numeric identifier</strong> of ISO-standardized currencies;</li>
<li><code>scale</code> – an integer of supported <strong>scale</strong> (decimal places);</li>
<li><code>kind</code> – a keyword with currency <strong>kind</strong>;</li>
<li><code>domain</code> – a keyword with currency <strong>domain</strong>;</li>
<li><code>weight</code> – an integer which helps in case there is a conflict of currency codes.</li>
</ul>
<p><strong>Currency ID</strong> is a unique identifier of a currency within a registry. Internally it is a keyword and optionally it can have a namespace. By default Bankster identifies standard currencies with IDs reflecting their official currency codes and cryptocurrencies with identifiers having <code>crypto</code> namespace.</p>
<p><strong>Currency code</strong> is a name of currency ID without its namespace. It is potentially conflicting attribute, therefore the mapping of currency codes to sets of currency objects exists in a registry. It allows to get currencies using their codes (and not add namespace, especially when interacting with some external API) and still maintain uniqueness of identifiers. If custom currency is created with the same code as already existing currency, it is possible to give it a <strong>weight</strong> which will decide whether its code will have priority during resolution (and getting from a registry).</p>
<p><strong>Currency domain</strong> is by default the same as a namespace of currency ID (if it is namespaced). For non-namespaced identifiers it can be set to anything. The domain of <code>:ISO-4217</code> informs the library that this is ISO-standardized currency. The purpose of domain is to classify currencies into different realms and create a boundary preventing naming conflicts when codes are the same.</p>
<p><strong>Currency kind</strong> is a keyword that should describe a class of currency. It can be set to anything, even nil, but the suggested values are:</p>
<ul>
<li><code>:FIAT</code> – legal tender issued by government or other authority,</li>
<li><code>:FIDUCIARY</code> - accepted medium of exchange issued by a fiduciary or fiduciaries,</li>
<li><code>:DECENTRALIZED</code> - accepted medium of exchange issued by a distributed ledger,</li>
<li><code>:COMBANK</code> - commercial bank money,</li>
<li><code>:COMMODITY</code> - accepted medium of exchange based on commodities,</li>
<li><code>:EXPERIMENTAL</code> - pseudo-currency used for testing purposes.</li>
</ul>
<p><strong>Currency scale</strong> is the nominal scale of a currency. If it is not set, the automatic scale will be used on monetary amounts using such a currency.</p>
<p>Registry-related functions are accepting currency representations based on their <strong>identifiers</strong>. Other functions and macros will usually accept <strong>currency codes</strong>. In case of conflict the currency with higher <strong>currency weight</strong> will be picked up.</p>
<p>Currencies can also have <strong>additional</strong>, external properties, like relations to countries, localized (l10n) settings etc. They are stored in registries too.</p>
<h3><a href="#money" id="money"></a>Money</h3>
<p>Having a currency defined we can create <strong>money</strong> objects (<code>Money</code>) which are based on records having 2 fields:</p>
<ul>
<li><code>currency</code> – a <code>Currency</code> object;</li>
<li><code>amount</code> – a <code>BigDecimal</code> value.</li>
</ul>
<p>The initial scale (number of decimal places) of an amount will be set to nominal scale of the currency. Math operations will then respect this scale during calculations and preserve it. In rare cases it is possible to rescale the amount, check whether the monetary object is rescaled and scale it back to a scale of the currency.</p>
<h2><a href="#sneak-peeks" id="sneak-peeks"></a>Sneak peeks</h2>
<ul>
<li>It <strong>shows information</strong> about a currency:</li>
</ul>
<pre><code class="language-clojure">;; global registry lookup with a keyword
(currency/of :PLN)
#currency{:id :PLN, :domain :ISO-4217, :kind :FIAT, :numeric 985, :scale 2}
;; global registry lookup using namespaced symbol
(currency/of crypto/ETH)
#currency{:id :crypto/ETH, :domain :CRYPTO, :kind :DECENTRALIZED, :scale 18, :weight 5}
;; global registry lookup with a string (incl. namespace a.k.a domain)
(currency/of "crypto/BTC")
#currency{:id :crypto/BTC, :domain :CRYPTO, :kind :DECENTRALIZED, :scale 8, weight 5}
;; global registry lookup with a currency code
;; (weight solves potential conflicts when two currencies have the same currency code)
(currency/of ETH)
#currency{:id :crypto/ETH, :domain :CRYPTO, :kind :DECENTRALIZED, :scale 18, :weight 5}
;; global registry lookup using ISO currency number
(currency/of 840)
#currency{:id :USD, :domain :ISO-4217, :kind :FIAT, :numeric 840, :scale 2}
;; global registry lookup using tagged literal with a currency code
#currency XLM
#currency{:id :crypto/XLM, :domain :CRYPTO, :kind :FIDUCIARY, :scale 8}
;; global registry lookup using tagged literal with a namespaced identifier
#currency crypto/XLM
#currency{:id :crypto/XLM, :domain :CRYPTO, :kind :FIDUCIARY, :scale 8}
;; global registry lookup using tagged literal with an ISO currency number
#currency 978
#currency{:id :EUR, :domain :ISO-4217, :kind :FIAT, :numeric 978, :scale 2}
</code></pre>
<ul>
<li>It allows to <strong>create a currency</strong> and <strong>register it</strong>:</li>
</ul>
<pre><code class="language-clojure">;; ad hoc currency creation using constructor function
(currency/new :petro/USD 999 2 :COMBANK)
#currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 999, :scale 2}
;; ad-hoc currency creation using tagged literal
#currency{:id :crypto/ETH :scale 18}
#currency{:id :crypto/ETH, :domain :CRYPTO, :scale 18}
;; putting new currency into a global, shared registry
(currency/register! (currency/new :petro/USD 9999 2 :COMBANK) :USA)
#Registry[{:currencies 221, :countries 250, :version "2021022121170359"} 0x11efe93f]
;; getting currency from a global registry
(currency/of :petro/USD)
#currency{:id :petro/USD, :domain :PETRO, :kind :COMBANK, :numeric 9999, :scale 2}
;; registering new currency expressed as a tagged literal
(currency/register! #currency{:id :crypto/AAA :scale 8})
#Registry[{:currencies 221, :countries 249, :version "2021022121170359"} 0x7eaf7a70]
</code></pre>
<ul>
<li>It allows to create <strong>monetary amounts</strong>:</li>
</ul>
<pre><code class="language-clojure">;; using money/of macro with keyword ID and an amount
(money/of :EUR 25)
#money[25.00 EUR]
;; using money/of macro with keyword ID and an amount as a first argument
(money/of 25 :EUR)
#money[25.00 EUR]
;; using money/of macro with joint keyword ID and an amount as a first argument
(money/of :25_EUR)
#money[25.00 EUR]
(money/of :25EUR)
#money[25.00 EUR]
;; using money/of macro with unquoted symbolic ID and an amount
(money/of EUR 25)
#money[25.00 EUR]
;; using money/of macro with joint unquoted symbolic ID and an amount
(money/of EUR_25)
#money[25.00 EUR]
(money/of EUR25)
#money[25.00 EUR]
;; using money/of macro with namespaced keyword ID and an amount
(money/of crypto/BTC 10.1)
#money/crypto[10.10000000 BTC]
;; using money/of macro with currency code and an amount
(money/of BTC 10.1)
#money/crypto[10.10000000 BTC]
;; using tagged literals
#money EUR
#money[0.00 EUR]
#money/crypto ETH
#money/crypto[0.000000000000000000 ETH]
#money[PLN 2.50]
#money[2.50 PLN]
;; using tagged literal with a namespace
#money/crypto[1.31337 ETH]
#money/crypto[1.313370000000000000 ETH]
;; using tagged literal with a currency code
#money[1.31337 ETH]
#money/crypto[1.313370000000000000 ETH]
;; using tagged literal with a namespace but the amount goes first
#money/crypto[BTC 1.31337]
#money/crypto[1.31337000 BTC]
;; using default currency in a lexical context
(currency/with EUR (money/of 1000))
#money[1000.00 EUR]
;; using default currency in a lexical context (alias for the above)
(money/with-currency EUR (money/of 1000))
#money[1000.00 EUR]
;; using composed amounts and currencies
#money EUR100
#money[100 EUR]
#money :100_EUR
#money[100 EUR]
#money :100EUR
#money[100 EUR]
#money "100 EUR"
#money[100 EUR]
(money/of "100EUR")
#money[100 EUR]
</code></pre>
<p>It allows to perform <strong>logical operations</strong> on monetary amounts:</p>
<pre><code class="language-clojure">(money/eq? #money[5 GBP] #money[GBP 5])
true
(money/ne? #money[5 GBP] #money[GBP 5])
false
(money/eq? #money[5 GBP] #money/crypto[5 ETH])
false
(money/gt? #money[1 JPY] #money[0 JPY])
true
(money/ge? #money[1 JPY] #money[1 JPY])
true
(money/lt? #money[1 JPY] #money[0 JPY])
false
(money/le? #money[1 JPY] #money[0 JPY])
false
(money/zero? #money[0 USD])
true
(money/neg? #money[-2 XXX])
true
(money/pos? #money[-2 XXX])
false
</code></pre>
<p>It allows to perform <strong>math operations</strong> on monetary amounts:</p>
<pre><code class="language-clojure">;; adding money expressed with tagged literals and with a macro call
(money/add #money[EUR 7] #money[0.54 EUR] (money/of 4.40 EUR))
#money[11.94 EUR]
;; dividing money by a number
(money/div #money/crypto[5 USDT] 2)
#money/crypto[2.50000000 USDT]
;; dividing money by numbers that separately would require rounding
(money/div #money[1 GBP] 8 0.5)
#money[0.25 GBP]
;; dividing money by numbers with rounding after each consecutive calculation
(money/with-rescaling HALF_UP
(money/div #money[1 GBP] 8 0.5))
#money[0.26 GBP]
;; dividing money by money (of the same currency)
(money/div #money/crypto[5 BTC] #money/crypto[2 BTC])
2.5M
;; dividing causing scale to exceed in one of the steps
;; but no rounding is necessary due to later operation
(money/div #money[1 PLN] 8 0.5)
#money[0.25 PLN]
;; dividing, scaled and rounded with each operation
(scale/with-rounding HALF_UP
(money/div-scaled #money[1 PLN] 8 0.5))
#money[0.26 PLN]
;; same as the above but shorter
(scale/with-rescaling HALF_UP
(money/div #money[1 PLN] 8 0.5))
#money[0.26 PLN]
;; handling non-terminating decimal expansion
(scale/with-rounding HALF_UP
(money/div #money[1 PLN] 3))
#money[0.33 PLN]
;; rounding with unit reduction
(scale/with-rounding HALF_UP
(money/div #money[1 PLN] #money[3 PLN]))
0.33M
;; rounding and unit reduction (regular numbers, dynamic scale)
(scale/with-rounding HALF_UP
(money/div 1 3))
0.33333M
;; multiplying money by numbers
(money/mul #money/crypto[5 ETH] 1 2 3 4 5)
#money/crypto[600.000000000000000000 ETH]
;; adding to major part
(money/add-major #money[1.23 PLN] 100)
#money[101.23 PLN]
;; adding to minor part
(money/add-minor #money[1.23 PLN] 77)
#Money[2.00 PLN]
;; converting
(money/convert #money/crypto[1.5 ETH] :crypto/USDT 1646.75)
#money/crypto[2470.12500000 USDT]
;; comparing
(sort money/compare [(money/of 10 PLN)
(money/of 0 PLN)
(money/of 30 PLN)
(money/of 1.23 PLN)])
(#money[0.00 PLN]
#money[1.23 PLN]
#money[10.00 PLN]
#money[30.00 PLN])
;; rounding to the given interval
(money/round-to #money[31.33 USD] 0.5)
#money[31.50 USD]
;;
;; using inter-ops
;;
(require '[io.randomseed.bankster.money.inter-ops :refer :all])
(+ 1 2 3)
6
(+ #money[USD 8] #money[USD 7.12])
#money[15.12 USD]
(* 1 2 3 4 5 #money/crypto[0.7 ETH])
#money/crypto[84.000000000000000000 ETH]
</code></pre>
<p>It allows to perform <strong>generic, polymorphic operations</strong> on monetary amounts and currencies:</p>
<pre><code class="language-clojure">(scale/of #currency PLN)
2
(scale/of #currency crypto/ETH)
18
(scale/of 123.45)
2
(scale/of #money[100 EUR])
2
(scale/of :GBP)
2
;; scale of the currency (low-level)
(scale/of :XXX)
-1
;; scale of the amount
(scale/of #money[12.34567 XXX])
5 ; current scale
;; nominal scale of the currency
(currency/scale #money[12.34567 XXX])
nil ; auto-scaled
(currency/auto-scaled? :XXX)
true
(currency/auto-scaled? #money[12.34567 XXX])
true
(scale/apply #money[10 USD] 8) ;; use with caution
#money[10.00000000 USD]
(scale/apply #currency USD 8) ;; use with caution
#currency{:id :USD, :domain :ISO-4217, :kind :FIAT, :numeric 840, :scale 8}
(money/rescale #money[10 USD] 8)
#money[10.00000000 USD]
;; unary variant of money/rescale
;; rescales back to nominal scale
(money/rescale
(money/rescale #money[10 USD] 8))
#money[10.00 USD]
(scale/amount #money[108.11 CHF])
108.11M
(scale/integer #money[108.11 CHF])
108M
(scale/fractional #money[108.11 CHF])
11M
(currency/iso? #money[1 GBP])
true
(currency/code #money[1 GBP])
"GBP"
</code></pre>
<p>And more…</p>
<h3><a href="#warning-about-literal-amounts" id="warning-about-literal-amounts"></a>Warning about literal amounts</h3>
<p>Clojure changes number literals into objects of various numeric data types. Some of them will have fixed precision when there is a decimal separator present, yet they will not be big decimals before entering monetary functions of Bankster.</p>
<p>Putting a decimal number having more than 16–17 digits will often effect in <strong>accidental approximation</strong> and casting it to a double value. This value may become the amount of money which probably is not what you want:</p>
<pre><code class="language-clojure">1234.5678910111213000001
; => 1234.5678910111212
</code></pre>
<p>To work around that you should:</p>
<ul>
<li>Use <strong>big decimal literals</strong> (e.g. <code>(money/of XXX 1234.56789101112M)</code> – note the <code>M</code>).</li>
<li>Use <strong>strings</strong> (e.g. <code>(money/of "1234.56789101112 XXX")</code>).</li>
<li>Use <code>money/of</code> macro or <code>#money</code> tagged literal with amount and currency in joint form (or with the above tactics applied), e.g.:</li>
<li><code>(money/of XXX123.45678)</code>,</li>
<li><code>#money XXX123.45678</code>,</li>
<li><code>#money "XXX123.45678"</code>,</li>
<li><code>#money "123.456789101112 XXX"</code>,</li>
<li><code>#money[123.45678M XXX]</code>.</li>
</ul>
<p>As it may not be a problem in case of regular currencies, it may pop-up when using scale-wide cryptocurrencies, like Ether or Ethereum tokens, having 18 decimal places.</p>
<h2><a href="#documentation" id="documentation"></a>Documentation</h2>
<p>Full documentation including usage examples is available at:</p>
<ul>
<li><a href="https://randomseed.io/software/bankster/">https://randomseed.io/software/bankster/</a></li>
</ul>
<h2><a href="#deum-ethereum" id="deum-ethereum"></a>Deum Ethereum</h2>
<p>I write Free Software for fun. If you are finding it useful and you are the Ether fan too, here it is: <code>0x2Bed4D2d9240F9fB321bC0194222A4888F62dd0d</code>.</p>
<p>Stellar Lumens are cool too: <code>GBMUQ6U6334Y5HWF3XGMCRQZKVMFDCSX4YADEVZO7ZXIBDJDXXX2BSME</code>.</p>
<h2><a href="#why" id="why"></a>Why?</h2>
<p>In one of my personal projects I needed support for both, ISO-standardized and custom currencies. My first try was Money (by Clojurewerkz), which is quite mature library based on Java’s Joda Money. However, I needed cryptocurrencies support, and I mean all of them, including those having non-standard codes (like <code>DASH</code>).</p>
<p>First I tried to modify Money and work-around this limitation by imitating such currencies with an additional map translating custom codes into standardized ones. Then I looked at Joda Money to see that the important classes are marked as final and the support for currencies is limited to the “official” ones.</p>
<h2><a href="#license" id="license"></a>License</h2>
<p>Copyright © 2021,2022 Paweł Wilk</p>
<p>Bankster is copyrighted software owned by Paweł Wilk (<a href="mailto:pw@gnu.org">pw@gnu.org</a>). You may redistribute and/or modify this software as long as you comply with the terms of the <a href="https://github.com/randomseed-io/bankster/blob/master/LICENSE">GNU Lesser General Public License</a> (version 3).</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p>
<h2><a href="#development" id="development"></a>Development</h2>
<h3><a href="#building-docs" id="building-docs"></a>Building docs</h3>
<pre><code class="language-bash">make docs
</code></pre>
<h3><a href="#building-jar" id="building-jar"></a>Building JAR</h3>
<pre><code class="language-bash">make jar
</code></pre>
<h3><a href="#rebuilding-pom" id="rebuilding-pom"></a>Rebuilding POM</h3>
<pre><code class="language-bash">make pom
</code></pre>
<h3><a href="#signing-pom" id="signing-pom"></a>Signing POM</h3>
<pre><code class="language-bash">make sig
</code></pre>
<h3><a href="#deploying-to-clojars" id="deploying-to-clojars"></a>Deploying to Clojars</h3>
<pre><code class="language-bash">make deploy
</code></pre>
<h3><a href="#interactive-development" id="interactive-development"></a>Interactive development</h3>
<pre><code class="language-bash">bin/repl
</code></pre>
<p>Starts REPL and nREPL server (port number is stored in <code>.nrepl-port</code>).</p>
</div></div></div></body></html>