Stake / Unstake via API

Has anyone here used the APIs for staking / unstaking found here?

*Staking may be started by transferring XRD from an account to a prepared_stake entity with a specific validator. Once XRD is in this entity, at some point the system will move this XRD to a Validator entity and mint StakeUnits into the originating account.

*Unstaking may be started by transfering StakeUnits from an account to a prepared_unstake entity. Once StakeUnits is in this entity, at some point the system will destroy this StakeUnits and transfer XRD from the Validator entity into your exiting_stake entity. Once the unlocking period is over the system will move that XRD from the exiting_stake entity into your account.

I’m trying to figure out how to construct the call, but hitting a roadblock.
Thanks!

I think you have to use the Gateway API like mentioned here: Making Transactions by Gateway API :: Radix Documentation
You can make a POST request to the /transaction/build endpoint of a Gateway (for example https://mainnet.radixdlt.com/transaction/build using cURL or Postman or whatever you prefer) with this syntax: ReDoc Interactive Demo

Stake

It would look like this:

{
  "network_identifier": {
    "network": "mainnet"
  },
  "actions": [
    {
      "type": "StakeTokens",
      "from_account": {
        "address": "rdx11..."
      },
      "to_validator": {
        "address": "rv1.."
      },
      "amount": {
        "token_identifier": {
          "rri": "xrd_rr1qy5wfsfh"
        },
        "value": "123000000000000000"
      }
    }
  ],
  "fee_payer": {
    "address": "rdx11.."
  },
  "disable_token_mint_and_burn": true
}

from_account’s address and fee_payer’s address should be the address where you want to delegate XRD from.

Save the response, you’re gonna need unsigned_transaction and payload_to_sign outputs.

Then, you have to sign the transaction locally, you can use Stuart’s python script here: How do I locally sign a transaction with a private key?
You have to insert payload_to_sign in " HASH_OF_BLOB_TO_SIGN".

Once signed, you should use unsigned_transaction’s output from the POST call and the signed output from Stuart’s script, as mentioned here: Making Transactions by Gateway API :: Radix Documentation

You can get your wallet’s public key (to finalize and submit the transaction, step 3) from, again, another of Stuarts scripts here: How to convert a Wallet Address into a Public Key


Unstake

To unstake, only the first POST call changes:

{
  "network_identifier": {
    "network": "mainnet"
  },
  "actions": [
    {
      "type": "UnstakeTokens",
      "from_validator": {
        "address": "rv1"
      },
      "to_account": {
        "address": "rdx11.."
      },
      "amount": {
        "token_identifier": {
          "rri": "xrd_rr1qy5wfsfh"
        },
        "value": "123000000000000000"
      }
    }
  ],
  "fee_payer": {
    "address": "rdx11.."
  },
  "disable_token_mint_and_burn": true
}

You can use either amount like above or unstake_percentage:

{
  "network_identifier": {
    "network": "mainnet"
  },
  "actions": [
    {
      "type": "UnstakeTokens",
      "from_validator": {
        "address": "rv1"
      },
      "to_account": {
        "address": "rdx11.."
      },
      "unstake_percentage": 100
    }
  ],
  "fee_payer": {
    "address": "rdx11.."
  },
  "disable_token_mint_and_burn": true
}

Let me know if it helps.

You rock cowboy!
Thanks!!!

1 Like

@Peachy also i can walk you through how to use Radixir.Gateway — radixir v0.0.4 and Radixir.Gateway — radixir v0.0.4