Skip to content

Commit

Permalink
fix type bignumber
Browse files Browse the repository at this point in the history
  • Loading branch information
kitounliu committed Aug 13, 2024
1 parent 96afd33 commit 42e0c45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
3 changes: 2 additions & 1 deletion scripts/lottery/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 18 additions & 18 deletions services/admin.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<AdminZkRandOptions> {
constructor(options: AdminZkRandOptions) {
Expand Down Expand Up @@ -110,16 +111,15 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
async _start(): Promise<void> {
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(
`ADMIN_PRIVATE_KEY is not set to zkRand admin ${adminFromContract}`
)
}


let currentIndexFromContract = await this.state.zkRandContract.currentIndex()
const currentIndexFromContract = await this.state.zkRandContract.currentIndex()
console.log("currentIndexFromContract", currentIndexFromContract)

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

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
Expand All @@ -174,29 +174,29 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
// 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();
}
}
Expand Down Expand Up @@ -276,13 +276,13 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
}

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`
Expand All @@ -308,7 +308,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
// 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"
Expand Down Expand Up @@ -355,7 +355,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
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)
Expand Down Expand Up @@ -408,9 +408,9 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
});
}

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++) {
Expand Down
23 changes: 12 additions & 11 deletions services/node.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -42,6 +42,7 @@ interface NodeZkRandOptions {
const optionSettings = {}
const gasLimitLow = 500000
const gasLimitHigh = 3000000
const zero = BigNumber.from(0);

export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
constructor(options: NodeZkRandOptions) {
Expand Down Expand Up @@ -91,9 +92,9 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {

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) {
Expand All @@ -109,19 +110,19 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
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)
}
}
}
Expand All @@ -134,13 +135,13 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
}

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`
Expand Down Expand Up @@ -269,7 +270,7 @@ export class NodeZkRandService extends BaseService<NodeZkRandOptions> {
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)

Expand Down

0 comments on commit 42e0c45

Please sign in to comment.