Unstaking
The process of unstaking your SOL can be broken into two stages:
- Undelegation
- Withdrawal
Each of these steps require a separate transaction. Fortunately our API provides two endpoints which handle all of the transaction building for you.
Undelegation
The first step for unstaking your SOL is to request our API to build the undelegation transaction.
Send the following request to our API:
curl -X POST \
'https://api.stake.fish/v1/solana/undelegate' \
-H 'Content-Type: application/json' \
-d '{
"stakeAuthorityPubkey": STAKE_AUTHORITY_PUBKEY,
"stakeAccountPubkey": STAKE_ACCOUNT_PUBKEY
}'
Important
This API requires Public API Key.
Required Fields:
stakeAccountPubkey
: The stake account which is currently delegated to stakefishstakeAuthorityPubkey
: The public key that controls the stake account and was used for the delegation
Optional Fields:
fromPubkey
: The public key that will fund the transaction- Must have sufficient balance to cover the stake amount and transaction fees
- If not specified, defaults to stakeAuthorityPubkey
Example Response:
After sending the POST request, you’ll receive a response similar to the following:
{
"serializedTransaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAcJ2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FnZCH7dHoNDHlIPWHBmK6xI9jAxw5LZZ1dY3qaY/1NgOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYGPfUNuv5FIOsgxu1spvxVhlhqZrI2JJujUn/UMSx0EGodgXkTdUKpg0N73+KnqyVX9TXIp4citopJ3AAAAAAAah2BelAgULaAeR5s5tuI4eW3FQ9h/GeQpOtNEAAAAABqfVFxjHdMkoVmOYaR1etoteuKObS21cc1VbIQAAAAAGp9UXGSxcUSGMyUw9SvF/WNruCJuh/UTj29mKAAAAAAan1RcZNYTQ/u2bs0MdEyBr5UQoG1e4VmzFN1/0AAAAMKk3GxmHF4T2PO02tbVAEVS1XJANrnS4smv6e2aHoSQDAgIAAXADAAAA2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FkUAAAAAAAAAHNmIHN0YWtlOiAxNzMxMzYzODQ1AOH1BQAAAADIAAAAAAAAAAah2BeRN1QqmDQ3vf4qerJVf1NcinhyK2ikncAAAAAABAIBB3QAAAAA2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FnYvH4zwxca1bfAHnBo64kVzpF9EtRr6B5lU6d1aVrUWQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQGAQMGCAUABAIAAAA="
}
Sign & Broadcast transaction
Once the transaction is prepared by our API, the final step is to sign and broadcast the transaction in order to initialize your undelegation.
Sign The Transaction:
Rehydrate the serialized transaction from the Base64 encoded wire transaction.
Use the same account which corresponds to the fromPubkey
(which defaults to the stakeAuthorityPubkey
) as provided when creating the transaction.
Send the Transaction
Broadcast the transaction to a Solana node. The operation is completely atomic which means that once the transaction succeeds your delegation process is complete.
Occasional failures are expected when broadcasting the transaction - when this occurs please follow the suggested guidance
Here’s the completed code snippet, using @solana/web3.js:
import { Keypair, Transaction, Connection } from "@solana/web3.js";
import bs58 from "bs58";
const connection = new Connection("https://api.mainnet-beta.solana.com");
const MY_BASE58_SOLANA_SECRET_KEY =
"YOUR SECRET KEY HERE";
const wallet = Keypair.fromSecretKey(bs58.decode(MY_BASE58_SOLANA_SECRET_KEY));
const serializedTransaction = BASE_64_ENCODED_TX_FROM_API
const serializedTransaction = Buffer.from(
response.serializedTransaction,
"base64"
);
const rehydratedTransaction = Transaction.from(serializedTransaction);
rehydratedTransaction.sign(wallet);
const transactionSignature = await connection.sendRawTransaction(
rehydratedTransaction.serialize()
);
console.log({ transactionSignature });
Configuration:
MY_BASE58_SOLANA_SECRET_KEY
: Your Solana secret key- Used to pay for the transaction
- Becomes the stake authority
- ⚠️ Note: This example uses a direct secret key for backend demonstration. For web applications, use wallet-adapter instead to handle user signatures securely.
BASE_64_ENCODED_TX_FROM_API
: The base64 encoded transaction obtained from our API
Withdrawal
Following the completion of the undelegation process, you need to complete the withdrawal transaction which will result in the return of your SOL.
Send the following request to our API:
curl -X POST \
'https://api.stake.fish/v1/solana/withdraw' \
-H 'Content-Type: application/json' \
-d '{
"stakeAuthorityPubkey": STAKE_AUTHORITY_PUBKEY,
"stakeAccountPubkey": STAKE_ACCOUNT_PUBKEY
}'
Important
This API requires Public API Key.
Required Fields:
stakeAccountPubkey
: The stake account which is currently delegated to stakefishstakeAuthorityPubkey
: The public key that controls the stake account and was used for the delegation
Optional Fields:
fromPubkey
: The public key that will fund the transaction- Must have sufficient balance to cover the stake amount and transaction fees
- If not specified, defaults to stakeAuthorityPubkey
amountToWithdraw
: The amount of SOL to withdraw, denominated in lamports.- If not specified, defaults to the entire staked balance
toPubkey
: The public key that will receive the withdrawn SOL- If not specified, defaults to stakeAuthorityPubkey
Example Response:
After sending the POST request, you’ll receive a response similar to the following:
{
"serializedTransaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAcJ2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FnZCH7dHoNDHlIPWHBmK6xI9jAxw5LZZ1dY3qaY/1NgOwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYGPfUNuv5FIOsgxu1spvxVhlhqZrI2JJujUn/UMSx0EGodgXkTdUKpg0N73+KnqyVX9TXIp4citopJ3AAAAAAAah2BelAgULaAeR5s5tuI4eW3FQ9h/GeQpOtNEAAAAABqfVFxjHdMkoVmOYaR1etoteuKObS21cc1VbIQAAAAAGp9UXGSxcUSGMyUw9SvF/WNruCJuh/UTj29mKAAAAAAan1RcZNYTQ/u2bs0MdEyBr5UQoG1e4VmzFN1/0AAAAMKk3GxmHF4T2PO02tbVAEVS1XJANrnS4smv6e2aHoSQDAgIAAXADAAAA2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FkUAAAAAAAAAHNmIHN0YWtlOiAxNzMxMzYzODQ1AOH1BQAAAADIAAAAAAAAAAah2BeRN1QqmDQ3vf4qerJVf1NcinhyK2ikncAAAAAABAIBB3QAAAAA2Lx+M8MXGtW3wB5waOuJFc6RfRLUa+geZVOndWla1FnYvH4zwxca1bfAHnBo64kVzpF9EtRr6B5lU6d1aVrUWQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQGAQMGCAUABAIAAAA=",
"amountToWithdraw": "10000000000",
"toPubkey": "5JCHgeysqJcBr4pW67ARMA5G9XEMxxZkkzeNXMW7rSTf"
}
Sign & Broadcast transaction
Sign The Transaction:
Rehydrate the serialized transaction from the Base64 encoded wire transaction.
Use the same account which corresponds to the fromPubkey
(which defaults to the stakeAuthorityPubkey
) as provided when creating the transaction.
Send the Transaction
Occasional failures are expected when broadcasting the transaction - when this occurs please follow the suggested guidance
Broadcast the transaction to a Solana node. Here’s a code snippet, using @solana/web3.js
import { Keypair, Transaction, Connection } from "@solana/web3.js";
import bs58 from "bs58";
const connection = new Connection("https://api.mainnet-beta.solana.com");
const MY_BASE58_SOLANA_SECRET_KEY =
"YOUR SECRET KEY HERE";
const wallet = Keypair.fromSecretKey(bs58.decode(MY_BASE58_SOLANA_SECRET_KEY));
const serializedTransaction = Buffer.from(
BASE_64_ENCODED_TX_FROM_API,
"base64"
);
const rehydratedTransaction = Transaction.from(serializedTransaction);
rehydratedTransaction.sign(wallet);
const transactionSignature = await connection.sendRawTransaction(
rehydratedTransaction.serialize()
);
console.log({ transactionSignature });
Configuration:
MY_BASE58_SOLANA_SECRET_KEY
: Your Solana secret key- Used to pay for the transaction
- Becomes the stake authority
- ⚠️ Note: This example uses a direct secret key for backend demonstration. For web applications, use wallet-adapter instead to handle user signatures securely.
BASE_64_ENCODED_TX_FROM_API
: The base64 encoded transaction obtained from our API
Updated 3 days ago