-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
235 lines (195 loc) · 6.35 KB
/
index.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import BotSwarm, { Task } from "@federationwtf/botswarm";
import {
FederationNounsGovernor,
FederationNounsRelayer,
NounsDAOLogicV3,
} from "@federationwtf/botswarm/contracts";
import Relic from "@relicprotocol/client";
import { ethers } from "ethers";
import { Provider } from "zksync-web3";
import { parseAbiItem, parseEventLogs } from "viem";
const { Ethereum, log } = BotSwarm();
const mainnetProvider = new ethers.providers.JsonRpcProvider(
process.env.MAINNET_RPC_URL
);
const mainnetSigner = new ethers.Wallet(
process.env.ETHEREUM_PRIVATE_KEY as string
).connect(mainnetProvider);
const zkSyncProvider = new Provider("https://mainnet.era.zksync.io");
const relic = await Relic.RelicClient.fromProviders(
zkSyncProvider,
mainnetProvider
);
async function publishBlockHash(block: number, wait: boolean) {
const canUseBlock = await relic.blockHistory.canVerifyBlock(block);
if (canUseBlock) {
log.warn(`Block ${block} is already verified on Relic, skipping`);
return;
}
log.active(`Publishing block hash to Relic for block ${block}`);
const blockHash = await mainnetProvider.getBlock(block).then((b) => b.hash);
console.log("block hash", blockHash);
const txData = await relic.bridge.sendBlock(blockHash);
console.log("txData", txData);
const tx = await mainnetSigner.sendTransaction(txData);
console.log("tx", tx);
if (wait) await relic.bridge.waitUntilBridged(blockHash);
log.success(
`Published block hash to Relic for block ${block} in tx ${tx.hash}`
);
}
const { addTask, watch, read, clients, contracts, schedule } = Ethereum({
contracts: {
FederationNounsGovernor,
FederationNounsRelayer,
NounsDAOLogicV3,
},
hooks: {
getBlockProof: async (task) => {
log.active(`Getting block proof for block ${task.execute.args[1]}`);
// This takes like 15 min
await publishBlockHash(Number(task.execute.args[1]), true);
const { hash } = await clients.mainnet.getTransaction({
blockNumber: task.execute.args[1],
index: 0,
});
const receipt = await mainnetProvider.getTransactionReceipt(hash);
const { proof } = await relic.transactionProver.getProofData(receipt);
return {
...task,
execute: { ...task.execute, args: [task.execute.args[0], proof] },
} satisfies Task;
},
getMessageProof: async (task) => {
const [proposalId] = task.execute.args;
const event = await (async () => {
const logs = await clients.zkSync.getLogs({
address: contracts.FederationNounsGovernor.deployments.zkSync,
event: parseAbiItem(
"event VotesSettled(uint256 proposal, uint256 forVotes, uint256 againstVotes, uint256 abstainVotes)"
),
fromBlock: 0n,
toBlock: await clients.zkSync.getBlockNumber(),
});
for (const log of logs) {
const tx = await clients.zkSync.getTransactionReceipt({
hash: log.transactionHash,
});
const events = parseEventLogs({
abi: FederationNounsGovernor.abi,
logs: tx.logs,
});
for (const event of events) {
if (
event.address ===
contracts.FederationNounsGovernor.deployments.zkSync
) {
if (event.eventName === "VotesSettled") {
if (event.args.proposal === proposalId) {
return event;
}
}
}
}
}
})();
if (!event) {
throw new Error(`No log found for prop ${proposalId}`);
}
const encodedMessage = ethers.utils.AbiCoder.prototype.encode(
["uint256", "uint256", "uint256", "uint256"],
[
Number(event.args.proposal),
Number(event.args.forVotes),
Number(event.args.againstVotes),
Number(event.args.abstainVotes),
]
) as `0x${string}`;
const { l1BatchNumber, l1BatchTxIndex, blockNumber } =
await zkSyncProvider.getTransactionReceipt(event.transactionHash);
console.log(l1BatchNumber, l1BatchTxIndex, blockNumber);
const messageHash = ethers.utils.keccak256(encodedMessage);
console.log("messageHash", messageHash);
const proofInfo = await zkSyncProvider.getMessageProof(
blockNumber,
contracts.FederationNounsGovernor.deployments.zkSync,
messageHash
);
console.log("proofInfo", proofInfo);
if (!proofInfo) {
throw new Error("No proof found");
}
return {
...task,
execute: {
...task.execute,
args: [
l1BatchNumber,
proofInfo.id,
l1BatchTxIndex,
encodedMessage,
proofInfo.proof,
],
},
} satisfies Task;
},
},
scripts: {
publishBlockHash,
},
privateKey: process.env.ETHEREUM_PRIVATE_KEY as string,
});
watch(
{
contract: "NounsDAOLogicV3",
chain: "mainnet",
event: "ProposalCreated",
},
async (event) => {
const [, , , , , , , , , , , castWindow, finalityBlocks] = await read({
contract: "FederationNounsGovernor",
chain: "zkSync",
functionName: "config",
});
// Publish proposal startBlock hash to Relic
schedule({
name: "publishBlockHash",
block: Number(event.args.startBlock) + 150, // 150 blocks is ~30 minutes and finality is ~15 minutes
chain: "mainnet",
args: [Number(event.args.startBlock), false],
});
// Settle votes on the governor
addTask({
schedule: {
block: event.args.endBlock - (castWindow + finalityBlocks) + 150n, // 150 blocks is ~30 minutes and finality is ~15 minutes
chain: "mainnet",
},
execute: {
hooks: ["getBlockProof"],
contract: "FederationNounsGovernor",
chain: "zkSync",
functionName: "settleVotes",
args: [
event.args.id,
// @ts-ignore
event.args.endBlock - (castWindow + finalityBlocks),
],
},
});
// Relay votes to the DAO
addTask({
schedule: {
block: event.args.endBlock - castWindow / 2n,
chain: "mainnet",
},
execute: {
hooks: ["getMessageProof"],
contract: "FederationNounsRelayer",
chain: "mainnet",
functionName: "relayVotes",
// @ts-ignore
args: [event.args.id],
},
});
}
);