Solana

Get Chain Total Staked and Staked Ratio

GET https://stake.nansen.ai/api/v1/noneth/sui/total_staked

Response
{
  "success": true,
  "error_code": 0,
  "result": {
    "totalStaked": "376976660.102478139",
    "stakedRatio": "0.81094003086521619911",
    "swuStaked": "61262.62476851",
    "commission": 0.05
  }
}

Get SOL Balances

const  web3 = require('@solana/web3.js');
const connection = new web3.Connection(SOLANA_ENDPOINT
);
const nativeBalance = await connection.getBalance(
  new web3.PublicKey(WALLET_ADDRESS)
);
const result = {
  balance: nativeBalance / web3.LAMPORTS_PER_SOL,
};

Get Delegations

const connection = new web3.Connection(SOLANA_ENDPOINT);
const res = await connection.getParsedProgramAccounts(web3.StakeProgram.programId, {
  commitment: 'confirmed',
  filters: [
    {
      memcmp: {
        offset: 44,
        bytes: WALLET_ADDRESS,
      },
    },
  ],
});

Build Stake Transaction

const connection = new web3.Connection(SOLANA_ENDPOINT);
const fromPubkey = new web3.PublicKey(WALLET_ADDRESS);
const votePubkey = new web3.PublicKey(VALIDATOR_ADDRESS);
const newStakeAccount = web3.Keypair.generate();
const stakePubkey = newStakeAccount.publicKey;
const transaction = new web3.Transaction().add(
  web3.StakeProgram.createAccount({
    fromPubkey,
    stakePubkey,
    authorized: new web3.Authorized(fromPubkey, fromPubkey),
    lamports: payload.amount * web3.LAMPORTS_PER_SOL,
    lockup: new web3.Lockup(0, 0, fromPubkey),
  }),
  web3.StakeProgram.delegate({
    stakePubkey,
    authorizedPubkey: fromPubkey,
    votePubkey: votePubkey,
  })
);
transaction.feePayer = fromPubkey;
transaction.recentBlockhash = (
  await connection.getLatestBlockhash()
).blockhash;
transaction.sign(newStakeAccount);

Build Deactivate Transaction

const connection = new web3.Connection(SOLANA_ENDPOINT);
const fromPubkey = new web3.PublicKey(WALLET_ADDRESS);
const stakePubkey = new web3.PublicKey(STAKER_ACCOUNT);
const transaction = new web3.Transaction().add(
  web3.StakeProgram.deactivate({
    stakePubkey,
    authorizedPubkey: fromPubkey,
  })
);
transaction.feePayer = fromPubkey;
transaction.recentBlockhash = (
  await connection.getLatestBlockhash()
).blockhash;

Build Withdraw Transaction

const connection = new web3.Connection(SOLANA_ENDPOINT);
const fromPubkey = new web3.PublicKey(WALLET_ADDRESS);
const stakePubkey = new web3.PublicKey(STAKER_ACCOUNT);
const transaction = new web3.Transaction().add(
  web3.StakeProgram.withdraw({
    stakePubkey,
    authorizedPubkey: fromPubkey,
    toPubkey: fromPubkey,
    lamports: payload.amount * web3.LAMPORTS_PER_SOL,
  })
);
transaction.feePayer = fromPubkey;
transaction.recentBlockhash = (
  await connection.getLatestBlockhash()
).blockhash;