Skip to content

Instantly share code, notes, and snippets.

@0xpatrickdev
Last active April 12, 2025 18:49
Show Gist options
  • Select an option

  • Save 0xpatrickdev/6c561123cbed2592de88833dacf35531 to your computer and use it in GitHub Desktop.

Select an option

Save 0xpatrickdev/6c561123cbed2592de88833dacf35531 to your computer and use it in GitHub Desktop.
testnet delegate and unbond
#!/bin/bash
DELEGATOR_ADDRESS="agoric1...."
NODE="https://devnet.rpc.agoric.net:443"
CHAIN_ID="agoricdev-23"
DENOM="ubld"
FLAGS="--node $NODE --chain-id $CHAIN_ID --keyring-backend test -y -b block"
FAUCET_URL="https://devnet.faucet.agoric.net/go"
FAUCET_QTY=75000000
## set to 33.4%. consumers of this script may want to start with smaller amounts
TARGET_VOTING_POWER_PERCENT=0.334
# Query validators and store the output
VALIDATORS_JSON=$(agd query staking validators --output json --node $NODE)
# Get the number of validators
NUM_VALIDATORS=$(echo "$VALIDATORS_JSON" | jq '.validators | length')
echo "Number of Validators: $NUM_VALIDATORS"
# Calculate total staked tokens
TOTAL_STAKED=$(echo "$VALIDATORS_JSON" | jq '[.validators[].tokens | tonumber] | add')
echo "Total Staked Tokens in the Network: $TOTAL_STAKED"
# Query DELEGATOR_ADDRESS balance
DELEGATOR_BALANCE=$(agd query bank balances $DELEGATOR_ADDRESS --node $NODE --output json | jq -r --arg DENOM "$DENOM" '[.balances[] | select(.denom == $DENOM).amount] | if length == 0 then 0 else .[0] end')
echo "Delegator's Current Balance: $DELEGATOR_BALANCE"
# Query DELEGATOR_ADDRESS staked tokens
STAKED_TOKENS_JSON=$(agd query staking delegations $DELEGATOR_ADDRESS --node $NODE --output json)
DELEGATOR_STAKED=$(echo "$STAKED_TOKENS_JSON" | jq '[.delegation_responses[].balance.amount | tonumber] | add // 0')
echo "Delegator's Current Staked: $DELEGATOR_STAKED"
# Calculate the delegator's current voting weight as a percentage
DELEGATOR_CURRENT_VOTING_WEIGHT=$(jq -n "(($DELEGATOR_STAKED / $TOTAL_STAKED) * 100)")
echo "Delegator's Current Voting Weight: $DELEGATOR_CURRENT_VOTING_WEIGHT%"
# Calculate TARGET_VOTING_POWER_PERCENT of the total staking power and round up
REQUIRED_STAKE=$(jq -n "($TOTAL_STAKED - $DELEGATOR_STAKED) * ($TARGET_VOTING_POWER_PERCENT / (1 - $TARGET_VOTING_POWER_PERCENT)) | ceil")
echo "Tokens representing Target Voting Power: $REQUIRED_STAKE"
##### FAUCET ACTIONS
ADDITIONAL_TOKENS_NEEDED=$(jq -n "if (${REQUIRED_STAKE} - ${DELEGATOR_BALANCE} < 0) then 0 else ${REQUIRED_STAKE} - ${DELEGATOR_BALANCE} end")
echo "Additional tokens needed: $ADDITIONAL_TOKENS_NEEDED"
TIMES_TO_HIT_FAUCET=$(jq -n "$ADDITIONAL_TOKENS_NEEDED / $FAUCET_QTY | if . > 0 then ceil else 0 end")
echo "Times to hit the faucet: $TIMES_TO_HIT_FAUCET"
# Request tokens from faucet
if [ "$TIMES_TO_HIT_FAUCET" -gt 0 ]; then
for i in $(seq 1 $TIMES_TO_HIT_FAUCET); do
curl "$FAUCET_URL" \
-H 'content-type: application/x-www-form-urlencoded' \
--data-raw "address=$DELEGATOR_ADDRESS&command=delegate&clientType=SMART_WALLET" \
--compressed
sleep 1
done
else
echo "No need to hit the faucet."
fi
##### STAKING ACTIONS
# Iterate over each validator and delegate tokens based on their current weight
echo "$VALIDATORS_JSON" | jq -c '.validators[]' | while read -r validator; do
validator_address=$(echo "$validator" | jq -r '.operator_address')
validator_tokens=$(echo "$validator" | jq -r '.tokens | tonumber')
# Calculate the validator's weight in the total staking and the tokens to delegate
tokens_to_delegate=$(echo "$validator" | jq --argjson total_staked "$TOTAL_STAKED" --argjson required_stake "$REQUIRED_STAKE" -r '($required_stake * (.tokens | tonumber) / $total_staked) | ceil')
# Delegate tokens_to_delegate to this validator
COMMAND="agd tx staking delegate $validator_address ${tokens_to_delegate}${DENOM} --from $DELEGATOR_ADDRESS $FLAGS"
echo "Executing: $COMMAND"
eval $COMMAND
sleep 1
done
#!/bin/bash
DELEGATOR_ADDRESS="agoric1....." # put your address here
NODE="https://devnet.rpc.agoric.net:443"
CHAIN_ID="agoricdev-23"
FLAGS="--node $NODE --chain-id $CHAIN_ID --keyring-backend test -y -b block"
# Query the delegations
RESPONSE=$(agd query staking delegations $DELEGATOR_ADDRESS --node $NODE --output json)
# Loop through the delegations and unbond from each
echo "$RESPONSE" | jq -c '.delegation_responses[] | {validator_address: .delegation.validator_address, amount: .balance.amount}' | while read -r line; do
VALIDATOR=$(echo $line | jq -r '.validator_address')
AMOUNT=$(echo $line | jq -r '.amount')
# Append denomination to the amount
AMOUNT_WITH_DENOM="${AMOUNT}ubld"
# Prepare the unbond command
COMMAND="agd tx staking unbond $VALIDATOR $AMOUNT_WITH_DENOM --from $DELEGATOR_ADDRESS $FLAGS"
# Echo the command for debugging purposes
echo "Executing: $COMMAND"
# Execute the unbond command
eval $COMMAND
# Add a delay if needed between transactions
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment