From bcefeea830a059052227e5c96198d07e88a7effe Mon Sep 17 00:00:00 2001 From: DrZoltanFazekas Date: Fri, 11 Jul 2025 10:25:05 +0200 Subject: [PATCH] Fix edge cases in scripts and in getAdditionalSteps --- claim.sh | 9 +++++++-- src/BaseDelegation.sol | 2 +- src/NonLiquidDelegation.sol | 4 ++-- stake.sh | 8 +++++++- stakeRewards.sh | 8 +++++++- unstake.sh | 3 +++ withdrawRewards.sh | 8 +++++++- 7 files changed, 34 insertions(+), 8 deletions(-) diff --git a/claim.sh b/claim.sh index 47dc070..e849f33 100755 --- a/claim.sh +++ b/claim.sh @@ -14,6 +14,12 @@ if [[ "$variant" == "$temp" ]]; then exit 1 fi +# ensure there are no other transactions from the staker in 5 block before the forge script is executed +# because we will compare the stakers balance after the forge script and before the forge script and +# the latter is queried from 5 blocks before the first block number we get after the script execution +echo "Waiting 10 seconds..." +sleep 10 + forge script script/Claim.s.sol --broadcast --legacy --sig "run(address payable)" $1 --private-key $2 -vvvv block=$(cast rpc eth_blockNumber) @@ -35,12 +41,11 @@ if [[ "$tmp" != "" ]]; then d2=${tmp[1]} #d1=$(echo $tmp | sed -n -e 1p | sed 's/\[[^]]*\]//g') #d2=$(echo $tmp | sed -n -e 2p | sed 's/\[[^]]*\]//g') - fi echo $(date +"%T,%3N") $block_num -block_num=$((block_num-1)) +block_num=$((block_num - 5)) block=$(echo $block_num | cast to-hex --base-in 10) echo rewardsBeforeClaiming = $(cast call $1 "getRewards()(uint256)" --block $block_num | sed 's/\[[^]]*\]//g') diff --git a/src/BaseDelegation.sol b/src/BaseDelegation.sol index e6cb542..6f84738 100644 --- a/src/BaseDelegation.sol +++ b/src/BaseDelegation.sol @@ -249,7 +249,7 @@ abstract contract BaseDelegation is IDelegation, PausableUpgradeable, Ownable2St // ************************************************************************ /// @dev The current version of all upgradeable contracts in the repository. - uint64 internal immutable VERSION = encodeVersion(1, 1, 2); + uint64 internal immutable VERSION = encodeVersion(1, 1, 3); /** * @dev Return the contracts' version. diff --git a/src/NonLiquidDelegation.sol b/src/NonLiquidDelegation.sol index eb34c13..a7c78fc 100644 --- a/src/NonLiquidDelegation.sol +++ b/src/NonLiquidDelegation.sol @@ -445,9 +445,9 @@ contract NonLiquidDelegation is IDelegation, BaseDelegation { function getAdditionalSteps() public view returns(uint64) { NonLiquidDelegationStorage storage $ = _getNonLiquidDelegationStorage(); uint256 first = $.lastStakingIndex[_msgSender()]; - if (first == 0) + if (first == 0 && $.stakingIndices[_msgSender()].length > 0) first = $.stakingIndices[_msgSender()][0]; - return uint64($.stakings.length - first - 1); + return $.stakings.length > first ? uint64($.stakings.length - first - 1) : 0; } /** diff --git a/stake.sh b/stake.sh index 34f203b..485ba15 100755 --- a/stake.sh +++ b/stake.sh @@ -14,6 +14,12 @@ if [[ "$variant" == "$temp" ]]; then exit 1 fi +# ensure there are no other transactions from the staker in 5 block before the forge script is executed +# because we will compare the stakers balance after the forge script and before the forge script and +# the latter is queried from 5 blocks before the first block number we get after the script execution +echo "Waiting 10 seconds..." +sleep 10 + forge script script/Stake.s.sol --broadcast --legacy --sig "run(address payable, uint256)" $1 $3 --private-key $2 block=$(cast rpc eth_blockNumber) @@ -39,7 +45,7 @@ fi echo $(date +"%T,%3N") $block_num -block_num=$((block_num-1)) +block_num=$((block_num - 5)) block=$(echo $block_num | cast to-hex --base-in 10) rewardsBeforeStaking=$(cast call $1 "getRewards()(uint256)" --block $block_num | sed 's/\[[^]]*\]//g') diff --git a/stakeRewards.sh b/stakeRewards.sh index e8fec4b..d845956 100755 --- a/stakeRewards.sh +++ b/stakeRewards.sh @@ -21,6 +21,12 @@ if [ "$variant" == "LiquidStaking" ] && [ "$staker" != "$owner" ]; then exit 1 fi +# ensure there are no other transactions from the staker in 5 block before the forge script is executed +# because we will compare the stakers balance after the forge script and before the forge script and +# the latter is queried from 5 blocks before the first block number we get after the script execution +echo "Waiting 10 seconds..." +sleep 10 + forge script script/StakeRewards.s.sol --broadcast --legacy --sig "run(address payable)" $1 --private-key $2 block=$(cast rpc eth_blockNumber) @@ -47,7 +53,7 @@ fi echo $(date +"%T,%3N") $block_num -block_num=$((block_num-1)) +block_num=$((block_num - 5)) block=$(echo $block_num | cast to-hex --base-in 10) stake=$(cast call $1 "getStake()(uint256)" --block $block_num | sed 's/\[[^]]*\]//g') diff --git a/unstake.sh b/unstake.sh index 05dd018..41b1ba3 100755 --- a/unstake.sh +++ b/unstake.sh @@ -43,6 +43,9 @@ fi echo $(date +"%T,%3N") $block_num +# if block_num retrieved after the forge script was higher that the block in which the transaction submitted by +# the script was included, decrementing block_num by only 1 will lead to fetching all values below from the state +# after the script execution block_num=$((block_num-1)) block=$(echo $block_num | cast to-hex --base-in 10) diff --git a/withdrawRewards.sh b/withdrawRewards.sh index 69b1f34..187193d 100755 --- a/withdrawRewards.sh +++ b/withdrawRewards.sh @@ -30,6 +30,12 @@ if [[ "$variant" != "NonLiquidStaking" ]]; then exit 1 fi +# ensure there are no other transactions from the staker in 5 block before the forge script is executed +# because we will compare the stakers balance after the forge script and before the forge script and +# the latter is queried from 5 blocks before the first block number we get after the script execution +echo "Waiting 10 seconds..." +sleep 10 + forge script script/WithdrawRewards.s.sol --broadcast --legacy --sig "run(address payable, string, string)" $1 $amount $steps --private-key $2 block=$(cast rpc eth_blockNumber) @@ -66,7 +72,7 @@ staker_rewards_after_withdrawal=$(cast to-unit $x ether) echo $(date +"%T,%3N") $block_num -block_num=$((block_num-1)) +block_num=$((block_num - 5)) block=$(echo $block_num | cast to-hex --base-in 10) rewardsBeforeWithdrawal=$(cast call $1 "getRewards()(uint256)" --block $block_num | sed 's/\[[^]]*\]//g')