Files
solana-agent-kit/src/tools/orca/orca_close_position.ts
0xCipherCoder 04a27f974c Merged main
2025-01-11 10:09:09 +05:30

83 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
Keypair,
PublicKey,
TransactionMessage,
VersionedTransaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../../agent";
import { Wallet } from "../../utils/keypair";
import {
ORCA_WHIRLPOOL_PROGRAM_ID,
WhirlpoolContext,
buildWhirlpoolClient,
PDAUtil,
} from "@orca-so/whirlpools-sdk";
import { sendTx } from "../../utils/send_tx";
import { Percentage } from "@orca-so/common-sdk";
/**
* # Closes a Liquidity Position in an Orca Whirlpool
*
* This function closes an existing liquidity position in a specified Orca Whirlpool. The user provides
* the position's mint address.
*
* ## Parameters
* - `agent`: The `SolanaAgentKit` instance representing the wallet and connection details.
* - `positionMintAddress`: The mint address of the liquidity position to close.
*
* ## Returns
* A `Promise` that resolves to a `string` containing the transaction ID of the transaction
*
* ## Notes
* - The function uses Orcas SDK to interact with the specified Whirlpool and close the liquidity position.
* - A maximum slippage of 1% is assumed for liquidity provision during the position closing.
* - The function automatically fetches the associated Whirlpool address and position details using the provided mint address.
*
* ## Throws
* An error will be thrown if:
* - The specified position mint address is invalid or inaccessible.
* - The transaction fails to send.
* - Any required position or Whirlpool data cannot be fetched.
*
* @param agent - The `SolanaAgentKit` instance representing the wallet and connection.
* @param positionMintAddress - The mint address of the liquidity position to close.
* @returns A promise resolving to the transaction ID (`string`).
*/
export async function orcaClosePosition(
agent: SolanaAgentKit,
positionMintAddress: PublicKey,
): Promise<string> {
try {
const wallet = new Wallet(agent.wallet);
const ctx = WhirlpoolContext.from(
agent.connection,
wallet,
ORCA_WHIRLPOOL_PROGRAM_ID,
);
const client = buildWhirlpoolClient(ctx);
const positionAddress = PDAUtil.getPosition(
ORCA_WHIRLPOOL_PROGRAM_ID,
positionMintAddress,
);
const position = await client.getPosition(positionAddress.publicKey);
const whirlpoolAddress = position.getData().whirlpool;
const whirlpool = await client.getPool(whirlpoolAddress);
const txBuilder = await whirlpool.closePosition(
positionAddress.publicKey,
Percentage.fromFraction(1, 100),
);
const txPayload = await txBuilder[0].build();
const txPayloadDecompiled = TransactionMessage.decompile(
(txPayload.transaction as VersionedTransaction).message,
);
const instructions = txPayloadDecompiled.instructions;
const signers = txPayload.signers as Keypair[];
const txId = await sendTx(agent, instructions, signers);
return txId;
} catch (error) {
throw new Error(`${error}`);
}
}