Staking & UnStaking
1. Initialize the library
import { SolidoSDK } from '@lidofinance/solido-sdk';
import { Connection } from '@solana/web3.js';
// Change rpc endpoint to yours, or whole connection
const connection = new Connection('https://test.rpcpool.com');
const solidoSDK = new SolidoSDK('mainnet-beta', connection, 'your_solana_referral_address');
- Easiest way
- Detailed way
- Profi way
2. Call stake method
type SetTxStageCallback = (props: {
txStage: TX_STAGE;
transactionHash?: TransactionSignature; // for TX_STAGE.AWAITING_BLOCK stage
deactivatingSolAccountAddress?: PublicKey; // for TX_STAGE.AWAITING_SIGNING stage unstake instructions
stSolAccountAddress?: PublicKey; // for TX_STAGE.AWAITING_SIGNING stage stake instructions
}) => void;
try {
const { transactionHash, stSolAccountAddress } = await solidoSDK.stake({
amount: 20, // The amount of SOL-s which need to stake
wallet: wallet, // Wallet instance
setTxStage: setTxStageCallback, // Optional callback for getting information about transaction stage (see TX_STAGE)
});
} catch (e) {
// Handle Errors
}
2. Prepare stake transaction
try {
const { transaction, stSolAccountAddress } = await solidoSDK.getStakeTransaction({
amount: 20, // The amount of SOL-s which need to stake
payerAddress: new PublicKey(wallet.publicKey),
});
} catch (e) {
// Handle Errors
}
3. Sign and confirm transaction
try {
const transactionHash = await solidoSDK.signAndConfirmTransaction({
transaction: transaction, // Previous step
wallet: wallet, // Wallet instance
setTxStage: setTxStageCallback, // Optional callback for getting information about transaction stage (see TX_STAGE)
});
} catch (e) {
// Handle Errors
}
2. Prepare stake transaction
try {
const { transaction, stSolAccountAddress } = await solidoSDK.getStakeTransaction({
amount: 20, // The amount of SOL-s which need to stake
payerAddress: new PublicKey(wallet.publicKey),
});
} catch (e) {
// Handle Errors
}
3. Sign and confirm transaction
try {
// Do something before singing transaction
const signed = await wallet.signTransaction(stakeTransaction);
const transactionHash = await connection.sendRawTransaction(signed.serialize());
// Do something before confirming transaction
const { value: status } = await connection.confirmTransaction(transactionHash);
if (status?.err) {
throw status.err;
}
// Do something after getting success transaction
} catch (e) {
// Handle Errors
}
caution
You may get an exception if amount exceed maximum available SOL-s in balance. In order to prevent this exception, you can call
const maxStakeAmount = await solidoSDK.calculateMaxStakeAmount(new PublicKey(wallet.publicKey));
Unstaking
Unstaking process if whole the same as staking, also arguments are the same, just methods rename:
getStakeTransaction
=>getUnStakeTransaction
calculateMaxStakeAmount
=>calculateMaxUnStakeAmount
stake
=>unStake
enum TX_STAGE {
AWAITING_SIGNING = 1,
AWAITING_BLOCK = 2,
SUCCESS = 3,
ERROR = 4,
}