Copy import { ShyftSdk , Network } from "@shyft-to/js" ;
const SHYFT_API_KEY = "YOUR_SHYFT_API_KEY" ;
const shyft = new ShyftSdk ({ apiKey : SHYFT_API_KEY , network : Network .Mainnet });
async function getPoolDetails (poolAddress) {
//querying pool details with LbPair address
const operationsDoc = `
query MyQuery {
meteora_dlmm_LbPair(
where: {pubkey: {_eq: ${ JSON .stringify (poolAddress) } }}
) {
activeId
tokenXMint
tokenYMint
pubkey
lastUpdatedAt
}
}
` ; //you can cherrypick the fields as per your requirement
const result = await fetch (
`https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`, //SHYFT's GQL endpoint
{
method : "POST" ,
body : JSON .stringify ({
query : operationsDoc ,
variables : {} ,
operationName : "MyQuery" ,
}) ,
} ,
);
const { errors , data } = await result .json ();
if (errors) {
return {
meteora_dlmm_LbPair : [] ,
};
}
return data;
}
async function getPositionsByPool (poolAddress) {
//querying pool details with LbPair address
const operationsDoc = `
query MyQuery {
meteora_dlmm_Position(
where: {lbPair: {_eq: ${ JSON .stringify (poolAddress) } }}
) {
_lamports
lastUpdatedAt
lbPair
owner
pubkey
}
meteora_dlmm_PositionV2(
where: {lbPair: {_eq: ${ JSON .stringify (poolAddress) } }}
) {
_lamports
lastUpdatedAt
lbPair
owner
pubkey
}
}
` ; //you can cherrypick the fields as per your requirement
const result = await fetch (
`https://programs.shyft.to/v0/graphql/accounts?api_key=${SHYFT_API_KEY}&network=mainnet-beta`, //SHYFT's GQL endpoint
{
method : "POST" ,
body : JSON .stringify ({
query : operationsDoc ,
variables : {} ,
operationName : "MyQuery" ,
}) ,
} ,
);
const { errors , data } = await result .json ();
if (errors) {
return {
positionsV1 : [] ,
positionV2 : [] ,
};
}
let positionsV1 = [];
let positionsV2 = [];
if ( data . meteora_dlmm_Position . length > 0 ) {
positionsV1 = data .meteora_dlmm_Position;
}
if ( data . meteora_dlmm_PositionV2 . length > 0 ) {
positionsV2 = data .meteora_dlmm_PositionV2;
}
return {
positionsV1 : positionsV1 ,
positionsV2 : positionsV2 ,
};
}
async function getUserWithdrawsByPositionV1 (positionAddress , tokenX , tokenY) {
let genesisTxnReached = false ;
let removeLiquidityTxns = [];
let lastSignature = undefined ;
while ( ! genesisTxnReached) {
const transactions = await shyft . transaction .history ({
account : positionAddress ,
network : Network .Mainnet ,
txNum : 10 ,
beforeTxSignature : lastSignature
});
transactions .map ((txn) => {
if ( txn .type === "REMOVE_LIQUIDITY" )
removeLiquidityTxns .push (txn);
});
if ( transactions . length < 10 ){
genesisTxnReached = true ;
break ;
}
lastSignature = transactions[ transactions . length - 1 ].signatures[ 0 ];
}
const removedLiquidityDetails = [];
if ( removeLiquidityTxns . length > 0 ){
removeLiquidityTxns .forEach ((removeLiquidityTxn) => {
let eachRemovedTxn = {
"txn_id" : removeLiquidityTxn .signatures[ 0 ] ,
"onchain_timestamp" : removeLiquidityTxn .timestamp ,
};
removeLiquidityTxn . actions .map ((action) => {
if ( action .type === "REMOVE_LIQUIDITY" ){
action . info . liquidity_removed .map ((liquidityDetails) => {
if ( liquidityDetails .token_address === tokenX){
eachRemovedTxn = {
... eachRemovedTxn ,
"tokenX_amount" : liquidityDetails .amount_raw ,
"tokenX_address" : liquidityDetails .token_address
}
}
if ( liquidityDetails .token_address === tokenY){
eachRemovedTxn = {
... eachRemovedTxn ,
"tokenY_amount" : liquidityDetails .amount_raw ,
"tokenY_address" : liquidityDetails .token_address
}
}
})
}
})
removedLiquidityDetails .push (eachRemovedTxn);
});
}
return removedLiquidityDetails;
}
async function getUserWithdrawsByPositionV2 (ownerAddress , positionAddress , lbPair , tokenX , tokenY) {
let genesisTxnReached = false ;
let tokenTransferTxns = [];
let lastSignature = undefined ;
while ( ! genesisTxnReached) {
const transactions = await shyft . transaction .history ({
account : positionAddress ,
network : Network .Mainnet ,
txNum : 10 ,
beforeTxSignature : lastSignature
});
transactions .map ((txn) => {
if ( txn .type === "TOKEN_TRANSFER" )
{
txn . actions .map ((action) => {
if(action.type === "TOKEN_TRANSFER" && action.info.sender === lbPair && action.info.receiver === ownerAddress){
tokenTransferTxns .push (txn)
}
})
}
});