-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.js
134 lines (118 loc) · 4.27 KB
/
initialize.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const { Web3 } = require("web3");
const { createPublicClient, http, parseAbi } = require("viem");
const { CloneFactory } = require("contracts-js");
const { ContractsLoader } = require("./src/ContractsLoader");
const { ContractsInMemoryIndexer } = require("./src/ContractsInMemoryIndexer");
const { ContractMapper } = require("./src/ContractMapper");
/**
*
* @param {import('viem').PublicClient} client
* @param {ContractsLoader} loader
* @param {any} config
* @param {(id, blockNumber) => void} onLogUpdate
* @returns {Promise<void>}
*/
const startWatch = async (client, loader, config, onLogUpdate) => {
try {
const contractAddresses = await loader.getContractList();
const addresses = [config.CLONE_FACTORY_ADDRESS, ...contractAddresses];
const cloneFactoryEvents = [
"contractCreated",
"clonefactoryContractPurchased",
"contractDeleteUpdated",
"purchaseInfoUpdated",
];
const eventsAbi = [
// Clone Factory Events
"event contractCreated(address indexed _address, string _pubkey)",
"event clonefactoryContractPurchased(address indexed _address, address indexed _validator)",
"event contractDeleteUpdated(address _address, bool _isDeleted)",
"event purchaseInfoUpdated(address indexed _address)",
// Implementation Events
"event closedEarly(uint8 reason)",
"event fundsClaimed()",
"event destinationUpdated(string newValidatorURL, string newDestURL)",
];
const unwatch = client.watchEvent({
address: addresses,
events: parseAbi(eventsAbi),
poll: true,
pollingInterval: 1000,
onLogs: (logs) => {
console.log(`Received logs: ${logs.length}`);
logs.forEach((log) => {
const { eventName, args, address, blockNumber } = log;
let contractAddress = null;
if (cloneFactoryEvents.includes(eventName)) {
contractAddress = args._address;
} else {
contractAddress = address;
}
console.log(`Received log for contract: ${contractAddress}`);
onLogUpdate(contractAddress, Number(blockNumber));
if (eventName === "contractCreated") {
console.log('Got contract created event, restating watch')
unwatch();
startWatch(client, loader, config, onLogUpdate);
}
});
},
onError: (error) => {
console.error("On Error Callback", error);
process.exit(1);
}
});
console.log(
`Started listen events for contracts: ${JSON.stringify(addresses)}, amount: ${addresses.length}`
);
return { addresses };
} catch (err) {
console.error("Error starting watch", err);
process.exit(1);
}
};
const initialize = async (config) => {
const httpEthNodeUrl = config.ETH_NODE_URL;
const client = createPublicClient({
transport: http(httpEthNodeUrl, {
retryCount: 10,
retryInterval: 1000,
}),
});
const web3 = new Web3(httpEthNodeUrl);
const cloneFactory = CloneFactory(web3, config.CLONE_FACTORY_ADDRESS);
const indexer = ContractsInMemoryIndexer.getInstance(new ContractMapper());
const loader = new ContractsLoader(web3, cloneFactory);
/**
*
* @param {string} contractId
* @param {number} blockNumber
* @param {number} [retryCount]
*/
const onEventUpdate = async (contractId, blockNumber, retryCount = 0) => {
try {
await new Promise((resolve) => setTimeout(resolve, retryCount * 1000));
const contract = await loader.getContract(contractId);
indexer.upsert(contractId, contract, blockNumber);
} catch (error) {
console.error(`Error updating contract ${contractId}, error: `, error, `retryCount: ${retryCount}`);
if (retryCount <= 10) {
onEventUpdate(contractId, blockNumber, retryCount + 1);
}
}
};
/**
*
* @param {string} contractId
* @param {Contract} contract
* @param {import("contracts-js").ImplementationContext} implInstance
* @param {number} blockNumber
*/
const onContractLoad = (contractId, contract, implInstance, blockNumber) => {
indexer.upsert(contractId, contract, blockNumber);
};
await startWatch(client, loader, config, onEventUpdate);
await loader.loadAll(onContractLoad);
console.log("All Contracts loaded");
};
module.exports = { initialize };