Skip to content

Commit

Permalink
exception: when commissions on the network do not exist and coingecko
Browse files Browse the repository at this point in the history
- customize the log style in group

Signed-off-by: Salman Wahib <[email protected]>
  • Loading branch information
sxlmnwb committed Aug 18, 2023
1 parent 7156ba5 commit 2ea0a1b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const start = async () => {
const reply = await useCommand(msg, client, chat);
if (reply) {
msg.reply(reply);
console.log(reply)
}
});

Expand Down
24 changes: 22 additions & 2 deletions src/libs/getApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@ const axios = require('axios');

module.exports = async function getApiData({ apiUrl, coingecko, valoper, valcons }) {
const summaryApi = await axios.get(`${apiUrl}/cosmos/base/tendermint/v1beta1/blocks/latest`);
const coingeckoApi = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coingecko}&vs_currencies=usd`);

let coingeckoApi;
try {
coingeckoApi = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coingecko}&vs_currencies=usd`);
} catch (error) {
if (error.response && error.response.status === 429) {
} else {
throw error;
}
}

const validatorApi = await axios.get(`${apiUrl}/cosmos/staking/v1beta1/validators/${valoper}`);
const signingInfosApi = await axios.get(`${apiUrl}/cosmos/slashing/v1beta1/signing_infos/${valcons}`);
const slashingParamsApi = await axios.get(`${apiUrl}/cosmos/slashing/v1beta1/params`);
const commissionApi = await axios.get(`${apiUrl}/cosmos/distribution/v1beta1/validators/${valoper}/commission`);

let commissionApi;
try {
commissionApi = await axios.get(`${apiUrl}/cosmos/distribution/v1beta1/validators/${valoper}/commission`);
} catch (error) {
if (error.response && error.response.status === 501) {
commissionApi = null;
} else {
throw error;
}
}

return {
summaryApi,
Expand Down
21 changes: 16 additions & 5 deletions src/libs/useApi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const getApi = require('./getApi');

module.exports = async function useApi({ apiUrl, valoper, valcons, denom, exponent, coingecko }) {
const apiData = await getApi({ apiUrl, coingecko, valoper, valcons });
let apiData;
if (coingecko) {
apiData = await getApi({ apiUrl, coingecko, valoper, valcons });
} else {
apiData = await getApi({ apiUrl, valoper, valcons });
}

// summary
const height = apiData.summaryApi.data.block.header.height;
Expand Down Expand Up @@ -47,14 +52,18 @@ module.exports = async function useApi({ apiUrl, valoper, valcons, denom, expone
function convertStatus(status) {
if (status === 'BOND_STATUS_BONDED') {
return 'Active';
} else if (status === 'BOND_STATUS_UNBONDED') {
} else if (status === 'BOND_STATUS_UNBONDED', 'BOND_STATUS_UNBONDING') {
return 'Inactive';
} else {
return status;
}
}
const bondStatus = convertStatus(validator.status);

let commissions = "N/A";

if (apiData.commissionApi) {
if (apiData.commissionApi.data.commission) {
const commission = apiData.commissionApi.data.commission.commission;
let commissionAmount = null;

Expand All @@ -65,10 +74,12 @@ module.exports = async function useApi({ apiUrl, valoper, valcons, denom, expone
}
}

let commissions = "N/A";
if (commissionAmount) {
const commissionFormated = parseFloat(commissionAmount) / (10 ** exponent);
commissions = commissionFormated.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2});}
const commissionFormated = parseFloat(commissionAmount) / (10 ** exponent);
commissions = commissionFormated.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2});
}
}
}

const bondedFormated = Number(BigInt(validator.tokens) / BigInt(10 ** exponent));
const totalBonded = bondedFormated.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});
Expand Down
23 changes: 17 additions & 6 deletions src/useCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ module.exports = async function useCommand(msg, client, chat) {
const user = await msg.getContact();
const userName = user.pushname || user.verifiedName || user.formattedName;
const time = new Date().toLocaleString().split(', ')[1];
const idMessage = msg.id._serialized.split('_').pop();
// const number = msg.from.replace('@c.us', '');
const idMessageParts = msg.id._serialized.split('_');
let idMessage;
let number;

console.log(chalk.white.bgYellowBright.bold('RECEIVED') +
if (msg.from.includes('@g.us', '@c.us')) {
idMessage = idMessageParts[idMessageParts.length - 2];
number = idMessageParts[idMessageParts.length - 1].split('@')[0];
} if (msg.from.includes('@c.us')) {
idMessage = idMessageParts.pop();
number = msg.from.replace('@c.us', '');
}

console.log(chalk.white.bgMagentaBright.bold('RECEIVED') +
" [" + chalk.blueBright(`${msg.body}`) + "] " +
chalk.green('ID') +
chalk.green('ID MSG') +
" [" + chalk.blueBright(`${idMessage}`) + "] " +
chalk.green('NUMBER') +
" [" + chalk.blueBright(`+${number}`) + "] " +
chalk.green('SENDER') +
" [" + chalk.blueBright(`${userName}`) + "] " +
chalk.green('TIME') +
Expand Down Expand Up @@ -66,7 +77,7 @@ module.exports = async function useCommand(msg, client, chat) {
} else {
for (const network of networks) {
try {
// await useMainnet(network);
await useMainnet(network);
console.log(chalk.white.bgYellowBright.bold('GET') + " [" + chalk.blueBright(`${network}`) + "] PREPERING FOR LOOPING");
} catch (error) {
console.error(chalk.white.bgRed.bold('ERROR') + " [" + chalk.redBright(`${error.message}`) + "] " + chalk.green('RECIPIENT') + " [" + chalk.blueBright(`${userName}`) + "]");
Expand Down Expand Up @@ -113,7 +124,7 @@ module.exports = async function useCommand(msg, client, chat) {
} else {
for (const network of networks) {
try {
// await useTestnet(network);
await useTestnet(network);
console.log(chalk.white.bgYellowBright.bold('GET') + " [" + chalk.blueBright(`${network}`) + "] PREPERING FOR LOOPING");
} catch (error) {
console.error(chalk.white.bgRed.bold('ERROR') + " [" + chalk.redBright(`${error.message}`) + "] " + chalk.green('RECIPIENT') + " [" + chalk.blueBright(`${userName}`) + "]");
Expand Down
2 changes: 1 addition & 1 deletion src/useLoopReq.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const stopLoop = async (networkInputs, chat) => {
messages.push(`Stopped sending ${networkInput} status updates.`);
delete networks[networkInput];
} else {
messages.push(`ERROR no status ${networkInput} updates to stop 💀⁉️`);
messages.push(`No status ${networkInput} updates to stop 💀⁉️`);
}
}
return messages;
Expand Down
1 change: 1 addition & 0 deletions src/useMainnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = async function useMainnet(network) {

return statusNode + validatorInfo;
} catch (error) {
// console.error(error)
throw new Error(`${network}: ${error.message.toLowerCase()}`);
}
}
1 change: 1 addition & 0 deletions src/useTestnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = async function useTestnet(network) {

return validatorInfo;
} catch (error) {
// console.error(error)
throw new Error(`${network}: ${error.message.toLowerCase()}`);
}
}

0 comments on commit 2ea0a1b

Please sign in to comment.