-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuyNft.sh
executable file
·148 lines (122 loc) · 4.7 KB
/
BuyNft.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
set -eu
thisDir=$(dirname "$0")
tempDir=$thisDir/temp
walletDir=$thisDir/wallets/$BLOCKCHAIN_PREFIX
# get walletaddress from seller
buyerwalletaddress=$(cat $walletDir/buyer.addr)
echo "Buyerwalletaddress UTXO" $buyerwalletaddress
./QueryUtxo.sh $buyerwalletaddress
# what we buy
value="2000000 lovelace + 1 $POLICYID.$ASSETNAME"
echo "We buy: " $value
# save protocol parameters
echo "Save protocol parameters"
cardano-cli query protocol-parameters \
$BLOCKCHAIN \
--out-file $tempDir/$BLOCKCHAIN_PREFIX/protocol-parameters.json
# look for the txins
changeOutput=$(cardano-cli-balance-fixer change --address $buyerwalletaddress $BLOCKCHAIN)
#echo "ChangeOutput: " $changeOutput
# if there are more tokens, we put it into extraoutput
extraOutput=""
if [ "$changeOutput" != "" ];then
extraOutput="+ $changeOutput"
fi
echo "ExtraOutput: " $extraOutput
# look for inputs
txins=$(cardano-cli-balance-fixer input --address $buyerwalletaddress $BLOCKCHAIN)
echo "Inputs: " $txins
# define some variables
bodyFile=$tempDir/lockb.raw
outFile=$tempDir/lockb-signed.raw
signingKey=$walletDir/buyer.skey
signingKeyHash=$(cat $walletDir/pkhs/buyer-pkh.txt)
smartcontractaddress=$(cat $tempDir/$BLOCKCHAIN_PREFIX/smartcontract.addr)
scriptDatumHash=$(cat $tempDir/$BLOCKCHAIN_PREFIX/datums/buy-hash.txt)
utxoScript=$(./QueryUtxo.sh $smartcontractaddress | grep $scriptDatumHash | head -n 1 | cardano-cli-balance-fixer parse-as-utxo)
plutusFile=$thisDir/plutus/buy.plutus
datumFile=$tempDir/$BLOCKCHAIN_PREFIX/datums/buy.json
redeemerFile="$tempDir/$BLOCKCHAIN_PREFIX/redeemers/buy.json"
sellerwalletaddress=$(cat $walletDir/seller.addr)
marketplaceAddr=$(cat $walletDir/marketplace.addr)
royalitiesAddr=$(cat $walletDir/royalties.addr)
sellerAmount="8000000 lovelace"
marketPlaceAmount="1000000 lovelace"
royaltiesAmount="1000000 lovelace"
collateral=$(cardano-cli-balance-fixer collateral --address $buyerwalletaddress $BLOCKCHAIN )
#collateral="1ca4cb3450f92e029b4e3ddfad3366f7e6864f358afb4b8cfc0e23cb294d17c4#5"
echo "Smartcontract address " $smartcontractaddress
echo "ScriptDatumHash: " $scriptDatumHash
echo "TXIN Smartcontract: " $utxoScript
echo "Buyer PKH: " $signingKeyHash
echo "Sellerwalletaddress " $sellerwalletaddress
echo "Marketplaceaddress " $marketplaceAddr
echo "Royaltiesaddress: " $royalitiesAddr
echo "Buyerwalletaddress: " $buyerwalletaddress
echo "Seller gets: " $sellerAmount
echo "Marketplace gets: " $marketPlaceAmount
echo "Royaltyrecevier gets: " $royaltiesAmount
echo "Buyer get: " $value
echo "Collateral Buyer: " $collateral
echo ""
# Create the Transaction
set -eux
cardano-cli transaction build \
--babbage-era \
$BLOCKCHAIN \
$txins \
--tx-in $utxoScript \
--script-valid \
--tx-in-script-file $plutusFile \
--tx-in-datum-file $datumFile \
--tx-in-redeemer-file $redeemerFile \
--required-signer-hash $signingKeyHash \
--tx-in-collateral $collateral \
--tx-out "$sellerwalletaddress + $sellerAmount" \
--tx-out "$buyerwalletaddress + $value" \
--tx-out "$buyerwalletaddress + 3000000 $extraOutput" \
--tx-out "$marketplaceAddr + $marketPlaceAmount" \
--tx-out "$royalitiesAddr + $royaltiesAmount" \
--change-address $buyerwalletaddress \
--protocol-params-file temp/$BLOCKCHAIN_PREFIX/protocol-parameters.json \
--out-file $bodyFile
# --tx-out "$buyerwalletaddress + 3000000 lovelace + 102 a2d817f64ae600846073774fa5bfdf551b7e759e5aee715e42b1752d.574654" \
# --tx-out-return-collateral "$buyerwalletaddress + 478767859 lovelace + 104 a2d817f64ae600846073774fa5bfdf551b7e759e5aee715e42b1752d.574654" \
# --tx-total-collateral 1500000 \
: '
# Old version
cardano-cli transaction build \
--babbage-era \
$BLOCKCHAIN \
$txins \
--tx-in $utxoScript \
--tx-in-script-file $plutusFile \
--tx-in-datum-file $datumFile \
--tx-in-redeemer-file $redeemerFile \
--required-signer-hash $signingKeyHash \
--tx-in-collateral $collateral \
--tx-out "$sellerwalletaddress + $sellerAmount" \
--tx-out "$buyerwalletaddress + $value" \
--tx-out "$marketplaceAddr + $marketPlaceAmount" \
--tx-out "$royalitiesAddr + $royaltiesAmount" \
--tx-out "$buyerwalletaddress + 3000000 lovelace $extraOutput" \
--change-address $buyerwalletaddress \
--protocol-params-file temp/$BLOCKCHAIN_PREFIX/protocol-parameters.json \
--out-file $bodyFile
'
# Sign the Transaction
echo "Sign the transaction"
cardano-cli transaction sign \
--tx-body-file $bodyFile \
--signing-key-file $signingKey \
$BLOCKCHAIN \
--out-file $outFile
# Submit
echo "Submit the transaction"
cardano-cli transaction submit \
$BLOCKCHAIN \
--tx-file $outFile
# Txhash
echo "Get the TxHash"
cardano-cli transaction txid --tx-file $outFile
set -eu