How to un-delegate and get key deposit back?

When we delegate to a pool for the first time, we pay 2 ada to register staking address in the blockchain. Later on we can re-delegate to a different different pools without registering staking address and paying more deposit.

Can we un-delegate and get the 2 ada key deposit back?

I understand the wallet software don’t have this feature (yet). I’m interested in command line options as well (cardano-cli). I think there should be a way to un-delegate and, afterwards, re-generate a new staking key for future staking operations.

1 Like

Hey @Jun_Sun

You simply could restore your wallet in Yoroi and delete the staking key there by going to the dashboard and then click on withdraw the rewards.

This way you would get back your deposit of 2 ada.

Cheers
Fabian

3 Likes

Thanks, @Zyroxa

Do you know how to do this from command line with “cardano-cli” client?

I found the answer myself. The key is to call “cardano-cli stake-address deregistration-certificate”. Below is the full commandline.

cardano-cli stake-address deregistration-certificate \
    --stake-verification-key-file stake.vkey \
    --out-file stake.cert

And then build a transaction with this certificate. Specifically you need to increase the change amount by 2 ADA and minus the transaction.

txOut=$((${total_balance}+${keyDeposit}-${fee}))
cardano-cli transaction build-raw \
    ${tx_in} \
    --tx-out $(cat payment.addr)+${txOut} \
    --invalid-hereafter $(( ${currentSlot} + 10000)) \
    --fee ${fee} \
    --certificate-file stake.cert \
    --mary-era \
    --out-file /tmp/tx.raw
3 Likes

See more details in this discussion

1 Like

Hello,

i send all ada to another wallet. but i need more help to get the Ada staking cost of 2 ada back.
Iam using Daedalus 4.0.5 and i dont find the option to delete.

Thank you

It is not possible to get the key deposit back with Daedalus wallet. That is what I found out. I believe someone said Yoroi is possible, but I have not verified. I tried to use commandline commands to get deposite back. It works flawlessly.

Hi,
I understand I need to use the command line to get the key deposit back for Deadalus Wallet but I don’t know where to start or how to fill in the information inside the command line. Is there a guide or video on how to do this step by step? Another question, when you do the command line (indicated by you), are you cancelling the delegation and getting 2 ADA back or you’re just getting the 2 ADA back and then you can proceed sending the ADA out?
Thank you in advance.
Marco

Attached is the script that I use to deregister stake address. After this operation one can get 2 ADA back to your UTXO (minus transaction fee obviously), and your wallet will be un-staked to any pool afterwards.

#!/bin/bash

set -e                  # exit on error
set -o pipefail         # exit on pipeline error
set -u                  # treat unset variable as error
#set -x

SCRIPT_FILE=$(readlink -f "$0")
SCRIPT_NAME=$(basename $SCRIPT_FILE)
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# we always excute under node-level directory
CARDANO_NODE_SOCKET_PATH=${CARDANO_NODE_SOCKET_PATH:-""}
if [[ -z $CARDANO_NODE_SOCKET_PATH ]]; then
    echo "please set env var $CARDANO_NODE_SOCKET_PATH"
    echo "you can either run a cardano node locally, or set up a ssh socket forward"
    echo
    echo "For example"
    echo
    echo "ssh -nNT -L /tmp/tmp-cardano-socket:/home/ubuntu/cardano/producer/db/socket ubuntu@mycardano.com"
    echo "export CARDANO_NODE_SOCKET_PATH=/tmp/tmp-cardano-socket"
    echo
    exit 1
fi

function myerror() {
    echo -e "ERROR : $1"
    echo
    echo -e "QUITING ...."
    exit 1
}
function calc_tx_in() {
    address=$1

    # calc input    
    local raw_balance=$(cardano-cli query utxo --address $address $NETWORK_MAGIC |tail -n +3 | sort -k3 -nr)

    echo "utxo (balance) for the account is :"
    tx_in=""
    total_balance=0
    txcnt=0
    while read -r utxo; do
        if [[ -z $utxo ]]; then break; fi
        in_addr=$(awk '{ print $1 }' <<< "${utxo}")
        idx=$(awk '{ print $2 }' <<< "${utxo}")
        utxo_balance=$(awk '{ print $3 }' <<< "${utxo}")
        total_balance=$((${total_balance}+${utxo_balance}))
        echo TxHash: ${in_addr}#${idx}
        echo ADA: ${utxo_balance}
        tx_in="${tx_in} --tx-in ${in_addr}#${idx}"
        txcnt=$((txcnt + 1))
    done <<< "$raw_balance"
    echo Total ADA balance: ${total_balance}
    echo Number of UTXOs: ${txcnt}
}

function calc_tx_in() {
    address=$1

    # calc input    
    local raw_balance=$(cardano-cli query utxo --address $address $NETWORK_MAGIC |tail -n +3 | sort -k3 -nr)

    echo "utxo (balance) for the account is :"
    tx_in=""
    total_balance=0
    txcnt=0
    while read -r utxo; do
        if [[ -z $utxo ]]; then break; fi
        in_addr=$(awk '{ print $1 }' <<< "${utxo}")
        idx=$(awk '{ print $2 }' <<< "${utxo}")
        utxo_balance=$(awk '{ print $3 }' <<< "${utxo}")
        total_balance=$((${total_balance}+${utxo_balance}))
        echo TxHash: ${in_addr}#${idx}
        echo ADA: ${utxo_balance}
        tx_in="${tx_in} --tx-in ${in_addr}#${idx}"
        txcnt=$((txcnt + 1))
    done <<< "$raw_balance"
    echo Total ADA balance: ${total_balance}
    echo Number of UTXOs: ${txcnt}
}

function deregister_stake() {
    echo "===> running deregister_stake ..."

    # gen stake cert
    cardano-cli stake-address deregistration-certificate \
        --stake-verification-key-file stake.vkey \
        --out-file stake-dereg.cert

    # calc deposite fee
    keyDeposit=$(cat $PARAMS_FILE | jq -r '.stakeAddressDeposit')
    echo keyDeposit: $keyDeposit

    # build tx.raw
    calc_tx_in ${PAYMENT_ADDR}
    if [[ $txcnt == 0 ]]; then
        myerror "Please fund the account first before registration"
    fi

    # get slot
    currentSlot=$(cardano-cli query tip $NETWORK_MAGIC | jq -r '.slot')
    echo Current Slot: $currentSlot

    # build tmp for fee estimate
    cardano-cli transaction build-raw \
        ${tx_in} \
        --tx-out ${PAYMENT_ADDR}+0 \
        --invalid-hereafter $(( ${currentSlot} + 10000)) \
        --fee 0 \
        --out-file /tmp/tx.tmp \
        --certificate stake-dereg.cert

    # estimate fee
    fee=$(cardano-cli transaction calculate-min-fee \
        --tx-body-file /tmp/tx.tmp \
        --tx-in-count ${txcnt} \
        --tx-out-count 1 \
        $NETWORK_MAGIC \
        --witness-count 2 \
        --byron-witness-count 0 \
        --protocol-params-file $PARAMS_FILE | awk '{ print $1 }')
    echo fee: $fee

    # calc change amount
    txOut=$((total_balance+keyDeposit-fee))
    echo Change Output: ${txOut}

    # build tx raw
    cardano-cli transaction build-raw \
        ${tx_in} \
        --tx-out ${PAYMENT_ADDR}+${txOut} \
        --invalid-hereafter $(( ${currentSlot} + 10000)) \
        --fee ${fee} \
        --certificate-file stake-dereg.cert \
        --out-file /tmp/tx.raw

    # sign
    cardano-cli transaction sign \
        --tx-body-file /tmp/tx.raw \
        --signing-key-file $PAYMENT_SKEY \
        --signing-key-file stake.skey \
        $NETWORK_MAGIC \
        --out-file /tmp/tx.signed

    # submit
    cardano-cli transaction submit \
        --tx-file /tmp/tx.signed \
        $NETWORK_MAGIC

    # clean up
    rm -f /tmp/tx.*
}

# =============   main  ================
if [[ $# == 2 && $1 == "--payer" ]]; then
    PAYMENT_ADDR=$(cat $2/payment.addr)
    PAYMENT_SKEY="$2/payment.skey"
elif [[ $# == 0 ]]; then
    PAYMENT_ADDR=$(cat payment.addr)
    PAYMENT_SKEY="payment.skey"
else
    usage
fi

read -p "For which network - mainnet or testnet? (default : testnet) "  NETWORK
if [[ -z $NETWORK ]]; then
    NETWORK="testnet"
elif [[ $NETWORK != "mainnet" && $NETWORK != "testnet" ]]; then
    myerror "Wrong network inputed : $NETWORK"
fi

if [[ $NETWORK == "mainnet" ]]; then
    NETWORK_MAGIC="--mainnet"
elif [[ $NETWORK == "testnet" ]]; then
    NETWORK_MAGIC="--testnet-magic 1097911063"
else
    echo "unknown network $NETWORK. quitting"
    exit 1
fi

PARAMS_FILE=$SCRIPT_DIR/${NETWORK}-params.json
if [[ ! -f $PARAMS_FILE ]]; then
    echo "generating params file $PARAMS_FILE ..."
    cardano-cli query protocol-parameters $NETWORK_MAGIC --out-file $PARAMS_FILE
fi

deregister_stake

echo "Initial build is done!"