-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
currency.go
108 lines (100 loc) · 1.98 KB
/
currency.go
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
package mono
import "errors"
var currencyCodes = map[int32]Currency{
840: {
Name: "US Dollar",
Code: "USD",
Symbol: "$",
},
980: {
Name: "Hryvnia",
Code: "UAH",
Symbol: "₴",
},
978: {
Name: "Euro",
Code: "EUR",
Symbol: "€",
},
643: {
Name: "Russian Ruble",
Code: "RUB",
Symbol: "₽",
},
826: {
Name: "Pound Sterling",
Code: "GBP",
Symbol: "£",
},
756: {
Name: "Swiss Franc",
Code: "CHF",
Symbol: "₣",
},
933: {
Name: "Belarussian Ruble",
Code: "BYN",
Symbol: "Br",
},
124: {
Name: "Canadian Dollar",
Code: "CAD",
Symbol: "$",
},
203: {
Name: "Czech Koruna",
Code: "CZK",
Symbol: "Kč",
},
208: {
Name: "Danish Krone",
Code: "DKK",
Symbol: "Kr",
},
348: {
Name: "Forint",
Code: "HUF",
Symbol: "Ft",
},
985: {
Name: "Zloty",
Code: "PLN",
Symbol: "zł",
},
949: {
Name: "Turkish Lira",
Code: "TRY",
Symbol: "₺",
},
}
// Currency is internal representation of fiat currencies.
type Currency struct {
Name string
Code string
Symbol string
}
// CurrencyFromISO4217 converts ISO4217 to matching currency.
func CurrencyFromISO4217(code int32) (Currency, error) {
if _, ok := currencyCodes[code]; !ok {
return Currency{}, errors.New("code is not valid")
}
return currencyCodes[code], nil
}
// Exchange contains market buy/sell rates.
// See https://api.monobank.ua/docs/#/definitions/CurrencyInfo for details.
type Exchange struct {
CodeA int32 `json:"currencyCodeA"`
CodeB int32 `json:"currencyCodeB"`
Date int32 `json:"date"`
RateSell float64 `json:"rateSell"`
RateBuy float64 `json:"rateBuy"`
RateCross float64 `json:"rateCross"`
}
// Base returns normal representation of CurrencyCodeA.
func (ex *Exchange) Base() (Currency, error) {
return CurrencyFromISO4217(ex.CodeA)
}
// Quote returns normal representation of CurrencyCodeB.
func (ex *Exchange) Quote() (Currency, error) {
return CurrencyFromISO4217(ex.CodeB)
}