Skip to content

Commit

Permalink
Improve bash and foundry scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
DrZoltanFazekas committed Oct 21, 2024
1 parent b228838 commit 41ac789
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 77 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ with the private key of the delegator account. Make sure the account's balance c
The output will look like this:
```
Running version: 2
Current stake: 10000000000000000000000000 ZIL
Current rewards: 110314207650273223687 ZIL
Current stake: 10000000000000000000000000 wei
Current rewards: 110314207650273223687 wei
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Owner balance: 10000000000000000000000000 LST
Staker balance before: 99899145245801454561224 ZIL 0 LST
Staker balance after: 99699145245801454561224 ZIL 199993793908430833324 LST
Staker balance before: 99899145245801454561224 wei 0 LST
Staker balance after: 99699145245801454561224 wei 199993793908430833324 LST
```

Note that the staker LST balance in the output will be different from the actual LST balance which you can query by running
Expand All @@ -120,12 +120,12 @@ with the private key of an account that holds some LST.
The output will look like this:
```
Running version: 2
Current stake: 10000000000000000000000000 ZIL
Current rewards: 331912568306010928520 ZIL
Current stake: 10000000000000000000000000 wei
Current rewards: 331912568306010928520 wei
LST address: 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83
Owner balance: 10000000000000000000000000 LST
Staker balance before: 99698814298179759361224 ZIL 199993784619390291653 LST
Staker balance after: 99698814298179759361224 ZIL 99993784619390291653 LST
Staker balance before: 99698814298179759361224 wei 199993784619390291653 LST
Staker balance after: 99698814298179759361224 wei 99993784619390291653 LST
```

Last but not least, to claim the amount that is available after the unbonding period, run
Expand All @@ -137,6 +137,6 @@ with the private key of an account that unstaked some LST.
The output will look like this:
```
Running version: 2
Staker balance before: 99698086421983460161224 ZIL
Staker balance after: 99798095485861371162343 ZIL
Staker balance before: 99698086421983460161224 wei
Staker balance after: 99798095485861371162343 wei
```
28 changes: 17 additions & 11 deletions claim.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/bin/bash

if [ $# -ne 1 ]; then
echo "Provide a staker private key as an arguments."
if [ $# -ne 2 ]; then
echo "Provide the delegation contract address and a staker private key as arguments."
exit 1
fi

forge script script/claim_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 --private-key $1 -vvvv && \
block=$(cast rpc eth_blockNumber --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo rewardsAfterClaiming = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsAfterClaiming = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTaxedRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo $(date +"%T,%3N") $block && \
block=$((block-1)) && \
echo rewardsBeforeClaiming = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsBeforeClaiming = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTaxedRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo $(date +"%T,%3N") $block
staker=$(cast wallet address $2) && \
forge script script/claim_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable)" $1 --private-key $2 -vvvv && \
block=$(cast rpc eth_blockNumber --rpc-url http://localhost:4201) && \
block_num=$(echo $block | tr -d '"' | cast to-dec --base-in 16) && \
echo rewardsAfterClaiming = $(cast call $1 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsAfterClaiming = $(cast call $1 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
staker_wei_after=$(cast rpc eth_getBalance $staker $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo $(date +"%T,%3N") $block_num && \
block_num=$((block_num-1)) && \
block=$(echo $block_num | cast to-hex --base-in 10) && \
echo rewardsBeforeClaiming = $(cast call $1 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsBeforeClaiming = $(cast call $1 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
staker_wei_before=$(cast rpc eth_getBalance $staker $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo claimed amount - gas fee = $(bc -l <<< $staker_wei_after-$staker_wei_before) wei
echo $(date +"%T,%3N") $block_num
4 changes: 2 additions & 2 deletions script/claim_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ contract Claim is Script {
delegation.version()
);

console.log("Staker balance before: %s ZIL",
console.log("Staker balance before: %s wei",
staker.balance
);

vm.broadcast();

delegation.claim();

console.log("Staker balance after: %s ZIL",
console.log("Staker balance after: %s wei",
staker.balance
);
}
Expand Down
6 changes: 3 additions & 3 deletions script/stake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract Stake is Script {
delegation.version()
);

console.log("Current stake: %s ZIL \r\n Current rewards: %s ZIL",
console.log("Current stake: %s wei \r\n Current rewards: %s wei",
delegation.getStake(),
delegation.getRewards()
);
Expand All @@ -35,7 +35,7 @@ contract Stake is Script {
lst.balanceOf(owner)
);

console.log("Staker balance before: %s ZIL %s LST",
console.log("Staker balance before: %s wei %s LST",
staker.balance,
lst.balanceOf(staker)
);
Expand All @@ -46,7 +46,7 @@ contract Stake is Script {
value: amount
}();

console.log("Staker balance after: %s ZIL %s LST",
console.log("Staker balance after: %s wei %s LST",
staker.balance,
lst.balanceOf(staker)
);
Expand Down
8 changes: 4 additions & 4 deletions script/unstake_Delegation.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract Unstake is Script {
delegation.version()
);

console.log("Current stake: %s ZIL \r\n Current rewards: %s ZIL",
console.log("Current stake: %s wei \r\n Current rewards: %s wei",
delegation.getStake(),
delegation.getRewards()
);
Expand All @@ -31,11 +31,11 @@ contract Unstake is Script {
address(lst)
);

console.log("Owner LST balance: %s LST",
console.log("Owner balance: %s LST",
lst.balanceOf(owner)
);

console.log("Staker balance before: %s ZIL %s LST",
console.log("Staker balance before: %s wei %s LST",
staker.balance,
lst.balanceOf(staker)
);
Expand All @@ -50,7 +50,7 @@ contract Unstake is Script {
amount
);

console.log("Staker balance after: %s ZIL %s LST",
console.log("Staker balance after: %s wei %s LST",
staker.balance,
lst.balanceOf(staker)
);
Expand Down
28 changes: 17 additions & 11 deletions stake.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#!/bin/bash

if [ $# -ne 2 ]; then
echo "Provide a staker private key and the amount in wei as arguments."
if [ $# -ne 3 ]; then
echo "Provide the delegation contract address, a staker private key and an amount in wei as arguments."
exit 1
fi

forge script script/stake_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint256)" 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 $2 --private-key $1 && \
block=$(cast rpc eth_blockNumber --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo rewardsAfterStaking = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsAfterStaking = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTaxedRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo $(date +"%T,%3N") $block && \
block=$((block-1)) && \
echo rewardsBeforeStaking = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsBeforeStaking = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTaxedRewards()(uint256)" --block $block --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo $(date +"%T,%3N") $block
staker=$(cast wallet address $2) && \
forge script script/stake_Delegation.s.sol --rpc-url http://localhost:4201 --broadcast --legacy --sig "run(address payable, uint256)" $1 $3 --private-key $2 && \
block=$(cast rpc eth_blockNumber --rpc-url http://localhost:4201) && \
block_num=$(echo $block | tr -d '"' | cast to-dec --base-in 16) && \
echo rewardsAfterStaking = $(cast call $1 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsAfterStaking = $(cast call $1 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
staker_wei_after=$(cast rpc eth_getBalance $staker $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo $(date +"%T,%3N") $block_num && \
block_num=$((block_num-1)) && \
block=$(echo $block_num | cast to-hex --base-in 10) && \
echo rewardsBeforeStaking = $(cast call $1 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo taxedRewardsBeforeStaking = $(cast call $1 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
staker_wei_before=$(cast rpc eth_getBalance $staker $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
echo staked amount + gas fee = $(bc -l <<< $staker_wei_before-$staker_wei_after) wei
echo $(date +"%T,%3N") $block_num
30 changes: 16 additions & 14 deletions state.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
#!/bin/bash

if [ $# -ne 1 ]; then
echo "Provide a staker address as an argument."
if [ $# -ne 2 ]; then
echo "Provide the delegation contract address and a staker address as arguments."
exit 1
fi

block=$(cast rpc eth_blockNumber --rpc-url http://localhost:4201) && \
block_num=$(echo $block | tr -d '"' | cast to-dec --base-in 16) && \
echo $(date +"%T,%3N") $block_num && \
echo rewardsBeforeUnstaking = $(cast rpc eth_getBalance 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
x=$(cast call 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83 "balanceOf(address)(uint256)" 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77 --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
owner=$(cast call $1 "owner()(address)" --block $block_num --rpc-url http://localhost:4201) && \
lst=$(cast call $1 "getLST()(address)" --block $block_num --rpc-url http://localhost:4201) && \
echo rewardsBeforeUnstaking = $(cast rpc eth_getBalance $1 $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
x=$(cast call $lst "balanceOf(address)(uint256)" $owner --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
owner_lst=$(cast to-unit $x ether) && \
x=$(cast rpc eth_getBalance 0x15fc323DFE5D5DCfbeEdc25CEcbf57f676634d77 $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
x=$(cast rpc eth_getBalance $owner $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
owner_zil=$(cast to-unit $x ether) && \
echo owner: $owner_lst LST && echo owner: $owner_zil ZIL && \
x=$(cast call 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83 "balanceOf(address)(uint256)" $1 --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
x=$(cast call $lst "balanceOf(address)(uint256)" $2 --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
staker_lst=$(cast to-unit $x ether) && \
x=$(cast rpc eth_getBalance $1 $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
x=$(cast rpc eth_getBalance $2 $block --rpc-url http://localhost:4201 | tr -d '"' | cast to-dec --base-in 16) && \
staker_zil=$(cast to-unit $x ether) && \
echo staker: $staker_lst LST && echo staker: $staker_zil ZIL && \
x=$(cast call 0x9e5c257D1c6dF74EaA54e58CdccaCb924669dc83 "totalSupply()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
y=$(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
z=$(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getStake()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
x=$(cast call $lst "totalSupply()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
y=$(cast call $1 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
z=$(cast call $1 "getStake()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
price=$(bc -l <<< \($y+$z\)/$x) && \
echo LST supply: $(cast to-unit $x ether) && \
echo LST price: $price && \
echo staker LST value: $(bc -l <<< $staker_lst*$price) ZIL && \
echo getStake = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getStake()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo getRewards = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo getTaxedRewards = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo getTotalWithdrawals = $(cast call 0x7A0b7e6D24eDe78260c9ddBD98e828B0e11A8EA2 "getTotalWithdrawals()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g')
echo getStake = $z && \
echo getRewards = $y && \
echo getTaxedRewards = $(cast call $1 "getTaxedRewards()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g') && \
echo getTotalWithdrawals = $(cast call $1 "getTotalWithdrawals()(uint256)" --block $block_num --rpc-url http://localhost:4201 | sed 's/\[[^]]*\]//g')
18 changes: 9 additions & 9 deletions test/Delegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -771,19 +771,19 @@ contract DelegationTest is Test {
to stake, unstake and claim on the network your local node is connected to.
Before and after running the STAKING, UNSTAKING and CLAIMING scripts presented below,
always execute the following bash script to capture the values needed in the Foundry test below.
always execute the STATE script to capture the values needed in the Foundry test below.
STATE:
chmod +x state.sh && ./state.sh <staker_address>
chmod +x state.sh && ./state.sh <delegation_contract_address> <staker_address>
STAKING:
chmod +x stake.sh && ./stake.sh <staker_private_key> 10000000000000000000000
chmod +x stake.sh && ./stake.sh <delegation_contract_address> <staker_private_key> 10000000000000000000000
UNSTAKING:
chmod +x unstake.sh && ./unstake.sh <staker_private_key>
chmod +x unstake.sh && ./unstake.sh <delegation_contract_address> <staker_private_key>
CLAIMING:
chmod +x claim.sh && ./claim.sh <staker_private_key>
chmod +x claim.sh && ./claim.sh <delegation_contract_address> <staker_private_key>
Before running the test, replace the address on the first line with <staker_address>
*/
Expand All @@ -798,7 +798,7 @@ contract DelegationTest is Test {
uint256 taxedRewardsAfterStaking =
rewardsBeforeStaking - (rewardsBeforeStaking - taxedRewardsBeforeStaking) / uint256(10);
Console.log("Expected taxed rewards after staking: %s.%s%s ZIL", taxedRewardsAfterStaking);
// Insert the following value output by the UNSTAKE script
// Insert the following value output by the UNSTAKING script
uint256 rewardsBeforeUnstaking = 233367080700403454378;
run(
10_000_000 ether,
Expand All @@ -812,16 +812,16 @@ contract DelegationTest is Test {
true // initialDeposit
);
// Replace the values below in the same order with the values output by the STATE script
// run after the CLAIM script or logged by the CLAIM script itself
// run after the CLAIMING script or logged by the CLAIMING script itself
// the staker's ZIL balance in wei according to the STATE script after claiming
// the staker's ZIL balance in wei according to the STATE script before claiming
// the claiming transaction fee in wei output by the CLAIM script
// the claiming transaction fee in wei output by the CLAIMING script
Console.log("Expected staker balance after claiming: %s.%s%s ZIL",
100_000 ether - delegatedAmount
+ 100013.464887553198739807 ether - 90013.819919979031083499 ether + 0.3897714316896 ether
);
// Replace the values below in the same order with values output by the STATE script
// run before the STAKE and after the UNSTAKE scripts or logged by those script themselves
// run before the STAKING and after the UNSTAKE scripts or logged by those script themselves
// the owner's ZIL balance in wei according to the STATE script after unstaking
// the owner's ZIL balance in wei according to the STATE script before staking
// the transaction fees in wei output by the STAKING and UNSTAKING scripts
Expand Down
Loading

0 comments on commit 41ac789

Please sign in to comment.