Skip to main content

Staking & Withdrawing


Initialization

import { PolidoSDK } from '@lidofinance/polygon-sdk';
import { useSDK } from '@lido-sdk/react';
import { useConnectorMetamask } from '@lido-sdk/web3-react';
import { CHAINS } from "@lido-sdk/constants";

// Can use a different connector provided in @lido-sdk/web3-react
const metamask = useConnectorMetamask()
await metamask.connect();

const { providerWeb3 } = useSDK();

// Pass the Web3 Provider from useSDK into the constructor
const polidoSDK = new PolidoSDK(CHAINS.Mainnet, providerWeb3);


Staking

1. Approve MATIC for Staking

type TxStageChangeCallback = (props: {
txStage: TX_STAGE;
code: TX_STAGE_ERROR_CODE;
error?: string;
}) => void;


try {
const { amount } = await polidoSDK.unlockTokens({
amount: '20', // The amount of MATIC to approve for staking
onTxStageChange: txStageChangeCallback, // Optional callback for getting information about transaction stage (see TX_STAGE)
});
} catch (e) {
// Handle Errors
}

2. Stake MATIC

type TxStageChangeCallback = (props: {
txStage: TX_STAGE;
code: TX_STAGE_ERROR_CODE;
transactionHash?: string;
error?: string;
additionalData?: Record<string, unknown>
}) => void;

try {
const { transactionHash, stAmount } = await polidoSDK.stake({
amount: '20',
referral: '0x...', // Optional address field for assigning a referrer in case there is one
onTxStageChange: txStageChangeCallback,
});
} catch (e) {
// Handle Errors
}
caution

You may get an exception if amount exceed maximum approved MATIC for staking. In order to prevent this exception, you can call

const allowed = await polidoSDK.checkAllowance({ amount: string, address: string });


Withdrawal

1. Request Withdrawal of stMATIC

type TxStageChangeCallback = (props: {
txStage: TX_STAGE;
code: TX_STAGE_ERROR_CODE;
transactionHash?: string;
error?: string;
additionalData?: Record<string, unknown>
}) => void;

try {
const { transactionHash, amount } = await polidoSDK.requestWithdrawal({
amount: '20',
address: '0x...',
referral: '0x...', // Optional address field for assigning a referrer in case there is one
setTxStage: setTxStageCallback,
});
} catch (e) {
// Handle Errors
}

2. Fetch un-bonding status of NFT tokens

type TokenItem = {
value: string;
amount: BigNumber;
text: string;
available: boolean;
checked: boolean;
}
try {
const { tokens, claimableAmount, pendingAmount, delay } = await polidoSDK.fetchTokens({
address: string
})
} catch (e) {
// Handle Errors
}


3. Claim MATIC rewards from NFT tokens

type TxStageChangeCallback = (props: {
txStage: TX_STAGE;
code: TX_STAGE_ERROR_CODE;
transactionHash?: string;
error?: string;
additionalData?: Record<string, unknown>
}) => void;

try {
const { transactionHash } = await polidoSDK.claimTokens({
tokenIds: string[]
setTxStage: setTxStageCallback,
});
} catch (e) {
// Handle Errors
}