Setup a LP Token Balance Checker on Solana
This guide will help you set up and use a script to fetch the LP (Liquidity Provider) token balance from a pool such as Meteora's USDC-USDT Pool. The script fetches the LP token balance in both Lamports and UI units. The UI balance is useful for checking against human-readable thresholds, such as a value of $1000 in the pool.
Pre-requisites
Ensure Node.js and npm (Node Package Manager) are installed on your system. You can verify the installation by running the following commands:
node -v
npm -v
Installation
Step 1: Install ts-node
Globally
ts-node
is a TypeScript execution environment for node. Install it globally using npm:
npm install -g ts-node
Step 2: Install Required Dependencies
Run the following command to install the necessary libraries:
npm install @metaplex-foundation/mpl-token-metadata @metaplex-foundation/mpl-toolbox @metaplex-foundation/umi @metaplex-foundation/umi-bundle-defaults
Step 3: Install Development Dependency
Install TypeScript as a development dependency:
npm i
Script Setup
Create a file named getLPBalance.ts
and paste the following code snippet into it:
import { mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata";
import {
findAssociatedTokenPda,
safeFetchMint,
safeFetchToken,
} from "@metaplex-foundation/mpl-toolbox";
import { publicKey } from "@metaplex-foundation/umi";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
const getLPBalance = async (
userPublicKeyString: string, // USER WALLET ADDRESS
mintPublicKeyString: string, // LP MINT ADDRESS
) => {
try {
// Get RPC from Helius or any other RPC provider of your choice
const endpoint = "ADD YOUR RPC HERE";
const umi = createUmi(endpoint).use(mplTokenMetadata());
const userPublicKey = publicKey(userPublicKeyString);
const mint = publicKey(mintPublicKeyString);
const associatedTokenAccount = findAssociatedTokenPda(umi, {
mint,
owner: userPublicKey,
});
const mintData = await safeFetchMint(umi, mint);
const userPublicKeyData = await safeFetchToken(
umi,
associatedTokenAccount[0]
);
const balanceInLamports = userPublicKeyData?.amount;
let uiBalance = BigInt(0);
if (balanceInLamports && mintData) {
uiBalance = balanceInLamports / BigInt(10 ** mintData?.decimals);
} else {
// Handle cases where balance or mint data is not available
console.error("Balance or mint data not available.");
}
// LAMPORTS AMOUNT
console.log(balanceInLamports);
// UI AMOUNT
console.log(uiBalance);
return {
balanceInLamports,
uiBalance
};
} catch (error) {
console.error(error);
}
};
Usage
The getLPBalance
function fetches the LP token balance of a given user wallet address and LP mint address. It returns the balance in both Lamports and UI units.
Parameters
userPublicKeyString
: The public key of the user wallet.mintPublicKeyString
: The public key of the LP mint address.
Example
(async () => {
const result = await getLPBalance(
"USER_WALLET_PUBLIC_KEY", // Replace with actual user wallet public key
"LP_MINT_PUBLIC_KEY" // Replace with actual LP mint public key
);
if (result) {
const { balanceInLamports, uiBalance } = result;
// Check if the user has provided $1000 worth in the LP Pool
const threshold = BigInt(1_000);
if (uiBalance > threshold) {
console.log("User has provided more than $1000 worth in the LP Pool.");
} else {
console.log("User has provided less than $1000 worth in the LP Pool.");
}
}
})();
Notes
- Ensure you replace "ADD YOUR RPC HERE" with the endpoint of your chosen RPC provider.
- Adjust the user wallet public key and LP mint public key according to your requirements.
To find the LP Mint Address, visit Meteora's USDC-USDT Pool and locate the LP Mint Address as shown in the image below.
This script provides a basic example of fetching LP token balances using the Metaplex Foundation's libraries. You can extend or modify it to suit your specific use case.
Next Steps: Setting Up Onchain API Task App to verify user task completions
Now that you have set up the LP token balance checker and have your endpoint, you can use this to set up an Onchain API Task App (opens in a new tab) to verify user task completions on the Solana blockchain.
This will allow you to automate the verification process and ensure that users are rewarded accurately based on their token balances.