-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.ts
76 lines (59 loc) · 2.1 KB
/
update.ts
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
'use strict';
import * as uuid from 'uuid';
import { bufferToHex } from 'ethereumjs-util'
import { helpers } from 'leap-core';
import Web3 from 'web3';
import { LeapTransaction, UnspentWithTx } from './types';
import dynamoDb from './dynamodb'
export const updateStats = async (_event, _context) => {
const plasmaProvider = process.env.PLASMA_PROVIDER;
console.log('Updating stats... ' + plasmaProvider);
const web3Plasma = helpers.extendWeb3(new Web3(plasmaProvider));
await web3Plasma.eth.net.getId();
const uTxos = await web3Plasma.getUnspentAll();
let unspents = await Promise.all(
uTxos.map((u: UnspentWithTx) =>
web3Plasma.eth.getTransaction(bufferToHex(u.outpoint.hash))
.then((tx: LeapTransaction) => {
u.transaction = tx;
return u;
})
)
);
const blockNumbers = [...new Set(unspents.map(u => u.transaction.blockNumber))]; // Removes duplicates
const blocksWithTimestamps = {};
await Promise.all(
blockNumbers.map(bn =>
web3Plasma.eth.getBlock(bn).then(block =>
blocksWithTimestamps[bn] = block.timestamp
)
)
);
// Filter out last 30 days UTXOs
const endTimestamp = new Date().getTime() / 1000;
const startTimestamp = endTimestamp - 30 * 24 * 60 * 60; // 30 days old
const activeUtxos = unspents.filter(u => {
const timestamp = blocksWithTimestamps[u.transaction.blockNumber];
const withinLast30days = timestamp >= startTimestamp && timestamp <= endTimestamp;
const notFromSelf = u.transaction.from !== u.transaction.to;
return withinLast30days && notFromSelf;
});
console.log('Active UTXOs metric ' + activeUtxos.length);
const params = {
TableName: process.env.STATS_TABLE,
Item: {
id: uuid.v1(),
count: activeUtxos.length,
createdAt: endTimestamp * 1000
}
};
console.log('Saving to DynamoDB...');
await new Promise((resolve, reject) =>
dynamoDb.put(params, (err, result) => err ? reject(err) : resolve(result))
);
console.log('Saved to DynamoDB successfully!');
return {
statusCode: 200,
body: JSON.stringify({ success: true })
};
};