delegate-stake.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env bash
  2. #
  3. # Delegate stake to a validator
  4. #
  5. set -e
  6. here=$(dirname "$0")
  7. # shellcheck source=multinode-demo/common.sh
  8. source "$here"/common.sh
  9. stake_sol=10 # default number of SOL to assign as stake (10 SOL)
  10. url=http://127.0.0.1:8899 # default RPC url
  11. usage() {
  12. if [[ -n $1 ]]; then
  13. echo "$*"
  14. echo
  15. fi
  16. cat <<EOF
  17. usage: $0 [OPTIONS] <SOL to stake ($stake_sol)>
  18. Add stake to a validator
  19. OPTIONS:
  20. --url RPC_URL - RPC URL to the cluster ($url)
  21. --label LABEL - Append the given label to the configuration files, useful when running
  22. multiple validators in the same workspace
  23. --no-airdrop - Do not attempt to airdrop the stake
  24. --keypair FILE - Keypair to fund the stake from
  25. --force - Override delegate-stake sanity checks
  26. --vote-account - Path to vote-account keypair file
  27. --stake-account - Path to stake-account keypair file
  28. EOF
  29. exit 1
  30. }
  31. common_args=()
  32. label=
  33. airdrops_enabled=1
  34. maybe_force=
  35. keypair=
  36. positional_args=()
  37. while [[ -n $1 ]]; do
  38. if [[ ${1:0:1} = - ]]; then
  39. if [[ $1 = --label ]]; then
  40. label="-$2"
  41. shift 2
  42. elif [[ $1 = --keypair || $1 = -k ]]; then
  43. keypair="$2"
  44. shift 2
  45. elif [[ $1 = --force ]]; then
  46. maybe_force=--force
  47. shift 1
  48. elif [[ $1 = --url || $1 = -u ]]; then
  49. url=$2
  50. shift 2
  51. elif [[ $1 = --vote-account ]]; then
  52. vote_account=$2
  53. shift 2
  54. elif [[ $1 = --stake-account ]]; then
  55. stake_account=$2
  56. shift 2
  57. elif [[ $1 = --no-airdrop ]]; then
  58. airdrops_enabled=0
  59. shift
  60. elif [[ $1 = -h ]]; then
  61. usage "$@"
  62. else
  63. echo "Unknown argument: $1"
  64. usage
  65. exit 1
  66. fi
  67. else
  68. positional_args+=("$1")
  69. shift
  70. fi
  71. done
  72. common_args+=(--url "$url")
  73. if [[ ${#positional_args[@]} -gt 1 ]]; then
  74. usage "$@"
  75. fi
  76. if [[ -n ${positional_args[0]} ]]; then
  77. stake_sol=${positional_args[0]}
  78. fi
  79. VALIDATOR_KEYS_DIR=$SOLANA_CONFIG_DIR/validator$label
  80. vote_account="${vote_account:-$VALIDATOR_KEYS_DIR/vote-account.json}"
  81. stake_account="${stake_account:-$VALIDATOR_KEYS_DIR/stake-account.json}"
  82. if [[ ! -f $vote_account ]]; then
  83. echo "Error: $vote_account not found"
  84. exit 1
  85. fi
  86. if ((airdrops_enabled)); then
  87. if [[ -z $keypair ]]; then
  88. echo "--keypair argument must be provided"
  89. exit 1
  90. fi
  91. $solana_cli \
  92. "${common_args[@]}" --keypair "$SOLANA_CONFIG_DIR/faucet.json" \
  93. transfer --allow-unfunded-recipient "$keypair" "$stake_sol"
  94. fi
  95. if [[ -n $keypair ]]; then
  96. common_args+=(--keypair "$keypair")
  97. fi
  98. if ! [[ -f "$stake_account" ]]; then
  99. $solana_keygen new --no-passphrase -so "$stake_account"
  100. else
  101. echo "$stake_account already exists! Using it"
  102. fi
  103. set -x
  104. $solana_cli "${common_args[@]}" \
  105. vote-account "$vote_account"
  106. $solana_cli "${common_args[@]}" \
  107. create-stake-account "$stake_account" "$stake_sol"
  108. $solana_cli "${common_args[@]}" \
  109. delegate-stake $maybe_force "$stake_account" "$vote_account"
  110. $solana_cli "${common_args[@]}" stake-account "$stake_account"