npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stakewise/methods

v0.1.1

Published

StakeWise Methods

Downloads

12

Readme

StakeWise Methods

Discord

The package contains a JavaScript class that provides methods to deposit ETH in staking and get deposit data.

Create an instance of a class

To create methods instance you need to provide ethers provider, wallet address and referrer address:

import Methods from '@stakewise/methods'

const methods = new Methods({
  provider, // ethers provider - https://docs.ethers.io/v5/api/providers/provider/
  sender, // wallet address
  referrer, // referrer address (optional)
})

Fetching token balances

Method getBalances returns balances of SWISE tokens, staked tokens, reward tokens and native tokens.

A balance for every token it is an object called below as TokenValue. It contains properties:

  • value - token balance in BigNumber
  • and fiatValues - object that contains fiat values in USD, EUR and GBP
type TokenValue = {
  value: BigNumber
  fiatValues: {
    usd: number
    eur: number
    gbp: number
  }
}
try {
  const balances = await methods.getBalances()

  const {
    swiseTokenBalance, // TokenValue with amount of SWISE 
    stakedTokenBalance, // TokenValue with amount of staked tokens (e.g. sETH2)
    rewardTokenBalance, // TokenValue with amount of reward tokens (e.g. rETH2)
    nativeTokenBalance, // TokenValue with amount of native tokens (e.g. ETH)
  } = balances

  // Formatted balance of native tokens (e.g. 0.318871759160055215)
  const nativeTokenValue = formatEther(nativeTokenBalance.value)

  // Formatted balance in USD (e.g. $956.35)
  const nativeTokenFiatValue = `$${nativeTokenBalance.fiatValues.usd}`
} catch (error) {
  console.error(error)
}

To get balances we make requests:

  1. Get native token balance: provider.getBalance(sender)

  2. Get staked token balance: stakedTokenContract.balanceOf(sender) Staked token contract address: 0xFe2e637202056d30016725477c5da089Ab0A043A

  3. Get reward token balance: rewardTokenContract.balanceOf(sender) Reward token contract address: 0x20BC832ca081b91433ff6c17f85701B6e92486c5

  4. Get SWISE token balance: swiseTokenContract.balanceOf(sender) SWISE token contract address: 0x48C3399719B582dD63eB5AADf12A40B4C3f52FA2

  5. Get fiat rates to calculate fiat balances: Promise.all([ ethUsd.latestAnswer(), eurUsd.latestAnswer(), gbpUsd.latestAnswer(), ])

EthUsd rate contract address: 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419

EurUsd rate contract address: 0xb49f677943BC038e9857d61E7d053CaA2C1734C1

GbpUsd rate contract address: 0x5c0Ab2d9b5a7ed9f470386e82BB36A3613cDd4b5

Fetching staking APR

StakingApr is a number of the annual yield percent.

try {
  const stakingApr = await methods.getStakingApr()
} catch (error) {
  console.error(error)
}

Reward token contract address: 0x20BC832ca081b91433ff6c17f85701B6e92486c5

  • Get reward token protocol fee: rewardTokenContract.protocolFee()

  • Get pool stats: https://api.stakewise.io/pool-stats/ Returns object with validatorsAPR number

Staking APR calculation:

const { validatorsAPR } = poolStats

const maintainerFee = protocolFee.toNumber()
const stakingAPR = validatorsAPR - validatorsAPR * (maintainerFee / 10_000)

Deposit

The deposit method allows sending available amount of native tokens (e.g. ETH) and receiving as the result staked tokens (e.g. sETH2).

Staked tokens by default will be submitted to the sender wallet, but could be submitted to another wallet (if address is provided).

try {
  const transaction = await methods.deposit({
    amount, // BigNumber with amount to deposit
    address, // Optional recipient wallet address (current wallet address by default)
  })
  
  // Wait for transaction confirmation
  if (transaction && transaction.hash) {
    await provider.waitForTransaction(transaction.hash)
  }
} catch (error) {
  console.error(error)
}

Pool contract address: 0xC874b064f465bdD6411D45734b56fac750Cda29A

First we are estimating gas, and after that sending deposit.

To estimate gas we call this.provider.getFeeData() to get { maxFeePerGas, maxPriorityFeePerGas } and call pool contract gas estimation method.

Depending on provided referrer and receiver addresses there are different methods should be called.

Gas estimation with referrer

On each call we have to provide parameters: from which is the sender address, and value which is the BigNumber amount to deposit.

If receiver address is the same as the address of the current wallet, we call:

poolContract.estimateGas.stakeWithReferrer(referrer, { from, value })

If not, we provide receiver address and call:

poolContract.estimateGas.stakeWithReferrerOnBehalf(referrer, address, { from, value })

Gas estimation without referrer

If receiver address is the same as the current wallet address, we call:

poolContract.estimateGas.stake({ from, value })

If receiver address is not the same as the current wallet address, we call:

poolContract.estimateGas.stakeOnBehalf(address, { from, value })

As the result in each case we'll receive gasLimit (BigNumber). This value we increase on 1000 to be able to spend a bit more gas if it will be needed:

const gasLimit = await estimateGas()

return gasLimit
  .mul(10000)
  .add(1000)
  .div(10000)

The next step is to send deposit, to make the deposit call we need to create a signed contract:

const signer = provider.getUncheckedSigner(address)
const signedContract = poolContract.connect(signer)

Now, after gas estimation we have:

maxFeePerGas: null | BigNumber
maxPriorityFeePerGas: null | BigNumber
gasLimit: BigNumber

We need to provide these parameters to the next contract call to send deposit.

The call is similar to gas estimation since it has different methods that should be called depending on provided referrer and receiver addresses.

Deposit with referrer

If receiver address is the same as the current wallet address, we call:

signedContract.stakeWithReferrer(referrer, { maxFeePerGas, maxPriorityFeePerGas, gasLimit })

If not, we provide receiver address and call:

signedContract.stakeWithReferrerOnBehalf(referrer, address, { maxFeePerGas, maxPriorityFeePerGas, gasLimit })

Deposit without referrer

If receiver address is the same as the current wallet address, we call:

signedContract.stake({ maxFeePerGas, maxPriorityFeePerGas, gasLimit })

If receiver address is not the same as the current wallet address, we call:

signedContract.stakeOnBehalf(address, { maxFeePerGas, maxPriorityFeePerGas, gasLimit })

As the result in each case we'll receive ContractTransaction. We can wait until transaction will be confirmed and after that we can call getBalances() to update balances