-
Notifications
You must be signed in to change notification settings - Fork 13
/
convert-swap.sh
executable file
·203 lines (174 loc) · 9.65 KB
/
convert-swap.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
dir="../../../ignored/swap-files/"
tmpDir="../../../ignored/tmp/"
ownerPubKeyFile="../../../ignored/wallets/01Stake.vkey"
swapAddrFile="${dir}twoWaySwap.addr"
swapDatumFile="${dir}swapDatum.json"
swapRedeemerFile="${dir}twoWaySpendingRedeemer.json"
beaconRedeemerFile="${dir}twoWayBeaconRedeemer.json"
# Generate the hash for the staking verification key.
echo "Calculating the staking pubkey hash for the borrower..."
ownerPubKeyHash=$(cardano-cli stake-address key-hash \
--stake-verification-key-file $ownerPubKeyFile)
# Create the spending redeemer.
echo "Creating the spending redeemer..."
cardano-swaps spending-redeemers two-way \
--update-with-mint \
--out-file $swapRedeemerFile
# Create the swap datum.
echo "Creating the swap datum..."
cardano-swaps datums two-way \
--first-asset lovelace \
--second-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31 \
--first-price '1 / 1000000' \
--second-price '3000000 / 1' \
--out-file $swapDatumFile
# Helper beacon variables.
echo "Calculating the beacon names..."
beaconPolicyId=$(cardano-swaps beacon-info two-way policy-id \
--stdout)
oldPairBeaconName=$(cardano-swaps beacon-info two-way pair-beacon \
--first-asset lovelace \
--second-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a \
--stdout)
oldAsset1BeaconName=$(cardano-swaps beacon-info two-way asset-beacon \
--first-asset lovelace \
--stdout)
oldAsset2BeaconName=$(cardano-swaps beacon-info two-way asset-beacon \
--second-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a \
--stdout)
oldPairBeacon="${beaconPolicyId}.${oldPairBeaconName}"
oldAsset1Beacon="${beaconPolicyId}.${oldAsset1BeaconName}"
oldAsset2Beacon="${beaconPolicyId}.${oldAsset2BeaconName}"
newPairBeaconName=$(cardano-swaps beacon-info two-way pair-beacon \
--first-asset lovelace \
--second-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31 \
--stdout)
newAsset1BeaconName=$(cardano-swaps beacon-info two-way asset-beacon \
--first-asset lovelace \
--stdout)
newAsset2BeaconName=$(cardano-swaps beacon-info two-way asset-beacon \
--second-asset c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31 \
--stdout)
newPairBeacon="${beaconPolicyId}.${newPairBeaconName}"
newAsset1Beacon="${beaconPolicyId}.${newAsset1BeaconName}"
newAsset2Beacon="${beaconPolicyId}.${newAsset2BeaconName}"
# Create the minting redeemer. This redeemer can also burn the old beacons.
echo "Creating the minting redeemer..."
cardano-swaps beacon-redeemers two-way \
--mint-or-burn \
--out-file $beaconRedeemerFile
# Create the transaction.
echo "Exporting the current protocol parameters..."
cardano-swaps protocol-params \
--testnet \
--out-file "${tmpDir}protocol.json"
initial_change=$((336576154))
echo "Building the initial transaction..."
cardano-cli transaction build-raw \
--tx-in ac20e6b1cb9723b736f5360253ba3628d17051b7d20a5abb6b83e45b43065128#1 \
--tx-in f6538bb7a6a0365f3c295aed389df29e752df504ebe691e34f45c0ab9f96272c#1 \
--tx-in 84e649f06340eb887663dd5211259367b27638f842778b14382359983130bfb2#0 \
--spending-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#0 \
--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 \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon} + 11 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + 3000000 lovelace + 20 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + ${initial_change} lovelace" \
--mint "-1 ${oldPairBeacon} + -1 ${oldAsset1Beacon} + -1 ${oldAsset2Beacon} + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon}" \
--mint-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#1 \
--mint-plutus-script-v2 \
--mint-reference-tx-in-execution-units "(0,0)" \
--mint-reference-tx-in-redeemer-file $beaconRedeemerFile \
--policy-id "$beaconPolicyId" \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral 21000000 \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee 0 \
--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.
spend_0_mem=$(echo "$exec_units" | jq '.result | .[] | select(.validator=="spend:0") | .budget.memory' )
spend_0_steps=$(echo "$exec_units" | jq '.result | .[] | select(.validator=="spend:0") | .budget.cpu' )
mint_mem=$(echo "$exec_units" | jq '.result | .[] | select(.validator=="mint:0") | .budget.memory' )
mint_steps=$(echo "$exec_units" | jq '.result | .[] | select(.validator=="mint:0") | .budget.cpu' )
echo "Rebuilding the transaction with proper execution budgets..."
cardano-cli transaction build-raw \
--tx-in ac20e6b1cb9723b736f5360253ba3628d17051b7d20a5abb6b83e45b43065128#1 \
--tx-in f6538bb7a6a0365f3c295aed389df29e752df504ebe691e34f45c0ab9f96272c#1 \
--tx-in 84e649f06340eb887663dd5211259367b27638f842778b14382359983130bfb2#0 \
--spending-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#0 \
--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 \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon} + 11 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + 3000000 lovelace + 20 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + ${initial_change} lovelace" \
--mint "-1 ${oldPairBeacon} + -1 ${oldAsset1Beacon} + -1 ${oldAsset2Beacon} + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon}" \
--mint-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#1 \
--mint-plutus-script-v2 \
--mint-reference-tx-in-execution-units "(${mint_steps},${mint_mem})" \
--mint-reference-tx-in-redeemer-file $beaconRedeemerFile \
--policy-id "$beaconPolicyId" \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral 21000000 \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee 0 \
--out-file "${tmpDir}tx.body"
echo "Calculating the required fee..."
calculated_fee=$(cardano-cli transaction calculate-min-fee \
--tx-body-file "${tmpDir}tx.body" \
--testnet-magic 1 \
--protocol-params-file "${tmpDir}protocol.json" \
--tx-in-count 3 \
--tx-out-count 3 \
--witness-count 2 | cut -d' ' -f1)
req_fee=$((calculated_fee+100000)) # Add 0.1 ADA to be safe since the fee must still be updated.
echo "Rebuilding the transaction with the required fee..."
cardano-cli transaction build-raw \
--tx-in ac20e6b1cb9723b736f5360253ba3628d17051b7d20a5abb6b83e45b43065128#1 \
--tx-in f6538bb7a6a0365f3c295aed389df29e752df504ebe691e34f45c0ab9f96272c#1 \
--tx-in 84e649f06340eb887663dd5211259367b27638f842778b14382359983130bfb2#0 \
--spending-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#0 \
--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 \
--tx-out "$(cat ${swapAddrFile}) + 3000000 lovelace + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon} + 11 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.54657374546f6b656e31" \
--tx-out-inline-datum-file $swapDatumFile \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + 3000000 lovelace + 20 c0f8644a01a6bf5db02f4afe30d604975e63dd274f1098a1738e561d.4f74686572546f6b656e0a" \
--tx-out "$(cat ../../../ignored/wallets/01.addr) + $((initial_change-req_fee)) lovelace " \
--mint "-1 ${oldPairBeacon} + -1 ${oldAsset1Beacon} + -1 ${oldAsset2Beacon} + 1 ${newPairBeacon} + 1 ${newAsset1Beacon} + 1 ${newAsset2Beacon}" \
--mint-tx-in-reference ec243b6e5c3a1f7de6a0b0d75bf29247009e92596ac13f67b9176a70dcf96915#1 \
--mint-plutus-script-v2 \
--mint-reference-tx-in-execution-units "(${mint_steps},${mint_mem})" \
--mint-reference-tx-in-redeemer-file $beaconRedeemerFile \
--policy-id "$beaconPolicyId" \
--tx-in-collateral 4cc5755712fee56feabad637acf741bc8c36dda5f3d6695ac6487a77c4a92d76#0 \
--tx-total-collateral 21000000 \
--required-signer-hash "$ownerPubKeyHash" \
--protocol-params-file "${tmpDir}protocol.json" \
--fee "$req_fee" \
--out-file "${tmpDir}tx.body"
echo "Signing the transaction..."
cardano-cli transaction sign \
--tx-body-file "${tmpDir}tx.body" \
--signing-key-file ../../../ignored/wallets/01.skey \
--signing-key-file ../../../ignored/wallets/01Stake.skey \
--testnet-magic 1 \
--out-file "${tmpDir}tx.signed"
echo "Submitting the transaction..."
cardano-swaps submit \
--testnet \
--tx-file "${tmpDir}tx.signed"