From 0e481f165bba422d8e1a05bb7a49ed18e3a0ff18 Mon Sep 17 00:00:00 2001 From: Korrrba Date: Fri, 2 Feb 2024 14:32:31 +0100 Subject: [PATCH 1/3] feat: implement BlocksInWeek script task The BlocksInWeek task provides a very close approximate of number of blocks mined during one week. Supported networks: mainnnet, sepolia. Example usage: npx tsx scripts/task/task.ts BlocksInWeek --network=mainnet npx tsx scripts/task/task.ts BlocksInWeek --network=sepolia Resolves: https://github.com/sherlock-audit/2023-12-ubiquity-judging/issues/230 --- cspell.json | 1 + .../scripts/shared/constants/networks.ts | 1 + packages/contracts/scripts/task/README.md | 23 +++++++++ .../scripts/task/dollar/blocks-in-week.ts | 51 +++++++++++++++++++ packages/contracts/scripts/task/manager.ts | 7 ++- packages/contracts/scripts/task/task.ts | 3 +- 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 packages/contracts/scripts/task/README.md create mode 100644 packages/contracts/scripts/task/dollar/blocks-in-week.ts diff --git a/cspell.json b/cspell.json index 0d77427e8..07237c723 100644 --- a/cspell.json +++ b/cspell.json @@ -120,6 +120,7 @@ "rpcutil", "rustup", "Sablier", + "sepolia", "setu", "Shouldset", "Sighash", diff --git a/packages/contracts/scripts/shared/constants/networks.ts b/packages/contracts/scripts/shared/constants/networks.ts index 21a162494..9e4f7ce00 100644 --- a/packages/contracts/scripts/shared/constants/networks.ts +++ b/packages/contracts/scripts/shared/constants/networks.ts @@ -2,6 +2,7 @@ export const Networks: Record = { mainnet: 1, optimism: 10, goerli: 5, + sepolia: 11155111, }; export const FALLBACK_RPC = "https://eth.ubq.fi/v1/mainnet"; diff --git a/packages/contracts/scripts/task/README.md b/packages/contracts/scripts/task/README.md new file mode 100644 index 000000000..0ceeab005 --- /dev/null +++ b/packages/contracts/scripts/task/README.md @@ -0,0 +1,23 @@ +# Dollar task scripts + +## BlocksInWeek + +BlocksInWeek task provides a close approximate of number of blocks mined in one week. + +Usage: + +Ethereum mainnet: + +``` +npx tsx scripts/task/task.ts BlocksInWeek --network=mainnet +``` + +Sepolia: + +Ethereum mainnet: + +``` +npx tsx scripts/task/task.ts BlocksInWeek --network=sepolia +``` + +Prerequisite: set ETHERSCAN_API_KEY in .env \ No newline at end of file diff --git a/packages/contracts/scripts/task/dollar/blocks-in-week.ts b/packages/contracts/scripts/task/dollar/blocks-in-week.ts new file mode 100644 index 000000000..6d1d6e8a7 --- /dev/null +++ b/packages/contracts/scripts/task/dollar/blocks-in-week.ts @@ -0,0 +1,51 @@ +import { OptionDefinition } from "command-line-args"; + +import { Networks, TaskFuncParam } from "../../shared"; +import { EtherscanProvider } from "ethers"; + +export const optionDefinitions: OptionDefinition[] = [ + { name: "task", defaultOption: true }, + { name: "network", alias: "n", type: String }, +]; + +const func = async (params: TaskFuncParam) => { + const { args, env } = params; + const { network } = args; + + const chainId = Networks[network] ?? undefined; + if (!chainId) { + throw new Error(`Unsupported network: ${network} Please configure it out first`); + } + + const provider = new EtherscanProvider(chainId, env.etherscanApiKey); + + console.log(`Calculating number of blocks in the last week...`); + const secondsInAWeek = 604800; // 24 * 7 * 60 * 60 seconds is one week + const currentBlockNumber = await provider.getBlockNumber(); + const currentBlockTimestamp = (await provider.getBlock(currentBlockNumber))?.timestamp; + const blockTimestampTwoBlocksAgo = (await provider.getBlock(currentBlockNumber - 2))?.timestamp; + + if (currentBlockTimestamp && blockTimestampTwoBlocksAgo) { + const avgBlockTime = (currentBlockTimestamp - blockTimestampTwoBlocksAgo) / 2; + console.log(`Recent average block time: ${avgBlockTime} seconds`); + + const oneWeekAgo = currentBlockTimestamp - secondsInAWeek; + const estimatedBlocksInAWeek = secondsInAWeek / avgBlockTime; + console.log(`Estimated blocks in a week best case ${estimatedBlocksInAWeek}`); + + let estimatedBlockNumber = currentBlockNumber - estimatedBlocksInAWeek; + let estimatedBlockTimestamp = (await provider.getBlock(estimatedBlockNumber))?.timestamp; + + if (estimatedBlockTimestamp) { + let deltaBlockTime = oneWeekAgo - estimatedBlockTimestamp; + estimatedBlockNumber += Math.trunc(deltaBlockTime / avgBlockTime); + estimatedBlockTimestamp = (await provider.getBlock(estimatedBlockNumber))?.timestamp || estimatedBlockTimestamp; + deltaBlockTime -= estimatedBlockTimestamp - oneWeekAgo; + + console.log(`Produced ${estimatedBlocksInAWeek - deltaBlockTime / avgBlockTime} blocks, ${deltaBlockTime / avgBlockTime} worst than the best case`); + } + } + + return "succeeded"; +}; +export default func; diff --git a/packages/contracts/scripts/task/manager.ts b/packages/contracts/scripts/task/manager.ts index 66b541df8..20a8b99bd 100644 --- a/packages/contracts/scripts/task/manager.ts +++ b/packages/contracts/scripts/task/manager.ts @@ -2,11 +2,16 @@ import { OptionDefinition } from "command-line-args"; import { TaskFuncCallBack } from "../shared"; -import PriceResetHandler, { optionDefinitions as priceResetOptions } from "./dollar/priceReset"; +import PriceResetHandler, { optionDefinitions, optionDefinitions as priceResetOptions } from "./dollar/price-reset"; +import BlocksInWeekHandler from "./dollar/blocks-in-week"; export const TASK_FUNCS: Record = { PriceReset: { handler: PriceResetHandler, options: priceResetOptions, }, + BlocksInWeek: { + handler: BlocksInWeekHandler, + options: optionDefinitions, + }, }; diff --git a/packages/contracts/scripts/task/task.ts b/packages/contracts/scripts/task/task.ts index 6acdbb273..ca54c1918 100644 --- a/packages/contracts/scripts/task/task.ts +++ b/packages/contracts/scripts/task/task.ts @@ -11,7 +11,7 @@ const main = async () => { throw new Error("You MUST put the task name in command arguments at least"); } - const envPath = path.join(__dirname, "../.env"); + const envPath = path.join(__dirname, "../../.env"); if (!fs.existsSync(envPath)) { throw new Error("Env file not found"); } @@ -26,6 +26,7 @@ const main = async () => { let args; try { args = CommandLineArgs(commandLineParseOptions); + // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { console.error(`Argument parse failed!, error: ${error}`); return; From 56207cbc0179a4186c1627bd295a71c6f414784b Mon Sep 17 00:00:00 2001 From: Korrrba Date: Fri, 2 Feb 2024 14:56:35 +0100 Subject: [PATCH 2/3] feat: update weekly block count to 49930 Set weekly block count to 49930 as measured in February 2024 npx tsx scripts/task/task.ts BlocksInWeek --network=mainnet ... Calculating number of blocks in the last week... Recent average block time: 12 seconds Estimated blocks in a week best case 50400 Produced 49930 blocks, 470 worst than the best case Resolves: https://github.com/sherlock-audit/2023-12-ubiquity-judging/issues/230 --- packages/contracts/src/dollar/libraries/LibStaking.sol | 4 ++-- .../contracts/src/dollar/upgradeInitializers/DiamondInit.sol | 2 +- packages/contracts/test/diamond/facets/ChefFacet.t.sol | 2 +- packages/contracts/test/diamond/facets/StakingFacet.t.sol | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/contracts/src/dollar/libraries/LibStaking.sol b/packages/contracts/src/dollar/libraries/LibStaking.sol index d8e5b93b3..ac3dc03d0 100644 --- a/packages/contracts/src/dollar/libraries/LibStaking.sol +++ b/packages/contracts/src/dollar/libraries/LibStaking.sol @@ -267,8 +267,8 @@ library LibStaking { // deposit new shares LibChef.deposit(msg.sender, _sharesAmount, _id); // calculate end locking period block number - // 1 week = 45361 blocks = 2371753*7/366 - // n = (block + duration * 45361) + // 1 week = 49930 blocks + // n = (block number + duration * 49930) stake.endBlock = block.number + _weeks * ss.blockCountInAWeek; // should be done after masterchef withdraw diff --git a/packages/contracts/src/dollar/upgradeInitializers/DiamondInit.sol b/packages/contracts/src/dollar/upgradeInitializers/DiamondInit.sol index 2614821ab..5499fb48f 100644 --- a/packages/contracts/src/dollar/upgradeInitializers/DiamondInit.sol +++ b/packages/contracts/src/dollar/upgradeInitializers/DiamondInit.sol @@ -71,7 +71,7 @@ contract DiamondInit is Modifiers { // staking LibStaking.StakingData storage ls = LibStaking.stakingStorage(); ls.stakingDiscountMultiplier = uint256(0.001 ether); // 0.001 - ls.blockCountInAWeek = 45361; + ls.blockCountInAWeek = 49930; // reentrancy guard _initReentrancyGuard(); diff --git a/packages/contracts/test/diamond/facets/ChefFacet.t.sol b/packages/contracts/test/diamond/facets/ChefFacet.t.sol index f8a6ac5c7..d8222a857 100644 --- a/packages/contracts/test/diamond/facets/ChefFacet.t.sol +++ b/packages/contracts/test/diamond/facets/ChefFacet.t.sol @@ -311,7 +311,7 @@ contract DepositStateChefTest is DepositStateChef { // advance the block number to staking time so the withdraw is possible uint256 currentBlock = block.number; - blocks = bound(blocks, 45361, 2 ** 128 - 1); + blocks = bound(blocks, 49930, 2 ** 128 - 1); assertEq(chefFacet.totalShares(), shares); uint256 preBal = governanceToken.balanceOf(fourthAccount); diff --git a/packages/contracts/test/diamond/facets/StakingFacet.t.sol b/packages/contracts/test/diamond/facets/StakingFacet.t.sol index f9c3968f5..e23eeed35 100644 --- a/packages/contracts/test/diamond/facets/StakingFacet.t.sol +++ b/packages/contracts/test/diamond/facets/StakingFacet.t.sol @@ -312,7 +312,7 @@ contract DepositStateTest is DepositStateStaking { // advance the block number to staking time so the withdraw is possible uint256 currentBlock = block.number; - blocks = bound(blocks, 45361, 2 ** 128 - 1); + blocks = bound(blocks, 49930, 2 ** 128 - 1); assertEq(chefFacet.totalShares(), shares); uint256 preBal = governanceToken.balanceOf(fourthAccount); From 04907d7f19ddca16f6c41c9679529c43000ccb24 Mon Sep 17 00:00:00 2001 From: Korrrba Date: Fri, 2 Feb 2024 18:13:45 +0100 Subject: [PATCH 3/3] feat: rename task function to funcBlocksInAWeek As proposed during pull request review. --- packages/contracts/scripts/task/dollar/blocks-in-week.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/contracts/scripts/task/dollar/blocks-in-week.ts b/packages/contracts/scripts/task/dollar/blocks-in-week.ts index 6d1d6e8a7..9899a07dd 100644 --- a/packages/contracts/scripts/task/dollar/blocks-in-week.ts +++ b/packages/contracts/scripts/task/dollar/blocks-in-week.ts @@ -8,7 +8,7 @@ export const optionDefinitions: OptionDefinition[] = [ { name: "network", alias: "n", type: String }, ]; -const func = async (params: TaskFuncParam) => { +const funcBlocksInAWeek = async (params: TaskFuncParam) => { const { args, env } = params; const { network } = args; @@ -48,4 +48,4 @@ const func = async (params: TaskFuncParam) => { return "succeeded"; }; -export default func; +export default funcBlocksInAWeek;