Withdraw staked DOT (WITHDRAWUNBONDED)

0. Initial setup

Copy
Copied
# imports
import requests

# Script parameters
ACCOUNT_ID = 4
LAM_USER = "lam_creator"
LAM_URL = "<LAM_URL>"
HEADERS = {"X-Ledger-API-User": LAM_USER}

1. Get info about you account

GET /accounts/{ACCOUNT_ID}

Copy
Copied
account = requests.get(LAM_URL + f"/accounts/{ACCOUNT_ID}", headers=HEADERS).json()

Example response :

Copy
Copied
{
  "address": "12MJp8QmUPMM78nQwkS6g9pq7oEX45tXyBdHHAhh762xyXJ5",
  "available_balance": "58329408236",
  "balance": "3049959737776",
  "coin_fields": {
    "anonymous_proxy": {
      "address": "16AL3CZ1UeBHj4nbvxnNJTQufAaGtyEXtHgCuUcF1quEW9hx",
      "balances": {
        "available": 9670000000,
        "bonded": 0,
        "fee_frozen": 0,
        "free": 19670000000,
        "misc_frozen": 0,
        "reserved": 330000000,
        "total": 20000000000
      },
      "block_height": 10737260,
      "created_at": "2022-06-14T09:00:24+00:00",
      "ext_index": 2,
      "index": 0,
      "is_controller": true,
      "is_main": true,
      "kind": "Any"
    },
    "balances": {
      "available": 58329408236,  // Funds will go to this balance
      "bonded": 2571220329540,
      "fee_frozen": 2781220329540,
      "free": 2849549737776,
      "misc_frozen": 2781220329540,
      "reserved": 200410000000,
      "total": 3049959737776,
      "total_unbonded": 210000000000,  // Funds will be taken from this balance
      "total_unbonding": 0,
      "unbonded": [
        {
          "amount": 210000000000,
          "waiting_time": 0
        }
      ],
      "unbonding": []
    },
    "staking": {
      "controller_address": "16AL3CZ1UeBHj4nbvxnNJTQufAaGtyEXtHgCuUcF1quEW9hx",
      "proxies": [
        "12TzkPCrVfrZxupsdkt3vxZQS7Ajw3DVcpFpBH2PBDU4Uyja"
      ],
      "rewards_destination": "Staked"
    },
    "type": "Polkadot"
  },
  "currency": "polkadot",
  "id": 4,
  "name": "Polkadot 0",
  "type": "Polkadot",
  [...]
}

2. Checks

Make sure you have some assets to withdraw. Otherwise, the transaction will do nothing

Copy
Copied
# Verify that bonded balance is high enough to unbond
if not account["coin_fields"]["balances"]["total_unbonded"]:
    raise ValueError("Nothing to withdraw")

3. Transaction creation

Create a WITHDRAWUNBONDED transaction request POST /transactions

Copy
Copied
# Create request for WITHDRAWUNBONDED transaction
transaction_result = requests.post(
    LAM_URL + f"/transactions",
    json={
        "account_name": account["name"],
        "amount": {
            "unit": "DOT",
            "value": None
        },
        "coin_fields": {"type": "WITHDRAWUNBONDED"},
        "max_fees": {"value": "0", "unit": "DOT"},
        "note": {
            "title": "LAM WITHDRAWUNBONDED TX",
            "content": "WITHDRAWUNBONDED TX MADE THROUGH THE LAM"
        },
        "recipient": "",
        "speed": "CUSTOM",
    },
    headers=HEADERS,
)
Copyright © Ledger Enterprise Platform 2023. All right reserved.