Skip to content

Commit

Permalink
Let main drand loop trigger JobsObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Aug 10, 2023
1 parent 76cc760 commit 7dd27f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
44 changes: 20 additions & 24 deletions jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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");
}
}
22 changes: 18 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
);
Expand All @@ -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.
Expand Down

0 comments on commit 7dd27f9

Please sign in to comment.