-
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
0 parents
commit 8414947
Showing
10 changed files
with
2,782 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
test.js |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 MultipleChain | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. |
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,157 @@ | ||
const axios = require('axios'); | ||
|
||
class CurrencyConverter { | ||
|
||
/** | ||
* @var {String} | ||
*/ | ||
api; | ||
|
||
/** | ||
* @var {String} | ||
*/ | ||
apiUrl; | ||
|
||
/** | ||
* @var {String|null} | ||
*/ | ||
apiKey = null; | ||
|
||
/** | ||
* @var {Object} | ||
*/ | ||
apis = { | ||
'cryptocompare': 'https://min-api.cryptocompare.com/data/price', | ||
'coinmarketcap': 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion' | ||
}; | ||
|
||
/** | ||
* @var {Array} | ||
*/ | ||
stableCoins = [ | ||
'USDT', | ||
'USDC', | ||
'DAI', | ||
'BUSD', | ||
'UST', | ||
'TUSD' | ||
]; | ||
|
||
/** | ||
* @param {String} api | ||
* @param {String|null} apiKey | ||
* @throws {Error} | ||
*/ | ||
constructor(api, apiKey = null) { | ||
if (!this.apis[api]) { | ||
throw new Error('Unsupported api!'); | ||
} | ||
|
||
this.api = api; | ||
this.apiKey = apiKey; | ||
this.apiUrl = this.apis[api]; | ||
} | ||
|
||
/** | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
convert(from, to, amount) { | ||
if (this.api == 'coinmarketcap') { | ||
return this.convertWithCoinMarketCap(from, to, amount); | ||
} else if (this.api == 'cryptocompare') { | ||
return this.convertWithCryptoCompare(from, to, amount); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* It is not currently used by js as requests are prohibited by coinmarketcap (only backend) | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
async convertWithCoinMarketCap(from, to, amount) { | ||
|
||
this.checkUsedApi('coinmarketcap'); | ||
|
||
let apiUrl = `${this.apiUrl}?amount=${amount}&symbol=${from}&convert=${to}`; | ||
|
||
let response = await axios.get(apiUrl, { | ||
headers: { | ||
'Accepts': 'application/json', | ||
'X-CMC_PRO_API_KEY': `${this.apiKey}`, | ||
} | ||
}); | ||
|
||
let convertData = response.data; | ||
|
||
if (convertData.data) { | ||
let price = convertData.data.quote[to.toUpperCase()].price; | ||
return parseFloat(price); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
async convertWithCryptoCompare(from, to, amount) { | ||
|
||
if (from.toLocaleUpperCase() == 'USD' || to.toLocaleUpperCase() == 'USD') { | ||
if (this.stableCoins.includes(from.toLocaleUpperCase()) || this.stableCoins.includes(to.toLocaleUpperCase())) { | ||
return amount; | ||
} | ||
} | ||
|
||
this.checkUsedApi('cryptocompare'); | ||
|
||
let apiUrl = this.apiUrl + '?fsym=' + from + '&tsyms=' + to; | ||
let response = await axios.get(apiUrl); | ||
let convertData = response.data; | ||
if (convertData[to]) { | ||
let price = amount * convertData[to]; | ||
return parseFloat(price.toFixed()); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {String} api | ||
* @return {void} | ||
* @throws {Error} | ||
*/ | ||
checkUsedApi(api) { | ||
if (this.api != api) { | ||
throw new Error(`The api chosen to be used is not the "${api}" api!`); | ||
} else { | ||
if (this.apiKey == null && this.api == 'coinmarketcap') { | ||
throw new Error("The key of the api selected to be used has not been entered."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {Array} symbols | ||
*/ | ||
addStableCoins(symbols) { | ||
this.stableCoins = stableCoins.concat(symbols); | ||
} | ||
|
||
} | ||
|
||
window.CurrencyConverter = CurrencyConverter; | ||
|
||
module.exports = CurrencyConverter; |
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,142 @@ | ||
const axios = require('axios'); | ||
|
||
const apis = { | ||
'CryptoCompare': 'https://min-api.cryptocompare.com/data/price', | ||
'CoinMarketCap': 'https://pro-api.coinmarketcap.com/v1/tools/price-conversion' | ||
}; | ||
|
||
const stableCoins = [ | ||
'USDT', | ||
'USDC', | ||
'DAI', | ||
'BUSD', | ||
'UST', | ||
'TUSD' | ||
]; | ||
|
||
const state = {}; | ||
|
||
/** | ||
* @param {String} api | ||
* @param {String|null} apiKey | ||
* @throws {Error} | ||
*/ | ||
let setApi = (api, apiKey = null) => { | ||
|
||
if (!apis[api]) { | ||
throw new Error('Unsupported api!'); | ||
} | ||
|
||
state.api = api; | ||
state.apiKey = apiKey; | ||
state.apiUrl = apis[api]; | ||
} | ||
|
||
/** | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
let convert = (from, to, amount) => { | ||
if (state.api == 'CoinMarketCap') { | ||
return convertWithCoinMarketCap(from, to, amount); | ||
} else if (state.api == 'CryptoCompare') { | ||
return convertWithCryptoCompare(from, to, amount); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* It is not currently used by js as requests are prohibited by coinmarketcap (only backend) | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
let convertWithCoinMarketCap = async (from, to, amount) => { | ||
|
||
checkUsedApi('CoinMarketCap'); | ||
|
||
let apiUrl = `${state.apiUrl}?amount=${amount}&symbol=${from}&convert=${to}`; | ||
|
||
let response = await axios.get(apiUrl, { | ||
headers: { | ||
'Accepts': 'application/json', | ||
'X-CMC_PRO_API_KEY': `${state.apiKey}`, | ||
} | ||
}); | ||
|
||
let convertData = response.data; | ||
|
||
if (convertData.data) { | ||
let price = convertData.data.quote[to.toUpperCase()].price; | ||
return parseFloat(price); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @param {String} from | ||
* @param {String} to | ||
* @param {Number} amount | ||
* @return {Float|null} | ||
* @throws {Error} | ||
*/ | ||
let convertWithCryptoCompare = async (from, to, amount) => { | ||
|
||
if (from.toLocaleUpperCase() == 'USD' || to.toLocaleUpperCase() == 'USD') { | ||
if (stableCoins.includes(from.toLocaleUpperCase()) || stableCoins.includes(to.toLocaleUpperCase())) { | ||
return amount; | ||
} | ||
} | ||
|
||
checkUsedApi('CryptoCompare'); | ||
|
||
let apiUrl = state.apiUrl + '?fsym=' + from + '&tsyms=' + to; | ||
let response = await axios.get(apiUrl); | ||
let convertData = response.data; | ||
if (convertData[to.toUpperCase()]) { | ||
let price = amount * convertData[to.toUpperCase()]; | ||
return parseFloat(price); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param {String} api | ||
* @return {void} | ||
* @throws {Error} | ||
*/ | ||
let checkUsedApi = (api) => { | ||
if (state.api != api) { | ||
throw new Error(`The api chosen to be used is not the "${api}" api!`); | ||
} else { | ||
if (state.apiKey == null && state.api == 'coinmarketcap') { | ||
throw new Error("The key of the api selected to be used has not been entered."); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {Array} symbols | ||
*/ | ||
let addStableCoins = (symbols) => { | ||
stableCoins = stableCoins.concat(symbols); | ||
} | ||
|
||
|
||
module.exports = { | ||
state, | ||
setApi, | ||
convert, | ||
addStableCoins, | ||
convertWithCoinMarketCap, | ||
convertWithCryptoCompare | ||
}; |
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 @@ | ||
declare module 'currency-converter'; |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.