meteora-dbc-toolkit
Version:
TypeScript toolkit for building buy/sell transactions against Meteora Dynamic Bonding Curves on Solana.
72 lines • 3.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MeteoraTrader = void 0;
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const bn_js_1 = __importDefault(require("bn.js"));
const basic_util_1 = require("./utils/basic.util");
class MeteoraTrader {
constructor(connection, dbcClient) {
this.connection = connection;
this.dbcClient = dbcClient;
}
async buy(params) {
const { mint, buyer, buyAmountLamports, slippageBasisPoints = basic_util_1.DEFAULT_SLIPPAGE_BASIS_POINTS, commitment } = params;
const pool = await this.dbcClient.getPoolByMint(mint, commitment ?? "finalized");
if (!pool) {
throw new Error(`Meteora DBC pool not found for mint: ${mint.toBase58()}`);
}
const expectedTokensOut = pool.calculateBuyAmount(buyAmountLamports);
const maxTokensOut = (0, basic_util_1.calculateWithSlippageBuy)(expectedTokensOut, slippageBasisPoints);
const associatedUser = await (0, spl_token_1.getAssociatedTokenAddress)(mint, buyer, false);
const tx = new web3_js_1.Transaction();
try {
await (0, spl_token_1.getAccount)(this.connection, associatedUser);
}
catch (_e) {
tx.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(buyer, associatedUser, buyer, mint));
}
const ix = await this.dbcClient.createBuyInstruction({
pool: pool.poolAddress,
user: buyer,
mint,
lamportsIn: new bn_js_1.default(buyAmountLamports.toString()),
maxTokensOut,
});
tx.add(ix);
return {
transaction: tx,
estimatedAmount: expectedTokensOut,
amountWithSlippage: maxTokensOut,
};
}
async sell(params) {
const { mint, seller, sellTokenAmount, slippageBasisPoints = basic_util_1.DEFAULT_SLIPPAGE_BASIS_POINTS, commitment } = params;
const pool = await this.dbcClient.getPoolByMint(mint, commitment ?? "finalized");
if (!pool) {
throw new Error(`Meteora DBC pool not found for mint: ${mint.toBase58()}`);
}
const expectedLamportsOut = pool.calculateSellAmount(sellTokenAmount);
const minLamportsOut = (0, basic_util_1.calculateWithSlippageSell)(expectedLamportsOut, slippageBasisPoints);
const associatedUser = await (0, spl_token_1.getAssociatedTokenAddress)(mint, seller, false);
const tx = new web3_js_1.Transaction();
const ix = await this.dbcClient.createSellInstruction({
pool: pool.poolAddress,
user: seller,
mint,
tokensIn: new bn_js_1.default(sellTokenAmount.toString()),
minLamportsOut,
});
tx.add(ix);
return {
transaction: tx,
estimatedAmount: expectedLamportsOut,
amountWithSlippage: minLamportsOut,
};
}
}
exports.MeteoraTrader = MeteoraTrader;
//# sourceMappingURL=meteoraTrader.js.map