Skip to content

Commit

Permalink
add mint_more_token & send_token functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rocklee44 committed Dec 25, 2023
1 parent d7410a6 commit 4f718a2
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 4 deletions.
101 changes: 99 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Wallet balance:
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```

7. Burn new token function only accept utxo as below format
7. Burn token & send token to other address functions only accept utxo as below format
```
TxHash TxIx Amount
--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -147,4 +147,101 @@ Wallet balance::
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
NOTE: If the amount of token you entered >= remain token in UTXO , it will burn or send all remain tokens

- This is output when use script to send token to other wallet:
```
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: 4
You have chosen: Send token to another wallet
Your wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
10eb88f4b23d002b09b7ab1b063ef9ef1b21360c0b3d009af3bd9aeff54c313b 0 9980027966 lovelace + 900 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
Please provide receiver address
Receiver Address: addr_test1qqr585tvlc7ylnqvz8pyqwauzrdu0mxag3m7q56grgmgu7sxu2hyfhlkwuxupa9d5085eunq2qywy7hvmvej456flknswgndm3
Please provide information about token that you want to send
Token name: bworks
tokenname in encode base 16: 62776f726b73
Amount of token: 500
Please pick TxHash , TxIx that you want to use to pay for minting tokens
Caution: We are forced to send at least a minimum of 2 ada (2000000 Lovelace) to the foreign address, make sure the one you choose has at least 3000000 lovelaces (3 ADA)
Insert your txhash here: 10eb88f4b23d002b09b7ab1b063ef9ef1b21360c0b3d009af3bd9aeff54c313b
Insert your TxIx here: 0
build raw transaction
calculating fee
fee: 179361
tokenremain: 400
lovelace remain: 9977848605
rebuild transaction
sign transaction
submit transaction
Transaction successfully submitted.
Sent token successfully
Wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
67bba5bab9405d1417d7b9c5efb53eb2f2ec20a2aaaf3fef355b48884fc16a0d 1 9977848605 lovelace + 400 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```

8. Mint more token function only accept utxo as below format
```
Your wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
67bba5bab9405d1417d7b9c5efb53eb2f2ec20a2aaaf3fef355b48884fc16a0d 1 9977848605 lovelace + 400 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
```
- TxHash , TxIx without existed token policyid.tokennameec16 will not be accepted , for ex:
```
Your wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```
- This is output when use script to mint more 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: 3
You have chosen: Mint more existing tokens
Your wallet balance:
TxHash TxIx Amount
--------------------------------------------------------------------------------------
67bba5bab9405d1417d7b9c5efb53eb2f2ec20a2aaaf3fef355b48884fc16a0d 1 9977848605 lovelace + 400 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
Please provide information about token that you want to mint
Token name: bworks
tokenname in encode base 16: 62776f726b73
Amount of token: 2000
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: 67bba5bab9405d1417d7b9c5efb53eb2f2ec20a2aaaf3fef355b48884fc16a0d
Insert your TxIx here: 1
build raw transaction
calculating fee
fee: 181253
Amount of token after minting more: 2400
lovelace remain: 9977667352
rebuild transaction
sign transaction
submit transaction
Transaction successfully submitted.
Minted more token successfully
Wallet balance::
TxHash TxIx Amount
--------------------------------------------------------------------------------------
ca4e391cef69af129426fd896f4669ff82d6df2ad922844ba31cff0a1ba19370 0 9977667352 lovelace + 2400 39a863a56e0aef381749e08c5903b46da65bfcc1730e4b839905ff98.62776f726b73 + TxOutDatumNone
f3f548266c71a95d379c7afd557b62821da93f01d2044df0f12cdc02c0e94821 0 10000000000 lovelace + TxOutDatumNone
```
90 changes: 90 additions & 0 deletions functions/mint_more_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
mint_more_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 mint"
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 & tokenresult
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]+$ ]]; then
tokenresult=$(expr $rs1 + $tokenamount);
break;
fi

done

# Start minting...
echo "build raw transaction"
$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix --tx-out $address+$output+"$tokenresult $policyid.$tokennameec16" --mint "$tokenamount $policyid.$tokennameec16" --minting-script-file tokens/policy/policy.script --out-file tokens/matx.raw
echo "calculating fee"
fee=$($CARDANO_CLI transaction calculate-min-fee --tx-body-file tokens/matx.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 "Amount of token after minting more: $tokenresult"
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+"$tokenresult $policyid.$tokennameec16" --mint "$tokenamount $policyid.$tokennameec16" --minting-script-file tokens/policy/policy.script --out-file tokens/matx.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/matx.raw --out-file tokens/matx.signed
echo "submit transaction"
$CARDANO_CLI transaction submit --tx-file tokens/matx.signed $NET

# check submit result
submit_exit_code=$?
if [ $submit_exit_code -eq 0 ]; then
echo "Minted more token successfully"
# Displays wallet balance after minting tokens
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
}

99 changes: 99 additions & 0 deletions functions/send_token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
send_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"
receiver_output="2000000"

echo "Please provide receiver address"
read -p "Receiver Address: " receiver

echo "Please provide information about token that you want to send"
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
Caution: We are forced to send at least a minimum of 2 ada (2000000 Lovelace) to the foreign address, make sure the one you choose has at least 3000000 lovelaces (3 ADA)"
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 < 2000000)) {
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 >= 2000000) {
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 sending...
echo "build raw transaction"
$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix --tx-out $receiver+$receiver_output+"$tokenamount $policyid.$tokennameec16" --tx-out $address+$output+"$tokenremain $policyid.$tokennameec16" --out-file tokens/rec_matx.raw
echo "calculating fee"
fee=$($CARDANO_CLI transaction calculate-min-fee --tx-body-file tokens/rec_matx.raw --tx-in-count 1 --tx-out-count 2 --witness-count 1 $NET --protocol-params-file tokens/protocol.json | cut -d " " -f1)
echo "fee: $fee"
echo "tokenremain: $tokenremain"
output=$(expr $funds - $fee - $receiver_output)
echo "lovelace remain: $output"
echo "rebuild transaction"
$CARDANO_CLI transaction build-raw --fee $fee --tx-in $txhash#$txix --tx-out $receiver+$receiver_output+"$tokenamount $policyid.$tokennameec16" --tx-out $address+$output+"$tokenremain $policyid.$tokennameec16" --out-file tokens/rec_matx.raw
echo "sign transaction"
$CARDANO_CLI transaction sign --signing-key-file tokens/payment.skey $NET --tx-body-file tokens/rec_matx.raw --out-file tokens/rec_matx.signed
echo "submit transaction"
$CARDANO_CLI transaction submit --tx-file tokens/rec_matx.signed $NET

# check submit result
submit_exit_code=$?
if [ $submit_exit_code -eq 0 ]; then
echo "Sent token successfully"
# Displays wallet balance after minting tokens
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
}

4 changes: 2 additions & 2 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ case $action in
;;
3)
echo "You have chosen: Mint more existing tokens"
#mint_more_token
mint_more_token
;;
4)
echo "You have chosen: Send token to another wallet"
#send_token
send_token
;;
5)
echo "You have chosen: Burn token"
Expand Down

0 comments on commit 4f718a2

Please sign in to comment.