Skip to content

Commit

Permalink
allow recommitment by reward bots
Browse files Browse the repository at this point in the history
  • Loading branch information
vanruch committed May 27, 2024
1 parent 8d3a718 commit 1548675
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
38 changes: 37 additions & 1 deletion packages/rewards-calculator/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,40 @@ async function sendApproveRequest(
return sendFordefiTransaction(request);
}

async function tryToRecommit(
fromBlock: number,
toBlock: number,
rewards: Rewards,
address: Hex,
commitment?: Hex,
) {
if (!commitment) return;
if (
await contracts.rewardsDistribution.read.alreadyApproved([
commitment,
address,
])
) {
return;
}
const { workerIds, rewardAmounts, stakedAmounts } = rewardsToTxArgs(rewards);
const tx = await sendCommitRequest(
BigInt(fromBlock),
BigInt(toBlock),
workerIds,
rewardAmounts,
stakedAmounts,
);
logger.log("Recommit rewards", tx);
return tx;
}

export async function approveRewards(
fromBlock: number,
toBlock: number,
rewards: Rewards,
address: Hex,
commitment?: Hex,
) {
const { workerIds, rewardAmounts, stakedAmounts } = rewardsToTxArgs(rewards);
if (
Expand All @@ -217,7 +246,14 @@ export async function approveRewards(
stakedAmounts,
]))
) {
logger.log("Cannot approve rewards", address);
const tx = await tryToRecommit(
fromBlock,
toBlock,
rewards,
address,
commitment,
);
if (!tx) logger.log("Cannot approve rewards", address);
return;
}
const tx = await sendApproveRequest(
Expand Down
2 changes: 1 addition & 1 deletion packages/rewards-calculator/src/clickhouseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class ClickhouseClient {
config.clickhouse.logsTableName
}.worker_timestamp >= '${formatDate(this.from)}' and ${
config.clickhouse.logsTableName
}.worker_timestamp <= '${formatDate(this.to)}'`;
}.worker_timestamp <= '${formatDate(this.to)}' order by query_hash`;
for await (const row of clickhouse.query(query).stream()) {
const worker = this.workers.add(row.worker_id);
await worker.processQuery(row);
Expand Down
5 changes: 4 additions & 1 deletion packages/rewards-calculator/src/rewardBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class RewardBot {
ranges.toBlock,
rewards,
this.address,
ranges.commitment,
);
if (tx) {
console.log(
Expand Down Expand Up @@ -149,6 +150,7 @@ async function approveRanges(): Promise<
shouldApprove: true;
fromBlock: number;
toBlock: number;
commitment?: Hex;
}
> {
const commitmentBlocks = (
Expand All @@ -159,10 +161,11 @@ async function approveRanges(): Promise<
),
fromBlock: 1n,
})
).map(({ args: { fromBlock, toBlock }, blockNumber }) => ({
).map(({ args: { fromBlock, toBlock, commitment }, blockNumber }) => ({
fromBlock: Number(fromBlock),
toBlock: Number(toBlock),
blockNumber: Number(blockNumber),
commitment,
}));

if (commitmentBlocks.length === 0) {
Expand Down

2 comments on commit 1548675

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.