How to use Anchor to Retrieve Serum Staking Analytics

Serum Pulse
2 min readJul 4, 2021

Getting data out of the Solana blockchain is really hard.. or well it used to be. Thanks to Anchor, Solana’s Sealevel framework (developed by the great and powerful https://twitter.com/armaniferrante), this has gotten a lot simpler.

In this tutorial, I will walk you through how the Serum Pulse team uses Anchor to retrieve the staking reward metrics for SRM and MSRM

Requirements:

  • nodeJS
  • macOS

Note** The version of anchor used must match the version used to create the program. In this case, the serum staking repo uses Anchor 0.4.4 https://github.com/project-serum/stake.git and that's what we will use.

In this tutorial, we will be retrieving the rewards given out as stake drops.

  • First, we need to install Solana and create a wallet run
sh -c "$(curl -sSfL https://release.solana.com/v1.7.4/install)"
solana-keygen new
  • Then we need to create a directory to store our code. Once that has been complete, create an index.js file and run npm init. Your directory should have two files now index.js and package.json
  • Next, let's install the correct package.
npm install -s @project-serum/anchor@0.4.4
  • Lastly, we need the IDL file.

“An interface description language or interface definition language (IDL), is a generic term for a language that lets a program or object written in one language communicate with another program written in an unknown language”

There are a few ways to get it. You can clone the staking contract and generate it (https://github.com/project-serum/stake) or fetch it using the anchor idl fetch program_idcommand. For simplicity sake, I have attached it here:

https://gist.github.com/Dassy23/1af0acd427c6d403905e9245dae2a456

Save this file into your project directory as registry.json

Now let's write our index.js code to access the rewards!

  • At the top of the file add the following:
const anchor = require('@project-serum/anchor');
const provider = anchor.Provider.env();
anchor.setProvider(provider);
  • Next, let's create our main function:
async function main(){}
  • Inside the main add the following:
const registry_idl = JSON.parse(require('fs').readFileSync('./registry.json', 'utf8'));const programId = new anchor.web3.PublicKey('GrAkKfEpTKQuVHG2Y97Y2FF4i7y7Q5AHLK94JBy7Y5yv');const registry = new anchor.Program(registry_idl, programId);const allRewards = await registry.account.rewardVendor.all();

In the above code, we are parsing the IDL and using Anchor to connect to the staking contract program id: GrAkKfEpTKQuVHG2Y97Y2FF4i7y7Q5AHLK94JBy7Y5yv

This allows us to start retrieving all the rewards.

  • Next, add the following code to parse the rewards into a readable format and log them out:
const parsedRewards =  allRewards.map(x => {return{expired:x.account.expired,expiryReceiver: x.account.expiryReceiver.toBase58(),expiryTs:parseInt(x.account.expiryTs),from:x.account.from.toBase58(),kind:Object.keys(x.account.kind)[0],mint:x.account.mint.toBase58(),nonce:x.account.nonce,poolTokenSuppy:parseInt(x.account.poolTokenSupply),registar:x.account.registrar.toBase58(),rewardEventQCursor:x.account.rewardEventQCursor,startTS:parseInt(x.account.startTs),total:parseInt(x.account.total),vault:x.account.vault.toBase58(),}})
console.log(parsedRewards)
  • Finally, at the bottom of your file add:
main()
  • and run in the terminal:
ANCHOR_PROVIDER_URL=https://solana-api.projectserum.com node index.js

Now in your terminal, you will see a printout of all the rewards! Simple eh?

We will be releasing more tutorials as we embark on this journey to provide the best-in-class analytics for the Serum ecosystem. Follow our Twitter page to keep up to date at https://twitter.com/PulseSerum.

--

--