diff --git a/scripts/lottery/play.ts b/scripts/lottery/play.ts index 4ce7267..f3110e1 100644 --- a/scripts/lottery/play.ts +++ b/scripts/lottery/play.ts @@ -31,7 +31,8 @@ async function main() { value: minBet, from: userAddress, }); - console.log(tx); + await tx.wait() + console.log("player", userAddress, "placed bet", minBet); } catch (err) { console.error(err); } diff --git a/services/admin.ts b/services/admin.ts index 60d5026..b1ad385 100644 --- a/services/admin.ts +++ b/services/admin.ts @@ -1,5 +1,5 @@ /* Imports: External */ -import {Contract, Wallet, BigNumber, providers} from 'ethers' +import {BigNumber, Contract, Wallet, providers} from 'ethers' import fs from "fs"; import {promisify} from "util"; import {exec} from "child_process"; @@ -57,6 +57,7 @@ const emptyAddress = '0x0000000000000000000000000000000000000000' const bytes32Zero = "0x" + "0".repeat(64); const gasLimitLow = 500000 const gasLimitHigh = 3000000 +const zero = BigNumber.from(0); export class AdminZkRandService extends BaseService { constructor(options: AdminZkRandOptions) { @@ -110,7 +111,7 @@ export class AdminZkRandService extends BaseService { async _start(): Promise { console.log('\n------------------------------ admin starts ------------------------------') - let adminFromContract = await this.state.zkRandContract.owner() + const adminFromContract = await this.state.zkRandContract.owner() console.log("admin in contract:", adminFromContract) if (adminFromContract !== this.options.l2Wallet.address) { throw new Error( @@ -118,8 +119,7 @@ export class AdminZkRandService extends BaseService { ) } - - let currentIndexFromContract = await this.state.zkRandContract.currentIndex() + const currentIndexFromContract = await this.state.zkRandContract.currentIndex() console.log("currentIndexFromContract", currentIndexFromContract) if (currentIndexFromContract != this.options.numberMembers) { @@ -163,7 +163,7 @@ export class AdminZkRandService extends BaseService { while (this.running) { try { - let contractPhase = await this.state.zkRandContract.contractPhase() + const contractPhase = await this.state.zkRandContract.contractPhase() console.log("contractPhase", contractPhase) if (contractPhase == Status.Registered) { // all the nodes have registered; start nidkg @@ -174,29 +174,29 @@ export class AdminZkRandService extends BaseService { // nidkg has completed; calculate global public parameters await this.createGpp() } else if (contractPhase == Status.Ready) { - let currentRoundNum = await this.state.zkRandContract.currentRoundNum() + const currentRoundNum: BigNumber = await this.state.zkRandContract.currentRoundNum() console.log("currentRoundNum", currentRoundNum.toString()) if (Date.now() < this.state.startDate) { const begin = new Date(this.state.startDate); console.log("randomness generation will begin at", begin.toUTCString()) } else { - if (currentRoundNum == 0) { + if (currentRoundNum.eq(zero)) { // random generation starts from 1 await this.initiateRand() } else { - let submissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRoundNum) - let roundToRandom = await this.state.zkRandContract.roundToRandom(currentRoundNum) + const submissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRoundNum) + const roundToRandom = await this.state.zkRandContract.roundToRandom(currentRoundNum) if (roundToRandom.value === bytes32Zero && submissionCount >= this.options.threshold) { await this.createRandom(currentRoundNum) } - let secondsElapsed = Math.floor( + const secondsElapsed = Math.floor( (Date.now() - this.state.timeOfLastRound) / 1000 ) console.log('Seconds elapsed since last random initiation:', secondsElapsed) - if (secondsElapsed > this.options.randGenInterval) { + if (secondsElapsed > this.options.randGenInterval && roundToRandom.value !== bytes32Zero) { await this.initiateRand(); } } @@ -276,13 +276,13 @@ export class AdminZkRandService extends BaseService { } async check_config() { - let threshold = await this.state.zkRandContract.threshold() + const threshold = await this.state.zkRandContract.threshold() if (threshold != this.options.threshold) { throw new Error( `threshold=${this.options.threshold} does not match threshold=${threshold} from contract` ) } - let memberCountFromContract = await this.state.zkRandContract.memberCount() + const memberCountFromContract = await this.state.zkRandContract.memberCount() if (memberCountFromContract != this.options.numberMembers) { throw new Error( `number_of_members=${this.options.numberMembers} does not match number_of_members=${memberCountFromContract} from contract` @@ -308,7 +308,7 @@ export class AdminZkRandService extends BaseService { // derive global public parameters const cmd = `${this.cmdPrefix} dkg derive` console.log("running command <", cmd, ">...") - let result = await execPromise(cmd) + const result = await execPromise(cmd) console.log(result[`stderr`]) const filePath = dkgDir + "gpk.json" @@ -355,7 +355,7 @@ export class AdminZkRandService extends BaseService { this.state.zkRandContract.on(eventRandThreshold, async (roundNum, input, event) => { console.log("\nevent", eventRandThreshold, `round ${roundNum} input "${input}"`) - let memberCountFromContract = await this.state.zkRandContract.memberCount() + const memberCountFromContract = await this.state.zkRandContract.memberCount() const evals: Eval[] = [] for (let i = 0; i < memberCountFromContract; i++) { const evalFromContract = await this.state.zkRandContract.roundToEval(roundNum, i) @@ -408,9 +408,9 @@ export class AdminZkRandService extends BaseService { }); } - async createRandom(roundNum: number) { - let memberCountFromContract = await this.state.zkRandContract.memberCount() - let input = await this.state.zkRandContract.roundInput(roundNum) + async createRandom(roundNum: BigNumber) { + const memberCountFromContract = await this.state.zkRandContract.memberCount() + const input = await this.state.zkRandContract.roundInput(roundNum) const evals: Eval[] = [] for (let i = 0; i < memberCountFromContract; i++) { diff --git a/services/node.ts b/services/node.ts index 19a6da5..d6b14b9 100644 --- a/services/node.ts +++ b/services/node.ts @@ -1,5 +1,5 @@ /* Imports: External */ -import {Contract, Wallet, providers} from 'ethers' +import {BigNumber, Contract, Wallet, providers} from 'ethers' import fs from "fs"; import {promisify} from "util"; import {exec} from "child_process"; @@ -42,6 +42,7 @@ interface NodeZkRandOptions { const optionSettings = {} const gasLimitLow = 500000 const gasLimitHigh = 3000000 +const zero = BigNumber.from(0); export class NodeZkRandService extends BaseService { constructor(options: NodeZkRandOptions) { @@ -91,9 +92,9 @@ export class NodeZkRandService extends BaseService { while (this.running) { try { - let contractPhase = await this.state.zkRandContract.contractPhase() + const contractPhase = await this.state.zkRandContract.contractPhase() console.log("contractPhase", contractPhase) - let addrToNode = await this.state.zkRandContract.addrToNode(this.options.l2Wallet.address) + const addrToNode = await this.state.zkRandContract.addrToNode(this.options.l2Wallet.address) // node address has been added by the admin if (addrToNode.nodeAddress == this.options.l2Wallet.address) { @@ -109,19 +110,19 @@ export class NodeZkRandService extends BaseService { await this.submitPP() } } else if (contractPhase == Status.Ready) { - let lastRoundSubmitted = await this.state.zkRandContract.lastSubmittedRound(this.options.l2Wallet.address) + const lastRoundSubmitted: BigNumber = await this.state.zkRandContract.lastSubmittedRound(this.options.l2Wallet.address) console.log("lastRoundSubmitted", lastRoundSubmitted.toString()) - if (this.state.nidkgDerived == false && lastRoundSubmitted == 0) { + if (this.state.nidkgDerived == false && lastRoundSubmitted.eq(zero)) { await this.nidkgDerive() } - const currentRound = await this.state.zkRandContract.currentRoundNum() + const currentRound: BigNumber = await this.state.zkRandContract.currentRoundNum() console.log("currentRound", currentRound.toString()) const roundSubmissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRound) console.log("roundSubmissionCount", roundSubmissionCount.toString()) - if (currentRound > lastRoundSubmitted && roundSubmissionCount < threshold) { - await this.submitPartialEval(threshold, currentRound) + if (currentRound.gt(lastRoundSubmitted) && roundSubmissionCount < threshold) { + await this.submitPartialEval(currentRound) } } } @@ -134,13 +135,13 @@ export class NodeZkRandService extends BaseService { } async check_config() { - let threshold = await this.state.zkRandContract.threshold() + const threshold = await this.state.zkRandContract.threshold() if (threshold != this.options.threshold) { throw new Error( `threshold=${this.options.threshold} does not match threshold=${threshold} from contract` ) } - let memberCountFromContract = await this.state.zkRandContract.memberCount() + const memberCountFromContract = await this.state.zkRandContract.memberCount() if (memberCountFromContract != this.options.numberMembers) { throw new Error( `number_of_members=${this.options.numberMembers} does not match number_of_members=${memberCountFromContract} from contract` @@ -269,7 +270,7 @@ export class NodeZkRandService extends BaseService { this.state.nidkgDerived = true } - async submitPartialEval(threshold: number, currentRound: number) { + async submitPartialEval(currentRound: BigNumber) { const input = await this.state.zkRandContract.roundInput(currentRound) const index = await this.state.zkRandContract.getIndexPlus(this.options.l2Wallet.address)