-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmint_nft.bash
executable file
·206 lines (169 loc) · 6.08 KB
/
mint_nft.bash
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
204
205
206
#!/bin/bash
cat << EOF
---------------------------------------------------
===================================================
CARDANO-NFT-MINTER
---------------------------------------------------
Make sure you are running your cardano-node before minting any nft.
You can use this guide here to run your cardano-node : https://developers.cardano.org/docs/get-started/running-cardano/
===================================================
---------------------------------------------------
EOF
echo Choose the cardano network where you will be minting the nft.
select TESTNETMAGIC in 'mainnet' 'testnet'
do
break
done
case $TESTNETMAGIC in
mainnet)
TESTNETMAGIC='764824073' # This id 764824073 is the mainnet magic. [ source : https://developers.cardano.org/docs/get-started/running-cardano/#mainnet--production ]
;;
testnet)
TESTNETMAGIC='1097911063'
;;
esac
echo selected network with magic : $TESTNETMAGIC
read -p 'Enter token name (without space) : ' tokenname
read -p 'Enter token amount (usually 1): ' tokenamount
read -p 'Enter image ipfs cid : ' ipfs_cid
fee="300000" #placeholder fee : source = https://github.com/cardano-foundation/developer-portal/pull/283
output="0"
mkdir -p tokens
cd tokens
mkdir -p $tokenname
cd $tokenname
[[ -f payment.skey && -f payment.vkey ]] || $(cardano-cli address key-gen --verification-key-file payment.vkey --signing-key-file payment.skey && \
cardano-cli address build --payment-verification-key-file payment.vkey --out-file payment.addr --testnet-magic $TESTNETMAGIC) && echo generated payment keys
address=$(cat payment.addr)
echo ------------------------------------------------------
echo payment address = $address
echo Make sure you add some ada into this address. \
You can use the faucet here : https://testnets.cardano.org/en/testnets/cardano/tools/faucet/ to request for test Ada if you are on testnet
echo ------------------------------------------------------
echo ;echo;
read -p "After you have loaded some ada, Press Enter to continue : "
echo Showing Utxo transaction table
cardano-cli query utxo --address $address --testnet-magic $TESTNETMAGIC
echo;echo;
read -p "If you see your transaction, Press Enter to continue : "
echo Fetching protocol parameters into protocol.json
cardano-cli query protocol-parameters --testnet-magic $TESTNETMAGIC --out-file protocol.json
#create policy
mkdir -p policy
[[ -f policy/policy.vkey ]] || \
cardano-cli address key-gen \
--verification-key-file policy/policy.vkey \
--signing-key-file policy/policy.skey && \
echo created policy/policy.vkey and policy/policy.skey
script="policy/policy.script"
touch $script
slotnumber=$(expr $(cardano-cli query tip --testnet-magic $TESTNETMAGIC | jq .slot?) + 10000)
echo calculated slotnumber = $slotnumber
echo creating policy script :
paymentKeyHash=$(cardano-cli address key-hash --payment-verification-key-file policy/policy.vkey)
cat << EOF > policy/policy.script
{
"type": "all",
"scripts":
[
{
"type": "before",
"slot": $slotnumber
},
{
"type": "sig",
"keyHash": "$paymentKeyHash"
}
]
}
EOF
echo created the below policy script :
echo ------------------------------------------
cat policy/policy.script
echo ------------------------------------------
echo;echo;
read -p "Press Enter to continue : "
echo;echo;
cardano-cli query tip --testnet-magic $TESTNETMAGIC
cardano-cli transaction policyid --script-file ./policy/policy.script > policy/policyID
policyid=$(cat policy/policyID)
echo Generated policy id = $policyid
echo ; echo;
echo Generating metadata.json
policyid=$(cat policy/policyID)
cat << END > metadata.json
{
"721": {
"$policyid": {
"$tokenname": {
"description": "This is my first NFT thanks to the Cardano network",
"name": "NFT token matadata property name",
"id": "1",
"image": ["ipfs://", "$ipfs_cid"]
}
}
}
}
END
echo created the below metadata.json :
echo ------------------------------------------
cat metadata.json
echo ------------------------------------------
echo ; echo;
read -p "Press Enter to continue : "
echo ; echo ;
echo Building transaction
echo Here are all the details generated :
echo --------------------------------
echo fee=$fee
echo address=$address
echo output=$output
echo token amount=$tokenamount
echo policyid=$policyid
echo token name=$tokenname
echo slot number=$slotnumber
echo script=$script
echo --------------------------------
echo ; echo ;
read -p 'Enter input transaction hash :' txhash
read -p 'Enter transaction index (TxIx) :' txix
read -p 'Enter the current utxo amount (In lovelace) : ' funds
cardano-cli transaction build-raw \
--fee $fee \
--tx-in $txhash#$txix \
--tx-out $address+$output+"$tokenamount $policyid.$tokenname" \
--mint="$tokenamount $policyid.$tokenname" \
--minting-script-file $script \
--metadata-json-file metadata.json \
--invalid-hereafter $slotnumber \
--out-file matx.raw
fee=$(cardano-cli transaction calculate-min-fee --tx-body-file matx.raw --tx-in-count 1 --tx-out-count 1 --witness-count 2 --testnet-magic $TESTNETMAGIC --protocol-params-file protocol.json | cut -d " " -f1)
output=$(expr $funds - $fee)
echo ; echo ;
echo calculated fees = $fee
echo calculated output = funds - fees = $output
echo ; echo ;
echo Building raw transaction based on new fees and output
cardano-cli transaction build-raw \
--fee $fee \
--tx-in $txhash#$txix \
--tx-out $address+$output+"$tokenamount $policyid.$tokenname" \
--mint="$tokenamount $policyid.$tokenname" \
--minting-script-file $script \
--metadata-json-file metadata.json \
--invalid-hereafter $slotnumber \
--out-file matx.raw
echo ; echo ;
echo Signing transaction
cardano-cli transaction sign \
--signing-key-file payment.skey \
--signing-key-file policy/policy.skey \
--testnet-magic $TESTNETMAGIC --tx-body-file matx.raw \
--out-file matx.signed
echo ; echo ;
read -p 'Press Enter to submit the transaction to the network : '
echo Submitting the transaction to network
cardano-cli transaction submit --tx-file matx.signed --testnet-magic $TESTNETMAGIC
echo; echo;
echo Updated Utxo transaction table
cardano-cli query utxo --address $address --testnet-magic $TESTNETMAGIC