Skip to content

Commit

Permalink
add burn token function
Browse files Browse the repository at this point in the history
  • Loading branch information
rocklee44 committed Dec 24, 2023
1 parent 2af0d1c commit d7410a6
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 7 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,56 @@ Wallet balance:
30a9759a9cf020ac8c895d18a8b0ad9a051a4b35097494611301cf8cdf50bcee 0 9980569965 lovelace + 1000 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```

7. Burn new token function only accept utxo as below format
```
TxHash TxIx Amount
--------------------------------------------------------------------------------------
30a9759a9cf020ac8c895d18a8b0ad9a051a4b35097494611301cf8cdf50bcee 0 9980569965 lovelace + 1000 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
```
- TxHash , TxIx without existed token policyid.tokennameec16 will not be accepted , for ex:
```
TxHash TxIx Amount
--------------------------------------------------------------------------------------
ba56c327d66b6da1d8e27628532533891ae59170f9dfd611c95aed410bdb6328 0 9980390604 lovelace + TxOutDatumNone
```
- This is output when use script to burn token
```
Choose the action you want:
1. Create new wallet
2. Mint new token
3. Mint more existing tokens
4. Send token to another wallet
5. Burn token
Press CTRL+C to exit
Enter the number corresponding to the action: 5
You have chosen: Burn token
Your wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
2ab313a6cd9382f89b3b489859cb55d1c95ebbfb14f8f5b93cecb8c102ba5926 0 9980209175 lovelace + 1000 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
Please provide information about token that you want to burn
Token name: bworks
tokenname in encode base 16: 62776f726b73
Amount of token: 100
Please pick TxHash , TxIx that you want to use to pay for minting tokens, make sure the one you choose has at least 1000000 lovelaces
Insert your txhash here: 2ab313a6cd9382f89b3b489859cb55d1c95ebbfb14f8f5b93cecb8c102ba5926
Insert your TxIx here: 0
build raw transaction
calculating fee
fee: 181209
tokenremain: 900
lovelace remain: 9980027966
rebuild transaction
sign transaction
submit transaction
Transaction successfully submitted.
Burned token successfully
Wallet balance::
TxHash TxIx Amount
--------------------------------------------------------------------------------------
10eb88f4b23d002b09b7ab1b063ef9ef1b21360c0b3d009af3bd9aeff54c313b 0 9980027966 lovelace + 900 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```
NOTE: If the amount of token you entered >= remain token in UTXO , it will burn all remain tokens
93 changes: 93 additions & 0 deletions functions/burn_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
burn_token() {
check_wallet_existence
check_policy_existence
address=$(cat tokens/payment.addr)

# Get wallet utxo
echo "Your wallet balance: "
$CARDANO_CLI query utxo --address $address $NET
selected_utxo=$($CARDANO_CLI query utxo --address $address $NET)
policyid=$(cat tokens/policy/policyID)
fee="0"
output="0"

echo "Please provide information about token that you want to burn"
read -p "Token name: " tokenname

# tokenname endcode base 16
tokennameec16=$(echo -n "$tokenname" | xxd -ps | tr -d '\n')
echo "tokenname in encode base 16: $tokennameec16"
read -p "Amount of token: " tokenamount

while true; do
echo "Please pick TxHash , TxIx that you want to use to pay for minting tokens, make sure the one you choose has at least 1000000 lovelaces"
read -p "Insert your txhash here: " txhash
read -p "Insert your TxIx here: " txix

# Check txhash , TxIx value

# Get funds
rs2=$(awk -v th="$txhash" -v ix="$txix" '
$1==th && $2==ix {
print $3;
}' <<<"$selected_utxo")
funds=$rs2

# Get tokenamount & tokenremain
rs1=$(awk -v th="$txhash" -v ix="$txix" '
$1==th && $2==ix {
if ($5 == "+" && $6 == "TxOutDatumNone") {
print "The TxHash , TxIx you selected does not have enough lovelace or is invalid. Please re-select appropriate TxHash , TxIx or press CTRL+C to exit.";
}
else if ($5 == "+" && $6 ~ /^[0-9]+$/ && ($7 != "'$policyid.$tokennameec16'" || $3 < 1000000)) {
print "The TxHash , TxIx you selected does not have enough lovelace or is invalid. Please re-select appropriate TxHash , TxIx or press CTRL+C to exit.";
}
else if ($5 == "+" && $6 ~ /^[0-9]+$/ && $7 == "'$policyid.$tokennameec16'" && $3 >= 1000000) {
print $6;
}
}' <<<"$selected_utxo")

if echo "$rs1" | grep -q "The TxHash , TxIx you selected"; then
echo "$rs1"
continue # pick another TxHash & TxIx if invalid
elif [[ $rs1 =~ ^[0-9]+$ && $tokenamount -ge $rs1 ]]; then
tokenremain=0;
tokenamount=$rs1;
break;
elif [[ $rs1 =~ ^[0-9]+$ && $tokenamount -lt $rs1 ]]; then
tokenremain=$(expr $rs1 - $tokenamount);
break;
fi

done

# Start burning...
echo "build raw transaction"
$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix --tx-out $address+$output+"$tokenremain $policyid.$tokennameec16" --mint="-$tokenamount $policyid.$tokennameec16" --minting-script-file tokens/policy/policy.script --out-file tokens/burning.raw
echo "calculating fee"
fee=$($CARDANO_CLI transaction calculate-min-fee --tx-body-file tokens/burning.raw --tx-in-count 1 --tx-out-count 1 --witness-count 2 $NET --protocol-params-file tokens/protocol.json | cut -d " " -f1)
echo "fee: $fee"
echo "tokenremain: $tokenremain"
output=$(expr $funds - $fee)
echo "lovelace remain: $output"
echo "rebuild transaction"
$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix --tx-out $address+$output+"$tokenremain $policyid.$tokennameec16" --mint="-$tokenamount $policyid.$tokennameec16" --minting-script-file tokens/policy/policy.script --out-file tokens/burning.raw
echo "sign transaction"
$CARDANO_CLI transaction sign --signing-key-file tokens/payment.skey --signing-key-file tokens/policy/policy.skey $NET --tx-body-file tokens/burning.raw --out-file tokens/burning.signed
echo "submit transaction"
$CARDANO_CLI transaction submit --tx-file tokens/burning.signed $NET

# check submit result
submit_exit_code=$?
if [ $submit_exit_code -eq 0 ]; then
echo "Burned token successfully"
sleep 10
echo "Wallet balance::"
$CARDANO_CLI query utxo --address $address $NET
exit 0
else
echo "Error submitting transaction. Exit code: $submit_exit_code"
exit $submit_exit_code
fi
}

9 changes: 3 additions & 6 deletions functions/check_cardano_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ check_cardano_env() {

if [ -z "$CARDANO_NODE_SOCKET_PATH" ] || [ -z "$CARDANO_CLI" ]; then
echo "cardano node socket and/or cardano-cli not found."
echo "Please ensure that you run this script in the environment there is already cardano node and cardano-cli.
CARDANO_NODE_SOCKET_PATH=path_to_cardano_node_socket
CARDANO_CLI=path_to_cardano-cli"

echo "Press CTRL+C to exit or wait 10 seconds to automatically exit."
sleep 10
echo "Please ensure that you run this script in the environment there is already cardano node and cardano-cli."
export CARDANO_NODE_SOCKET_PATH=`pwd`"/cardano-node-ipc/node.socket"
export CARDANO_CLI=`pwd`"/cardano-wallet/cardano-cli"
fi
}
2 changes: 1 addition & 1 deletion script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ case $action in
;;
5)
echo "You have chosen: Burn token"
#burn_token
burn_token
;;
esac

0 comments on commit d7410a6

Please sign in to comment.