forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
76 lines (66 loc) · 2.05 KB
/
python-app.yml
File metadata and controls
76 lines (66 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const axios = require('axios');
const mongoose = require('mongoose');
// MongoDB connection
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Define Mongoose Schema
const blockSchema = new mongoose.Schema({
height: Number,
hash: String,
time: Number,
txIndexes: [Number],
});
const transactionSchema = new mongoose.Schema({
hash: String,
time: Number,
inputs: Array,
outputs: Array,
});
// Create models
const Block = mongoose.model('Block', blockSchema);
const Transaction = mongoose.model('Transaction', transactionSchema);
// Fetch Latest Block
async function fetchLatestBlock() {
try {
const response = await axios.get("https://blockchain.info/latestblock");
const blockData = response.data;
// Check if block already exists
const existingBlock = await Block.findOne({ height: blockData.height });
if (!existingBlock) {
const newBlock = new Block(blockData);
await newBlock.save();
console.log(`Block ${blockData.height} saved.`);
} else {
console.log(`Block ${blockData.height} already exists.`);
}
} catch (error) {
console.error("Error fetching block:", error);
}
}
// Fetch Transactions for Your Bitcoin Address
async function fetchTransactions(address) {
try {
const response = await axios.get(`https://blockchain.info/rawaddr/${address}`);
const transactions = response.data.txs;
for (const tx of transactions) {
const existingTx = await Transaction.findOne({ hash: tx.hash });
if (!existingTx) {
const newTx = new Transaction(tx);
await newTx.save();
console.log(`Transaction ${tx.hash} saved.`);
}
}
} catch (error) {
console.error("Error fetching transactions:", error);
}
}
// Run both functions
async function updateData() {
await fetchLatestBlock();
await fetchTransactions("bc1qn56zm7hsxzdshuxdc7s7ytcv3qznf7wntj80g3"); // Your BTC address
}
// Run every 5 minutes
setInterval(updateData, 5 * 60 * 1000);
console.log("Bitcoin block & transaction tracker started...");