-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from jim380/ethereum
ethereum Txs
- Loading branch information
Showing
10 changed files
with
2,714 additions
and
20 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
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,28 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const TxsSchema = new mongoose.Schema({ | ||
blockNumber: { | ||
type: Number, | ||
required: true, | ||
unique: false, | ||
}, | ||
hash: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
timestamp: Number, | ||
transactionIndex: Number, | ||
from: String, | ||
to: String, | ||
value: String, | ||
nonce: Number | ||
|
||
}); | ||
|
||
//set bitcoin chain schema | ||
const ethereumTransactions = mongoose.model("ethereum-Transactions", TxsSchema); | ||
|
||
module.exports = { | ||
ethereumTxsModel: ethereumTransactions, | ||
}; |
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,23 @@ | ||
const express = require("express"); | ||
const app = express(); | ||
const corsMiddleware = require("../../../corsMiddleware.js"); | ||
const { createEthereumCronJob } = require("../../../cron.js"); | ||
const Model = require("../../../Model/ethereum/Model.jsx"); | ||
const ethereumTxsHandler = require("../../../data/chainQueries/ethereum/handlers.js"); | ||
|
||
//cron task for Bitcoin | ||
createEthereumCronJob(); | ||
|
||
// Define a helper function to prefix the routes with "/ethereum" | ||
function ethereumRoute(routePrefix, path, handler) { | ||
return app.get(`/${routePrefix}${path}`, corsMiddleware, handler); | ||
} | ||
|
||
// Define the routes | ||
const ethereumRoutes = (routePrefix, txsModel) => { | ||
ethereumRoute(routePrefix, "/txs", ethereumTxsHandler(txsModel)); | ||
}; | ||
|
||
ethereumRoutes("ethereum", Model.ethereumTxsModel); | ||
|
||
module.exports = app; |
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
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,12 @@ | ||
const { getAllTxs } = require("../../dbQueries"); | ||
|
||
const ethereumTxsHandler = (txModel) => async (req, res) => { | ||
try { | ||
const data = await getAllTxs(req, res, txModel); | ||
res.json(data); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = ethereumTxsHandler; |
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,85 @@ | ||
const { Network, Alchemy } = require("alchemy-sdk"); | ||
const { Web3 } = require("web3"); | ||
const Model = require("../../../Model/ethereum/Model.jsx"); | ||
|
||
// Initialize a Web3 instance (connect to an Ethereum node if needed) | ||
const web3 = new Web3(); | ||
|
||
const settings = { | ||
apiKey: "iHiSvMYKjRZEuwF_ma9IecHZUjiHiagG", | ||
network: Network.ETH_MAINNET, | ||
}; | ||
|
||
const alchemy = new Alchemy(settings); | ||
|
||
async function fetchEthereumTxs() { | ||
try { | ||
// Retry logic for network errors | ||
const retryCount = 3; | ||
let attempt = 0; | ||
let block; | ||
|
||
while (attempt < retryCount) { | ||
attempt++; | ||
|
||
try { | ||
// Get the latest block | ||
block = await alchemy.core.getBlock("latest"); | ||
break; | ||
} catch (networkError) { | ||
console.error( | ||
`Network error (attempt ${attempt}):`, | ||
networkError.message | ||
); | ||
|
||
// Add delay before retrying (you can adjust the delay duration) | ||
await new Promise((resolve) => setTimeout(resolve, 5000)); | ||
} | ||
} | ||
|
||
if (!block) { | ||
console.error( | ||
"Failed to retrieve the latest block after multiple attempts." | ||
); | ||
return; | ||
} | ||
|
||
// Create an array to store transaction data objects | ||
const transactionsDataArray = []; | ||
|
||
// Fetch transaction details for each transaction in the block | ||
for (const txHash of block.transactions) { | ||
const tx = await alchemy.core.getTransaction(txHash); | ||
const value = web3.utils.hexToNumberString(tx.value._hex); | ||
// convert value to ethers | ||
const convertValue = value / 10 ** 18; | ||
|
||
// Create a new transaction data object and push it to the array | ||
const transactionsData = new Model.ethereumTxsModel({ | ||
blockNumber: tx.blockNumber, | ||
hash: tx.hash, | ||
timestamp: block.timestamp, | ||
transactionIndex: tx.transactionIndex, | ||
from: tx.from, | ||
to: tx.to, | ||
value: convertValue, | ||
nonce: tx.nonce, | ||
}); | ||
|
||
transactionsDataArray.push(transactionsData); | ||
} | ||
|
||
// Insert all transaction data objects in a single batch | ||
await Model.ethereumTxsModel.insertMany(transactionsDataArray); | ||
} catch (error) { | ||
if (error.code === 11000) { | ||
// Duplicate key error, you can log it and continue | ||
return; | ||
} else { | ||
// Handle other errors | ||
console.error("Error:", error); | ||
} | ||
} | ||
} | ||
|
||
module.exports = fetchEthereumTxs; |
Oops, something went wrong.