forked from ArbitrumFoundation/governance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurityCouncilElectionTracker.ts
71 lines (63 loc) · 2.52 KB
/
securityCouncilElectionTracker.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
import { Wallet, BigNumber } from "ethers";
import { SecurityCouncilNomineeElectionGovernor__factory } from "../typechain-types";
import { JsonRpcProvider } from "@ethersproject/providers";
import { getL1BlockNumberFromL2 } from "./utils";
export class SecurityCouncilElectionTracker {
public readonly retryTime = 10 * 1000;
public constructor(
public readonly govChainProvider: JsonRpcProvider,
public readonly parentChainProvider: JsonRpcProvider,
public readonly nomineeElectionGovAddress: string,
public readonly connectedSigner?: Wallet
) {}
public async checkAndCreateElection() {
const gov = SecurityCouncilNomineeElectionGovernor__factory.connect(
this.nomineeElectionGovAddress,
this.govChainProvider
);
const l1BlockNumber = await getL1BlockNumberFromL2(this.govChainProvider);
const { timestamp: parentChainTimestamp } = await this.parentChainProvider.getBlock(
l1BlockNumber.toNumber()
);
const electionTimestamp = await gov.electionToTimestamp(await gov.electionCount());
const timeToElectionSeconds = electionTimestamp.sub(parentChainTimestamp).toNumber();
if (timeToElectionSeconds <= 0) {
console.log("Ready to Create Election");
if (this.connectedSigner) {
const govWriter = SecurityCouncilNomineeElectionGovernor__factory.connect(
this.nomineeElectionGovAddress,
this.connectedSigner
);
const res = await govWriter.createElection();
await res.wait();
}
setTimeout(this.run.bind(this), this.retryTime);
} else {
console.log(`Next election can be initiated at timestamp ${electionTimestamp}; that's in ${secondsToString(timeToElectionSeconds)}`);
setTimeout(
this.run.bind(this),
Math.max(
Math.min(timeToElectionSeconds * 1000 / 2, 2147483647 /**32 bit int max */),
this.retryTime
)
);
}
}
public async run() {
try {
this.checkAndCreateElection();
} catch (e) {
console.log("SecurityCouncilElectionCreator error:", e);
setTimeout(this.run.bind(this), this.retryTime);
}
}
}
function secondsToString(seconds: number) {
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
return (
numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds"
);
}