@marinade.finance/kamino-sdk
Version:
136 lines • 7.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeBudgetAndAtaIxns = exports.isSOLMint = exports.getTokenNameFromCollateralInfo = exports.getPriceLowerUpper = exports.getStrategyPriceRangeRaydium = exports.getStrategyPriceRangeOrca = exports.sendTransactionWithLogs = exports.assignBlockInfoToTransaction = exports.createTransactionWithExtraBudget = exports.createAddExtraComputeUnitsTransaction = exports.createAssociatedTokenAccountInstruction = exports.getAssociatedTokenAddress = exports.getAssociatedTokenAddressAndData = exports.DECIMALS_SOL = exports.SOL_MINTS = exports.TOKEN_PROGRAM_ID = exports.ASSOCIATED_TOKEN_PROGRAM_ID = void 0;
const web3_js_1 = require("@solana/web3.js");
const borsh_1 = require("@project-serum/borsh");
const utils_1 = require("./utils");
const whirlpool_sdk_1 = require("@orca-so/whirlpool-sdk");
exports.ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
exports.TOKEN_PROGRAM_ID = new web3_js_1.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
exports.SOL_MINTS = [
new web3_js_1.PublicKey('So11111111111111111111111111111111111111111'),
new web3_js_1.PublicKey('So11111111111111111111111111111111111111112'),
];
exports.DECIMALS_SOL = 9;
async function getAssociatedTokenAddressAndData(connection, mint, owner) {
const ata = await getAssociatedTokenAddress(mint, owner);
const data = await connection.getAccountInfo(ata);
return [ata, data];
}
exports.getAssociatedTokenAddressAndData = getAssociatedTokenAddressAndData;
function getAssociatedTokenAddress(mint, owner, allowOwnerOffCurve = true, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) {
if (!allowOwnerOffCurve && !web3_js_1.PublicKey.isOnCurve(owner.toBuffer()))
throw new Error('Token owner off curve');
const [address] = web3_js_1.PublicKey.findProgramAddressSync([owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId);
return address;
}
exports.getAssociatedTokenAddress = getAssociatedTokenAddress;
function createAssociatedTokenAccountInstruction(payer, associatedToken, owner, mint, programId = exports.TOKEN_PROGRAM_ID, associatedTokenProgramId = exports.ASSOCIATED_TOKEN_PROGRAM_ID) {
const keys = [
{ pubkey: payer, isSigner: true, isWritable: true },
{ pubkey: associatedToken, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: false, isWritable: false },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: web3_js_1.SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: programId, isSigner: false, isWritable: false },
];
return new web3_js_1.TransactionInstruction({
keys,
programId: associatedTokenProgramId,
data: Buffer.alloc(0),
});
}
exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction;
function createAddExtraComputeUnitsTransaction(owner, units) {
const p = new web3_js_1.PublicKey('ComputeBudget111111111111111111111111111111');
const params = { instruction: 0, units, fee: 0 };
const layout = (0, borsh_1.struct)([(0, borsh_1.u8)('instruction'), (0, borsh_1.u32)('units'), (0, borsh_1.u32)('fee')]);
const data = Buffer.alloc(layout.span);
layout.encode(params, data);
const keys = [{ pubkey: owner, isSigner: false, isWritable: false }];
return new web3_js_1.TransactionInstruction({
keys,
programId: p,
data,
});
}
exports.createAddExtraComputeUnitsTransaction = createAddExtraComputeUnitsTransaction;
function createTransactionWithExtraBudget(payer, extraUnits = 400000) {
const tx = new web3_js_1.Transaction();
const increaseBudgetIx = createAddExtraComputeUnitsTransaction(payer, extraUnits);
tx.add(increaseBudgetIx);
return tx;
}
exports.createTransactionWithExtraBudget = createTransactionWithExtraBudget;
async function assignBlockInfoToTransaction(connection, transaction, payer) {
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = payer;
transaction.lastValidBlockHeight = lastValidBlockHeight;
return transaction;
}
exports.assignBlockInfoToTransaction = assignBlockInfoToTransaction;
async function sendTransactionWithLogs(connection, tx, payer, signers, commitment = 'processed', skipPreflight = false) {
let txn = await assignBlockInfoToTransaction(connection, tx, payer);
try {
let res = await (0, web3_js_1.sendAndConfirmTransaction)(connection, txn, signers, {
commitment: commitment,
skipPreflight: skipPreflight,
});
return res;
}
catch (e) {
console.log('ERROR:', e);
await (0, utils_1.sleep)(5000);
// @ts-ignore
const sig = e.toString().split(' failed ')[0].split('Transaction ')[1];
let res = await connection.getTransaction(sig, { commitment: 'confirmed' });
if (res && res.meta) {
console.log('Txn', res.meta.logMessages);
}
return null;
}
}
exports.sendTransactionWithLogs = sendTransactionWithLogs;
function getStrategyPriceRangeOrca(tickLowerIndex, tickUpperIndex, strategy, poolPrice) {
const { priceLower, priceUpper } = getPriceLowerUpper(tickLowerIndex, tickUpperIndex, Number(strategy.tokenAMintDecimals.toString()), Number(strategy.tokenBMintDecimals.toString()));
const strategyOutOfRange = poolPrice.lt(priceLower) || poolPrice.gt(priceUpper);
return { priceLower, poolPrice, priceUpper, strategyOutOfRange };
}
exports.getStrategyPriceRangeOrca = getStrategyPriceRangeOrca;
function getStrategyPriceRangeRaydium(tickLowerIndex, tickUpperIndex, tickCurrent, tokenADecimals, tokenBDecimals) {
const { priceLower, priceUpper } = getPriceLowerUpper(tickLowerIndex, tickUpperIndex, tokenADecimals, tokenBDecimals);
const poolPrice = (0, whirlpool_sdk_1.tickIndexToPrice)(tickCurrent, tokenADecimals, tokenBDecimals);
const strategyOutOfRange = poolPrice.lt(priceLower) || poolPrice.gt(priceUpper);
return { priceLower, poolPrice, priceUpper, strategyOutOfRange };
}
exports.getStrategyPriceRangeRaydium = getStrategyPriceRangeRaydium;
function getPriceLowerUpper(tickLowerIndex, tickUpperIndex, tokenAMintDecimals, tokenBMintDecimals) {
const priceLower = (0, whirlpool_sdk_1.tickIndexToPrice)(tickLowerIndex, Number(tokenAMintDecimals.toString()), Number(tokenBMintDecimals.toString()));
const priceUpper = (0, whirlpool_sdk_1.tickIndexToPrice)(tickUpperIndex, Number(tokenAMintDecimals.toString()), Number(tokenBMintDecimals.toString()));
return { priceLower, priceUpper };
}
exports.getPriceLowerUpper = getPriceLowerUpper;
function getTokenNameFromCollateralInfo(collateralInfo) {
return String.fromCharCode(...collateralInfo.name.filter((x) => x > 0));
}
exports.getTokenNameFromCollateralInfo = getTokenNameFromCollateralInfo;
const isSOLMint = (mint) => {
return exports.SOL_MINTS.filter((m) => m.equals(mint)).length > 0;
};
exports.isSOLMint = isSOLMint;
function removeBudgetAndAtaIxns(ixns, mints) {
return ixns.filter((ixn) => {
const { programId, keys } = ixn;
if (programId.toString() === web3_js_1.ComputeBudgetProgram.programId.toString()) {
return false;
}
if (programId.toString() === exports.ASSOCIATED_TOKEN_PROGRAM_ID.toString()) {
const mint = keys[3];
return !mints.includes(mint.pubkey.toString());
}
return true;
});
}
exports.removeBudgetAndAtaIxns = removeBudgetAndAtaIxns;
//# sourceMappingURL=tokenUtils.js.map