Solana

How to stake tokens in Solana network using P2P.ORG Staking SDK.

  1. Initialize the API client object via the StakingApiClient constructor.
const client = new StakingApiClient({
  baseUrl: 'https://api.p2p.org',
  token: 'eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9',
});
  1. Retrieve the staking accounts from your P2P.ORG profile with getStakingAccounts().
var result = await client.solana.getStakingAccounts({
  'network': 'mainnet-beta',
  'status': 'active',
});

const account = result.accounts[0];
  1. Create a new stake transaction with stake(). The result will contain a base64-encoded unsigned transaction which you can convert to a Solana Transaction object.
var result = await client.solana.stake({
  network: 'mainnet-beta',
  feePayer: account.stakeAccount,
  fromPublicKey: account.stakeAccount,
  stakeAuthority: account.stakeAuthority,
  withdrawAuthority: account.withdrawAuthority,
  amount: 1002282880,
});

var transaction = client.solana.decodeTransaction(result.unsignedTransaction);
  1. Sign the transaction using the dedicated wallet software. P2P.ORG Staking SDK provides a simple method signWithKeypairs(), though it is intended for testing and development only. In production, replace it with the software or hardware wallet integration or a dedicated signing service integration.
const key = process.env.SOLANA_PRIVATE_KEY;
client.solana.signWithKeypairs(transaction, [key]);
  1. Verify the signed transaction with verifyTransaction(). This ensures that the transaction was not modified by external software before signing.
client.solana.verifyTransaction({
  tx: transaction,
  expectedFeePayer: account.stakeAuthority,
  expectedStakeAuthority: account.stakeAuthority,
  expectedWithdrawAuthority: account.withdrawAuthority,
  strict: true,
});
  1. Broadcast the transaction to the Solana network with sendTransaction().
await client.solana.sendTransaction({
  signedTransaction: transaction,
  network: 'mainnet-beta',
});

What's next?