Skip to content
Access Protocol Docs

Node.js Examples

With this guide we’ll be building custom vanila Typescript transpiled to ES6 Javascript CLI app showcasing usage of our Javascript bindings and basic interaction with Solana blockchain.

Github Repo is here: https://github.com/Access-Labs-Inc/node-js-examples

Install dependencies

  1. Solana CLI tools: https://docs.solana.com/cli/install-solana-cli-tools
  1. Node.js >18.16.0 (stable LTS): https://nodejs.org/en

Bootstraping Typescript project

Let’s create directory to isolate the project:

mkdir access-protocol-project && cd access-protocol-project

Initialize Typescript:

npm i typescript --save-dev
npx tsc --init

Modify tsconfig.json to this:

{
  "compilerOptions": {
    "outDir": "dist",
    "module": "es6", 
    "moduleResolution": "nodenext", 
    "esModuleInterop": true,       
    "forceConsistentCasingInFileNames": true,
    "strict": true,    
    "skipLibCheck": true
  }
}

Update your package.json type to “module” like so:

...
  "type": "module",
...

Add required packages

Add Solana and Access protocol bindings:

npm i @accessprotocol/js
npm i @solana/web3.js

Example scripts

Get details about creator’s pool

Add our first script called creators_pool.ts:

touch creators_pool.ts

This script will display basic information about your pool such as min. required lock amount, total locked amount, who’s the owner of the pool and what percentage of rewards goes to stakers.

Open the file and add this code:

import { Connection, PublicKey } from "@solana/web3.js";
import { StakePool } from "@accessprotocol/js";

// This is example pool on our devnet contract.
// If you have your own, replace this with your own pubkey.
const MY_POOL_PUBKEY = new PublicKey("2hQSDVwJLbtwHzi3CKj8pmiQzLyfKZs5ZDhT1QZdHXv3");

// We want to connect to devnet
const SOLANA_RPC_PROVIDER_URL = "https://api.devnet.solana.com";
const connection = new Connection(SOLANA_RPC_PROVIDER_URL);

const main = async () => {
  let poolAccount: StakePool | undefined = undefined;
  try {
    // Get the account data for my pool.
    poolAccount = await StakePool.retrieve(connection, MY_POOL_PUBKEY);
  } catch (e) {
    console.error("Could not find stake pool account. Error: ", e)
  }

  if (poolAccount) {
    // Our numbers on Solana our represented as BN.js (bignumber) therefore we have to call toNumber()
    // Also, our currency (ACS) has 6 decimal points so we have to divide by 6 decimal points to get ACS
    console.log(`Minimum lock amount is ${poolAccount.minimumStakeAmount.toNumber() / 10 ** 6} ACS`);
    console.log(`Total locked amount is ${poolAccount.totalStaked.toNumber() / 10 ** 6} ACS`);
    // Each pubkey on Solana is encoded into base58 ()
    console.log(`Owner's pubkey is ${poolAccount.owner.toBase58()}`);
    console.log(`Stakers receive ${poolAccount.stakersPart.toNumber()}% of rewards`);
  } else {
    console.error("Pool account not found.");
  }
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

To be able to run this code we have to add to our package.json this lines:

...
  "scripts": {
    "creators-pool": "npx tsc && node dist/creators_pool.js"
  },
...

And run the script with:

npm run creators-pool

and you should see something like this:

> creators-pool
> npx tsc && node dist/creators_pool.js

Minimum lock amount is 1000 ACS
Total locked amount is 49019 ACS
Owner's pubkey is 7Q34nmDP1srbYSQJH5b43heZpPtsssiRZa17hLnx5Gqx
Stakers receive 50% of rewards

Check if user locked tokens against creator’s pool

Add new script called is_user_locked_in_pool.ts:

touch is_user_locked_in_pool.ts

Open the file and add this code:

import { Connection, PublicKey } from "@solana/web3.js";
import { hasValidSubscriptionForPool } from "@accessprotocol/js";

const SOLANA_RPC_PROVIDER_URL = "https://api.devnet.solana.com";
const ACCESS_PROGRAM_PUBKEY = new PublicKey("9LPrKE24UaN9Bsf5rXCS4ZGor9VmjAUxkLCMKHr73sdV");
const MY_POOL_PUBKEY = new PublicKey("2hQSDVwJLbtwHzi3CKj8pmiQzLyfKZs5ZDhT1QZdHXv3");
const USER_PUBKEY = new PublicKey("7Q34nmDP1srbYSQJH5b43heZpPtsssiRZa17hLnx5Gqx");

const main = async () => {
  const connection = new Connection(SOLANA_RPC_PROVIDER_URL);
  const eligible = await hasValidSubscriptionForPool(connection, ACCESS_PROGRAM_PUBKEY, MY_POOL_PUBKEY, USER_PUBKEY);
  if (eligible) {
    console.log("User is eligible for benefits of the pool.");
  } else {
    console.log("User is NOT eligable for benefits of the pool.");
  }
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

To be able to run this code we have to add to our package.json this lines:

...
  "scripts": {
    ...
    "is-user-locked-in-pool": "npx tsc && node dist/is_user_locked_in_pool.js"
  },
...

And run the script with:

npm run is-user-locked-in-pool

and you should see something like this:

> is-user-locked-in-pool
> npx tsc && node dist/is_user_locked_in_pool.js

Amount locked is 1000 ACS
No airdrops found for this user.
User is eligable for benefits of the pool.