meteora-dbc-toolkit
Version:
TypeScript toolkit for building buy/sell transactions against Meteora Dynamic Bonding Curves on Solana.
64 lines • 2.85 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 spl_token_1 = require("@solana/spl-token");
const bn_js_1 = __importDefault(require("bn.js"));
const basic_util_1 = require("./utils/basic.util");
const dynamic_bonding_curve_sdk_1 = require("@meteora-ag/dynamic-bonding-curve-sdk");
class MeteoraTrader {
constructor(connection) {
this.connection = connection;
this.dbcClient = new dynamic_bonding_curve_sdk_1.DynamicBondingCurveClient(connection, "confirmed");
}
async buy(params) {
const { mint, buyer, buyAmountLamports, slippageBasisPoints = basic_util_1.DEFAULT_SLIPPAGE_BASIS_POINTS } = params;
const poolAcc = await this.dbcClient.state.getPoolByBaseMint(mint);
if (!poolAcc) {
throw new Error(`DBC pool not found for mint: ${mint.toBase58()}`);
}
const pool = poolAcc.account;
const config = await this.dbcClient.state.getPoolConfig(pool.config);
const quoteMint = config.quoteMint;
const isMigrated = pool.isMigrated;
if (isMigrated || !quoteMint.equals(spl_token_1.NATIVE_MINT)) {
throw new Error(`this is not meteora dynamic bonding curve`);
}
const tx = await this.dbcClient.pool.swap({
owner: buyer,
amountIn: new bn_js_1.default(buyAmountLamports),
minimumAmountOut: new bn_js_1.default(0),
swapBaseForQuote: false,
pool: poolAcc.publicKey,
referralTokenAccount: null,
});
return tx;
}
async sell(params) {
const { mint, seller, sellTokenAmount, slippageBasisPoints = basic_util_1.DEFAULT_SLIPPAGE_BASIS_POINTS } = params;
const poolAcc = await this.dbcClient.state.getPoolByBaseMint(mint);
if (!poolAcc) {
throw new Error(`DBC pool not found for mint: ${mint.toBase58()}`);
}
const pool = poolAcc.account;
const config = await this.dbcClient.state.getPoolConfig(pool.config);
const quoteMint = config.quoteMint;
const isMigrated = pool.isMigrated;
if (isMigrated || !quoteMint.equals(spl_token_1.NATIVE_MINT)) {
throw new Error(`this is not meteora dynamic bonding curve`);
}
const tx = await this.dbcClient.pool.swap({
owner: seller,
amountIn: new bn_js_1.default(sellTokenAmount),
minimumAmountOut: new bn_js_1.default(0),
swapBaseForQuote: true,
pool: poolAcc.publicKey,
referralTokenAccount: null,
});
return tx;
}
}
exports.MeteoraTrader = MeteoraTrader;
//# sourceMappingURL=index.js.map