-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth.js
96 lines (92 loc) · 2.47 KB
/
eth.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fetch = require('node-fetch');
const web3Utils = require('web3-utils');
const { owlAddress, etherscanKey } = require('./settings.json');
const {
isReceiving,
isClapping,
formatDate,
extractAddress,
getUserName,
getEvent,
} = require('./utils.js');
const CURRENCY = 'xDAI';
const AUTHOR = 'macaw';
const etherscanURL = 'https://blockscout.com/poa/xdai/api';
const params = {
module: 'account',
action: 'txlist',
address: owlAddress,
startblock: 0,
endblock: 99999999,
sort: 'asc',
apikey: etherscanKey,
};
const fetcher = async () => {
const url = new URL(etherscanURL);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
const response = await fetch(url);
if (!response.ok) {
throw new Error(response);
}
const data = await response.json();
if (data.message !== 'OK') {
throw new Error(data);
}
return data.result;
}
const getRecords = async () => {
const records = [];
const txList = await fetcher();
for (const tx of txList) {
if (tx.confirmations < 6) {
continue;
}
const receiving = await isReceiving(tx.to);
const clapping = isClapping(tx.input);
const date = formatDate(tx.timeStamp);
const event = receiving ? '' : await getEvent(tx.to);
const userAddress = receiving ? tx.from : extractAddress(tx.input);
const userName = await getUserName(userAddress) || userAddress;
const creditReceiving = userName === 'deploy' ? 'activo' : 'pasivo';
let description = receiving
? 'fund owl'
: clapping
? 'aplausos'
: 'inscripción saliente';
if (tx.isError == '1') {
description += ' (error)';
}
const txFeeWei = tx.gasUsed * tx.gasPrice;
const txFee = web3Utils.fromWei(String(txFeeWei));
const debit = receiving ? 'activo:owl' : 'gasto:cambio';
const credit = receiving ? `${creditReceiving}:${userName}` : 'activo:owl';
const record = {
date,
event,
userName,
description,
amount: web3Utils.fromWei(tx.value),
currency: CURRENCY,
debit,
credit,
author: AUTHOR,
};
if (record.amount != 0 && tx.isError == '0') {
records.push(record);
}
const feeRecord = {
date,
event,
userName,
description: `tarifa ${description}`,
amount: txFee,
currency: CURRENCY,
debit: 'gasto:tarifas:xdai',
credit,
author: AUTHOR,
};
records.push(feeRecord);
}
return records;
}
module.exports = { getRecords };