Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BeycanDeveloper committed Mar 23, 2023
0 parents commit 8414947
Show file tree
Hide file tree
Showing 10 changed files with 2,782 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
test.js
21 changes: 21 additions & 0 deletions LICENSE
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.
157 changes: 157 additions & 0 deletions currency-converter.class.js
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;
142 changes: 142 additions & 0 deletions currency-converter.js
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
};
1 change: 1 addition & 0 deletions currency-converter.js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'currency-converter';
1 change: 1 addition & 0 deletions dist/currency-converter.js

Large diffs are not rendered by default.

Loading

0 comments on commit 8414947

Please sign in to comment.