Skip to content

Commit

Permalink
Merge pull request #55 from shapeshift/rewards-distribution-cli-feedback
Browse files Browse the repository at this point in the history
chore: rewards distribution cli improvements
  • Loading branch information
0xApotheosis authored May 31, 2024
2 parents 1a1fa9c + 0025954 commit 8367d52
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 84 deletions.
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
140 changes: 140 additions & 0 deletions scripts/rewards-distribution/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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 validateToBlock = (value: number, answers: { fromBlock: number }) => {
if (value <= answers.fromBlock) {
return "'to' block must be greater than 'from' block";
}
return validateBlockNumber(value);
};

const questions: QuestionCollection<{
fromBlock: number;
toBlock: number;
}> = [
{
type: "number",
name: "fromBlock",
validate: validateBlockNumber,
// clear the input on validation error. Return type here is `number|undefined` not boolean (eew)
filter: (input) =>
validateBlockNumber(input) === true ? input : undefined,
message: "What is the START block number of this epoch?",
default: 216083216, // TODO: remove this default
},
{
type: "number",
name: "toBlock",
validate: validateToBlock,
// clear the input on validation error. Return type here is `number|undefined` not boolean (eew)
filter: (input, answers) =>
validateToBlock(input, answers) === true ? input : undefined,
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,
// clear the input on validation error. Return type here is `number|undefined` not boolean (eew)
filter: (input) =>
validatePositiveNumber(input) === true ? input : undefined,
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);
}
};

0 comments on commit 8367d52

Please sign in to comment.