From 7dd27f90cbd09c90d0d6f7a02a0f5cb23b063a74 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 10 Aug 2023 15:02:54 +0200 Subject: [PATCH] Let main drand loop trigger JobsObserver --- jobs.ts | 44 ++++++++++++++++++++------------------------ main.ts | 22 ++++++++++++++++++---- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/jobs.ts b/jobs.ts index 352f2f4..2f14fb3 100644 --- a/jobs.ts +++ b/jobs.ts @@ -21,37 +21,33 @@ function parseRound(job: Job): number { return Uint53.fromString(round).toNumber(); } +function formatDuration(durationInMs: number): string { + const inSeconds = durationInMs / 1000; + return `${inSeconds.toFixed(1)}s`; +} + export class JobsObserver { - private readonly client: CosmWasmClient; + private readonly noisClient: CosmWasmClient; private readonly gateway: string; - private readonly abort: AbortController; - private readonly intervalId: number; public constructor( noisClient: CosmWasmClient, gatewayAddress: string, - abortController: AbortController, - pollInterval = 1000, ) { - this.client = noisClient; + this.noisClient = noisClient; this.gateway = gatewayAddress; - this.abort = abortController; - - this.intervalId = setInterval(() => { - const query = { jobs_desc: { offset: null, limit: 3 } }; - this.client.queryContractSmart(this.gateway, query).then( - ({ jobs }: JobsResponse) => { - if (jobs.length === 0) return; // Nothing to do for us - - const rounds = jobs.map(parseRound); - const roundInfos = rounds.map((round) => { - const due = timeOfRound(round) - Date.now() / 1000; - return `#${round} (due ${due.toFixed(1)}s)`; - }); - console.log(`Jobs pending for rounds: %c${roundInfos.join(", ")}`, "color: orange"); - }, - (err) => console.error(err), - ); - }, pollInterval); + } + + public async check(): Promise { + const query = { jobs_desc: { offset: null, limit: 3 } }; + const { jobs }: JobsResponse = await this.noisClient.queryContractSmart(this.gateway, query); + if (jobs.length === 0) return; // Nothing to do for us + + const rounds = jobs.map(parseRound); + const roundInfos = rounds.map((round) => { + const due = timeOfRound(round) - Date.now(); + return `#${round} (due ${formatDuration(due)})`; + }); + console.log(`Jobs pending for rounds: %c${roundInfos.join(", ")}`, "color: orange"); } } diff --git a/main.ts b/main.ts index edc4350..22be5ba 100644 --- a/main.ts +++ b/main.ts @@ -118,13 +118,14 @@ if (import.meta.main) { await Promise.all([ sleep(500), // the min waiting time (async function () { - const listed = await queryIsAllowListed(client, config.contract, botAddress); + const listed = await queryIsAllowListed(client, config.drandAddress, botAddress); console.info(`Bot allow listed for rewards: ${listed}`); })(), ]); + let jobs: JobsObserver | undefined; if (config.gatewayAddress) { - const _jobs = new JobsObserver(client, config.gatewayAddress, new AbortController()); + jobs = new JobsObserver(client, config.gatewayAddress); } // Initialize local sign data @@ -148,7 +149,7 @@ if (import.meta.main) { // enough for the query to finish. In case the query is not yet done, // we can wait for the promise to be resolved. // console.log(`Now : ${new Date().toISOString()}\nPublish time: ${new Date(timeOfRound(round)).toISOString()}`); - const promise = queryIsIncentivized(client, config.contract, [m], botAddress).then( + const promise = queryIsIncentivized(client, config.drandAddress, [m], botAddress).then( (incentivized) => !!incentivized[0], (_err) => false, ); @@ -164,11 +165,24 @@ if (import.meta.main) { gasLimitAddBeacon, gasPrice: config.gasPrice, botAddress, - drandAddress: config.contract, + drandAddress: config.drandAddress, userAgent, incentivizedRounds, }, beacon); + // Check jobs every 1.5s, shifted 1200ms from the drand receiving + const shift = 1200; + setTimeout(() => + jobs?.check().then( + (_) => {}, + (err) => console.error(err), + ), shift); + setTimeout(() => + jobs?.check().then( + (_) => {}, + (err) => console.error(err), + ), shift + 1500); + if (didSubmit) { // Some seconds after the submission when things are idle, check and log // the balance of the bot.