-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdate-price.sh
executable file
·203 lines (176 loc) · 9.16 KB
/
update-price.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/sh
# Variables
tmpDir="/tmp/cardano-swaps/"
# Make the tmpDir if it doesn't already exist.
mkdir -p $tmpDir
ownerPubKeyFile="$HOME/wallets/01Stake.vkey"
beaconScriptFile="${tmpDir}oneWayBeacons.plutus"
beaconAddrFile="${tmpDir}oneWayBeaconStake.addr"
swapAddrFile="${tmpDir}oneWaySwap.addr"
swapDatumFile="${tmpDir}swapDatum.json"
swapRedeemerFile="${tmpDir}oneWaySpendingRedeemer.json"
beaconRedeemerFile="${tmpDir}oneWayBeaconRedeemer.json"
# The reference scripts are permanently locked in the swap address without a staking credential!
# You can use the `cardano-swaps query personal-address` command to see them.
beaconScriptPreprodTestnetRef="9fecc1d2cf99088facad02aeccbedb6a4f783965dc6c02bd04dc8b348e9a0858#1"
beaconScriptSize=4432
spendingScriptPreprodTestnetRef="9fecc1d2cf99088facad02aeccbedb6a4f783965dc6c02bd04dc8b348e9a0858#0"
spendingScriptSize=4842
# Generate the hash for the staking verification key.
echo "Calculating the staking pubkey hash for the borrower..."
ownerPubKeyHash=$(cardano-cli conway stake-address key-hash \
--stake-verification-key-file $ownerPubKeyFile)
# Export the beacon script.
echo "Exporting the beacon script..."
cardano-swaps scripts one-way beacon-script \
--out-file $beaconScriptFile
# Create the beacon stake address.
echo "Creating the beacon reward address..."
cardano-cli conway stake-address build \
--stake-script-file $beaconScriptFile \
--testnet-magic 1 \
--out-file $beaconAddrFile
# Create the spending redeemer.
echo "Creating the spending redeemer..."
cardano-swaps spending-redeemers one-way \
--update-with-stake \
--out-file $swapRedeemerFile
# Create the beacon script redeemer.
echo "Creating the beacon redeemer..."
cardano-swaps beacon-redeemers one-way \
--update-only \
--out-file $beaconRedeemerFile
# Create the new swap datum.
echo "Creating the new swap datum..."
cardano-swaps datums one-way \
--ask-asset lovelace \
--offer-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a \
--offer-price 2000000 \
--out-file $swapDatumFile
# Helper beacon variables.
echo "Calculating the beacon names..."
beaconPolicyId=$(cardano-swaps beacon-info one-way policy-id \
--stdout)
pairBeaconName=$(cardano-swaps beacon-info one-way pair-beacon \
--ask-asset lovelace \
--offer-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a \
--stdout)
offerBeaconName=$(cardano-swaps beacon-info one-way offer-beacon \
--offer-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a \
--stdout)
askBeaconName=$(cardano-swaps beacon-info one-way ask-beacon \
--ask-asset lovelace \
--stdout)
pairBeacon="${beaconPolicyId}.${pairBeaconName}"
offerBeacon="${beaconPolicyId}.${offerBeaconName}"
askBeacon="${beaconPolicyId}.${askBeaconName}"
# Create the transaction.
echo "Exporting the current protocol parameters..."
cardano-swaps query protocol-params \
--testnet \
--out-file "${tmpDir}protocol.json"
initial_change=$((316605149))
echo "Building the initial transaction..."
cardano-cli conway transaction build-raw \
--tx-in 5befc3f68d6ff1f8f7842bf27163a415cefd7f3dfca1c1f47b7668937307cca1#1 \
--tx-in a0253efa191f62d2eae47230e9deacfa16923faf6d13074d460f57a934f2ca0e#0 \
--spending-tx-in-reference $spendingScriptPreprodTestnetRef \
--spending-plutus-script-v2 \
--spending-reference-tx-in-inline-datum-present \
--spending-reference-tx-in-execution-units "(0,0)" \
--spending-reference-tx-in-redeemer-file $swapRedeemerFile \
--withdrawal "$(cat ${beaconAddrFile})+0" \
--withdrawal-tx-in-reference $beaconScriptPreprodTestnetRef \
--withdrawal-plutus-script-v2 \
--withdrawal-reference-tx-in-execution-units "(0,0)" \
--withdrawal-reference-tx-in-redeemer-file $beaconRedeemerFile \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${pairBeacon} + 1 ${offerBeacon} + 1 ${askBeacon} + 10 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat $HOME/wallets/01.addr) + ${initial_change} lovelace" \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral 21000000 \
--tx-out-return-collateral "$(cat $HOME/wallets/01.addr) 21000000 lovelace" \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee 5000000 \
--out-file "${tmpDir}tx.body"
echo "Getting the execution units estimations..."
exec_units=$(cardano-swaps evaluate-tx \
--testnet \
--tx-file "${tmpDir}tx.body")
# MAKE SURE THE INDEXES MATCH THE LEXICOGRAPHICAL ORDERING FOR INPUTS AND POLICY IDS.
# You can use `cardano-cli debug transaction view --tx-file "${tmpDir}tx.body` to view the prior
# transaction with everything in the correct order.
stake_mem=$(echo $exec_units | jq '.result | .[] | select(.validator.purpose=="withrdrawal" and .validator.index==0) | .budget.memory' )
stake_steps=$(echo $exec_units | jq '.result | .[] | select(.validator.purpose=="withrdrawal" and .validator.index==0) | .budget.cpu' )
spend_0_mem=$(echo $exec_units | jq '.result | .[] | select(.validator.purpose=="spend" and .validator.index==0) | .budget.memory' )
spend_0_steps=$(echo $exec_units | jq '.result | .[] | select(.validator.purpose=="spend" and .validator.index==0) | .budget.cpu' )
echo "Rebuilding the transaction with proper execution budgets..."
cardano-cli conway transaction build-raw \
--tx-in 5befc3f68d6ff1f8f7842bf27163a415cefd7f3dfca1c1f47b7668937307cca1#1 \
--tx-in a0253efa191f62d2eae47230e9deacfa16923faf6d13074d460f57a934f2ca0e#0 \
--spending-tx-in-reference $spendingScriptPreprodTestnetRef \
--spending-plutus-script-v2 \
--spending-reference-tx-in-inline-datum-present \
--spending-reference-tx-in-execution-units "(${spend_0_steps},${spend_0_mem})" \
--spending-reference-tx-in-redeemer-file $swapRedeemerFile \
--withdrawal "$(cat ${beaconAddrFile})+0" \
--withdrawal-tx-in-reference $beaconScriptPreprodTestnetRef \
--withdrawal-plutus-script-v2 \
--withdrawal-reference-tx-in-execution-units "(${stake_steps},${stake_mem})" \
--withdrawal-reference-tx-in-redeemer-file $beaconRedeemerFile \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${pairBeacon} + 1 ${offerBeacon} + 1 ${askBeacon} + 10 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat $HOME/wallets/01.addr) + ${initial_change} lovelace" \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral 21000000 \
--tx-out-return-collateral "$(cat $HOME/wallets/01.addr) 21000000 lovelace" \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee 5000000 \
--out-file "${tmpDir}tx.body"
echo "Calculating the required fee..."
calculated_fee=$(cardano-cli conway transaction calculate-min-fee \
--tx-body-file "${tmpDir}tx.body" \
--protocol-params-file "${tmpDir}protocol.json" \
--reference-script-size $((beaconScriptSize+spendingScriptSize)) \
--witness-count 2 | cut -d' ' -f1)
req_fee=$((calculated_fee+50000)) # Add 0.05 ADA to be safe since the fee must still be updated.
req_collateral=$(printf %.0f $(echo "${req_fee}*1.5" | bc))
echo "Rebuilding the transaction with the required fee..."
cardano-cli conway transaction build-raw \
--tx-in 5befc3f68d6ff1f8f7842bf27163a415cefd7f3dfca1c1f47b7668937307cca1#1 \
--tx-in a0253efa191f62d2eae47230e9deacfa16923faf6d13074d460f57a934f2ca0e#0 \
--spending-tx-in-reference $spendingScriptPreprodTestnetRef \
--spending-plutus-script-v2 \
--spending-reference-tx-in-inline-datum-present \
--spending-reference-tx-in-execution-units "(${spend_0_steps},${spend_0_mem})" \
--spending-reference-tx-in-redeemer-file $swapRedeemerFile \
--withdrawal "$(cat ${beaconAddrFile})+0" \
--withdrawal-tx-in-reference $beaconScriptPreprodTestnetRef \
--withdrawal-plutus-script-v2 \
--withdrawal-reference-tx-in-execution-units "(${stake_steps},${stake_mem})" \
--withdrawal-reference-tx-in-redeemer-file $beaconRedeemerFile \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${pairBeacon} + 1 ${offerBeacon} + 1 ${askBeacon} + 10 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat $HOME/wallets/01.addr) + $((initial_change-req_fee)) lovelace " \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral $req_collateral \
--tx-out-return-collateral "$(cat $HOME/wallets/01.addr) $((21000000-$req_collateral)) lovelace" \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee "$req_fee" \
--out-file "${tmpDir}tx.body"
echo "Signing the transaction..."
cardano-cli conway transaction sign \
--tx-body-file "${tmpDir}tx.body" \
--signing-key-file $HOME/wallets/01.skey \
--signing-key-file $HOME/wallets/01Stake.skey \
--testnet-magic 1 \
--out-file "${tmpDir}tx.signed"
echo "Submitting the transaction..."
cardano-swaps submit \
--testnet \
--tx-file "${tmpDir}tx.signed"
# Add a newline after the submission response.
echo ""