-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxyColonyRequested.ts
75 lines (66 loc) · 2.1 KB
/
proxyColonyRequested.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
import {
ContractEventsSignatures,
EventHandler,
ProxyColoniesListener,
ProxyColonyEvents,
} from '@joincolony/blocks';
import { output } from '@joincolony/utils';
import { utils } from 'ethers';
import blockManager from '~blockManager';
import rpcProvider from '~provider';
import { writeActionFromEvent } from '~utils/actions/writeAction';
import { ColonyActionType } from '@joincolony/graphql';
import { getAndSyncMultiChainInfo } from '~utils/crossChain';
// @NOTE this one listens to the ProxyColonyRequested event on the colony, not the network!
export const handleProxyColonyRequested: EventHandler = async (
event,
listener,
) => {
const { colonyAddress } = listener as ProxyColoniesListener;
if (!colonyAddress) {
output(`No colony address passed to handleProxyColonyRequested listener.`);
return;
}
const { blockNumber, transactionHash } = event;
const { agent, destinationChainId } = event.args;
const logs = await rpcProvider.getProviderInstance().getLogs({
fromBlock: blockNumber,
toBlock: blockNumber,
topics: [
[
utils.id(ContractEventsSignatures.ProxyColonyRequested),
utils.id(ContractEventsSignatures.LogMessagePublished),
],
],
});
const events = await Promise.all(
logs.map((log) =>
blockManager.mapLogToContractEvent(log, ProxyColonyEvents),
),
);
const wormholeEvent = events.find(
(event) =>
ContractEventsSignatures.LogMessagePublished === event?.signature,
);
const proxyRequestedEvent = events.find(
(event) =>
ContractEventsSignatures.ProxyColonyRequested === event?.signature,
);
if (!wormholeEvent || !proxyRequestedEvent) {
output(
`ProxyColonyRequested or LogMessagePublished are not present in the same block`,
);
return;
}
const multiChainInfoId = await getAndSyncMultiChainInfo(
wormholeEvent,
transactionHash,
Number(destinationChainId),
);
await writeActionFromEvent(event, colonyAddress, {
type: ColonyActionType.AddProxyColony,
initiatorAddress: agent,
targetChainId: destinationChainId.toNumber(),
multiChainInfoId,
});
};