Skip to content

SWBERA Vault APR Calculation

The SWBERA vault APR represents the annualized return rate for staking WBERA in the vault. This guide explains how to calculate this APR programmatically.

Overview

The SWBERA vault APR is calculated by measuring the change in share value over a 24-26 hour period and annualizing the return. This provides a real-time view of the yield being generated by the vault.

Requirements

  • NodeJS v22.17.1 or greater
  • sWBERA Vault Address
  • Berachain RPC Endpoint

Calculation Method

Step 1: Get Share Value

Use the previewRedeem function to determine how much WBERA you would receive for 1 share:

solidity
function previewRedeem(uint256 shares) external view returns (uint256 assets)

Parameters:

  • shares: Number of shares (use 1 share with 18 decimals: 1000000000000000000)

Returns:

  • assets: Amount of WBERA you would receive for the given shares

Step 2: Calculate 24-26 Hour Return

  1. Get the current share value: previewRedeem(1e18) at current block
  2. Get the share value 24-26 hours ago: previewRedeem(1e18) at block 24-26 hours ago
  3. Calculate the difference: current_value - previous_value

Step 3: Annualize the Return

Multiply the 24-26 hour return by 365 to get the annualized rate:

APR=CurrentShareValuePreviousShareValuePreviousShareValue×365×100

Implementation Example

Here's a complete Node.js script to calculate the SWBERA vault APR:

File: script.js

js
const { ethers } = require("ethers");

// Configuration
const RPC_URL = "https://rpc.berachain.com/";
const VAULT_ADDRESS = "0x118D2cEeE9785eaf70C15Cd74CD84c9f8c3EeC9a";
const VAULT_ABI = [
  "function previewRedeem(uint256 shares) external view returns (uint256 assets)"
];

async function calculateSWBERAAPR() {
  try {
    const provider = new ethers.JsonRpcProvider(RPC_URL);
    const currentBlock = await provider.getBlockNumber();

    // Calculate blocks for 24-26 hours (assuming 2-second block time)
    const blocksPerHour = 3600 / 2; // 1800 blocks per hour
    const blocksPerDay = blocksPerHour * 24; // 43200 blocks per day
    const blocksPer26Hours = blocksPerHour * 26; // 46800 blocks for 26 hours

    const previousBlock24h = currentBlock - blocksPerDay;
    const previousBlock26h = currentBlock - blocksPer26Hours;

    const vaultContract = new ethers.Contract(VAULT_ADDRESS, VAULT_ABI, provider);

    // Get share values
    const currentShareValue = await vaultContract.previewRedeem(ethers.parseEther("1"));
    const previousShareValue24h = await vaultContract.previewRedeem(
      ethers.parseEther("1"),
      {
        blockTag: previousBlock24h
      }
    );
    const previousShareValue26h = await vaultContract.previewRedeem(
      ethers.parseEther("1"),
      {
        blockTag: previousBlock26h
      }
    );

    // Calculate returns
    const return24h = currentShareValue - previousShareValue24h;
    const return26h = currentShareValue - previousShareValue26h;

    // Calculate APRs
    const apr24h = (return24h * 365n * 10000n) / previousShareValue24h;
    const apr26h = (return26h * 365n * 10000n) / previousShareValue26h;

    // Calculate average APR
    const avgAPR = (Number(apr24h) + Number(apr26h)) / 2;

    return {
      currentShareValue: ethers.formatEther(currentShareValue),
      previousShareValue24h: ethers.formatEther(previousShareValue24h),
      previousShareValue26h: ethers.formatEther(previousShareValue26h),
      return24h: ethers.formatEther(return24h),
      return26h: ethers.formatEther(return26h),
      apr24h: Number(apr24h) / 100,
      apr26h: Number(apr26h) / 100,
      avgAPR: avgAPR
    };
  } catch (error) {
    console.error("Error calculating APR:", error.message);
    throw error;
  }
}

// Usage
calculateSWBERAAPR()
  .then((result) => {
    console.log("APR Results:", result);
  })
  .catch((error) => {
    console.error("Calculation failed:", error);
  });

When you run the script, you'll get output similar to this:

bash
# FROM: /
node script.js;
# [Expected Similar Output]:
# {
#   "currentShareValue": "1.044773554183778978",
#   "previousShareValue24h": "1.041352132320814436",
#   "previousShareValue26h": "1.041352132320814436",
#   "return24h": "0.003421421862964542",
#   "return26h": "0.003421421862964542",
#   "apr24h": 119.92,
#   "apr26h": 119.92,
#   "avgAPR": 119.92
# }

Calculation Breakdown

Using the values from the sample output:

Formula: $$APR = \frac{Current Share Value - Previous Share Value}{Previous Share Value} \times 365 \times 100$$

24-hour calculation:

  • Current Share Value: 1.044773554183778978 $WBERA
  • Previous Share Value (24h ago): 1.041352132320814436 $WBERA
  • 24h Return: 0.003421421862964542 $WBERA
APR=0.0034214218629645421.041352132320814436×365×100=119.92%

This shows the $SWBERA vault is currently generating approximately 119.92% APR based on the 24-hour performance.

Key Considerations

Time Frame

  • Current: 24-26 hour measurement period for more stable calculations
  • Future: Planning to move to weekly measurements for even more stable APR calculations

Precision

  • All calculations use 18 decimal precision
  • Share values are denominated in $WBERA
  • APR is calculated in basis points and converted to percentage

Block Considerations

  • Ensure you have access to historical block data
  • Account for potential chain reorganizations
  • Consider using multiple data points for more accurate calculations
  • Use 24-26 hour lookback to account for slight variations in block timing