Skip to content

Commit

Permalink
Merge pull request #62 from shapeshift/lock-number-naming
Browse files Browse the repository at this point in the history
chore: improve naming of block number variables
  • Loading branch information
0xApotheosis authored Jun 11, 2024
2 parents d608a44 + 787eaac commit 29bf2a8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
24 changes: 10 additions & 14 deletions scripts/rewards-distribution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,24 @@ const main = async () => {
const contractCreationBlockNumber = initLog.blockNumber;
const currentBlockNumber = currentBlock.number;

const { fromBlock, toBlock } = await inquireBlockRange(
contractCreationBlockNumber,
currentBlockNumber,
);
const { epochStartBlockNumber, epochEndBlockNumber } =
await inquireBlockRange(contractCreationBlockNumber, currentBlockNumber);

const totalRuneAmountToDistroBaseUnit =
await inquireTotalRuneAmountToDistroBaseUnit();

await confirmResponses(
fromBlock,
toBlock,
epochStartBlockNumber,
epochEndBlockNumber,
fromBaseUnit(totalRuneAmountToDistroBaseUnit, RUNE_DECIMALS),
);

const [previousEpochEndBlock, epochEndBlock] = await Promise.all([
publicClient.getBlock({
blockNumber: fromBlock - 1n,
blockNumber: epochStartBlockNumber - 1n,
}),
publicClient.getBlock({
blockNumber: toBlock,
blockNumber: epochEndBlockNumber,
}),
]);

Expand All @@ -64,7 +62,7 @@ const main = async () => {
const logs = await getLogsChunked(
publicClient,
contractCreationBlockNumber,
toBlock,
epochEndBlockNumber,
);

// sort logs by block number and log index, ascending
Expand All @@ -86,14 +84,14 @@ const main = async () => {
const epochEndStakingInfoByAccount = await getStakingInfoByAccount(
publicClient,
accounts,
toBlock,
epochEndBlockNumber,
);

await validateRewardsDistribution(
publicClient,
earnedRewardsByAccount,
fromBlock,
toBlock,
epochStartBlockNumber,
epochEndBlockNumber,
);

console.log("Calculating rewards distribution...");
Expand All @@ -118,8 +116,6 @@ const main = async () => {
);

console.table(tableRows);

// TODO: Confirm details again before proceeding
};

main();
41 changes: 25 additions & 16 deletions scripts/rewards-distribution/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,31 @@ export const inquireBlockRange = async (
minimumBlockNumber: bigint,
maximumBlockNumber: bigint,
): Promise<{
fromBlock: bigint;
toBlock: bigint;
epochStartBlockNumber: bigint;
epochEndBlockNumber: bigint;
}> => {
const validateBlockNumber = createValidateBlockNumber(
minimumBlockNumber,
maximumBlockNumber,
);

const validateToBlock = (value: number, answers: { fromBlock: number }) => {
if (value <= answers.fromBlock) {
const validateEpochEndBlockNumber = (
value: number,
answers: { epochStartBlockNumber: number },
) => {
if (value <= answers.epochStartBlockNumber) {
return "'to' block must be greater than 'from' block";
}
return validateBlockNumber(value);
};

const questions: QuestionCollection<{
fromBlock: number;
toBlock: number;
epochStartBlockNumber: number;
epochEndBlockNumber: number;
}> = [
{
type: "number",
name: "fromBlock",
name: "epochStartBlockNumber",
validate: validateBlockNumber,
// clear the input on validation error. Return type here is `number|undefined` not boolean (eew)
filter: (input) =>
Expand All @@ -72,19 +75,25 @@ export const inquireBlockRange = async (
},
{
type: "number",
name: "toBlock",
validate: validateToBlock,
name: "epochEndBlockNumber",
validate: validateEpochEndBlockNumber,
// clear the input on validation error. Return type here is `number|undefined` not boolean (eew)
filter: (input, answers) =>
validateToBlock(input, answers) === true ? input : undefined,
validateEpochEndBlockNumber(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);
const { epochStartBlockNumber, epochEndBlockNumber } =
await prompt(questions);

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

export const inquireTotalRuneAmountToDistroBaseUnit =
Expand Down Expand Up @@ -114,8 +123,8 @@ export const inquireTotalRuneAmountToDistroBaseUnit =
};

export const confirmResponses = async (
fromBlock: bigint,
toBlock: bigint,
epochStartBlockNumber: bigint,
epochEndBlockNumber: bigint,
totalRuneAmountToDistroBaseUnit: number,
) => {
const questions: QuestionCollection<{ confirm: boolean }> = [
Expand All @@ -124,8 +133,8 @@ export const confirmResponses = async (
name: "confirm",
message: [
"Do you want to proceed with these values?",
`* Start block: ${fromBlock}`,
`* End block: ${toBlock}`,
`* Start block: ${epochStartBlockNumber}`,
`* End block: ${epochEndBlockNumber}`,
`* Total RUNE to distribute: ${totalRuneAmountToDistroBaseUnit} RUNE`,
].join("\n"),
},
Expand Down

0 comments on commit 29bf2a8

Please sign in to comment.