Skip to content

Commit

Permalink
Create shells scripts that registers a stake pool and then registers,
Browse files Browse the repository at this point in the history
delegates and withdraws rewards from  a script staking address. Also
create a script that can deregister a script staking address.
  • Loading branch information
Jimbo4350 committed Oct 6, 2021
1 parent b150dbe commit ea31fac
Show file tree
Hide file tree
Showing 6 changed files with 669 additions and 6 deletions.
9 changes: 6 additions & 3 deletions cardano-cli/test/Test/Golden/Shelley/Genesis/Create.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ golden_shelleyGenesisCreate = propertyOnce $ do
actualStartTime === fmtStartTime
actualDelegateCount === delegateCount
actualDelegateCount === utxoCount
actualTotalSupply === supply -- Check that the sum of the initial fund amounts matches the total supply
actualTotalSupply === supply - 1000000 -- Check that the sum of the initial fund amounts matches the total supply
-- We don't use the entire supply so there is ada in the treasury. This is
-- required for stake pool rewards.

-- Check uniqueness and count of hash keys
S.size (S.fromList actualHashKeys) === length actualHashKeys -- This isn't strictly necessary because we use aeson which guarantees uniqueness of keys
Expand Down Expand Up @@ -174,8 +176,9 @@ golden_shelleyGenesisCreate = propertyOnce $ do
actualStartTime === fmtStartTime
actualDelegateCount === delegateCount
actualDelegateCount === utxoCount
actualTotalSupply === supply -- Check that the sum of the initial fund amounts matches the total supply

actualTotalSupply === supply - 1000000 -- Check that the sum of the initial fund amounts matches the total supply
-- We don't use the entire supply so there is ada in the treasury. This is
-- required for stake pool rewards.
-- Check uniqueness and count of hash keys
S.size (S.fromList actualHashKeys) === length actualHashKeys -- This isn't strictly necessary because we use aeson which guarantees uniqueness of keys
S.size (S.fromList actualHashKeys) === delegateCount
Expand Down
20 changes: 17 additions & 3 deletions scripts/byron-to-alonzo/mkfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,15 @@ cardano-cli genesis create --testnet-magic 42 --genesis-dir shelley
# and K=10, but we'll keep long KES periods so we don't have to bother
# cycling KES keys
sed -i shelley/genesis.spec.json \
-e 's/"slotLength": 1/"slotLength": 0.2/' \
-e 's/"slotLength": 1/"slotLength": 0.1/' \
-e 's/"activeSlotsCoeff": 5.0e-2/"activeSlotsCoeff": 0.1/' \
-e 's/"securityParam": 2160/"securityParam": 10/' \
-e 's/"epochLength": 432000/"epochLength": 1500/' \
-e 's/"epochLength": 432000/"epochLength": 500/' \
-e 's/"maxLovelaceSupply": 0/"maxLovelaceSupply": 1000000000000/' \
-e 's/"decentralisationParam": 1.0/"decentralisationParam": 0.7/' \
-e 's/"major": 0/"major": 2/' \
-e 's/"major": 0/"major": 5/' \
-e 's/"rho": 0.0/"rho": 0.1/' \
-e 's/"tau": 0.0/"tau": 0.1/' \
-e 's/"updateQuorum": 5/"updateQuorum": 2/'

# Now generate for real:
Expand All @@ -327,6 +329,18 @@ cardano-cli genesis create \
--gen-genesis-keys ${NUM_BFT_NODES} \
--gen-utxo-keys 1

cardano-cli stake-address key-gen \
--verification-key-file shelley/utxo-keys/utxo-stake.vkey \
--signing-key-file shelley/utxo-keys/utxo-stake.skey

cardano-cli address key-gen \
--verification-key-file shelley/utxo-keys/utxo2.vkey \
--signing-key-file shelley/utxo-keys/utxo2.skey

cardano-cli stake-address key-gen \
--verification-key-file shelley/utxo-keys/utxo2-stake.vkey \
--signing-key-file shelley/utxo-keys/utxo2-stake.skey

echo "====================================================================="
echo "Generated genesis keys and genesis files:"
echo
Expand Down
5 changes: 5 additions & 0 deletions scripts/plutus/scripts/guess-42-stake.plutus
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "PlutusScriptV1",
"description": "",
"cborHex": "587a58780100003233322232323233223232225335300a333500900800233500700d4815040184d400d2411d496e636f727265637420646174756d2e2045787065637465642034322e001235002353003335738002008930930900090008900091199ab9a3375e00400200c00a24002244004244002400246ea00041"
}
91 changes: 91 additions & 0 deletions scripts/plutus/staking-example/claim-script-staking-rewards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash

set -e
# Unoffiical bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail


export BASE="${BASE:-.}"
export WORK="${WORK:-example/work}"
export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/node-bft1/node.sock}"
export TESTNET_MAGIC="${TESTNET_MAGIC:-42}"
export UTXO_VKEY1="${UTXO_VKEY1:-example/shelley/utxo-keys/utxo1.vkey}"
export UTXO_SKEY1="${UTXO_SKEY1:-example/shelley/utxo-keys/utxo1.skey}"
export UTXO_VKEY2="${UTXO_VKEY1:-example/shelley/utxo-keys/utxo2.vkey}"
export UTXO_SKEY2="${UTXO_SKEY1:-example/shelley/utxo-keys/utxo2.skey}"
export UTXO_STAKING_VKEY1="${UTXO_STAKING_VKEY1:=example/shelley/utxo-keys/utxo-stake.vkey}"
export UTXO_STAKING_SKEY1="${UTXO_STAKING_SKEY1:=example/shelley/utxo-keys/utxo-stake.skey}"
export UTXO_STAKING_VKEY2="${UTXO_STAKING_VKEY2:=example/shelley/utxo-keys/utxo2-stake.vkey}"
export UTXO_STAKING_SKEY2="${UTXO_STAKING_SKEY2:=example/shelley/utxo-keys/utxo2-stake.skey}"

utxoaddr=$(cardano-cli address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file "$UTXO_VKEY1")


cardano-cli query utxo \
--address "$utxoaddr" \
--cardano-mode \
--testnet-magic "$TESTNET_MAGIC" \
--out-file "$WORK/utxo-1.json"

echo "UTxO"
cat "$WORK/utxo-1.json"
echo ""

txin=$(jq -r 'keys[0]' $WORK/utxo-1.json)
txinlovelace=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo-1.json)
txincollateral=$(jq -r 'keys[1]' $WORK/utxo-1.json)
scriptpaymentaddrwithstakecred=$(cardano-cli address build --payment-verification-key-file $UTXO_VKEY1 --stake-script-file "scripts/plutus/scripts/guess-42-stake.plutus" --testnet-magic 42)
stakingscriptaddr=$(cardano-cli stake-address build --stake-script-file scripts/plutus/scripts/guess-42-stake.plutus --testnet-magic 42)

# STEP 1 - Get reward account balance

cardano-cli query stake-address-info \
--address "$stakingscriptaddr" \
--testnet-magic 42 \
--out-file "$WORK/scriptdelegationstatusrewards.json"

rewardamt=$(jq -r '.[0].rewardAccountBalance' $WORK/scriptdelegationstatusrewards.json)

totalspendable=$(expr $rewardamt + $txinlovelace - 289563)
echo "Lovelace at utxo: $txinlovelace"
echo "Rewards: $rewardamt"
echo "Combined: $totalspendable"

cardano-cli transaction build \
--alonzo-era \
--testnet-magic "$TESTNET_MAGIC" \
--change-address "$utxoaddr" \
--tx-in "$txin" \
--tx-in-collateral "$txincollateral" \
--tx-out "$scriptpaymentaddrwithstakecred+$totalspendable" \
--withdrawal "$stakingscriptaddr+$rewardamt" \
--withdrawal-script-file "scripts/plutus/scripts/guess-42-stake.plutus" \
--withdrawal-redeemer-file "scripts/plutus/data/42.redeemer" \
--protocol-params-file "$WORK/pparams.json" \
--out-file "$WORK/script-withdrawal.txbody"

cardano-cli transaction sign \
--tx-body-file "$WORK/script-withdrawal.txbody" \
--testnet-magic "$TESTNET_MAGIC" \
--signing-key-file "$UTXO_SKEY1" \
--out-file "$WORK/script-withdrawal.tx"

echo "Submitting withdrawal..."

cardano-cli transaction submit \
--tx-file "$WORK/script-withdrawal.tx" \
--testnet-magic "$TESTNET_MAGIC"

echo "Waiting 5 seconds...."
sleep 5

cardano-cli query stake-address-info \
--address "$stakingscriptaddr" \
--testnet-magic 42 \
--out-file "$WORK/scriptrewardscheck.json"

scriptrewardscheck=$(jq -r '.[0]' $WORK/scriptrewardscheck.json)
echo "Checking if script rewards withdrawal was successful..."
echo "$scriptrewardscheck"
Loading

0 comments on commit ea31fac

Please sign in to comment.