-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (163 loc) · 5.84 KB
/
Copy pathindex.js
File metadata and controls
183 lines (163 loc) · 5.84 KB
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
const cheerio = require("cheerio")
const got = require("got")
class CryptoConverter {
currencies = {
"ZRX": "0x",
"AION": "Aion",
"ARDR": "Ardor",
"ARK": "Ark",
"REP": "Augur",
"BNT": "Bancor",
"BAT": "Basic Attention Token",
"BNB": "Binance Coin",
"BTS": "BitShares",
"BTC": "Bitcoin",
"BCH": "Bitcoin Cash",
"BTG": "Bitgem",
"BCN": "Bytecoin",
"BTM": "Bytom",
"ADA": "Cardano",
"LINK": "ChainLink",
"DASH": "Dash",
"MANA": "Decentraland",
"DCR": "Decred",
"DGB": "DigiByte",
"XDN": "DigitalNote",
"DGD": "DigixDAO",
"DOGE": "Dogecoin",
"EOS": "EOS",
"ENG": "Enigma",
"ETH": "Ether",
"ETC": "Ethereum Classic",
"BQX": "Ethos",
"FCT": "Factom",
"FUN": "FunFair",
"GAME": "GameCredits",
"GAS": "Gas",
"GNO": "Gnosis",
"GNT": "Golem",
"GRS": "Groestlcoin",
"HSR": "Hshare",
"ICX": "ICON",
"KNC": "KingN Coin",
"KMD": "Komodo",
"LSK": "Lisk",
"LTC": "Litecoin",
"MAID": "MaidSafeCoin",
"MONA": "MonaCoin",
"XMR": "Monero",
"XEM": "NEM",
"NEO": "NEO",
"NANO": "Nano",
"NEBL": "Neblio",
"NXS": "Nexus",
"NXT": "Nxt",
"OMG": "OmiseGO",
"ONT": "Ontology",
"PIVX": "PIVX",
"PPT": "Populous",
"POWR": "Power Ledger",
"QASH": "QASH",
"QTUM": "Qtum",
"QSP": "Quantstamp",
"RDN": "Raiden Network Token",
"RDD": "ReddCoin",
"REQ": "Request Network",
"SALT": "SALT",
"SAN": "Santiment Network Token",
"SC": "Siacoin",
"SNT": "Status",
"STEEM": "Steem",
"XLM": "Stellar",
"STRAT": "Stratis",
"SUB": "Substratum",
"SYS": "Syscoin",
"TRX": "TRON",
"PAY": "TenX",
"USDT": "Tether",
"TNB": "Time New Bank",
"XVG": "Verge",
"VERI": "Veritaseum",
"WTC": "Waltonchain",
"WAN": "Wanchain",
"WAVES": "Waves",
"XRP": "XRP",
"ZCL": "ZClassic",
"ZIL": "Zilliqa",
"ELF": "aelf",
"ICN": "iCoin"
}
currencyCode = ["ZRX", "AION", "ARDR", "ARK", "REP", "BNT", "BAT", "BNB", "BTS", "BTC", "BCH", "BTG", "BCN", "BTM", "ADA", "LINK", "DASH", "MANA", "DCR", "DGB", "XDN", "DGD", "DOGE", "EOS", "ENG", "ETH", "ETC", "BQX", "FCT", "FUN", "GAME", "GAS", "GNO", "GNT", "GRS", "HSR", "ICX", "KNC", "KMD", "LSK", "LTC", "MAID", "MONA", "XMR", "XEM", "NEO", "NANO", "NEBL", "NXS", "NXT", "OMG", "ONT", "PIVX", "PPT", "POWR", "QASH", "QTUM", "QSP", "RDN", "RDD", "REQ", "SALT", "SAN", "SC", "SNT", "STEEM", "XLM", "STRAT", "SUB", "SYS", "TRX", "PAY", "USDT", "TNB", "XVG", "VERI", "WTC", "WAN", "WAVES", "XRP", "ZCL", "ZIL", "ELF", "ICN"]
constructor(params) {
this.currencyFrom = ""
this.currencyTo = ""
this.currencyAmount = 1
this.convertedValue = 0
if(params != undefined){
if(params["from"] !== undefined)
this.from(params["from"])
if(params["to"] !== undefined)
this.to(params["to"])
if(params["amount"] !== undefined)
this.amount(params["amount"])
}
}
from (currencyFrom) {
if(typeof currencyFrom !== "string")
throw new TypeError("currency code should be a string")
if(!this.currencyCode.includes(currencyFrom.toUpperCase()))
throw new Error(`${currencyFrom} is not a valid currency code`)
this.currencyFrom = currencyFrom.toUpperCase()
return this
}
to (currencyTo) {
if(typeof currencyTo !== "string")
throw new TypeError("currency code should be a string")
if(!this.currencyCode.includes(currencyTo.toUpperCase()))
throw new Error(`${currencyTo} is not a valid currency code`)
this.currencyTo = currencyTo
return this
}
amount (currencyAmount){
if(typeof currencyAmount !== "number")
throw new TypeError("amount should be a number")
if(currencyAmount <= 0)
throw new Error("amount should be a positive number")
this.currencyAmount = currencyAmount
return this
}
rates(){
if(this.currencyFrom === this.currencyTo)
return new Promise((resolve, _) => {resolve(1) })
else
return got(`https://www.google.com/finance/quote/${this.currencyFrom}-${this.currencyTo}`)
.then((html) => {return cheerio.load(html.body)})
.then(($) => {return $(".fxKbKc").text()})
.then((rates) => {
return parseFloat(rates.replace(",", ""))
})
}
convert(currencyAmount){
if(currencyAmount !== undefined){
this.amount(currencyAmount)
}
if(this.currencyFrom == "")
throw new Error("currency code cannot be an empty string")
if(this.currencyTo == "")
throw new Error("currency code cannot be an empty string")
if(this.currencyAmount == 0)
throw new Error("currency amount should be a positive value")
return this.rates().then((rates) =>{
this.convertedValue = rates * this.currencyAmount
return this.convertedValue
})
}
currencyName(currencyCode_){
if(typeof currencyCode_ != "string")
throw new TypeError("currency code should be a string")
if(!this.currencyCode.includes(currencyCode_.toUpperCase()))
throw new Error(`${currencyCode_} is not a valid currency code`)
return this.currencies[currencyCode_]
}
}
module.exports = CryptoConverter