Created
August 5, 2025 04:38
-
-
Save codewithgun/62c4c3924ba06e6600482103b8821b1e to your computer and use it in GitHub Desktop.
DLMM add liquidity check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DLMM, { StrategyType } from "@meteora-ag/dlmm"; | |
import { clusterApiUrl, Connection, Keypair, PublicKey } from "@solana/web3.js"; | |
import BN from "bn.js"; | |
import fs from "fs"; | |
import os from "os"; | |
const privateKeyFilepath = `${os.homedir()}/.config/solana/mainnet.json`; | |
const rawPrivateKey = fs.readFileSync(privateKeyFilepath, "utf-8"); | |
const payer = Keypair.fromSecretKey(new Uint8Array(JSON.parse(rawPrivateKey))); | |
async function createImbalancePosition( | |
connection: Connection, | |
dlmmPool: DLMM, | |
payer: Keypair, | |
usdcAmount: number, | |
slippage: number | |
) { | |
const activeBin = await dlmmPool.getActiveBin(); | |
const binId = activeBin.binId; | |
console.log("🔵 Bin active ID :", binId.toString()); | |
const totalXAmount = new BN(Math.floor(usdcAmount * 10 ** 6)); | |
const totalYAmount = new BN(0); | |
const positionKP = Keypair.generate(); | |
console.log("🆕 Position Keypair :", positionKP.publicKey.toBase58()); | |
try { | |
const tx = await dlmmPool.initializePositionAndAddLiquidityByStrategy({ | |
positionPubKey: positionKP.publicKey, | |
user: payer.publicKey, | |
totalXAmount, | |
totalYAmount, | |
strategy: { | |
strategyType: StrategyType.Spot, | |
minBinId: binId, | |
maxBinId: binId, | |
}, | |
slippage, | |
}); | |
console.log("🔨 Transaction construite"); | |
tx.sign(payer, positionKP); | |
console.log(tx.serialize().toString("base64")); | |
} catch (err) { | |
console.error("❌ Erreur création de la position :", err); | |
} | |
} | |
export async function createPositionFromPool( | |
poolAddr: PublicKey, | |
usdcAmount = 100, | |
slippage = 1 | |
) { | |
const connection = new Connection(clusterApiUrl("mainnet-beta")); | |
const dlmmInstance = await DLMM.create(connection, poolAddr); | |
await createImbalancePosition( | |
connection, | |
dlmmInstance, | |
payer, | |
usdcAmount, | |
slippage | |
); | |
} | |
const USDC_USDT_POOL = "ARwi1S4DaiTG5DX7S4M4ZsrXqpMD1MrTmbu9ue2tpmEq"; | |
createPositionFromPool(new PublicKey(USDC_USDT_POOL), 50, 1) | |
.then(() => console.log("✅ Done")) | |
.catch((err) => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment