-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathclaim-script-staking-rewards.sh
executable file
·108 lines (82 loc) · 3.42 KB
/
claim-script-staking-rewards.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
set -e
# Unofficial bash strict mode.
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -u
set -o pipefail
export WORK="${WORK:-example/work}"
export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/main.sock}"
export TESTNET_MAGIC="${TESTNET_MAGIC:-42}"
export UTXO_VKEY1="${UTXO_VKEY1:-example/utxo-keys/utxo1.vkey}"
export UTXO_SKEY1="${UTXO_SKEY1:-example/utxo-keys/utxo1.skey}"
export PV=v1 # Plutus Script Version
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)
txin1=$(jq -r 'keys[1]' $WORK/utxo-1.json)
txinlovelace=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo-1.json)
txincollateral=$(jq -r 'keys[1]' $WORK/utxo-1.json)
withdrawingscript="scripts/plutus/scripts/v2/stake-script.plutus"
scriptpaymentaddrwithstakecred=$(cardano-cli address build --payment-verification-key-file $UTXO_VKEY1 --stake-script-file "$withdrawingscript" --testnet-magic 42)
stakingscriptaddr=$(cardano-cli stake-address build --stake-script-file $withdrawingscript --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"
# Get plutus script reference input
plutusreferencescripttxin=$(jq -r 'keys[0]' $WORK/dummy-address-ref-script.json)
cardano-cli transaction build \
--babbage-era \
--testnet-magic "$TESTNET_MAGIC" \
--change-address "$utxoaddr" \
--tx-in "$txin" \
--tx-in "$txin1" \
--tx-in-collateral "$txincollateral" \
--tx-out "$scriptpaymentaddrwithstakecred+$totalspendable" \
--withdrawal "$stakingscriptaddr+$rewardamt" \
--withdrawal-tx-in-reference "$plutusreferencescripttxin" \
--withdrawal-plutus-script-v2 \
--withdrawal-reference-tx-in-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..."
echo ""
echo "Reward balance before withdrawal"
cardano-cli query stake-address-info \
--address "$stakingscriptaddr" \
--testnet-magic 42 \
--out-file "$WORK/scriptrewardscheck-before.json"
scriptrewardscheckbefore=$(jq -r '.[0]' $WORK/scriptrewardscheck-before.json)
echo "$scriptrewardscheckbefore"
cardano-cli transaction submit \
--tx-file "$WORK/script-withdrawal.tx" \
--testnet-magic "$TESTNET_MAGIC"
echo ""
echo "Waiting 5 seconds...."
sleep 5
echo ""
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...balance should be 0"
echo "$scriptrewardscheck"