Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor client #72

Merged
merged 11 commits into from
Sep 27, 2023
9 changes: 7 additions & 2 deletions backend/Model/bitcoin/Models.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ const TxsSchema = new mongoose.Schema({
is_double_spend : Boolean,
outputs_count : Number,
outputs_value : Number,
inputs: [],
outputs: [],
witness_hash: String,
inputs: {
type: Object
},
outputs: {
type: Object
},
});

//set bitcoin chain schema
Expand Down
12 changes: 11 additions & 1 deletion backend/Routes/Chains/bitcoin/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ require("dotenv").config();

const BITCOIN_API = process.env.BITCOIN_REST_API;

let RESTAPI = [];

if (BITCOIN_API) {
try {
RESTAPI = JSON.parse(BITCOIN_API);
} catch (error) {
console.error("Error parsing BITCOIN_API :", error);
}
}

//cron task for Bitcoin
createBitcoinCronJob(BITCOIN_API, Model.bitcoinTxsModel);
createBitcoinCronJob(RESTAPI, Model.bitcoinTxsModel);

// Define a helper function to prefix the routes with "/bitcoin"
function bitcoinRoute(routePrefix, path, handler) {
Expand Down
229 changes: 86 additions & 143 deletions backend/Routes/Chains/cosmos/mainnet/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,158 +48,101 @@ const {
const { BlockModel: cosmosBlockModel, TxsModel: cosmosTxsModel } =
createBlockchainModels("cosmos");

const COSMOSHUB_REST_API = process.env.COSMOSHUB_REST_API;
const COSMOSHUB_RPC = process.env.COSMOSHUB_RPC;

let RESTAPI = [];
let RPC = [];

if (COSMOSHUB_REST_API) {
try {
RESTAPI = JSON.parse(COSMOSHUB_REST_API);
} catch (error) {
console.error("Error parsing COSMOSHUB_REST_API:", error);
}
}

if (COSMOSHUB_RPC) {
try {
RPC = JSON.parse(COSMOSHUB_RPC);
} catch (error) {
console.error("Error parsing COSMOSHUB_RPC:", error);
}
}

const cosmosHubCronOptions = {
apiUrl: RESTAPI,
txsModel: cosmosTxsModel,
blockModel: cosmosBlockModel,
};
// cron task for cosmos
createCronJob(process.env.COSMOS_REST_API, cosmosTxsModel, cosmosBlockModel);
createCronJob(cosmosHubCronOptions);

function prefixedRoute(routePrefix, path, handler) {
return app.get(`/${routePrefix}${path}`, corsMiddleware, handler);
}

// Define a generic route function
const defineRoutes = (routePrefix, blockModel, txsModel, api, rpc) => {
prefixedRoute(routePrefix, "/blocks/latest", latestBlocksHandler(blockModel));
prefixedRoute(routePrefix, "/txs", allTxsHandler(txsModel));
prefixedRoute(routePrefix, "/all_validators", allValidatorsHandler(api));
prefixedRoute(
routePrefix,
"/active_validators",
activeValidatorsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_validator_details",
chainValidatorsDetailsHandler(api)
);
prefixedRoute(routePrefix, "/chain_inflation", chainInflationHandler(api));
prefixedRoute(
routePrefix,
"/chain_community_pool",
chainCommunityPoolHandler(api)
);
prefixedRoute(routePrefix, "/chain_pool", chainPoolHandler(api));
prefixedRoute(
routePrefix,
"/block_height_details",
chainBlockHeightDetailsHandler(api)
);
prefixedRoute(
routePrefix,
"/block_height_txs",
chainBlockHeightTxsHandler(api)
);
prefixedRoute(routePrefix, "/chain_txs_hash", chainTxsByHashHandler(api));
prefixedRoute(
routePrefix,
"/chain_validator_slashing_signing_info_details",
chainValidatorsSlashingSigningInfosDetailsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_validator_delegations",
chainValidatorDelegationsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_validator_undelegations",
chainValidatorUnDelegationsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_validator_redelegations",
chainValidatorReDelegationsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_consensus",
chainConsensusStateHandler(rpc)
);
prefixedRoute(
routePrefix,
"/chain_minting_params",
chainMintingParamsHandler(api)
);
prefixedRoute(routePrefix, "/chain_gov_params", chainGovParamsHandler(api));
prefixedRoute(
routePrefix,
"/chain_slashing_params",
chainSlashingParamsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_staking_params",
chainStakingParamsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_distribution_params",
chainDistributionParamsHandler(api)
);
prefixedRoute(routePrefix, "/chain_node_info", chainNodeInfoHandler(api));
prefixedRoute(routePrefix, "/chain_proposals", chainProposalsHandler(api));
prefixedRoute(
routePrefix,
"/chain_proposal_details",
chainProposalDetailsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_proposal_voting_options",
chainProposalVotingOptionsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_proposal_tally_options",
chainProposalTallyOptionsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_proposal_deposits",
chainProposalDepositsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_auth_account",
chainAuthAccountHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_txs_by_events",
chainAccountTxsByEventsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_balance",
chainAccountBalanceHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_delegation_rewards",
chainAccountDelegationRewardsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_delegations",
chainAccountDelegationsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_redelegations",
chainAccountReDelegationsHandler(api)
);
prefixedRoute(
routePrefix,
"/chain_account_undelegations",
chainAccountUnDelegationsHandler(api)
);
const defineRoutes = (routePrefix, options) => {
const { blockModel, txsModel, api, rpc } = options;

const routeMappings = [
["/blocks/latest", latestBlocksHandler(blockModel)],
["/txs", allTxsHandler(txsModel)],
["/all_validators", allValidatorsHandler(api)],
["/active_validators", activeValidatorsHandler(api)],
["/chain_validator_details", chainValidatorsDetailsHandler(api)],
["/chain_inflation", chainInflationHandler(api)],
["/chain_community_pool", chainCommunityPoolHandler(api)],
["/chain_pool", chainPoolHandler(api)],
["/block_height_details", chainBlockHeightDetailsHandler(api)],
["/block_height_txs", chainBlockHeightTxsHandler(api)],
["/chain_txs_hash", chainTxsByHashHandler(api)],
[
"/chain_validator_slashing_signing_info_details",
chainValidatorsSlashingSigningInfosDetailsHandler(api),
],
["/chain_validator_delegations", chainValidatorDelegationsHandler(api)],
["/chain_validator_undelegations", chainValidatorUnDelegationsHandler(api)],
["/chain_validator_redelegations", chainValidatorReDelegationsHandler(api)],
["/chain_consensus", chainConsensusStateHandler(rpc)],
["/chain_minting_params", chainMintingParamsHandler(api)],
["/chain_gov_params", chainGovParamsHandler(api)],
["/chain_slashing_params", chainSlashingParamsHandler(api)],
["/chain_staking_params", chainStakingParamsHandler(api)],
["/chain_distribution_params", chainDistributionParamsHandler(api)],
["/chain_node_info", chainNodeInfoHandler(api)],
["/chain_proposals", chainProposalsHandler(api)],
["/chain_proposal_details", chainProposalDetailsHandler(api)],
["/chain_proposal_voting_options", chainProposalVotingOptionsHandler(api)],
["/chain_proposal_tally_options", chainProposalTallyOptionsHandler(api)],
["/chain_proposal_deposits", chainProposalDepositsHandler(api)],
["/chain_auth_account", chainAuthAccountHandler(api)],
["/chain_account_txs_by_events", chainAccountTxsByEventsHandler(api)],
["/chain_account_balance", chainAccountBalanceHandler(api)],
[
"/chain_account_delegation_rewards",
chainAccountDelegationRewardsHandler(api),
],
["/chain_account_delegations", chainAccountDelegationsHandler(api)],
["/chain_account_redelegations", chainAccountReDelegationsHandler(api)],
["/chain_account_undelegations", chainAccountUnDelegationsHandler(api)],
];

routeMappings.forEach(([route, handler]) => {
prefixedRoute(routePrefix, route, handler);
});
};

// Define options object
const options = {
blockModel: cosmosBlockModel,
txsModel: cosmosTxsModel,
api: RESTAPI,
rpc: RPC,
};

// Use the generic route function for "cosmos" prefix
defineRoutes(
"cosmos",
cosmosBlockModel,
cosmosTxsModel,
process.env.COSMOS_REST_API,
process.env.COSMOS_RPC_API
);
defineRoutes("cosmos", options);

module.exports = app;
4 changes: 3 additions & 1 deletion backend/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const fetchLatestBlocksAndTxs = require("./data/chainQueries/cosmos/latestBlocks
const fetchBitcoinTxs = require("./data/chainQueries/bitcoin/index.js");

//cron to run at every 3sec to get latest blocks
function createCronJob(apiUrl, txsModel, blockModel) {
function createCronJob(options) {
const { apiUrl, txsModel, blockModel } = options;

return cron.schedule("*/3 * * * * *", async () => {
try {
await fetchLatestBlocksAndTxs(apiUrl, txsModel, blockModel);
Expand Down
Loading
Loading