0.1.0 • Published 6 months ago

@normalfinance/pools-client v0.1.0

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
6 months ago

Orca Pools Client SDK

Overview

This package provides developers with low-level functionalities for interacting with the Pool Program on Solana. It serves as a foundational tool that allows developers to manage and integrate detailed operations into their Typescript projects, particularly those related to Normal's Pool Program. While a high-level SDK is available for easier integration, @normalfinance/pools, this package offers more granular control for advanced use cases.

Note: This SDK uses Solana Kit. It is not compatible with Solana Web3.js.

Key Features

  • Codama Client: The package includes a set of generated client code based on the Pool Program IDL. This ensures all the necessary program information is easily accessible in a structured format and handles all decoding and encoding of instructions and account data, making it much easier to interact with the program.
  • GPA (Get Program Accounts) Filters: This feature contains utilities to add filters to program accounts, allowing developers to fetch program account data more selectively and efficiently.
  • PDA (Program Derived Addresses) Utilities: This feature contains utility functions that help derive Program Derived Addresses (PDAs) for accounts within the Pool Program, simplifying address generation for developers.

Installation

You can install the package via npm:

npm install @normalfinance/pools-client

Usage

Here are some basic examples of how to use the package.

Fetching Pool Accounts with Filters

The following example demonstrates how to fetch Pools accounts based on specific filters, using the GPA utilities:

import { createSolanaRpc, address, devnet } from '@solana/kit';
import { fetchAllPoolWithFilter, poolTokenMintAFilter } from "@normalfinance/pools-client";

const rpc = createSolanaRpc(devnet("https://api.devnet.solana.com"));

const tokenMintA = address("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k"); //DevUSDC
const filter = poolTokenMintAFilter(tokenMintA);

const accounts = await fetchAllPoolWithFilter(rpc, filter);
console.log(accounts);

Deriving a PDA

To derive a PDA for a Pool account, you can use the getPoolAddress PDA utility.

import { getPoolAddress } from "@normalfinance/pools-client";
import { address } from '@solana/kit';

const poolConfigAddress = address("FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR");
const tokenMintA = address("So11111111111111111111111111111111111111112"); //wSOL
const tokenMintB = address("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k"); //DevUSDC
const tickSpacing = 64;

const poolPda = await getPoolAddress(
    poolConfigAddress,
    tokenMintA,
    tokenMintB,
    tickSpacing,
);
console.log(poolPda);

Example: Initialize Pool Instruction

The following example demonstrates how to create an InitializePool instruction using the Codama-IDL autogenerated code:

import { getInitializePoolV2Instruction, getTokenBadgeAddress, getPoolAddress, getFeeTierAddress } from "@normalfinance/pools-client";
import { address, generateKeyPairSigner } from '@solana/kit';

const poolConfigAddress = address("FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR");
const tokenMintA = address("So11111111111111111111111111111111111111112"); // wSOL
const tokenMintB = address("BRjpCHtyQLNCo8gqRUr8jtdAj5AjPYQaoqbvcZiHok1k"); // DevUSDC
const tokenBadgeA = await getTokenBadgeAddress(poolConfigAddress, tokenMintA)
const tokenBadgeB = await getTokenBadgeAddress(poolConfigAddress, tokenMintB)
const wallet = await generateKeyPairSigner(); // CAUTION: this wallet is not persistent
const tickSpacing = 8;
const pool = await getPoolAddress(poolConfigAddress, tokenMintA, tokenMintB, tickSpacing);
const tokenVaultA = await generateKeyPairSigner();
const tokenVaultB = await generateKeyPairSigner();
const feeTier = await getFeeTierAddress(poolConfigAddress, tickSpacing);
const tokenProgramA = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const tokenProgramB = address("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
const initialSqrtPrice = BigInt(7459106261056563200n);

const initializePoolInstruction = getInitializePoolV2Instruction({
  poolsConfig: poolConfigAddress,
  tokenMintA,
  tokenMintB,
  tokenBadgeA,
  tokenBadgeB,
  funder: wallet,
  pool,
  tokenVaultA,
  tokenVaultB,
  feeTier,
  poolBump: 1,
  tickSpacing,
  tokenProgramA,
  tokenProgramB,
  initialSqrtPrice
});

console.log(initializePoolInstruction);
0.1.0

6 months ago