Advertisement
Gryph_The_Grey

delegate-stake.sh

Nov 29th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. cat delegate-account.sh
  2. #!/bin/sh
  3.  
  4. # Disclaimer:
  5. #
  6. # The following use of shell script is for demonstration and understanding
  7. # only, it should *NOT* be used at scale or for any sort of serious
  8. # deployment, and is solely used for learning how the node and blockchain
  9. # works, and how to interact with everything.
  10. #
  11. # It also asumes that `jcli` is in the same folder with the script.
  12. #
  13. # Tutorials can be found here: https://github.com/input-output-hk/shelley-testnet/wiki
  14.  
  15. ### CONFIGURATION
  16. CLI="jcli"
  17. COLORS=1
  18. ADDRTYPE="--testing"
  19. SLOT_DURATION=2
  20. TIMEOUT_NO_OF_BLOCKS=200
  21.  
  22. getTip() {
  23. echo $($CLI rest v0 tip get -h "${REST_URL}")
  24. }
  25.  
  26. waitNewBlockCreated() {
  27. COUNTER=${TIMEOUT_NO_OF_BLOCKS}
  28. echo " ##Waiting for new block to be created (timeout = ${COUNTER} blocks = $((${COUNTER}*${SLOT_DURATION}))s)"
  29. initialTip=$(getTip)
  30. actualTip=$(getTip)
  31.  
  32. while [ "${actualTip}" = "${initialTip}" ]; do
  33. sleep ${SLOT_DURATION}
  34. actualTip=$(getTip)
  35. COUNTER=$((COUNTER - 1))
  36. if [ ${COUNTER} -lt 2 ]; then
  37. echo " !!!!!! ERROR: Waited $((${TIMEOUT_NO_OF_BLOCKS} * ${SLOT_DURATION}))s secs (${TIMEOUT_NO_OF_BLOCKS}*${SLOT_DURATION}) and no new block created"
  38. exit 1
  39. fi
  40. done
  41. echo "New block was created - $(getTip)"
  42. }
  43.  
  44. ### COLORS
  45. if [ ${COLORS} -eq 1 ]; then
  46. GREEN=`printf "\033[0;32m"`
  47. RED=`printf "\033[0;31m"`
  48. BLUE=`printf "\033[0;33m"`
  49. WHITE=`printf "\033[0m"`
  50. else
  51. GREEN=""
  52. RED=""
  53. BLUE=""
  54. WHITE=""
  55. fi
  56.  
  57. if [ $# -ne 3 ]; then
  58. echo "usage: $0 <STAKE_POOL_ID> <REST-LISTEN-PORT> <ACCOUNT-SK>"
  59. echo " <STAKE_POOL_ID> The ID of the Stake Pool you want to delegate to"
  60. echo " <REST-PORT> The REST Listen Port set in node-config.yaml file (EX: 3101)"
  61. echo " <ACCOUNT-SK> The Secret key of the Account address"
  62. exit 1
  63. fi
  64.  
  65. STAKE_POOL_ID="$1"
  66. REST_PORT="$2"
  67. ACCOUNT_SK="$3"
  68.  
  69. REST_URL="http://127.0.0.1:${REST_PORT}/api"
  70. BLOCK0_HASH=$($CLI rest v0 settings get -h "${REST_URL}" | grep 'block0Hash:' | sed -e 's/^[[:space:]]*//' | sed -e 's/block0Hash: //')
  71. FEE_CONSTANT=$($CLI rest v0 settings get -h "${REST_URL}" | grep 'constant:' | sed -e 's/^[[:space:]]*//' | sed -e 's/constant: //')
  72. FEE_COEFFICIENT=$($CLI rest v0 settings get -h "${REST_URL}" | grep 'coefficient:' | sed -e 's/^[[:space:]]*//' | sed -e 's/coefficient: //')
  73. FEE_CERTIFICATE=$($CLI rest v0 settings get -h "${REST_URL}" | grep 'certificate:' | sed -e 's/^[[:space:]]*//' | sed -e 's/certificate: //')
  74.  
  75. echo "================DELEGATE ACCOUNT================="
  76. echo "REST_PORT: ${REST_PORT}"
  77. echo "ACCOUNT_SK: ${ACCOUNT_SK}"
  78. echo "BLOCK0_HASH: ${BLOCK0_HASH}"
  79. echo "FEE_CONSTANT: ${FEE_CONSTANT}"
  80. echo "FEE_COEFFICIENT: ${FEE_COEFFICIENT}"
  81. echo "FEE_CERTIFICATE: ${FEE_CERTIFICATE}"
  82. echo "=================================================="
  83.  
  84. STAGING_FILE="staging.$$.transaction"
  85.  
  86. #CLI transaction
  87. if [ -f "${STAGING_FILE}" ]; then
  88. echo "error: staging already exist. restart"
  89. exit 2
  90. fi
  91.  
  92. ACCOUNT_PK=$(echo ${ACCOUNT_SK} | $CLI key to-public)
  93. ACCOUNT_ADDR=$($CLI address account ${ADDRTYPE} ${ACCOUNT_PK})
  94.  
  95. echo " ##1. Create the delegation certificate for the Account address"
  96.  
  97. ACCOUNT_SK_FILE="account.prv"
  98. CERTIFICATE_FILE="account_delegation_certificate"
  99. echo ${ACCOUNT_SK} > ${ACCOUNT_SK_FILE}
  100.  
  101. $CLI certificate new stake-delegation \
  102. ${ACCOUNT_PK} ${STAKE_POOL_ID} \
  103. -o ${CERTIFICATE_FILE}
  104.  
  105. ACCOUNT_COUNTER=$( $CLI rest v0 account get "${ACCOUNT_ADDR}" -h "${REST_URL}" | grep '^counter:' | sed -e 's/counter: //' )
  106. ACCOUNT_AMOUNT=$((${FEE_CONSTANT} + ${FEE_COEFFICIENT} + ${FEE_CERTIFICATE}))
  107.  
  108. echo " ##2. Create the offline delegation transaction for the Account address"
  109. $CLI transaction new --staging ${STAGING_FILE}
  110.  
  111. echo " ##3. Add input account to the transaction"
  112. $CLI transaction add-account "${ACCOUNT_ADDR}" "${ACCOUNT_AMOUNT}" --staging "${STAGING_FILE}"
  113.  
  114. echo " ##4. Add the certificate to the transaction"
  115. cat ${CERTIFICATE_FILE} | xargs $CLI transaction add-certificate --staging ${STAGING_FILE}
  116.  
  117. echo " ##5. Finalize the transaction"
  118. $CLI transaction finalize --staging ${STAGING_FILE}
  119.  
  120. # get the transaction id
  121. TRANSACTION_ID=$($CLI transaction id --staging ${STAGING_FILE})
  122.  
  123. echo " ##6. Create the withness"
  124. WITNESS_SECRET_FILE="witness.secret.$$"
  125. WITNESS_OUTPUT_FILE="witness.out.$$"
  126. printf "${ACCOUNT_SK}" > ${WITNESS_SECRET_FILE}
  127.  
  128. $CLI transaction make-witness ${TRANSACTION_ID} \
  129. --genesis-block-hash ${BLOCK0_HASH} \
  130. --type "account" --account-spending-counter "${ACCOUNT_COUNTER}" \
  131. ${WITNESS_OUTPUT_FILE} ${WITNESS_SECRET_FILE}
  132.  
  133. echo " ##7. Add the witness to the transaction"
  134. $CLI transaction add-witness ${WITNESS_OUTPUT_FILE} --staging "${STAGING_FILE}"
  135.  
  136. echo " ##8. Show the transaction info"
  137. $CLI transaction info --fee-constant ${FEE_CONSTANT} --fee-coefficient ${FEE_COEFFICIENT} --fee-certificate ${FEE_CERTIFICATE} --staging "${STAGING_FILE}"
  138.  
  139. echo " ##9. Finalize the transaction and send it to the blockchain"
  140. $CLI transaction seal --staging "${STAGING_FILE}"
  141. $CLI transaction auth -k ${ACCOUNT_SK_FILE} --staging "${STAGING_FILE}"
  142. $CLI transaction to-message --staging "${STAGING_FILE}" | $CLI rest v0 message post -h "${REST_URL}"
  143.  
  144. waitNewBlockCreated
  145.  
  146. echo " ##10. Check the account's delegation status"
  147. $CLI rest v0 account get ${ACCOUNT_ADDR} -h ${REST_URL}
  148.  
  149. rm ${STAGING_FILE} ${ACCOUNT_SK_FILE} ${CERTIFICATE_FILE} ${WITNESS_SECRET_FILE} ${WITNESS_OUTPUT_FILE}
  150.  
  151. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement