Skip to content

Commit 42e0c45

Browse files
committed
fix type bignumber
1 parent 96afd33 commit 42e0c45

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

scripts/lottery/play.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ async function main() {
3131
value: minBet,
3232
from: userAddress,
3333
});
34-
console.log(tx);
34+
await tx.wait()
35+
console.log("player", userAddress, "placed bet", minBet);
3536
} catch (err) {
3637
console.error(err);
3738
}

services/admin.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Imports: External */
2-
import {Contract, Wallet, BigNumber, providers} from 'ethers'
2+
import {BigNumber, Contract, Wallet, providers} from 'ethers'
33
import fs from "fs";
44
import {promisify} from "util";
55
import {exec} from "child_process";
@@ -57,6 +57,7 @@ const emptyAddress = '0x0000000000000000000000000000000000000000'
5757
const bytes32Zero = "0x" + "0".repeat(64);
5858
const gasLimitLow = 500000
5959
const gasLimitHigh = 3000000
60+
const zero = BigNumber.from(0);
6061

6162
export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
6263
constructor(options: AdminZkRandOptions) {
@@ -110,16 +111,15 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
110111
async _start(): Promise<void> {
111112
console.log('\n------------------------------ admin starts ------------------------------')
112113

113-
let adminFromContract = await this.state.zkRandContract.owner()
114+
const adminFromContract = await this.state.zkRandContract.owner()
114115
console.log("admin in contract:", adminFromContract)
115116
if (adminFromContract !== this.options.l2Wallet.address) {
116117
throw new Error(
117118
`ADMIN_PRIVATE_KEY is not set to zkRand admin ${adminFromContract}`
118119
)
119120
}
120121

121-
122-
let currentIndexFromContract = await this.state.zkRandContract.currentIndex()
122+
const currentIndexFromContract = await this.state.zkRandContract.currentIndex()
123123
console.log("currentIndexFromContract", currentIndexFromContract)
124124

125125
if (currentIndexFromContract != this.options.numberMembers) {
@@ -163,7 +163,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
163163

164164
while (this.running) {
165165
try {
166-
let contractPhase = await this.state.zkRandContract.contractPhase()
166+
const contractPhase = await this.state.zkRandContract.contractPhase()
167167
console.log("contractPhase", contractPhase)
168168
if (contractPhase == Status.Registered) {
169169
// all the nodes have registered; start nidkg
@@ -174,29 +174,29 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
174174
// nidkg has completed; calculate global public parameters
175175
await this.createGpp()
176176
} else if (contractPhase == Status.Ready) {
177-
let currentRoundNum = await this.state.zkRandContract.currentRoundNum()
177+
const currentRoundNum: BigNumber = await this.state.zkRandContract.currentRoundNum()
178178
console.log("currentRoundNum", currentRoundNum.toString())
179179
if (Date.now() < this.state.startDate) {
180180
const begin = new Date(this.state.startDate);
181181
console.log("randomness generation will begin at", begin.toUTCString())
182182
} else {
183-
if (currentRoundNum == 0) {
183+
if (currentRoundNum.eq(zero)) {
184184
// random generation starts from 1
185185
await this.initiateRand()
186186
} else {
187-
let submissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRoundNum)
188-
let roundToRandom = await this.state.zkRandContract.roundToRandom(currentRoundNum)
187+
const submissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRoundNum)
188+
const roundToRandom = await this.state.zkRandContract.roundToRandom(currentRoundNum)
189189

190190
if (roundToRandom.value === bytes32Zero && submissionCount >= this.options.threshold) {
191191
await this.createRandom(currentRoundNum)
192192
}
193193

194-
let secondsElapsed = Math.floor(
194+
const secondsElapsed = Math.floor(
195195
(Date.now() - this.state.timeOfLastRound) / 1000
196196
)
197197
console.log('Seconds elapsed since last random initiation:', secondsElapsed)
198198

199-
if (secondsElapsed > this.options.randGenInterval) {
199+
if (secondsElapsed > this.options.randGenInterval && roundToRandom.value !== bytes32Zero) {
200200
await this.initiateRand();
201201
}
202202
}
@@ -276,13 +276,13 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
276276
}
277277

278278
async check_config() {
279-
let threshold = await this.state.zkRandContract.threshold()
279+
const threshold = await this.state.zkRandContract.threshold()
280280
if (threshold != this.options.threshold) {
281281
throw new Error(
282282
`threshold=${this.options.threshold} does not match threshold=${threshold} from contract`
283283
)
284284
}
285-
let memberCountFromContract = await this.state.zkRandContract.memberCount()
285+
const memberCountFromContract = await this.state.zkRandContract.memberCount()
286286
if (memberCountFromContract != this.options.numberMembers) {
287287
throw new Error(
288288
`number_of_members=${this.options.numberMembers} does not match number_of_members=${memberCountFromContract} from contract`
@@ -308,7 +308,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
308308
// derive global public parameters
309309
const cmd = `${this.cmdPrefix} dkg derive`
310310
console.log("running command <", cmd, ">...")
311-
let result = await execPromise(cmd)
311+
const result = await execPromise(cmd)
312312
console.log(result[`stderr`])
313313

314314
const filePath = dkgDir + "gpk.json"
@@ -355,7 +355,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
355355
this.state.zkRandContract.on(eventRandThreshold, async (roundNum, input, event) => {
356356
console.log("\nevent", eventRandThreshold, `round ${roundNum} input "${input}"`)
357357

358-
let memberCountFromContract = await this.state.zkRandContract.memberCount()
358+
const memberCountFromContract = await this.state.zkRandContract.memberCount()
359359
const evals: Eval[] = []
360360
for (let i = 0; i < memberCountFromContract; i++) {
361361
const evalFromContract = await this.state.zkRandContract.roundToEval(roundNum, i)
@@ -408,9 +408,9 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
408408
});
409409
}
410410

411-
async createRandom(roundNum: number) {
412-
let memberCountFromContract = await this.state.zkRandContract.memberCount()
413-
let input = await this.state.zkRandContract.roundInput(roundNum)
411+
async createRandom(roundNum: BigNumber) {
412+
const memberCountFromContract = await this.state.zkRandContract.memberCount()
413+
const input = await this.state.zkRandContract.roundInput(roundNum)
414414

415415
const evals: Eval[] = []
416416
for (let i = 0; i < memberCountFromContract; i++) {

services/node.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Imports: External */
2-
import {Contract, Wallet, providers} from 'ethers'
2+
import {BigNumber, Contract, Wallet, providers} from 'ethers'
33
import fs from "fs";
44
import {promisify} from "util";
55
import {exec} from "child_process";
@@ -42,6 +42,7 @@ interface NodeZkRandOptions {
4242
const optionSettings = {}
4343
const gasLimitLow = 500000
4444
const gasLimitHigh = 3000000
45+
const zero = BigNumber.from(0);
4546

4647
export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
4748
constructor(options: NodeZkRandOptions) {
@@ -91,9 +92,9 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
9192

9293
while (this.running) {
9394
try {
94-
let contractPhase = await this.state.zkRandContract.contractPhase()
95+
const contractPhase = await this.state.zkRandContract.contractPhase()
9596
console.log("contractPhase", contractPhase)
96-
let addrToNode = await this.state.zkRandContract.addrToNode(this.options.l2Wallet.address)
97+
const addrToNode = await this.state.zkRandContract.addrToNode(this.options.l2Wallet.address)
9798

9899
// node address has been added by the admin
99100
if (addrToNode.nodeAddress == this.options.l2Wallet.address) {
@@ -109,19 +110,19 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
109110
await this.submitPP()
110111
}
111112
} else if (contractPhase == Status.Ready) {
112-
let lastRoundSubmitted = await this.state.zkRandContract.lastSubmittedRound(this.options.l2Wallet.address)
113+
const lastRoundSubmitted: BigNumber = await this.state.zkRandContract.lastSubmittedRound(this.options.l2Wallet.address)
113114
console.log("lastRoundSubmitted", lastRoundSubmitted.toString())
114115

115-
if (this.state.nidkgDerived == false && lastRoundSubmitted == 0) {
116+
if (this.state.nidkgDerived == false && lastRoundSubmitted.eq(zero)) {
116117
await this.nidkgDerive()
117118
}
118119

119-
const currentRound = await this.state.zkRandContract.currentRoundNum()
120+
const currentRound: BigNumber = await this.state.zkRandContract.currentRoundNum()
120121
console.log("currentRound", currentRound.toString())
121122
const roundSubmissionCount = await this.state.zkRandContract.roundSubmissionCount(currentRound)
122123
console.log("roundSubmissionCount", roundSubmissionCount.toString())
123-
if (currentRound > lastRoundSubmitted && roundSubmissionCount < threshold) {
124-
await this.submitPartialEval(threshold, currentRound)
124+
if (currentRound.gt(lastRoundSubmitted) && roundSubmissionCount < threshold) {
125+
await this.submitPartialEval(currentRound)
125126
}
126127
}
127128
}
@@ -134,13 +135,13 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
134135
}
135136

136137
async check_config() {
137-
let threshold = await this.state.zkRandContract.threshold()
138+
const threshold = await this.state.zkRandContract.threshold()
138139
if (threshold != this.options.threshold) {
139140
throw new Error(
140141
`threshold=${this.options.threshold} does not match threshold=${threshold} from contract`
141142
)
142143
}
143-
let memberCountFromContract = await this.state.zkRandContract.memberCount()
144+
const memberCountFromContract = await this.state.zkRandContract.memberCount()
144145
if (memberCountFromContract != this.options.numberMembers) {
145146
throw new Error(
146147
`number_of_members=${this.options.numberMembers} does not match number_of_members=${memberCountFromContract} from contract`
@@ -269,7 +270,7 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
269270
this.state.nidkgDerived = true
270271
}
271272

272-
async submitPartialEval(threshold: number, currentRound: number) {
273+
async submitPartialEval(currentRound: BigNumber) {
273274
const input = await this.state.zkRandContract.roundInput(currentRound)
274275
const index = await this.state.zkRandContract.getIndexPlus(this.options.l2Wallet.address)
275276

0 commit comments

Comments
 (0)