> For the complete documentation index, see [llms.txt](https://sugar-stake-pool.gitbook.io/cardano-kb/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sugar-stake-pool.gitbook.io/cardano-kb/cardano-node-operation/cardano-cli/operation-notes.md).

# Withdrawing rewards

#### 公式サイト

{% embed url="<https://docs.cardano.org/projects/cardano-node/en/latest/stake-pool-operations/withdraw-rewards.html>" %}

#### 仕様

一部だけを引き出すそうとすると下記のエラーでできません。

エラー：\[LedgerFailure (DelegsFailure (WithdrawalsNotInRewardsDELEGS (fromList \[(RewardAcnt {getRwdNetwork = Mainnet, getRwdCred = KeyHashObj (KeyHash “***Hash value***”)},Coin **"coin amout"**)])))

#### Shell（〜トランザクションのビルド）

`stake.addr`に紐づく報酬を`payment.addr`のアドレスへ全て移します。\
\* skeyを使うオペレーションなど、それぞれ適切な環境で実行します。

{% code title="withdraw\_rewards.sh" %}

```bash
#!/bin/bash

dstAddress=$(cat payment.addr) 

echo '---------------------'
echo dstAddress: ${dstAddress}

rewards=$(cardano-cli query stake-address-info \
--mainnet \
--address $(cat stake.addr) | jq '.[0].rewardAccountBalance')

echo stakeRewards: ${rewards}
echo '---------------------'

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

cardano-cli query utxo \
    --address $(cat payment.addr) \
    --mainnet > fullUtxo.out

tail -n +3 fullUtxo.out | sort -k3 -nr > balance.out

cat balance.out

tx_in=""
total_balance=0
while read -r utxo; do
    in_addr=$(echo ${utxo} | awk '{ print $1 }')
    idx=$(echo ${utxo} | awk '{ print $2 }')
    utxo_balance=$(echo ${utxo} | awk '{ print $3 }')
    total_balance=$((${total_balance}+${utxo_balance}))
    echo TxHash: ${in_addr}#${idx}
    echo ADA: ${utxo_balance}
    tx_in="${tx_in} --tx-in ${in_addr}#${idx}"
done < balance.out
txcnt=$(cat balance.out | wc -l)
echo Total ADA balance: ${total_balance}
echo Number of UTXOs: ${txcnt}

cardano-cli transaction build-raw \
${tx_in} \
--tx-out $(cat payment.addr)+0 \
--withdrawal $(cat stake.addr)+0 \
--invalid-hereafter 0 \
--fee 0 \
--out-file withdraw_rewards.draft

fee=$(cardano-cli transaction calculate-min-fee \
--tx-body-file withdraw_rewards.draft  \
--tx-in-count 1 \
--tx-out-count 1 \
--witness-count 2 \
--byron-witness-count 0 \
--mainnet \
--protocol-params-file protocol.json | awk '{print $1}')

echo fee: $fee

cardano-cli transaction build-raw \
${tx_in} \
--tx-out $(cat payment.addr)+$(( ${total_balance} - ${fee} + ${rewards} )) \
--withdrawal $(cat stake.addr)+${rewards} \
--invalid-hereafter $(( ${currentSlot} +10000)) \
--fee ${fee} \
--out-file withdraw_rewards.raw
```

{% endcode %}

#### サイン

```bash
cardano-cli transaction sign \
--tx-body-file withdraw_rewards.raw  \
--signing-key-file payment.skey \
--signing-key-file stake.skey \
--mainnet \
--out-file withdraw_rewards.signed
```

#### サブミット

```bash
cardano-cli transaction submit \
--tx-file withdraw_rewards.signed \
--mainnet
```

#### 移ったことを確認

```bash
cardano-cli query utxo \
--address $(cat payment.addr) \
--mainnet

cardano-cli query stake-address-info \
--mainnet \
--address $(cat stake.addr)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sugar-stake-pool.gitbook.io/cardano-kb/cardano-node-operation/cardano-cli/operation-notes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
