-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
71 lines (61 loc) · 2.21 KB
/
app.js
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
const nearAPI = require('near-api-js');
const request = require('request');
// const config = require('./config');
// NEAR_RPC_API = config.NEAR_RPC_API;
// follow_address = config.follow_address;
const { NEAR_RPC_API, follow_address, push_url } = require('./config.js');
function isContractFollow(transaction) {
if (follow_address.includes(transaction['signer_id']) || follow_address.includes(transaction['receiver_id']))
return true;
return false;
}
function pushData(transaction) {
request.post(
push_url,
{ form: { data: JSON.stringify(transaction) } },
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
);
}
// var actions = ["FunctionCall", "CreateAccount", "Transfer", "AddKey", "DeleteAccount"];
var latestBlockHeight = 0;
async function listenLatestBlock() {
// Create a provider and get the latest block repeatedly
const provider = new nearAPI.providers.JsonRpcProvider(NEAR_RPC_API);
setInterval(async () => {
const latestBlock = await provider.block({ finality: 'final' });
const height = latestBlock.header.height;
if (height === latestBlockHeight) {
return;
}
latestBlockHeight = height;
console.log('Latest Block: ' + height);
const chunks = latestBlock.chunks;
for (const chunk of chunks) {
const chunkTemp = await provider.chunk(chunk.chunk_hash);
const transactions = chunkTemp.transactions;
if (transactions.length > 0) {
for (const transaction of transactions) {
// const action = Object.keys(transaction.actions[0])[0];
// if (!actions.includes(action)) {
// console.log(transaction);
// console.log(JSON.stringify(transaction));
// actions.push(action);
// }
if (isContractFollow(transaction)) {
//send to python process
console.log(JSON.stringify(transaction));
pushData(transaction);
}
// DEBUG
// console.log(JSON.stringify(transaction));
}
}
}
}, 1000); // Poll every 1 seconds (adjust the interval as needed)
}
// Call the function
listenLatestBlock().catch(console.error);