This repository was archived by the owner on Dec 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathdecodeProjectEvents.js
More file actions
101 lines (87 loc) · 3.26 KB
/
decodeProjectEvents.js
File metadata and controls
101 lines (87 loc) · 3.26 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const Web3 = require('web3');
const LPPCappedMilestoneArtifact = require('lpp-capped-milestone/build/LPPCappedMilestone.json');
const LPMilestoneArtifact = require('lpp-milestones/build/LPMilestone.json');
const BridgedMilestoneArtifact = require('lpp-milestones/build/BridgedMilestone.json');
const LiquidPledgingArtifact = require('giveth-liquidpledging/build/LiquidPledging.json');
const logger = require('winston');
const topicsFromArtifacts = require('../../src/blockchain/lib/topicsFromArtifacts');
const eventDecodersFromArtifact = require('../../src/blockchain/lib/eventDecodersFromArtifact');
const toWrapper = require('../../src/utils/to');
const configFileName = 'default'; // config file name
// eslint-disable-next-line import/no-dynamic-require
const config = require(`../../config/${configFileName}.json`);
const { nodeUrl } = config.blockchain;
const instantiateWeb3 = url => {
const provider =
url && url.startsWith('ws')
? new Web3.providers.WebsocketProvider(url, {
clientConfig: {
maxReceivedFrameSize: 100000000,
maxReceivedMessageSize: 100000000,
},
})
: url;
return new Web3(provider);
};
async function getEvent(web3, txHash) {
const decoders = {
...eventDecodersFromArtifact(LPPCappedMilestoneArtifact),
...eventDecodersFromArtifact(LPMilestoneArtifact),
...eventDecodersFromArtifact(BridgedMilestoneArtifact),
...eventDecodersFromArtifact(LiquidPledgingArtifact),
};
const [err, receipt] = await toWrapper(web3.eth.getTransactionReceipt(txHash));
if (err || !receipt) {
logger.error('Error fetching transaction, or no tx receipt found ->', err, receipt);
return undefined;
}
const topics = topicsFromArtifacts(
[
LiquidPledgingArtifact,
LPPCappedMilestoneArtifact,
LPMilestoneArtifact,
BridgedMilestoneArtifact,
],
[
'ProjectAdded',
'CancelProject',
'MilestoneCompleteRequested',
'MilestoneCompleteRequestRejected',
'MilestoneCompleteRequestApproved',
'MilestoneChangeReviewerRequested',
'MilestoneReviewerChanged',
'MilestoneChangeCampaignReviewerRequested',
'MilestoneCampaignReviewerChanged',
'MilestoneChangeRecipientRequested',
'MilestoneRecipientChanged',
'RequestReview',
'RejectCompleted',
'ApproveCompleted',
'ReviewerChanged',
'RecipientChanged',
'PaymentCollected',
],
);
// get logs we're interested in.
const logs = receipt.logs.filter(log => topics.some(t => t.hash === log.topics[0]));
if (logs.length === 0) return undefined;
const [log] = logs;
// const { data } = log;
// // Just keep homeTx
// log.data = `0x${'0'.repeat(128)}${data.substring(130, 130 + 64)}${'0'.repeat(
// data.length - 2 - 128 - 64,
// )}`;
const topic = topics.find(t => t.hash === log.topics[0]);
const event = decoders[topic.name](log);
return event;
}
const main = async txHash => {
const web3 = await instantiateWeb3(nodeUrl);
return getEvent(web3, txHash);
};
const terminateScript = (message = '', code = 0) =>
process.stdout.write(`${message}\n`, () => process.exit(code));
main(process.argv[2]).then(event => {
if (event) terminateScript(`Event: ${JSON.stringify(event, null, 2)}`);
else terminateScript('', 1);
});