Skip to content

Commit

Permalink
add network foreach cryprto in getPrices (#288)
Browse files Browse the repository at this point in the history
**Dear team,**

**This PR** introduces an important update to the cryptoDetails variable
by adding the networkSupported and contractAddress fields for each
cryptocurrency. The information for these fields will be retrieved using
the following API endpoint:
https://pro-api.coinmarketcap.com/v2/cryptocurrency/info.

The following changes have been made:

**Adding networkSupported and contractAddress Fields**: We have enhanced
the cryptoDetails variable by including the networkSupported and
contractAddress fields for each cryptocurrency. These fields provide
information about the supported network and contract address associated
with each cryptocurrency. The data for these fields will be fetched from
the specified API endpoint.
Reviewers, please ensure that the networkSupported and contractAddress
fields have been successfully added to the cryptoDetails variable by
fetching the necessary data from the provided API endpoint. Verify that
the information is accurate and aligned with the respective
cryptocurrencies. Your feedback, suggestions, and alternative approaches
to improve the implementation of these additions are highly appreciated.

Thank you for your time and effort in enhancing the functionality and
data representation of our platform.

Best regards,
Rania Morheg
  • Loading branch information
HamdiBenK committed Jul 24, 2023
2 parents e70daae + af4f81e commit 2a4fa8f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ CMC_GLOBL_URL=https://pro-api.coinmarketcap.com/v1/global-metrics/quotes/latest
CMC_HISTORY_URL=https://pro-api.coinmarketcap.com/v3/cryptocurrency/quotes/historical
CMC_URl=https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest
CMC_CRYPTO_URL=https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest
CMC_CRYPTO_DETAILS=https://pro-api.coinmarketcap.com/v2/cryptocurrency/info
# Address of smart contract deployed by owner and the wallet password
CAMPAIGN_OWNER=
CAMPAIGN_OWNER_PASS=
Expand Down
78 changes: 72 additions & 6 deletions web3/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const { timeout } = require('../helpers/utils')
const { list } = require('tar')



exports.unlock = async (req, res) => {
try {
let UserId = req.user._id
Expand Down Expand Up @@ -505,6 +506,41 @@ exports.getAllWallets = async (req, res) => {
} else if (Object.keys(res).length !== 0)
return res.status(401).end('Account not found')
}

const getNetworkByToken = async (idCrypto) => {
try {
if (
cache.get('networks') &&
Date.now() - new Date(cache.get('networks').date).getTime() < 604800000
) {
return cache.get('networks').data;
} else {
const options = {
method: 'GET',
url: process.env.CMC_CRYPTO_DETAILS,
params: {
id: idCrypto,
},
headers: {
'X-CMC_PRO_API_KEY': process.env.CMCAPIKEY,
},
};

const result = await rp.request(options);
const networksContract = Object.values(result.data.data).map((innerObj) => ({
symbol: innerObj.symbol,
contract_address: innerObj.contract_address,
}));

const networks = { data: networksContract, date: Date.now() };
cache.put('networks', networks);

return networksContract;
}
} catch (err) {
throw new Error('Error fetching networks');
}
};
exports.getPrices = async () => {
try {
if (
Expand Down Expand Up @@ -538,6 +574,7 @@ exports.getPrices = async () => {
'X-CMC_PRO_API_KEY': process.env.CMCAPIKEY,
},
}



let result
Expand All @@ -559,8 +596,14 @@ exports.getPrices = async () => {
response.data.data.push(responseSattJet.data.data.JET)
response.data.data.push(responseSattJet.data.data.BTT)
let priceMap

try {
priceMap = response.data.data.map((elem) => {




priceMap = response.data.data.map( (elem) => {

var obj = {}
let tokenAddress = null
if (elem.platform?.name === 'BNB') {
Expand All @@ -572,6 +615,7 @@ exports.getPrices = async () => {
cmc_rank: elem.cmc_rank,
network:
(elem.platform?.name === 'BNB' && 'BEP20') || null,
networkSupported: [],
tokenAddress: tokenAddress,
symbol: elem.symbol,
name: elem.name,
Expand All @@ -598,6 +642,7 @@ exports.getPrices = async () => {
cmc_rank: elem.cmc_rank,
network:
(elem.platform?.name === 'BNB' && 'BEP20') || null,
networkSupported:[],
tokenAddress: tokenAddress,
symbol: elem.symbol,
name: elem.name,
Expand All @@ -624,12 +669,28 @@ exports.getPrices = async () => {
}

var finalMap = {}
for (var i = 0; i < priceMap.length; i++) {
finalMap[priceMap[i].symbol] = priceMap[i]
delete finalMap[priceMap[i].symbol].symbol
}

const idcrypto = priceMap.map((token) => token.id.toString());

priceMap.forEach((token) => {
finalMap[token.symbol] = { ...token, networkSupported: '' };
delete finalMap[token.symbol].symbol;
});



const networksContract = await getNetworkByToken(idcrypto.join(','));
networksContract.forEach((network) => {
if (finalMap[network.symbol]) {
finalMap[network.symbol].networkSupported = network.contract_address;
}
});




for (var i = 0; i < token200.length; i++) {

var token = token200[i]
if (finalMap[token.symbol]) {
finalMap[token.symbol].network =
Expand All @@ -645,7 +706,10 @@ exports.getPrices = async () => {
cache.put('prices', prices)
return finalMap
}
} catch (err) {}

} catch (err) {
throw new Error('Error fetching prices ')
}
}

exports.getChartVariation = async(cryptolist) => {
Expand All @@ -672,6 +736,7 @@ exports.getChartVariation = async(cryptolist) => {
},

}

var results
try {
results = await Promise.all([ rp.request(options)])
Expand All @@ -680,6 +745,7 @@ exports.getChartVariation = async(cryptolist) => {
throw new Error('Error fetching prices chart')
}
var result = await results[0]

var cryptoInfo
var priceVariation =[]

Expand Down

0 comments on commit 2a4fa8f

Please sign in to comment.