-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Wallet } from "ethers"; | ||
import { Multicall2__factory } from "../token-bridge-contracts/build/types"; | ||
import { SecurityCouncilNomineeElectionGovernor__factory } from "../typechain-types"; | ||
|
||
export class SecurityCouncilElectionCreator { | ||
retryTime = 10 * 1000; | ||
public constructor( | ||
public readonly connectedSigner: Wallet, | ||
public readonly nomineeElectionGovAddress: string, | ||
public readonly multicallAddress: string | ||
) {} | ||
|
||
public async checkAndCreateElection() { | ||
const multicall = Multicall2__factory.connect( | ||
this.multicallAddress, | ||
this.connectedSigner.provider | ||
); | ||
const gov = SecurityCouncilNomineeElectionGovernor__factory.connect( | ||
this.nomineeElectionGovAddress, | ||
this.connectedSigner.provider | ||
); | ||
const parentChainTimestamp = await multicall.getCurrentBlockTimestamp(); | ||
const electionTimestamp = await gov.electionToTimestamp(await gov.electionCount()); | ||
|
||
const timeToElectionSeconds = electionTimestamp.sub(parentChainTimestamp).toNumber(); | ||
if (timeToElectionSeconds <= 0) { | ||
const res = await gov.createElection(); | ||
await res.wait(); | ||
setTimeout(this.run, this.retryTime); | ||
} else { | ||
console.log(`Next election starts in ${timeToElectionSeconds} seconds`); | ||
setTimeout(this.run, Math.max(timeToElectionSeconds * 1000, this.retryTime)); | ||
} | ||
} | ||
|
||
public async run() { | ||
try { | ||
this.checkAndCreateElection(); | ||
} catch (e) { | ||
console.log("SecurityCouncilElectionCreator error:", e); | ||
setTimeout(this.run, this.retryTime); | ||
} | ||
} | ||
} |