Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rewards distribution cli improvements #55

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 24 additions & 84 deletions scripts/rewards-distribution/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { prompt, type QuestionCollection } from "inquirer";
import {
ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS,
RUNE_DECIMALS,
Expand All @@ -7,85 +6,35 @@ import { fromBaseUnit, getLogsChunked, toBaseUnit } from "./helpers";
import { publicClient } from "./client";
import { calculateRewards } from "./calculateRewards/calculateRewards";
import { stakingV1Abi } from "./generated/abi-types";
import assert from "assert";
import { validateRewardsDistribution } from "./validation";
import {
confirmResponses,
inquireBlockRange,
inquireTotalRuneAmountToDistroBaseUnit,
} from "./input";

const inquireBlockRange = async (): Promise<{
fromBlock: bigint;
toBlock: bigint;
}> => {
const questions: QuestionCollection<{
fromBlock: number;
toBlock: number;
}> = [
{
type: "number",
name: "fromBlock",
message: "What is the START block number of this epoch?",
default: 216083216, // TODO: remove this default
},
{
type: "number",
name: "toBlock",
message: "What is the END block number of this epoch?",
default: 216092990, // TODO: remove this default
},
];

const { fromBlock, toBlock } = await prompt(questions);
assert(fromBlock < toBlock, "Start block must be less than end block");
return { fromBlock: BigInt(fromBlock), toBlock: BigInt(toBlock) };
};
const main = async () => {
const [currentBlock, [initLog]] = await Promise.all([
publicClient.getBlock({
blockTag: "latest",
}),
publicClient.getContractEvents({
address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS,
abi: stakingV1Abi,
eventName: "Initialized",
fromBlock: "earliest",
toBlock: "latest",
}),
]);

const inquireTotalRuneAmountToDistroBaseUnit = async (): Promise<bigint> => {
const questions: QuestionCollection<{ totalRuneAmountPrecision: number }> = [
{
type: "number",
name: "totalRuneAmountPrecision",
message:
"What is the total amount of RUNE to distribute this epoch? Enter this amount in RUNE, not in base units (RUNE*10^8).",
},
];
const contractCreationBlockNumber = initLog.blockNumber;
const currentBlockNumber = currentBlock.number;

const { totalRuneAmountPrecision } = await prompt(questions);
console.log(totalRuneAmountPrecision);
const totalRuneAmountBaseUnit = toBaseUnit(
totalRuneAmountPrecision,
RUNE_DECIMALS,
const { fromBlock, toBlock } = await inquireBlockRange(
contractCreationBlockNumber,
currentBlockNumber,
);

return totalRuneAmountBaseUnit;
};

const confirmResponses = async (
fromBlock: bigint,
toBlock: bigint,
totalRuneAmountToDistroBaseUnit: number,
) => {
const questions: QuestionCollection<{ confirm: boolean }> = [
{
type: "confirm",
name: "confirm",
message: [
"Do you want to proceed with these values?",
`* Start block: ${fromBlock}`,
`* End block: ${toBlock}`,
`* Total RUNE to distribute: ${totalRuneAmountToDistroBaseUnit} RUNE`,
].join("\n"),
},
];

const { confirm } = await prompt(questions);

if (!confirm) {
console.log("Exiting...");
process.exit(0);
}
};

const main = async () => {
const { fromBlock, toBlock } = await inquireBlockRange();

const totalRuneAmountToDistroBaseUnit =
await inquireTotalRuneAmountToDistroBaseUnit();

Expand All @@ -95,24 +44,15 @@ const main = async () => {
fromBaseUnit(totalRuneAmountToDistroBaseUnit, RUNE_DECIMALS),
);

const [previousEpochEndBlock, epochEndBlock, [initLog]] = await Promise.all([
const [previousEpochEndBlock, epochEndBlock] = await Promise.all([
publicClient.getBlock({
blockNumber: fromBlock - 1n,
}),
publicClient.getBlock({
blockNumber: toBlock,
}),
publicClient.getContractEvents({
address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS,
abi: stakingV1Abi,
eventName: "Initialized",
fromBlock: "earliest",
toBlock: "latest",
}),
]);

const contractCreationBlockNumber = initLog.blockNumber;

const contractCreationBlock = await publicClient.getBlock({
blockNumber: contractCreationBlockNumber,
});
Expand Down
129 changes: 129 additions & 0 deletions scripts/rewards-distribution/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { prompt, type QuestionCollection } from "inquirer";
import { RUNE_DECIMALS } from "./constants";
import { toBaseUnit } from "./helpers";

const validatePositiveNumber = (value: number) => {
if (isNaN(value)) {
return "Please enter a valid number";
}

if (value < 0) {
return "Please enter a positive value";
}

return true;
};

const validatePositiveInteger = (value: number) => {
if (!Number.isInteger(value)) {
return "Please enter an integer";
}

return validatePositiveNumber(value);
};

const createValidateBlockNumber = (
minimumBlockNumber: bigint,
maximumBlockNumber: bigint,
) => {
return (value: number) => {
if (value < minimumBlockNumber) {
return `Value must be greater than or equal to ${minimumBlockNumber}`;
}
if (value > maximumBlockNumber) {
return `Value must be less than or equal to ${maximumBlockNumber}`;
}
return validatePositiveInteger(value);
};
};

export const inquireBlockRange = async (
minimumBlockNumber: bigint,
maximumBlockNumber: bigint,
): Promise<{
fromBlock: bigint;
toBlock: bigint;
}> => {
const validateBlockNumber = createValidateBlockNumber(
minimumBlockNumber,
maximumBlockNumber,
);

const questions: QuestionCollection<{
fromBlock: number;
toBlock: number;
}> = [
{
type: "number",
name: "fromBlock",
validate: validateBlockNumber,
message: "What is the START block number of this epoch?",
default: 216083216, // TODO: remove this default
},
{
type: "number",
name: "toBlock",
validate: (value: number, answers: { fromBlock: number }) => {
if (value <= answers.fromBlock) {
return "'to' block must be greater than 'from' block";
}
return validateBlockNumber(value);
},
message: "What is the END block number of this epoch?",
default: 216092990, // TODO: remove this default
},
];

const { fromBlock, toBlock } = await prompt(questions);

return { fromBlock: BigInt(fromBlock), toBlock: BigInt(toBlock) };
};

export const inquireTotalRuneAmountToDistroBaseUnit =
async (): Promise<bigint> => {
const questions: QuestionCollection<{ totalRuneAmountPrecision: number }> =
[
{
type: "number",
name: "totalRuneAmountPrecision",
validate: validatePositiveNumber,
message:
"What is the total amount of RUNE to distribute this epoch? Enter this amount in RUNE, not in base units (RUNE*10^8).",
},
];

const { totalRuneAmountPrecision } = await prompt(questions);
console.log(totalRuneAmountPrecision);
const totalRuneAmountBaseUnit = toBaseUnit(
totalRuneAmountPrecision,
RUNE_DECIMALS,
);

return totalRuneAmountBaseUnit;
};

export const confirmResponses = async (
fromBlock: bigint,
toBlock: bigint,
totalRuneAmountToDistroBaseUnit: number,
) => {
const questions: QuestionCollection<{ confirm: boolean }> = [
{
type: "confirm",
name: "confirm",
message: [
"Do you want to proceed with these values?",
`* Start block: ${fromBlock}`,
`* End block: ${toBlock}`,
`* Total RUNE to distribute: ${totalRuneAmountToDistroBaseUnit} RUNE`,
].join("\n"),
},
];

const { confirm } = await prompt(questions);

if (!confirm) {
console.log("Exiting...");
process.exit(0);
}
};