Vana SDK - v2.2.3
    Preparing search index...

    Class StakingController

    Controller for VanaPool staking operations.

    Provides methods to query staking information from the VanaPool contracts. This includes total VANA staked across the protocol, entity information, and individual staker positions.

    // Get total VANA staked in the protocol
    const totalStaked = await vana.staking.getTotalVanaStaked();
    console.log(`Total staked: ${totalStaked} wei`);

    // Get entity information
    const entity = await vana.staking.getEntity(1n);
    console.log(`Entity name: ${entity.name}`);

    Hierarchy

    • BaseController
      • StakingController
    Index

    Methods

    • Gets the total amount of VANA staked in the VanaPool protocol or a specific entity.

      Parameters

      • OptionalentityId: bigint

        Optional entity ID to get staked amount for a specific entity

      Returns Promise<bigint>

      The total amount of VANA staked in wei

      When called without an entityId, this retrieves the sum of activeRewardPool across all active entities in the VanaPool protocol. When called with an entityId, this returns the activeRewardPool for that specific entity. The value is returned in wei (10^18 = 1 VANA).

      // Get total staked across all entities
      const totalStaked = await vana.staking.getTotalVanaStaked();
      console.log(`Total staked: ${Number(totalStaked) / 1e18} VANA`);

      // Get staked amount for a specific entity
      const entityStaked = await vana.staking.getTotalVanaStaked(1n);
      console.log(`Entity 1 staked: ${Number(entityStaked) / 1e18} VANA`);
    • Gets information about a specific staking entity.

      Parameters

      • entityId: bigint

        The ID of the entity to query

      Returns Promise<EntityInfo>

      The entity information including name, APY, shares, and reward pools

      const entity = await vana.staking.getEntity(1n);
      console.log(`Entity: ${entity.name}`);
      console.log(`Total shares: ${entity.totalShares}`);
      console.log(`Max APY: ${Number(entity.maxAPY) / 100}%`);
    • Gets the total number of staking entities in the protocol.

      Returns Promise<bigint>

      The count of entities

      const count = await vana.staking.getEntitiesCount();
      console.log(`Total entities: ${count}`);
    • Gets the IDs of all active staking entities.

      Returns Promise<readonly bigint[]>

      Array of active entity IDs

      const activeIds = await vana.staking.getActiveEntities();
      console.log(`Active entities: ${activeIds.join(', ')}`);
    • Gets a staker's position in a specific entity.

      Parameters

      • staker: `0x${string}`

        The address of the staker

      • entityId: bigint

        The ID of the entity

      Returns Promise<StakerEntityInfo>

      The staker's position information

      const position = await vana.staking.getStakerPosition(
      '0x742d35...',
      1n
      );
      console.log(`Shares: ${position.shares}`);
      console.log(`Rewards: ${position.realizedRewards}`);
    • Gets the earned rewards for a staker in an entity.

      Parameters

      • staker: `0x${string}`

        The address of the staker

      • entityId: bigint

        The ID of the entity

      Returns Promise<bigint>

      The earned rewards amount in wei

      const rewards = await vana.staking.getEarnedRewards(
      '0x742d35...',
      1n
      );
      console.log(`Earned: ${Number(rewards) / 1e18} VANA`);
    • Gets the minimum stake amount required to stake.

      Returns Promise<bigint>

      The minimum stake amount in wei

      const minStake = await vana.staking.getMinStakeAmount();
      console.log(`Minimum stake: ${Number(minStake) / 1e18} VANA`);
    • Gets the count of active stakers in the protocol.

      Returns Promise<bigint>

      The number of active stakers

      const count = await vana.staking.getActiveStakersCount();
      console.log(`Active stakers: ${count}`);
    • Gets the bonding period for staking.

      Returns Promise<bigint>

      The bonding period in seconds

      const bondingPeriod = await vana.staking.getBondingPeriod();
      console.log(`Bonding period: ${bondingPeriod / 86400n} days`);
    • Gets the total distributed rewards for a specific entity from the subgraph.

      Parameters

      • entityId: bigint

        The ID of the entity to query

      • options: { subgraphUrl?: string } = {}

        Optional configuration including custom subgraph URL

      Returns Promise<bigint>

      The total distributed rewards in wei

      This queries the VanaPool subgraph to retrieve the cumulative totalDistributedRewards for a staking entity. This value represents the sum of all rewards that have been processed (moved from lockedRewardPool to activeRewardPool) minus any forfeited rewards that were returned.

      Requires a configured subgraphUrl in the Vana constructor options.

      When subgraph URL is not configured or query fails

      const totalRewards = await vana.staking.getTotalDistributedRewards(1n);
      const totalRewardsVana = Number(totalRewards) / 1e18;
      console.log(`Total distributed rewards: ${totalRewardsVana.toLocaleString()} VANA`);
    • Gets a comprehensive staking summary for a staker in an entity.

      Parameters

      • staker: `0x${string}`

        The address of the staker

      • entityId: bigint

        The ID of the entity

      Returns Promise<StakerEntitySummary>

      A comprehensive summary of the staker's position

      This method aggregates all relevant staking information for a staker in a single call, including staked amount, current value, bonding status, and all reward types.

      Reward breakdown:

      • earnedRewards = pendingInterest + vestedRewards + realizedRewards
      • pendingInterest = currentValue - costBasis (unvested appreciation)
      • vestedRewards = rewards that have vested but not yet withdrawn
      • realizedRewards = rewards already withdrawn during unstakes
      const summary = await vana.staking.getStakerSummary('0x742d35...', 1n);
      console.log(`Total staked: ${Number(summary.totalStaked) / 1e18} VANA`);
      console.log(`Current value: ${Number(summary.currentValue) / 1e18} VANA`);
      console.log(`In bonding period: ${summary.isInBondingPeriod}`);
      console.log(`Remaining bonding time: ${Number(summary.remainingBondingTime) / 86400} days`);
      console.log(`Vested rewards: ${Number(summary.vestedRewards) / 1e18} VANA`);
      console.log(`Unvested rewards: ${Number(summary.unvestedRewards) / 1e18} VANA`);
      console.log(`Realized rewards: ${Number(summary.realizedRewards) / 1e18} VANA`);
      console.log(`Total earned: ${Number(summary.earnedRewards) / 1e18} VANA`);
    • Stakes VANA to an entity in the VanaPool protocol.

      Parameters

      • params: {
            entityId: bigint;
            amount: string | bigint;
            recipient?: `0x${string}`;
            minShares?: bigint;
        }

        The staking parameters

        • entityId: bigint

          The ID of the entity to stake to

        • amount: string | bigint

          The amount of VANA to stake (in wei, or as a string like "1.5" for 1.5 VANA)

        • Optionalrecipient?: `0x${string}`

          Optional recipient address for the shares (defaults to the sender)

        • OptionalminShares?: bigint

          Optional minimum shares to receive (slippage protection, defaults to 0)

      • Optionaloptions: TransactionOptions

      Returns Promise<`0x${string}`>

      The transaction hash

      This method stakes native VANA tokens to a specified entity. The staker will receive shares in proportion to their stake amount. A bonding period applies during which rewards cannot be fully claimed without penalty.

      Requires a wallet client to be configured in the Vana constructor.

      When wallet client is not configured

      // Stake 100 VANA to entity 1
      const txHash = await vana.staking.stake({
      entityId: 1n,
      amount: "100", // 100 VANA
      });
      console.log(`Staked! Transaction: ${txHash}`);

      // Stake with slippage protection
      const txHash2 = await vana.staking.stake({
      entityId: 1n,
      amount: parseEther("50"), // 50 VANA in wei
      minShares: parseEther("49"), // Expect at least 49 shares
      });
    • Gets the maximum amount of VANA that can be unstaked in a single transaction.

      Parameters

      • staker: `0x${string}`

        The address of the staker

      • entityId: bigint

        The ID of the entity

      Returns Promise<
          {
              maxVana: bigint;
              maxShares: bigint;
              limitingFactor: number;
              isInBondingPeriod: boolean;
          },
      >

      Object containing maxVana, maxShares, limitingFactor, and isInBondingPeriod

      This calls the contract's getMaxUnstakeAmount which returns the minimum of:

      1. The withdrawable VANA (costBasis if in bonding period, shareValue if eligible)
      2. The entity's activeRewardPool (what the entity has available)
      3. The treasury balance (what can be paid out)

      The limiting factor indicates what's constraining the unstake:

      • 0 = user shares/costBasis
      • 1 = activeRewardPool
      • 2 = treasury
      const result = await vana.staking.getMaxUnstakeAmount('0x742d35...', 1n);
      console.log(`Max unstake: ${Number(result.maxVana) / 1e18} VANA`);
      console.log(`Max shares: ${result.maxShares}`);
      console.log(`In bonding period: ${result.isInBondingPeriod}`);
    • Computes the new bonding period end timestamp after adding stake.

      Parameters

      • params: { staker: `0x${string}`; entityId: bigint; stakeAmount: bigint }

        The parameters for computing the new bonding period

        • staker: `0x${string}`

          The address of the staker

        • entityId: bigint

          The ID of the entity

        • stakeAmount: bigint

          The amount of VANA to stake (in wei)

      Returns Promise<
          {
              newEligibilityTimestamp: bigint;
              newRemainingBondingTime: bigint;
              currentEligibilityTimestamp: bigint;
              currentRemainingBondingTime: bigint;
              currentShares: bigint;
              estimatedNewShares: bigint;
              totalSharesAfter: bigint;
              bondingPeriodDuration: bigint;
              currentTimestamp: bigint;
          },
      >

      Object containing the new eligibility timestamp and related info

      When a staker adds more stake to an existing position, the reward eligibility timestamp is recalculated as a weighted average of the existing and new positions. This function allows you to preview what the new eligibility timestamp would be without executing the stake transaction.

      The formula used (matching the contract):

      newEligibility = (existingShares * existingEligibility + newShares * newEligibility) / totalShares
      

      Where newEligibility for the incoming stake is currentTimestamp + bondingPeriod.

      // Preview bonding period after staking 100 VANA
      const preview = await vana.staking.computeNewBondingPeriod({
      staker: '0x742d35...',
      entityId: 1n,
      stakeAmount: parseEther("100"),
      });
      console.log(`New eligibility: ${new Date(Number(preview.newEligibilityTimestamp) * 1000)}`);
      console.log(`Remaining bonding time: ${Number(preview.newRemainingBondingTime) / 86400} days`);
    • Unstakes VANA from an entity in the VanaPool protocol.

      Parameters

      • params: { entityId: bigint; amount: string | bigint; maxShares?: bigint }

        The unstaking parameters

        • entityId: bigint

          The ID of the entity to unstake from

        • amount: string | bigint

          The amount of VANA to unstake (in wei, or as a string like "1.5" for 1.5 VANA)

        • OptionalmaxShares?: bigint

          Maximum shares to burn for slippage protection (defaults to 0, no protection)

      • Optionaloptions: TransactionOptions

      Returns Promise<`0x${string}`>

      The transaction hash

      This method unstakes native VANA tokens from a specified entity. The amount that can be unstaked depends on whether the staker is in the bonding period:

      • During bonding period: Only cost basis can be withdrawn; rewards are forfeited
      • After bonding period: Full current value (cost basis + rewards) can be withdrawn

      Use getMaxUnstakeAmount to determine the maximum amount that can be unstaked.

      Requires a wallet client to be configured in the Vana constructor.

      When wallet client is not configured

      // Get max unstake amount first
      const maxUnstake = await vana.staking.getMaxUnstakeAmount(address, 1n);

      // Unstake the maximum amount with slippage protection
      const txHash = await vana.staking.unstake({
      entityId: 1n,
      amount: maxUnstake.maxVana,
      maxShares: maxUnstake.maxShares,
      });
      console.log(`Unstaked! Transaction: ${txHash}`);