-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathburn-token.sh
75 lines (64 loc) · 3 KB
/
burn-token.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
#!/usr/bin/env bash
set -e
# set -x
# This script creates, signs, and submits a transaction that creates some new tokens.
# It uses the output of the transaction from update-4.sh.
NETWORK_MAGIC=3
SOURCE_ADDRESS=$(cat ../../csk005/base.addr)
DESTINATION_ADDRESS=$(cat ../../csk005/base.addr)
PAYMENT_SKEY=../../csk005/payment.skey
TOKEN_NAME=SPAM9511token
POLICY_SKEY=${TOKEN_NAME}/policy.skey
POLICY_SCRIPT=${TOKEN_NAME}/policy.script
TOKEN_POLICY=67990188e55b6141bdcfa9958759cb021be22984431eb76f5726d6ae.SPAM9511token
BURN_AMOUNT=1
cardano-cli query protocol-parameters \
--testnet-magic ${NETWORK_MAGIC} \
--mary-era \
--out-file /tmp/protparams.json
IN_UTXO=$(cardano-cli query utxo \
--mary-era \
--testnet-magic ${NETWORK_MAGIC} \
--address ${SOURCE_ADDRESS} | grep -v "^ \|^-" | awk '{print "--tx-in "$1"#"$2}' | xargs)
UTXO_COUNT=$(cardano-cli query utxo \
--mary-era \
--testnet-magic ${NETWORK_MAGIC} \
--address ${SOURCE_ADDRESS} | grep -v "^ \|^-" | wc -l)
# calc balances
rm -f /tmp/balance*
cardano-cli query utxo \
--mary-era \
--testnet-magic ${NETWORK_MAGIC} \
--address ${SOURCE_ADDRESS} | grep -v "^ \|^-" | sed 's| + |\n|g' | sed 's|.* \([0-9].*lovelace\)|\1|g' > /tmp/balances
awk '{print $2}' /tmp/balances | uniq | while read token; do grep ${token} /tmp/balances | awk '{s+=$1} END {print s}' > /tmp/balance.${token}; done
sed -i "s|$(cat /tmp/balance.${TOKEN_POLICY})|$(( $(cat /tmp/balance.${TOKEN_POLICY}) - ${BURN_AMOUNT} ))|g" /tmp/balance.${TOKEN_POLICY}
OTHER_COINS=$(for balance_file in $(ls -1 /tmp/balance.* | grep -v lovelace); do BALANCE=$(cat ${balance_file}); TOKEN=$(echo $balance_file | sed 's|/tmp/balance.||g'); echo +${BALANCE} ${TOKEN}; done | xargs)
cardano-cli transaction build-raw \
--mary-era \
--fee 0 \
${IN_UTXO} \
--tx-out="${SOURCE_ADDRESS}+$(cat /tmp/balance.lovelace) lovelace${OTHER_COINS}" \
--mint="-${BURN_AMOUNT} ${TOKEN_POLICY}" \
--out-file ${TOKEN_NAME}.txbody
ACTUAL_MIN_FEE=$(cardano-cli transaction calculate-min-fee \
--tx-body-file ${TOKEN_NAME}.txbody \
--tx-in-count ${UTXO_COUNT} \
--tx-out-count 1 \
--witness-count 1 \
--byron-witness-count 0 \
--protocol-params-file /tmp/protparams.json | awk '{print $1}')
cardano-cli transaction build-raw \
--mary-era \
--fee ${ACTUAL_MIN_FEE} \
${IN_UTXO} \
--tx-out="${SOURCE_ADDRESS}+$(( $(cat /tmp/balance.lovelace) - ${ACTUAL_MIN_FEE})) lovelace${OTHER_COINS}" \
--mint="-${BURN_AMOUNT} ${TOKEN_POLICY}" \
--out-file ${TOKEN_NAME}.txbody-ok-fee
cardano-cli transaction sign \
--testnet-magic ${NETWORK_MAGIC} \
--signing-key-file ${PAYMENT_SKEY} \
--signing-key-file ${POLICY_SKEY} \
--script-file ${POLICY_SCRIPT} \
--tx-body-file ${TOKEN_NAME}.txbody-ok-fee \
--out-file ${TOKEN_NAME}.tx.signed
cardano-cli transaction submit --tx-file ${TOKEN_NAME}.tx.signed --testnet-magic ${NETWORK_MAGIC}