-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c82a05
commit bc63b48
Showing
8 changed files
with
175 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './lib'; | ||
export { default as CryptoCurrencies } from './types/CryptoCurrencies'; | ||
export { default as FiatCurrencies } from './types/FiatCurrencies'; | ||
export { default as getCurrencySymbol } from './utils/getCurrencySymbol'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import CryptoCurrencies from '../../types/CryptoCurrencies'; | ||
import FiatCurrencies from '../../types/FiatCurrencies'; | ||
import getCurrencySymbol from '../../utils/getCurrencySymbol'; | ||
|
||
type TickerConfig = { | ||
base: CryptoCurrencies | FiatCurrencies; | ||
quote: CryptoCurrencies | FiatCurrencies; | ||
frequency?: number; | ||
request: () => Promise<number>; | ||
onUpdate?: (price: number) => void; | ||
onError?: (e: any) => void; | ||
} | ||
|
||
type TickerHistory = Array<{ | ||
price: number; | ||
date: Date; | ||
}>; | ||
|
||
class Ticker { | ||
private readonly _baseName: string; | ||
private readonly _baseSymbol: string; | ||
|
||
private readonly _quoteName: string; | ||
private readonly _quoteSymbol: string; | ||
|
||
private readonly _frequency: number; | ||
private readonly _request: () => Promise<number>; | ||
private readonly _onUpdate?: (price: number) => void; | ||
private readonly _onError?: (e: any) => void; | ||
|
||
private _price: number = 0; | ||
private _history: TickerHistory = []; | ||
|
||
private _interval: NodeJS.Timeout | undefined = undefined; | ||
private _fetching: boolean = false; | ||
|
||
constructor(config: TickerConfig) { | ||
const baseSymbol = getCurrencySymbol(config.base); | ||
if (!baseSymbol) throw new Error('Invalid base currency.'); | ||
|
||
const quoteSymbol = getCurrencySymbol(config.quote); | ||
if (!quoteSymbol) throw new Error('Invalid quote currency.'); | ||
|
||
this._baseName = config.base.toString(); | ||
this._baseSymbol = baseSymbol; | ||
|
||
this._quoteName = config.quote.toString(); | ||
this._quoteSymbol = quoteSymbol; | ||
|
||
this._frequency = config.frequency || 1000; | ||
this._request = config.request; | ||
this._onUpdate = config.onUpdate; | ||
this._onError = config.onError; | ||
} | ||
|
||
public get baseName(): string { return this._baseName; } | ||
public get baseSymbol(): string { return this._baseSymbol; } | ||
|
||
public get quoteName(): string { return this._quoteName; } | ||
public get quoteSymbol(): string { return this._quoteSymbol; } | ||
|
||
public get price(): number { return this._price; } | ||
public get history(): TickerHistory { return this._history; } | ||
|
||
public get active(): boolean { return this._interval !== undefined; } | ||
|
||
public update(): void { | ||
if (!this._fetching) { | ||
this._fetching = true; | ||
this._request() | ||
.then(price => { | ||
this._price = price; | ||
this._history.push({ price, date: new Date() }); | ||
if (this._onUpdate) this._onUpdate(price); | ||
}) | ||
.catch(e => { | ||
if (this._onError) this._onError(e); | ||
}) | ||
.finally(() => { | ||
this._fetching = false; | ||
}); | ||
} | ||
} | ||
|
||
public start(): void { | ||
if (this._interval === undefined) { | ||
this._interval = setInterval(() => { | ||
this.update(); | ||
}, this._frequency); | ||
} | ||
} | ||
|
||
public stop(): void { | ||
if (this._interval !== undefined) { | ||
clearTimeout(this._interval); | ||
this._interval = undefined; | ||
} | ||
} | ||
|
||
public reset(): void { | ||
this.stop(); | ||
this._price = 0; | ||
this._history = []; | ||
} | ||
} | ||
|
||
export default Ticker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './Ticker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as Ticker } from './Ticker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
enum CryptoCurrencies { | ||
ADA, | ||
ALGO, | ||
ATOM, | ||
BNB, | ||
BTC, | ||
DASH, | ||
DOT, | ||
DAI, | ||
DNT, | ||
ETH, | ||
GLM, | ||
LINK, | ||
LTC, | ||
REN, | ||
STORJ, | ||
XLM, | ||
XTZ, | ||
XRP | ||
AAVE = 'Aave', | ||
ADA = 'Cardano', | ||
ALGO = 'Algorland', | ||
ATOM = 'Cosmos', | ||
AVAX = 'Avalanche', | ||
BAT = 'Basic Attention Token', | ||
BCH = 'Bitcoin Cash', | ||
BNB = 'Binance Coin', | ||
BTC = 'Bitcoin', | ||
BUSD = 'Binance USD', | ||
COMP = 'Compound', | ||
DASH = 'Dash', | ||
DAI = 'Dai', | ||
DOT = 'Polkadot', | ||
DNT = 'District0x', | ||
EOS = 'EOS', | ||
ETC = 'Ethereum Classic', | ||
ETH = 'Ethereum', | ||
FIL = 'Filecoin', | ||
GLM = 'Golem', | ||
GRT = 'The Graph', | ||
LINK = 'Chainlink', | ||
LTC = 'Litecoin', | ||
MKR = 'Maker', | ||
NEO = 'Neo', | ||
ONT = 'Ontology', | ||
REN = 'Ren', | ||
REP = 'Augur', | ||
STORJ = 'Storj', | ||
USDC = 'USD Coin', | ||
USDT = 'Tether', | ||
VET = 'VeChain', | ||
XLM = 'Stellar', | ||
XMR = 'Monero', | ||
XTZ = 'Tezos', | ||
XRP = 'Ripple', | ||
YFI = 'yearn.finance', | ||
ZEC = 'Zcash', | ||
ZRX = '0x' | ||
} | ||
|
||
export default CryptoCurrencies; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
enum FiatCurrencies { | ||
AUD = 'Australian Dollar', | ||
CAD = 'Canadian Dollar', | ||
CHF = 'Swiss Franc', | ||
CNY = 'Chinese Yuan', | ||
EUR = 'Euro', | ||
USD = 'US Dollar' | ||
GBP = 'Pound Sterling', | ||
JPY = 'Japanese Yen', | ||
MXN = 'Mexican Peso', | ||
USD = 'United States Dollar' | ||
} | ||
|
||
export default FiatCurrencies; | ||
export default FiatCurrencies; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import CryptoCurrencies from '../types/CryptoCurrencies'; | ||
import FiatCurrencies from '../types/FiatCurrencies'; | ||
|
||
function getCurrencySymbol(currency: CryptoCurrencies | FiatCurrencies): string | undefined { | ||
return Object.keys(CryptoCurrencies).find(key => { | ||
const crypto = CryptoCurrencies[key as keyof typeof CryptoCurrencies]; | ||
if (currency === crypto) return key; | ||
}) || Object.keys(FiatCurrencies).find(key => { | ||
const fiat = FiatCurrencies[key as keyof typeof FiatCurrencies]; | ||
if (currency === fiat) return key; | ||
}); | ||
} | ||
|
||
export default getCurrencySymbol; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters