Quick and dirty -- delegate your stake ###################
# HERE BE FUN DRAGONS -- last updated ~ Wed Oct 2 09:02:13 UTC 2019
###################

# THIS IS THE OFFICIAL DOC I FOLLOWED

# *NOTE*
# these instructions have only been tested on linux machines

# PRE-REQ
# You will need to have setup your node for this... sort of -- Setup Shelley Node
# You will need to have test ADA in an account -- Get testnet ADA
# You will need to have created a stake pool -- Create Stake Pool

# get into the working dir, set a var
cd ~/pub_testnet01/accounts
APIURL="http://127.0.0.1:3101/api"

# create the delegation cert
# - using the node id and pubkey from account created earlier
jcli certificate new stake-delegation $(cat ~/pub_testnet01/pools/stake_pool.id) $(cat ~/pub_testnet01/accounts/account.pk) > stake_delegation.cert

# sign the delegation cert
cat stake_delegation.cert | jcli certificate sign ~/pub_testnet01/accounts/account.sk | tee stake_delegation_signed.cert
cert1qYOUROUTPUT

# create transaction
jcli transaction new --staging tx

# add funds to transaction 11050 == tx fee
jcli transaction add-account $(cat ~/pub_testnet01/accounts/account.addr) 11050 --staging tx

# add cert to transaction
jcli transaction add-certificate --staging tx $(cat stake_delegation_signed.cert)

# finalize transacton
jcli transaction finalize --staging tx

# get a transaction id
jcli transaction id --staging tx > transaction.id

# create a witness for transaction
# - if you've used this account before the account-spending-counter value will differ
# - check that by looking at the counter: value. update spending counter value of the
#   make witness to match the value of this output
jcli rest v0 account get ca1YOURACCOUNTADDR -h ${APIURL}
---
counter: 1
delegation:
  pools: []
  value: 99999889500
# this will grab it and set it as a var
COUNTER=$(jcli rest v0 account get ca1YOURACCOUNTADDR -h ${APIURL} | grep ^counter: | awk '{print $NF}')
jcli transaction make-witness $(cat transaction.id) --genesis-block-hash adbdd5ede31637f6c9bad5c271eec0bc3d0cb9efb86a5b913bb55cba549d0770 --type "account" --account-spending-counter ${COUNTER} witness ~/pub_testnet01/accounts/account.sk

# add witness to transaction
jcli transaction add-witness witness --staging tx

# seal transaction
jcli transaction seal --staging tx

# submit transaction to chain -- will stdout fragment_id
jcli transaction to-message --staging tx | jcli rest v0 message post -h ${APIURL}
YOURFRAGMENTID

# check to see if account is delegating to your pool
jcli rest v0 account get $(cat ~/pub_testnet01/accounts/account.addr) -h ${APIURL}

# TODO:

# NEXT STEP: SUPER IMPORTANT

# The Lovelace Community Pool is a no fee staking pool run by @kyleo
# Join us on Telegram

# If this was useful and you can/want to support us, our donation address is:
Ae2tdPwUPEYw3rz8KGHbnTusd9QWQ8ePhogEWkm1agugTtW51skA59DrKe8