From ab7d03b2af04e1e8ea8a334c9b41b993a62adff2 Mon Sep 17 00:00:00 2001 From: calintje Date: Wed, 25 Dec 2024 13:51:56 +0100 Subject: [PATCH 1/8] Add compatibility with devnet. Update pool creation to support SOL wrapping. Update send tx function. --- src/langchain/index.ts | 15 +- .../create_orca_single_sided_whirlpool.ts | 53 +++--- src/utils/send_tx.ts | 154 ++++++++++-------- test/index.ts | 2 +- 4 files changed, 122 insertions(+), 102 deletions(-) diff --git a/src/langchain/index.ts b/src/langchain/index.ts index afb214f..ec1189e 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -751,15 +751,15 @@ export class SolanaCompressedAirdropTool extends Tool { export class SolanaCreateSingleSidedWhirlpoolTool extends Tool { name = "create_orca_single_sided_whirlpool"; - description = `Create a single-sided Whirlpool with liquidity. + description = `Create a single-sided liquidity pools with liquidity on Orca. Inputs (input is a JSON string): - - depositTokenAmount: number, eg: 1000000000 (required, in units of deposit token including decimals) - - depositTokenMint: string, eg: "DepositTokenMintAddress" (required, mint address of deposit token) - - otherTokenMint: string, eg: "OtherTokenMintAddress" (required, mint address of other token) - - initialPrice: number, eg: 0.001 (required, initial price of deposit token in terms of other token) - - maxPrice: number, eg: 5.0 (required, maximum price at which liquidity is added) - - feeTier: number, eg: 0.30 (required, fee tier for the pool)`; + - depositTokenAmount: number, in units of deposit token including decimals, eg: 1000000000 (required) + - depositTokenMint: string, mint address of deposit token, eg: "DepositTokenMintAddress" (required) + - otherTokenMint: string, mint address of other token, eg: "OtherTokenMintAddress" (required) + - initialPrice: number, initial price of deposit token in terms of other token, eg: 0.001, (required) + - maxPrice: number, maximum price at which liquidity is added, eg: 5.0 (required) + - feeTier: number, fee tier for the pool in %. Possible values on mainnet are: 0.01, 0.02, 0.04, 0.05, 0.16, 0.30, 0.65, 1.0, 2.0 (required)`; constructor(private solanaKit: SolanaAgentKit) { super(); @@ -803,7 +803,6 @@ export class SolanaCreateSingleSidedWhirlpoolTool extends Tool { } } - export class SolanaRaydiumCreateAmmV4 extends Tool { name = "raydium_create_ammV4"; description = `Raydium's Legacy AMM that requiers an OpenBook marketID diff --git a/src/tools/create_orca_single_sided_whirlpool.ts b/src/tools/create_orca_single_sided_whirlpool.ts index aefc93c..e0375f9 100644 --- a/src/tools/create_orca_single_sided_whirlpool.ts +++ b/src/tools/create_orca_single_sided_whirlpool.ts @@ -1,11 +1,10 @@ -import { Keypair, PublicKey, Transaction } from "@solana/web3.js"; +import { Keypair, PublicKey, Transaction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { BN, Wallet } from "@coral-xyz/anchor"; import { Decimal } from "decimal.js"; import { PDAUtil, ORCA_WHIRLPOOL_PROGRAM_ID, - ORCA_WHIRLPOOLS_CONFIG, WhirlpoolContext, TickUtil, PriceMath, @@ -110,7 +109,7 @@ export const FEE_TIERS = { * const otherTokenMint = new PublicKey("OTHER_TOKEN_ADDRESS"); * const initialPrice = new Decimal(0.001); * const maxPrice = new Decimal(5.0); - * const feeTier = 0.30; + * const feeTier = 0.02; * * const txId = await createOrcaSingleSidedWhirlpool( * agent, @@ -133,6 +132,14 @@ export async function createOrcaSingleSidedWhirlpool( maxPrice: Decimal, feeTier: keyof typeof FEE_TIERS, ): Promise { + let whirlpoolsConfigAddress: PublicKey; + if (agent.connection.rpcEndpoint.includes('mainnet')) { + whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); + } else if (agent.connection.rpcEndpoint.includes('devnet')) { + whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); + } else { + throw new Error('Unsupported network'); + } const wallet = new Wallet(agent.wallet); const ctx = WhirlpoolContext.from(agent.connection, wallet, ORCA_WHIRLPOOL_PROGRAM_ID); const fetcher = ctx.fetcher; @@ -163,7 +170,7 @@ export async function createOrcaSingleSidedWhirlpool( }; const feeTierKey = PDAUtil.getFeeTier( ORCA_WHIRLPOOL_PROGRAM_ID, - ORCA_WHIRLPOOLS_CONFIG, + whirlpoolsConfigAddress, tickSpacing, ).publicKey; const initSqrtPrice = PriceMath.tickIndexToSqrtPriceX64(initialTick); @@ -171,24 +178,24 @@ export async function createOrcaSingleSidedWhirlpool( const tokenVaultBKeypair = Keypair.generate(); const whirlpoolPda = PDAUtil.getWhirlpool( ORCA_WHIRLPOOL_PROGRAM_ID, - ORCA_WHIRLPOOLS_CONFIG, + whirlpoolsConfigAddress, mintA, mintB, FEE_TIERS[feeTier], ); const tokenBadgeA = PDAUtil.getTokenBadge( ORCA_WHIRLPOOL_PROGRAM_ID, - ORCA_WHIRLPOOLS_CONFIG, + whirlpoolsConfigAddress, mintA, ).publicKey; const tokenBadgeB = PDAUtil.getTokenBadge( ORCA_WHIRLPOOL_PROGRAM_ID, - ORCA_WHIRLPOOLS_CONFIG, + whirlpoolsConfigAddress, mintB, ).publicKey; const baseParamsPool = { initSqrtPrice, - whirlpoolsConfig: ORCA_WHIRLPOOLS_CONFIG, + whirlpoolsConfig: whirlpoolsConfigAddress, whirlpoolPda, tokenMintA: mintA, tokenMintB: mintB, @@ -301,7 +308,7 @@ export async function createOrcaSingleSidedWhirlpool( wallet.publicKey, undefined, ctx.accountResolverOpts.allowPDAOwnerAddress, - ctx.accountResolverOpts.createWrappedSolAccountMethod, + "ata", ); const { address: tokenOwnerAccountA, ...tokenOwnerAccountAIx } = ataA; const { address: tokenOwnerAccountB, ...tokenOwnerAccountBIx } = ataB; @@ -378,22 +385,18 @@ export async function createOrcaSingleSidedWhirlpool( }); txBuilder.addInstruction(liquidityIx); - const txPayload = await txBuilder.build({ - maxSupportedTransactionVersion: "legacy" - }); + const txPayload = await txBuilder.build(); + const instructions = TransactionMessage.decompile( + (txPayload.transaction as VersionedTransaction).message).instructions - if (txPayload.transaction instanceof Transaction) { - try { - const txId = await sendTx( - agent, - txPayload.transaction, - [positionMintKeypair, tokenVaultAKeypair, tokenVaultBKeypair], - ); - return txId; - } catch (error) { - throw new Error(`Failed to create pool: ${JSON.stringify(error)}`); - } - } else { - throw new Error('Failed to create pool: Transaction not created'); + try { + const txId = await sendTx( + agent, + instructions, + [positionMintKeypair, tokenVaultAKeypair, tokenVaultBKeypair], + ); + return txId; + } catch (error) { + throw new Error(`Failed to create pool: ${JSON.stringify(error)}`); } } diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index 903ddd5..7250c0c 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -1,68 +1,60 @@ import { SolanaAgentKit } from "../agent"; -import { Transaction, Keypair, TransactionInstruction } from "@solana/web3.js"; -import { Connection, ComputeBudgetProgram } from "@solana/web3.js"; +import { Keypair, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { Connection, ComputeBudgetProgram, } from "@solana/web3.js"; + + +const feeTiers = { + min: 1, + mid: 0.5, + max: 0.95 +} /** * Get priority fees for the current block * @param connection - Solana RPC connection * @returns Priority fees statistics and instructions for different fee levels */ -export async function getPriorityFees(connection: Connection): Promise<{ - min: number; - median: number; - max: number; - instructions?: { - low: TransactionInstruction; - medium: TransactionInstruction; - high: TransactionInstruction; - }; -}> { +export async function getComputeBudgetInstructions(agent: SolanaAgentKit, instructions: TransactionInstruction[], feeTier: keyof typeof feeTiers): Promise<{ + blockhash: string; + computeBudgetLimitInstruction: TransactionInstruction; + computeBudgetPriorityFeeInstructions: TransactionInstruction; + } > { try { - // Get recent prioritization fees - const priorityFees = await connection.getRecentPrioritizationFees(); + const blockhash = (await agent.connection.getLatestBlockhash()).blockhash; + const messageV0 = new TransactionMessage({ + payerKey: agent.wallet_address, + recentBlockhash: blockhash, + instructions: instructions, + }).compileToV0Message(); + const transaction = new VersionedTransaction(messageV0); + const simulatedTx = agent.connection.simulateTransaction(transaction); + const estimatedComputeUnits = (await simulatedTx).value.unitsConsumed; + const safeComputeUnits = Math.ceil( + estimatedComputeUnits ? + Math.max(estimatedComputeUnits + 100000, estimatedComputeUnits * 1.2) + : 200000 + ); + const computeBudgetLimitInstruction = ComputeBudgetProgram.setComputeUnitLimit({ + units: safeComputeUnits, + }); - if (!priorityFees.length) { - return { - min: 0, - median: 0, - max: 0, - }; - } + const priorityFee = await agent.connection.getRecentPrioritizationFees() + .then(fees => + fees.sort((a, b) => a.prioritizationFee - b.prioritizationFee) + [Math.floor(fees.length * feeTiers[feeTier])].prioritizationFee + ); - // Sort fees by value - const sortedFees = priorityFees - .map((x) => x.prioritizationFee) - .sort((a, b) => a - b); - - // Calculate statistics - const min = sortedFees[0] ?? 0; - const max = sortedFees[sortedFees.length - 1] ?? 0; - const mid = Math.floor(sortedFees.length / 2); - const median = - sortedFees.length % 2 === 0 - ? ((sortedFees[mid - 1] ?? 0) + (sortedFees[mid] ?? 0)) / 2 - : sortedFees[mid] ?? 0; - - // Helper to create priority fee IX based on chosen strategy - const createPriorityFeeIx = (fee: number) => { - return ComputeBudgetProgram.setComputeUnitPrice({ - microLamports: fee, - }); - }; + const computeBudgetPriorityFeeInstructions = ComputeBudgetProgram.setComputeUnitPrice({ + microLamports: priorityFee, + }); return { - min, - median, - max, - // Return instructions for different fee levels - instructions: { - low: createPriorityFeeIx(min), - medium: createPriorityFeeIx(median), - high: createPriorityFeeIx(max), - }, + blockhash, + computeBudgetLimitInstruction, + computeBudgetPriorityFeeInstructions }; } catch (error) { - console.error("Error getting priority fees:", error); + console.error("Error getting compute budget instructions fees:", error); throw error; } } @@ -75,24 +67,50 @@ export async function getPriorityFees(connection: Connection): Promise<{ */ export async function sendTx( agent: SolanaAgentKit, - tx: Transaction, + instructions: TransactionInstruction[], otherKeypairs?: Keypair[] ) { - tx.recentBlockhash = (await agent.connection.getLatestBlockhash()).blockhash; - tx.feePayer = agent.wallet_address; - const fees = await getPriorityFees(agent.connection); - if (fees.instructions) { - tx.add(fees.instructions.medium!); - } + console.log(instructions) - tx.sign(agent.wallet, ...(otherKeypairs ?? [])); - let txid = await agent.connection.sendRawTransaction(tx.serialize()); - await agent.connection.confirmTransaction({ - signature: txid, - blockhash: (await agent.connection.getLatestBlockhash()).blockhash, - lastValidBlockHeight: ( - await agent.connection.getLatestBlockhash() - ).lastValidBlockHeight, - }); - return txid; + const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid"); + const allInstructions = [ + ixComputeBudget.computeBudgetLimitInstruction, + ixComputeBudget.computeBudgetPriorityFeeInstructions, + ...instructions]; + const messageV0 = new TransactionMessage({ + payerKey: agent.wallet_address, + recentBlockhash: ixComputeBudget.blockhash, + instructions: allInstructions, + }).compileToV0Message(); + const transaction = new VersionedTransaction(messageV0); + transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]); + + const timeoutMs = 90000; + const startTime = Date.now(); + while (Date.now() - startTime < timeoutMs) { + const transactionStartTime = Date.now(); + + const signature = await agent.connection.sendTransaction( + transaction, + { + maxRetries: 0, + skipPreflight: true, + }); + + const statuses = await agent.connection.getSignatureStatuses([signature]); + if (statuses.value[0]) { + if (!statuses.value[0].err) { + return signature; + } else { + throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`); + } + } + + const elapsedTime = Date.now() - transactionStartTime; + const remainingTime = Math.max(0, 1000 - elapsedTime); + if (remainingTime > 0) { + await new Promise(resolve => setTimeout(resolve, remainingTime)); + } + } + throw new Error("Transaction timeout"); } diff --git a/test/index.ts b/test/index.ts index 00faade..bc16847 100644 --- a/test/index.ts +++ b/test/index.ts @@ -36,7 +36,7 @@ const WALLET_DATA_FILE = "wallet_data.txt"; async function initializeAgent() { try { const llm = new ChatOpenAI({ - modelName: "gpt-4o-mini", + modelName: "gpt-4o", temperature: 0.7, }); From 7b0fdd6cd8d64b067202765ba7849e66793a08a1 Mon Sep 17 00:00:00 2001 From: calintje Date: Wed, 25 Dec 2024 14:53:37 +0100 Subject: [PATCH 2/8] Remove console.log --- src/utils/send_tx.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index 7250c0c..b32f2e8 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -70,8 +70,6 @@ export async function sendTx( instructions: TransactionInstruction[], otherKeypairs?: Keypair[] ) { - console.log(instructions) - const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid"); const allInstructions = [ ixComputeBudget.computeBudgetLimitInstruction, From 4dd96558478b5a196c3f7d603a89cdb792c95f55 Mon Sep 17 00:00:00 2001 From: calintje Date: Wed, 25 Dec 2024 14:58:13 +0100 Subject: [PATCH 3/8] Cleaup, fix feeTier --- src/utils/send_tx.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index b32f2e8..f357b8e 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -1,10 +1,10 @@ import { SolanaAgentKit } from "../agent"; import { Keypair, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; -import { Connection, ComputeBudgetProgram, } from "@solana/web3.js"; +import { ComputeBudgetProgram, } from "@solana/web3.js"; const feeTiers = { - min: 1, + min: 0.01, mid: 0.5, max: 0.95 } From 7976de50d7669e8b108ba0d3f0a677e49c5c6414 Mon Sep 17 00:00:00 2001 From: calintje Date: Sat, 28 Dec 2024 05:30:27 +0100 Subject: [PATCH 4/8] Add Orca tools. --- pnpm-lock.yaml | 121 ++++++++-------- src/agent/index.ts | 41 +++++- src/langchain/index.ts | 129 +++++++++++++++-- src/tools/index.ts | 4 +- src/tools/orca_create_clmm.ts | 117 +++++++++++++++ ...rca_create_single_sided_liquidity_pool.ts} | 75 ++++------ ...a_open_centered_position_with_liquidity.ts | 136 ++++++++++++++++++ src/tools/orca_open_single_sided_position.ts | 0 src/utils/send_tx.ts | 82 ++++++----- test/index.ts | 4 +- 10 files changed, 546 insertions(+), 163 deletions(-) create mode 100644 src/tools/orca_create_clmm.ts rename src/tools/{create_orca_single_sided_whirlpool.ts => orca_create_single_sided_liquidity_pool.ts} (88%) create mode 100644 src/tools/orca_open_centered_position_with_liquidity.ts create mode 100644 src/tools/orca_open_single_sided_position.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ddfc583..1270412 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,7 +22,7 @@ importers: version: 0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) '@langchain/langgraph': specifier: ^0.2.27 - version: 0.2.34(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) + version: 0.2.36(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) '@langchain/openai': specifier: ^0.3.13 version: 0.3.16(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) @@ -91,7 +91,7 @@ importers: version: 4.0.1 langchain: specifier: ^0.3.6 - version: 0.3.7(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))(@langchain/groq@0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))))(axios@1.7.9)(openai@4.77.0(zod@3.24.1)) + version: 0.3.8(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))(@langchain/groq@0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))))(axios@1.7.9)(openai@4.77.0(zod@3.24.1)) openai: specifier: ^4.75.0 version: 4.77.0(zod@3.24.1) @@ -235,8 +235,8 @@ packages: '@langchain/langgraph-sdk@0.0.32': resolution: {integrity: sha512-KQyM9kLO7T6AxwNrceajH7JOybP3pYpvUPnhiI2rrVndI1WyZUJ1eVC1e722BVRAPi6o+WcoTT4uMSZVinPOtA==} - '@langchain/langgraph@0.2.34': - resolution: {integrity: sha512-fSlmLYre+Skh5XJgBGe5YRtXaHyGMTlhu5UN3LzIgA3E9CmGODvH+Ydyk5vJzhXMjnPpLr8icqlKxKrYmZ3gTw==} + '@langchain/langgraph@0.2.36': + resolution: {integrity: sha512-zxk7ZCVxP0/Ut9785EiXCS7BE7sXd8cu943mcZUF2aNFUaQRTBbbiKpNdR3nb1+xO/B+HVktrJT2VFdkAywnng==} engines: {node: '>=18'} peerDependencies: '@langchain/core': '>=0.2.36 <0.3.0 || >=0.3.9 < 0.4.0' @@ -433,17 +433,17 @@ packages: '@scure/base@1.2.1': resolution: {integrity: sha512-DGmGtC8Tt63J5GfHgfl5CuAXh96VF/LD8K9Hr/Gv0J2lAoRGlPOMpqMpMbCTOoOJMZCk2Xt+DskdDyn6dEFdzQ==} - '@shikijs/core@1.24.3': - resolution: {integrity: sha512-VRcf4GYUIkxIchGM9DrapRcxtgojg4IWKUtX5EtW+4PJiGzF2xQqZSv27PJt+WLc18KT3CNLpNWow9JYV5n+Rg==} + '@shikijs/core@1.24.4': + resolution: {integrity: sha512-jjLsld+xEEGYlxAXDyGwWsKJ1sw5Pc1pnp4ai2ORpjx2UX08YYTC0NNqQYO1PaghYaR+PvgMOGuvzw2he9sk0Q==} - '@shikijs/engine-javascript@1.24.3': - resolution: {integrity: sha512-De8tNLvYjeK6V0Gb47jIH2M+OKkw+lWnSV1j3HVDFMlNIglmVcTMG2fASc29W0zuFbfEEwKjO8Fe4KYSO6Ce3w==} + '@shikijs/engine-javascript@1.24.4': + resolution: {integrity: sha512-TClaQOLvo9WEMJv6GoUsykQ6QdynuKszuORFWCke8qvi6PeLm7FcD9+7y45UenysxEWYpDL5KJaVXTngTE+2BA==} - '@shikijs/engine-oniguruma@1.24.3': - resolution: {integrity: sha512-iNnx950gs/5Nk+zrp1LuF+S+L7SKEhn8k9eXgFYPGhVshKppsYwRmW8tpmAMvILIMSDfrgqZ0w+3xWVQB//1Xw==} + '@shikijs/engine-oniguruma@1.24.4': + resolution: {integrity: sha512-Do2ry6flp2HWdvpj2XOwwa0ljZBRy15HKZITzPcNIBOGSeprnA8gOooA/bLsSPuy8aJBa+Q/r34dMmC3KNL/zw==} - '@shikijs/types@1.24.3': - resolution: {integrity: sha512-FPMrJ69MNxhRtldRk69CghvaGlbbN3pKRuvko0zvbfa2dXp4pAngByToqS5OY5jvN8D7LKR4RJE8UvzlCOuViw==} + '@shikijs/types@1.24.4': + resolution: {integrity: sha512-0r0XU7Eaow0PuDxuWC1bVqmWCgm3XqizIaT7SM42K03vc69LGooT0U8ccSR44xP/hGlNx4FKhtYpV+BU6aaKAA==} '@shikijs/vscode-textmate@9.3.1': resolution: {integrity: sha512-79QfK1393x9Ho60QFyLti+QfdJzRQCVLFb97kOIV7Eo9vQU/roINgk7m24uv0a7AUvN//RDH36FLjjK48v0s9g==} @@ -908,8 +908,8 @@ packages: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} engines: {node: '>=10'} - chalk@5.4.0: - resolution: {integrity: sha512-ZkD35Mx92acjB2yNJgziGqT9oKHEOxjTBTDRpOsRWtdecL/0jM3z5kM/CTzHWvHIen1GvkM85p6TuFfDGfc8/Q==} + chalk@5.4.1: + resolution: {integrity: sha512-zgVZuo2WcZgfUEmsn6eO3kINexW8RAE4maiQ8QNs8CtpPCSyMiYsULR3HQYkm3w8FIA3SberyMJMSldGsW+U3w==} engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} character-entities-html4@2.1.0: @@ -953,8 +953,8 @@ packages: create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} - cross-fetch@3.1.8: - resolution: {integrity: sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==} + cross-fetch@3.2.0: + resolution: {integrity: sha512-Q+xVJLoGOeIMXZmbUK4HYk+69cQH6LudR0Vu/pRm2YlU/hDV9CiS0gKUMaWY5f2NeUH9C1nV3bsTlCo0FsTV1Q==} cross-spawn@7.0.6: resolution: {integrity: sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==} @@ -1406,8 +1406,8 @@ packages: keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} - langchain@0.3.7: - resolution: {integrity: sha512-6/Gkk9Zez3HkbsETFxZVo1iKLmaK3OzkDseC5MYFKVmYFDXFAOyJR3srJ9P61xF8heVdsPixqYIsejBn7/9dXg==} + langchain@0.3.8: + resolution: {integrity: sha512-EiAHFgBdThuXFmIx9j81wjdPItpRsw0Ck4r5dyhB74gyhehRGna/UK2CTqeKVnIUM/f4g4JbxUgAU4voXljDMw==} engines: {node: '>=18'} peerDependencies: '@langchain/anthropic': '*' @@ -1452,8 +1452,8 @@ packages: typeorm: optional: true - langsmith@0.2.13: - resolution: {integrity: sha512-16EOM5nhU6GlMCKGm5sgBIAKOKzS2d30qcDZmF21kSLZJiUhUNTROwvYdqgZLrGfIIzmSMJHCKA7RFd5qf50uw==} + langsmith@0.2.14: + resolution: {integrity: sha512-ClAuAgSf3m9miMYotLEaZKQyKdaWlfjhebCuYco8bc6g72dU2VwTg31Bv4YINBq7EH2i1cMwbOiJxbOXPqjGig==} peerDependencies: openai: '*' peerDependenciesMeta: @@ -1592,8 +1592,8 @@ packages: once@1.4.0: resolution: {integrity: sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==} - oniguruma-to-es@0.8.0: - resolution: {integrity: sha512-rY+/a6b+uCgoYIL9itjY0x99UUDHXmGaw7Jjk5ZvM/3cxDJifyxFr/Zm4tTmF6Tre18gAakJo7AzhKUeMNLgHA==} + oniguruma-to-es@0.8.1: + resolution: {integrity: sha512-dekySTEvCxCj0IgKcA2uUCO/e4ArsqpucDPcX26w9ajx+DvMWLc5eZeJaRQkd7oC/+rwif5gnT900tA34uN9Zw==} openai@4.77.0: resolution: {integrity: sha512-WWacavtns/7pCUkOWvQIjyOfcdr9X+9n9Vvb0zFeKVDAqwCMDHB+iSr24SVaBAhplvSG6JrRXFpcNM9gWhOGIw==} @@ -1706,14 +1706,14 @@ packages: regenerator-runtime@0.14.1: resolution: {integrity: sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==} - regex-recursion@5.0.0: - resolution: {integrity: sha512-UwyOqeobrCCqTXPcsSqH4gDhOjD5cI/b8kjngWgSZbxYh5yVjAwTjO5+hAuPRNiuR70+5RlWSs+U9PVcVcW9Lw==} + regex-recursion@5.1.1: + resolution: {integrity: sha512-ae7SBCbzVNrIjgSbh7wMznPcQel1DNlDtzensnFxpiNpXt1U2ju/bHugH422r+4LAVS1FpW1YCwilmnNsjum9w==} regex-utilities@2.3.0: resolution: {integrity: sha512-8VhliFJAWRaUiVvREIiW2NXXTmHs4vMNnSzuJVhscgmGav3g9VDxLrQndI3dZZVVdp0ZO/5v0xmX516/7M9cng==} - regex@5.0.2: - resolution: {integrity: sha512-/pczGbKIQgfTMRV0XjABvc5RzLqQmwqxLHdQao2RTXPk+pmTXB2P0IaUHYdYyk412YLwUIkaeMd5T+RzVgTqnQ==} + regex@5.1.1: + resolution: {integrity: sha512-dN5I359AVGPnwzJm2jN1k0W9LPZ+ePvoOeVMMfqIMFz53sSwXkxaJoxr50ptnsC771lK95BnTrVSZxq0b9yCGw==} resolve-from@4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} @@ -1758,8 +1758,8 @@ packages: resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==} engines: {node: '>=8'} - shiki@1.24.3: - resolution: {integrity: sha512-eMeX/ehE2IDKVs71kB4zVcDHjutNcOtm+yIRuR4sA6ThBbdFI0DffGJiyoKCodj0xRGxIoWC3pk/Anmm5mzHmA==} + shiki@1.24.4: + resolution: {integrity: sha512-aVGSFAOAr1v26Hh/+GBIsRVDWJ583XYV7CuNURKRWh9gpGv4OdbisZGq96B9arMYTZhTQkmRF5BrShOSTvNqhw==} slash@3.0.0: resolution: {integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==} @@ -2068,7 +2068,7 @@ snapshots: bs58: 4.0.1 buffer-layout: 1.2.2 camelcase: 6.3.0 - cross-fetch: 3.1.8 + cross-fetch: 3.2.0 crypto-hash: 1.3.0 eventemitter3: 4.0.7 pako: 2.1.0 @@ -2153,7 +2153,7 @@ snapshots: camelcase: 6.3.0 decamelize: 1.2.0 js-tiktoken: 1.0.16 - langsmith: 0.2.13(openai@4.77.0(zod@3.24.1)) + langsmith: 0.2.14(openai@4.77.0(zod@3.24.1)) mustache: 4.2.0 p-queue: 6.6.2 p-retry: 4.6.2 @@ -2185,7 +2185,7 @@ snapshots: p-retry: 4.6.2 uuid: 9.0.1 - '@langchain/langgraph@0.2.34(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))': + '@langchain/langgraph@0.2.36(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))': dependencies: '@langchain/core': 0.3.26(openai@4.77.0(zod@3.24.1)) '@langchain/langgraph-checkpoint': 0.0.13(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) @@ -2463,27 +2463,27 @@ snapshots: '@scure/base@1.2.1': {} - '@shikijs/core@1.24.3': + '@shikijs/core@1.24.4': dependencies: - '@shikijs/engine-javascript': 1.24.3 - '@shikijs/engine-oniguruma': 1.24.3 - '@shikijs/types': 1.24.3 + '@shikijs/engine-javascript': 1.24.4 + '@shikijs/engine-oniguruma': 1.24.4 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 '@types/hast': 3.0.4 hast-util-to-html: 9.0.4 - '@shikijs/engine-javascript@1.24.3': + '@shikijs/engine-javascript@1.24.4': dependencies: - '@shikijs/types': 1.24.3 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 - oniguruma-to-es: 0.8.0 + oniguruma-to-es: 0.8.1 - '@shikijs/engine-oniguruma@1.24.3': + '@shikijs/engine-oniguruma@1.24.4': dependencies: - '@shikijs/types': 1.24.3 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 - '@shikijs/types@1.24.3': + '@shikijs/types@1.24.4': dependencies: '@shikijs/vscode-textmate': 9.3.1 '@types/hast': 3.0.4 @@ -2613,18 +2613,18 @@ snapshots: '@solana/errors@2.0.0-preview.2': dependencies: - chalk: 5.4.0 + chalk: 5.4.1 commander: 12.1.0 '@solana/errors@2.0.0-preview.4(typescript@5.7.2)': dependencies: - chalk: 5.4.0 + chalk: 5.4.1 commander: 12.1.0 typescript: 5.7.2 '@solana/errors@2.0.0-rc.1(typescript@5.7.2)': dependencies: - chalk: 5.4.0 + chalk: 5.4.1 commander: 12.1.0 typescript: 5.7.2 @@ -3123,7 +3123,7 @@ snapshots: ansi-styles: 4.3.0 supports-color: 7.2.0 - chalk@5.4.0: {} + chalk@5.4.1: {} character-entities-html4@2.1.0: {} @@ -3153,7 +3153,7 @@ snapshots: create-require@1.1.1: {} - cross-fetch@3.1.8: + cross-fetch@3.2.0: dependencies: node-fetch: 2.7.0 transitivePeerDependencies: @@ -3634,7 +3634,7 @@ snapshots: dependencies: json-buffer: 3.0.1 - langchain@0.3.7(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))(@langchain/groq@0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))))(axios@1.7.9)(openai@4.77.0(zod@3.24.1)): + langchain@0.3.8(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1)))(@langchain/groq@0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))))(axios@1.7.9)(openai@4.77.0(zod@3.24.1)): dependencies: '@langchain/core': 0.3.26(openai@4.77.0(zod@3.24.1)) '@langchain/openai': 0.3.16(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) @@ -3642,7 +3642,7 @@ snapshots: js-tiktoken: 1.0.16 js-yaml: 4.1.0 jsonpointer: 5.0.1 - langsmith: 0.2.13(openai@4.77.0(zod@3.24.1)) + langsmith: 0.2.14(openai@4.77.0(zod@3.24.1)) openapi-types: 12.1.3 p-retry: 4.6.2 uuid: 10.0.0 @@ -3656,7 +3656,7 @@ snapshots: - encoding - openai - langsmith@0.2.13(openai@4.77.0(zod@3.24.1)): + langsmith@0.2.14(openai@4.77.0(zod@3.24.1)): dependencies: '@types/uuid': 10.0.0 commander: 10.0.1 @@ -3801,11 +3801,11 @@ snapshots: dependencies: wrappy: 1.0.2 - oniguruma-to-es@0.8.0: + oniguruma-to-es@0.8.1: dependencies: emoji-regex-xs: 1.0.0 - regex: 5.0.2 - regex-recursion: 5.0.0 + regex: 5.1.1 + regex-recursion: 5.1.1 openai@4.77.0(zod@3.24.1): dependencies: @@ -3898,13 +3898,14 @@ snapshots: regenerator-runtime@0.14.1: {} - regex-recursion@5.0.0: + regex-recursion@5.1.1: dependencies: + regex: 5.1.1 regex-utilities: 2.3.0 regex-utilities@2.3.0: {} - regex@5.0.2: + regex@5.1.1: dependencies: regex-utilities: 2.3.0 @@ -3954,12 +3955,12 @@ snapshots: shebang-regex@3.0.0: {} - shiki@1.24.3: + shiki@1.24.4: dependencies: - '@shikijs/core': 1.24.3 - '@shikijs/engine-javascript': 1.24.3 - '@shikijs/engine-oniguruma': 1.24.3 - '@shikijs/types': 1.24.3 + '@shikijs/core': 1.24.4 + '@shikijs/engine-javascript': 1.24.4 + '@shikijs/engine-oniguruma': 1.24.4 + '@shikijs/types': 1.24.4 '@shikijs/vscode-textmate': 9.3.1 '@types/hast': 3.0.4 @@ -4065,7 +4066,7 @@ snapshots: lunr: 2.3.9 markdown-it: 14.1.0 minimatch: 9.0.5 - shiki: 1.24.3 + shiki: 1.24.4 typescript: 5.7.2 yaml: 2.6.1 diff --git a/src/agent/index.ts b/src/agent/index.ts index 56f66f4..b39eaa7 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -24,10 +24,12 @@ import { getTokenDataByTicker, stakeWithJup, sendCompressedAirdrop, - createOrcaSingleSidedWhirlpool, + orcaCreateSingleSidedLiquidityPool, + orcaCreateCLMM, + orcaOpenCenteredPositionWithLiquidity, + FEE_TIERS, fetchPrice, pythFetchPrice, - FEE_TIERS, getAllDomainsTLDs, getAllRegisteredAllDomains, getOwnedDomainsForTLD, @@ -36,6 +38,7 @@ import { resolveAllDomains, create_gibwork_task, } from "../tools"; + import { CollectionDeployment, CollectionOptions, @@ -199,7 +202,22 @@ export class SolanaAgentKit { ); } - async createOrcaSingleSidedWhirlpool( + async orcaCreateCLMM( + mintA: PublicKey, + mintB: PublicKey, + initialPrice: Decimal, + feeTier: keyof typeof FEE_TIERS, + ) { + return orcaCreateCLMM( + this, + mintA, + mintB, + initialPrice, + feeTier, + ); + } + + async orcaCreateSingleSidedLiquidityPool( depositTokenAmount: BN, depositTokenMint: PublicKey, otherTokenMint: PublicKey, @@ -207,7 +225,7 @@ export class SolanaAgentKit { maxPrice: Decimal, feeTier: keyof typeof FEE_TIERS, ) { - return createOrcaSingleSidedWhirlpool( + return orcaCreateSingleSidedLiquidityPool( this, depositTokenAmount, depositTokenMint, @@ -218,6 +236,21 @@ export class SolanaAgentKit { ); } + async orcaOpenCenteredPositionWithLiquidity( + whirlpoolAddress: PublicKey, + priceOffsetBps: number, + inputTokenMint: PublicKey, + inputAmount: Decimal, + ) { + return orcaOpenCenteredPositionWithLiquidity( + this, + whirlpoolAddress, + priceOffsetBps, + inputTokenMint, + inputAmount, + ); + } + async resolveAllDomains(domain: string): Promise { return resolveAllDomains(this, domain); } diff --git a/src/langchain/index.ts b/src/langchain/index.ts index 2ec7389..21da295 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -754,17 +754,72 @@ export class SolanaCompressedAirdropTool extends Tool { } } -export class SolanaCreateSingleSidedWhirlpoolTool extends Tool { - name = "create_orca_single_sided_whirlpool"; - description = `Create a single-sided liquidity pools with liquidity on Orca. +export class SolanaOrcaCreateCLMM extends Tool { + name = "orca_create_clmm"; + description = `Create a Concentrated Liquidity Market Maker (CLMM) pool on Orca, the most efficient and capital-optimized CLMM on Solana. This function initializes a CLMM pool but does not add liquidity. You can add liquidity later using a centered position or a single-sided position. Single-sided positions are ideal for directional price expectations, acting as sort of a limit call. Centered positions are ideal for generating yield. The tighter, the better. - Inputs (input is a JSON string): - - depositTokenAmount: number, in units of deposit token including decimals, eg: 1000000000 (required) - - depositTokenMint: string, mint address of deposit token, eg: "DepositTokenMintAddress" (required) - - otherTokenMint: string, mint address of other token, eg: "OtherTokenMintAddress" (required) - - initialPrice: number, initial price of deposit token in terms of other token, eg: 0.001, (required) - - maxPrice: number, maximum price at which liquidity is added, eg: 5.0 (required) - - feeTier: number, fee tier for the pool in %. Possible values on mainnet are: 0.01, 0.02, 0.04, 0.05, 0.16, 0.30, 0.65, 1.0, 2.0 (required)`; + Inputs (JSON string): + - mintA: string, mint address of the first token, e.g., "MintAAddress" (required). + - mintB: string, mint address of the second token, e.g., "MintBAddress" (required). + - initialPrice: number, initial price of mintA in terms of mintB, e.g., 0.001 (required). + - feeTier: number, fee tier in bps. Options: 1, 2, 4, 5, 16, 30, 65, 100, 200 (required).`; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + async _call(input: string): Promise { + try { + const inputFormat = JSON.parse(input); + const mintA = new PublicKey(inputFormat.mintA); + const mintB = new PublicKey(inputFormat.mintB); + const initialPrice = new Decimal(inputFormat.initialPrice); + const feeTier = inputFormat.feeTier; + + if (!feeTier || !(feeTier in FEE_TIERS)) { + throw new Error( + `Invalid feeTier. Available options: ${Object.keys(FEE_TIERS).join( + ", ", + )}`, + ); + } + + const txId = await this.solanaKit.orcaCreateCLMM( + mintA, + mintB, + initialPrice, + feeTier, + ); + + return JSON.stringify({ + status: "success", + message: "CLMM pool created successfully. Note: No liquidity was added.", + transaction: txId, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + + +export class SolanaOrcaCreateSingleSideLiquidityPool extends Tool { + name = "orca_create_single_sided_liquidity_pool"; + description = `Create a single-sided liquidity pool on Orca, the most efficient and capital-optimized CLMM platform on Solana. + + This function initializes a single-sided liquidity pool, ideal for community driven project, fair launches, and fundraising. Minimize price impact by setting a narrow price range. + + Inputs (JSON string): + - depositTokenAmount: number, in units of the deposit token including decimals, e.g., 1000000000 (required). + - depositTokenMint: string, mint address of the deposit token, e.g., "DepositTokenMintAddress" (required). + - otherTokenMint: string, mint address of the other token, e.g., "OtherTokenMintAddress" (required). + - initialPrice: number, initial price of the deposit token in terms of the other token, e.g., 0.001 (required). + - maxPrice: number, maximum price at which liquidity is added, e.g., 5.0 (required). + - feeTier: number, fee tier for the pool in bps. Options: 1, 2, 4, 5, 16, 30, 65, 100, 200 (required).`; constructor(private solanaKit: SolanaAgentKit) { super(); @@ -788,7 +843,7 @@ export class SolanaCreateSingleSidedWhirlpoolTool extends Tool { ); } - const txId = await this.solanaKit.createOrcaSingleSidedWhirlpool( + const txId = await this.solanaKit.orcaCreateSingleSidedLiquidityPool( depositTokenAmount, depositTokenMint, otherTokenMint, @@ -812,6 +867,54 @@ export class SolanaCreateSingleSidedWhirlpoolTool extends Tool { } } +export class SolanaOrcaOpenCenteredPosition extends Tool { + name = "orca_open_centered_position_with_liquidity"; + description = `Add liquidity to a CLMM by opening a position in an Orca Whirlpool, the most efficient CLMM on Solana for precise liquidity management. A tighter range concentrates more liquidity, increasing yield potential. This function calculates the required amount of the other token based on the input token and desired offset. Centered positions are ideal for optimizing returns within defined ranges. + + Inputs (JSON string): + - whirlpoolAddress: string, address of the Orca Whirlpool (required). + - priceOffsetBps: number, bps offset (one side) from the center price, e.g., 500 for 5% (required). + - inputTokenMint: string, mint address of the deposit token (required). + - inputAmount: number, amount of the deposit token, e.g., 100.0 (required).`; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + async _call(input: string): Promise { + try { + const inputFormat = JSON.parse(input); + const whirlpoolAddress = new PublicKey(inputFormat.whirlpoolAddress); + const priceOffsetBps = parseInt(inputFormat.priceOffsetBps, 10); + const inputTokenMint = new PublicKey(inputFormat.inputTokenMint); + const inputAmount = new Decimal(inputFormat.inputAmount); + + if (priceOffsetBps <= 0 || priceOffsetBps > 10_000) { + throw new Error("Invalid priceOffsetBps. It must be greater than 0 and less than or equal to 10,000."); + } + + const txId = await this.solanaKit.orcaOpenCenteredPositionWithLiquidity( + whirlpoolAddress, + priceOffsetBps, + inputTokenMint, + inputAmount, + ); + + return JSON.stringify({ + status: "success", + message: "Centered liquidity position opened successfully.", + transaction: txId, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + export class SolanaRaydiumCreateAmmV4 extends Tool { name = "raydium_create_ammV4"; description = `Raydium's Legacy AMM that requiers an OpenBook marketID @@ -1254,7 +1357,9 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) { new SolanaRaydiumCreateClmm(solanaKit), new SolanaRaydiumCreateCpmm(solanaKit), new SolanaOpenbookCreateMarket(solanaKit), - new SolanaCreateSingleSidedWhirlpoolTool(solanaKit), + new SolanaOrcaCreateCLMM(solanaKit), + new SolanaOrcaCreateSingleSideLiquidityPool(solanaKit), + new SolanaOrcaOpenCenteredPosition(solanaKit), new SolanaPythFetchPrice(solanaKit), new SolanaResolveDomainTool(solanaKit), new SolanaGetOwnedDomains(solanaKit), diff --git a/src/tools/index.ts b/src/tools/index.ts index 9a18cf4..a87f931 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -15,7 +15,9 @@ export * from "./get_token_data"; export * from "./stake_with_jup"; export * from "./fetch_price"; export * from "./send_compressed_airdrop"; -export * from "./create_orca_single_sided_whirlpool"; +export * from "./orca_create_clmm"; +export * from "./orca_create_single_sided_liquidity_pool"; +export * from "./orca_open_centered_position_with_liquidity"; export * from "./get_all_domains_tlds"; export * from "./get_all_registered_all_domains"; export * from "./get_owned_domains_for_tld"; diff --git a/src/tools/orca_create_clmm.ts b/src/tools/orca_create_clmm.ts new file mode 100644 index 0000000..4b6d465 --- /dev/null +++ b/src/tools/orca_create_clmm.ts @@ -0,0 +1,117 @@ +import { Keypair, PublicKey, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { SolanaAgentKit } from "../agent"; +import { Wallet } from "@coral-xyz/anchor"; +import { Decimal } from "decimal.js"; +import { + ORCA_WHIRLPOOL_PROGRAM_ID, + WhirlpoolContext, + PriceMath, + PoolUtil, + buildWhirlpoolClient, +} from "@orca-so/whirlpools-sdk"; + +import { sendTx } from "../utils/send_tx"; +import { FEE_TIERS } from "./orca_create_single_sided_liquidity_pool"; + +/** + * # Creates a CLMM Pool (Concentrated Liquidity Market Maker Pool). + * + * This function initializes a new Whirlpool (CLMM Pool) on Orca. It only sets up the pool and does not seed it with liquidity. + * + * ## Example Usage: + * Suppose you want to create a CLMM pool with two tokens, SHARK and USDC, and set the initial price of SHARK to 0.001 USDC. + * You would call this function with `mintA` as SHARK's mint address and `mintB` as USDC's mint address. The pool is created + * with the specified fee tier and tick spacing associated with that fee tier. + * + * ### Note for Experts: + * The Whirlpool program determines the token mint order, which might not match your expectation. This function + * adjusts the input order as needed and inverts the initial price accordingly. + * + * @param agent - The `SolanaAgentKit` instance representing the wallet and connection details. + * @param mintA - The mint address of the first token in the pool (e.g., SHARK). + * @param mintB - The mint address of the second token in the pool (e.g., USDC). + * @param initialPrice - The initial price of `mintA` in terms of `mintB`. + * @param feeTier - The fee tier bps for the pool, determining tick spacing and fee collection rates. + * + * @returns A promise that resolves to a transaction ID (`string`) of the transaction creating the pool. + * + * @throws Will throw an error if: + * - Mint accounts for the tokens cannot be fetched. + * - The network is unsupported. + * + * @remarks + * This function only initializes the CLMM pool and does not add liquidity. For adding liquidity, you can use + * a separate function after the pool is successfully created. + * ``` + */ +export async function orcaCreateCLMM( + agent: SolanaAgentKit, + mintA: PublicKey, + mintB: PublicKey, + initialPrice: Decimal, + feeTier: keyof typeof FEE_TIERS, +): Promise { + try { + let whirlpoolsConfigAddress: PublicKey; + if (agent.connection.rpcEndpoint.includes('mainnet')) { + whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); + } else if (agent.connection.rpcEndpoint.includes('devnet')) { + whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); + } else { + throw new Error('Unsupported network'); + } + const wallet = new Wallet(agent.wallet); + const ctx = WhirlpoolContext.from( + agent.connection, + wallet, + ORCA_WHIRLPOOL_PROGRAM_ID, + ); + const fetcher = ctx.fetcher; + const client = buildWhirlpoolClient(ctx) + + const correctTokenOrder = PoolUtil.orderMints( + mintA, + mintB, + ).map((addr) => addr.toString()); + const isCorrectMintOrder = + correctTokenOrder[0] === mintA.toString(); + if (!isCorrectMintOrder) { + [mintA, mintB] = [mintB, mintA]; + initialPrice = new Decimal(1 / initialPrice.toNumber()); + } + const mintAAccount = await fetcher.getMintInfo(mintA); + const mintBAccount = await fetcher.getMintInfo(mintB); + if (mintAAccount === null || mintBAccount === null) { + throw Error("Mint account not found"); + } + + const initialTick = PriceMath.priceToTickIndex(initialPrice, mintAAccount.decimals, mintBAccount.decimals) + const tickSpacing = FEE_TIERS[feeTier]; + + const { poolKey, tx: txBuilder } = await client.createPool( + whirlpoolsConfigAddress, + mintA, + mintB, + tickSpacing, + initialTick, + wallet.publicKey, + ) + + const txPayload = await txBuilder.build(); + const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + const instructions = txPayloadDecompiled.instructions; + + const txId = await sendTx( + agent, + instructions, + txPayload.signers as Keypair[] + ); + return JSON.stringify({ + transactionId: txId, + whirlpoolAddress: poolKey.toString(), + }); + } catch (error) { + console.log(error) + throw new Error(`${error}`); + } +} \ No newline at end of file diff --git a/src/tools/create_orca_single_sided_whirlpool.ts b/src/tools/orca_create_single_sided_liquidity_pool.ts similarity index 88% rename from src/tools/create_orca_single_sided_whirlpool.ts rename to src/tools/orca_create_single_sided_liquidity_pool.ts index b861c94..932f119 100644 --- a/src/tools/create_orca_single_sided_whirlpool.ts +++ b/src/tools/orca_create_single_sided_liquidity_pool.ts @@ -1,4 +1,9 @@ -import { Keypair, PublicKey, Transaction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { + Keypair, + PublicKey, + TransactionMessage, + VersionedTransaction +} from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { BN, Wallet } from "@coral-xyz/anchor"; import { Decimal } from "decimal.js"; @@ -34,7 +39,7 @@ import { import { sendTx } from "../utils/send_tx"; /** - * Maps fee tier percentages to their corresponding tick spacing values in the Orca Whirlpool protocol. + * Maps fee tier bps to their corresponding tick spacing values in the Orca Whirlpool protocol. * * @remarks * Fee tiers determine the percentage of fees collected on swaps, while tick spacing affects @@ -45,22 +50,22 @@ import { sendTx } from "../utils/send_tx"; * - [Whirlpool Parameters](https://orca-so.github.io/whirlpools/Architecture%20Overview/Whirlpool%20Parameters) * * @example - * const tickSpacing = FEE_TIERS[0.30]; // Returns 64 + * const tickSpacing = FEE_TIERS[1]; // returns 1 */ export const FEE_TIERS = { - 0.01: 1, - 0.02: 2, - 0.04: 4, - 0.05: 8, - 0.16: 16, - 0.3: 64, - 0.65: 96, - 1.0: 128, - 2.0: 256, + 1: 1, + 2: 2, + 4: 4, + 5: 8, + 16: 16, + 30: 64, + 65: 96, + 100: 128, + 200: 256, } as const; /** - * # Creates a single-sided Whirlpool. + * # Creates a single-sided liquidity pool. * * This function initializes a new Whirlpool (liquidity pool) on Orca and seeds it with liquidity from a single token. * @@ -84,7 +89,7 @@ export const FEE_TIERS = { * @param otherTokenMint - The mint address of the other token in the pool, eg. USDC. * @param initialPrice - The initial price of the deposit token in terms of the other token. * @param maxPrice - The maximum price at which liquidity is added. - * @param feeTier - The fee tier percentage for the pool, determining tick spacing and fee collection rates. + * @param feeTier - The fee tier bps for the pool, determining tick spacing and fee collection rates. * * @returns A promise that resolves to a transaction ID (`string`) of the transaction creating the pool. * @@ -95,42 +100,15 @@ export const FEE_TIERS = { * @remarks * This function is designed for single-sided deposits where users only contribute one type of token, * and the function manages mint order and necessary calculations. - * - * @example - * ```typescript - * import { SolanaAgentKit } from "your-sdk"; - * import { PublicKey } from "@solana/web3.js"; - * import { BN } from "@coral-xyz/anchor"; - * import Decimal from "decimal.js"; - * - * const agent = new SolanaAgentKit(wallet, connection); - * const depositAmount = new BN(1_000_000_000_000); // 1 million SHARK if SHARK has 6 decimals - * const depositTokenMint = new PublicKey("DEPOSTI_TOKEN_ADDRESS"); - * const otherTokenMint = new PublicKey("OTHER_TOKEN_ADDRESS"); - * const initialPrice = new Decimal(0.001); - * const maxPrice = new Decimal(5.0); - * const feeTier = 0.02; - * - * const txId = await createOrcaSingleSidedWhirlpool( - * agent, - * depositAmount, - * depositTokenMint, - * otherTokenMint, - * initialPrice, - * maxPrice, - * feeTier, - * ); - * console.log(`Single sided whirlpool created in transaction: ${txId}`); - * ``` */ -export async function createOrcaSingleSidedWhirlpool( +export async function orcaCreateSingleSidedLiquidityPool( agent: SolanaAgentKit, depositTokenAmount: BN, depositTokenMint: PublicKey, otherTokenMint: PublicKey, initialPrice: Decimal, maxPrice: Decimal, - feeTier: keyof typeof FEE_TIERS, + feeTierBps: keyof typeof FEE_TIERS, ): Promise { let whirlpoolsConfigAddress: PublicKey; if (agent.connection.rpcEndpoint.includes('mainnet')) { @@ -167,7 +145,7 @@ export async function createOrcaSingleSidedWhirlpool( if (mintAAccount === null || mintBAccount === null) { throw Error("Mint account not found"); } - const tickSpacing = FEE_TIERS[feeTier]; + const tickSpacing = FEE_TIERS[feeTierBps]; const tickIndex = PriceMath.priceToTickIndex( initialPrice, mintAAccount.decimals, @@ -196,7 +174,7 @@ export async function createOrcaSingleSidedWhirlpool( whirlpoolsConfigAddress, mintA, mintB, - FEE_TIERS[feeTier], + FEE_TIERS[feeTierBps], ); const tokenBadgeA = PDAUtil.getTokenBadge( ORCA_WHIRLPOOL_PROGRAM_ID, @@ -419,7 +397,8 @@ export async function createOrcaSingleSidedWhirlpool( const txPayload = await txBuilder.build(); const instructions = TransactionMessage.decompile( - (txPayload.transaction as VersionedTransaction).message).instructions + (txPayload.transaction as VersionedTransaction).message, + ).instructions; try { const txId = await sendTx( @@ -429,6 +408,6 @@ export async function createOrcaSingleSidedWhirlpool( ); return txId; } catch (error) { - throw new Error(`Failed to create pool: ${JSON.stringify(error)}`); + throw new Error(`Failed to send transaction: ${JSON.stringify(error)}`); } -} +} \ No newline at end of file diff --git a/src/tools/orca_open_centered_position_with_liquidity.ts b/src/tools/orca_open_centered_position_with_liquidity.ts new file mode 100644 index 0000000..de89a28 --- /dev/null +++ b/src/tools/orca_open_centered_position_with_liquidity.ts @@ -0,0 +1,136 @@ +import { Keypair, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { SolanaAgentKit } from "../agent"; +import { Wallet } from "@coral-xyz/anchor"; +import { Decimal } from "decimal.js"; +import { + ORCA_WHIRLPOOL_PROGRAM_ID, + WhirlpoolContext, + PriceMath, + buildWhirlpoolClient, + increaseLiquidityQuoteByInputToken, + TokenExtensionContextForPool, + NO_TOKEN_EXTENSION_CONTEXT, +} from "@orca-so/whirlpools-sdk"; + +import { sendTx } from "../utils/send_tx"; +import { Percentage } from "@orca-so/common-sdk"; + +/** + * # Opens a Centered Liquidity Position in an Orca Whirlpool + * + * This function opens a centered liquidity position in a specified Orca Whirlpool. The user defines + * a basis point (bps) offset from the center price to set the lower and upper bounds of the position. + * The user also specifies the token mint and the amount to deposit. The required amount of the other token + * is calculated automatically. + * + * ## Parameters + * - `agent`: The `SolanaAgentKit` instance representing the wallet and connection details. + * - `whirlpoolAddress`: The address of the Orca Whirlpool where the position will be opened. + * - `priceOffsetBps`: The basis point (bps) offset (on one side) from the center price. For example, + * 500 bps (5%) creates a range from 95% to 105% of the center price. + * - `inputTokenMint`: The mint address of the token being deposited (e.g., USDC or another token). + * - `inputAmount`: The amount of the input token to deposit, specified as a `Decimal` value. + * + * ## Returns + * A `Promise` that resolves to the transaction ID (`string`) of the transaction that opens the position. + * + * ## Notes + * - The `priceOffsetBps` specifies the range symmetrically around the current price. + * - The specified `inputTokenMint` determines which token is deposited directly. The function calculates + * the required amount of the other token based on the specified price range. + * - This function supports Orca's token extensions for managing tokens with special behaviors. + * - The function assumes a maximum slippage of 1% for liquidity provision. + * + * ## Throws + * An error will be thrown if: + * - The specified Whirlpool address is invalid or inaccessible. + * - The transaction fails to send. + * - Any required mint information cannot be fetched. + * + * @param agent - The `SolanaAgentKit` instance representing the wallet and connection. + * @param whirlpoolAddress - The address of the Orca Whirlpool. + * @param priceOffsetBps - The basis point offset (one side) from the center price. + * @param inputTokenMint - The mint address of the token to deposit. + * @param inputAmount - The amount of the input token to deposit. + * @returns A promise resolving to the transaction ID (`string`). + */ +export async function orcaOpenCenteredPositionWithLiquidity( + agent: SolanaAgentKit, + whirlpoolAddress: PublicKey, + priceOffsetBps: number, + inputTokenMint: PublicKey, + inputAmount: Decimal, +): Promise { + try { + const wallet = new Wallet(agent.wallet); + const ctx = WhirlpoolContext.from( + agent.connection, + wallet, + ORCA_WHIRLPOOL_PROGRAM_ID, + ); + const client = buildWhirlpoolClient(ctx) + + const whirlpool = await client.getPool(whirlpoolAddress); + const mintInfoA = whirlpool.getTokenAInfo() + const mintInfoB = whirlpool.getTokenBInfo() + const price = PriceMath.sqrtPriceX64ToPrice( + whirlpool.getData().sqrtPrice, + mintInfoA.decimals, + mintInfoB.decimals + ) + + const lowerPrice = price.mul(1 - priceOffsetBps / 10000) + const upperPrice = price.mul(1 + priceOffsetBps / 10000) + const lowerTick = PriceMath.priceToTickIndex(lowerPrice, mintInfoA.decimals, mintInfoB.decimals) + const upperTick = PriceMath.priceToTickIndex(upperPrice, mintInfoA.decimals, mintInfoB.decimals) + + const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([lowerTick, upperTick]) + let instructions: TransactionInstruction[] = [] + let signers: Keypair[] = [] + if (txBuilderTickArrays !== null) { + const txPayloadTickArrays = await txBuilderTickArrays.build(); + const txPayloadTickArraysDecompiled = TransactionMessage.decompile((txPayloadTickArrays.transaction as VersionedTransaction).message); + const instructionsTickArrays = txPayloadTickArraysDecompiled.instructions; + instructions = instructions.concat(instructionsTickArrays); + signers = signers.concat(txPayloadTickArrays.signers as Keypair[]); + } + + const tokenExtensionCtx: TokenExtensionContextForPool = { + ...NO_TOKEN_EXTENSION_CONTEXT, + tokenMintWithProgramA: mintInfoA, + tokenMintWithProgramB: mintInfoB, + }; + const increaseLiquiditQuote = increaseLiquidityQuoteByInputToken( + inputTokenMint, + inputAmount, + lowerTick, + upperTick, + Percentage.fromFraction(1, 100), + whirlpool, + tokenExtensionCtx + ) + const { positionMint, tx: txBuilder } = await whirlpool.openPositionWithMetadata( + lowerTick, + upperTick, + increaseLiquiditQuote + ) + + const txPayload = await txBuilder.build(); + const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + instructions = instructions.concat(txPayloadDecompiled.instructions); + signers = signers.concat(txPayload.signers as Keypair[]); + + const txId = await sendTx( + agent, + instructions, + signers + ); + return JSON.stringify({ + transactionId: txId, + positionMint: positionMint.toString(), + }) + } catch (error) { + console.log(error) + throw new Error(`${error}`); + } +} \ No newline at end of file diff --git a/src/tools/orca_open_single_sided_position.ts b/src/tools/orca_open_single_sided_position.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index f357b8e..ad42204 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -1,5 +1,5 @@ import { SolanaAgentKit } from "../agent"; -import { Keypair, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { Keypair, Signer, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; import { ComputeBudgetProgram, } from "@solana/web3.js"; @@ -70,45 +70,55 @@ export async function sendTx( instructions: TransactionInstruction[], otherKeypairs?: Keypair[] ) { - const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid"); - const allInstructions = [ - ixComputeBudget.computeBudgetLimitInstruction, - ixComputeBudget.computeBudgetPriorityFeeInstructions, - ...instructions]; - const messageV0 = new TransactionMessage({ - payerKey: agent.wallet_address, - recentBlockhash: ixComputeBudget.blockhash, - instructions: allInstructions, - }).compileToV0Message(); - const transaction = new VersionedTransaction(messageV0); - transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]); + try { + const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid"); + const allInstructions = [ + ixComputeBudget.computeBudgetLimitInstruction, + ixComputeBudget.computeBudgetPriorityFeeInstructions, + ...instructions]; + const messageV0 = new TransactionMessage({ + payerKey: agent.wallet_address, + recentBlockhash: ixComputeBudget.blockhash, + instructions: allInstructions, + }).compileToV0Message(); + const transaction = new VersionedTransaction(messageV0); + transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]); - const timeoutMs = 90000; - const startTime = Date.now(); - while (Date.now() - startTime < timeoutMs) { - const transactionStartTime = Date.now(); + const timeoutMs = 90000; + const startTime = Date.now(); + try { + while (Date.now() - startTime < timeoutMs) { + const transactionStartTime = Date.now(); - const signature = await agent.connection.sendTransaction( - transaction, - { - maxRetries: 0, - skipPreflight: true, - }); + const signature = await agent.connection.sendTransaction( + transaction, + { + maxRetries: 0, + skipPreflight: true, + }); - const statuses = await agent.connection.getSignatureStatuses([signature]); - if (statuses.value[0]) { - if (!statuses.value[0].err) { - return signature; - } else { - throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`); + const statuses = await agent.connection.getSignatureStatuses([signature]); + if (statuses.value[0]) { + if (!statuses.value[0].err) { + return signature; + } else { + throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`); + } + } + + const elapsedTime = Date.now() - transactionStartTime; + const remainingTime = Math.max(0, 1000 - elapsedTime); + if (remainingTime > 0) { + await new Promise(resolve => setTimeout(resolve, remainingTime)); + } } + throw new Error("Transaction timeout"); + } catch (error) { + console.log("Error sending transaction:", error); + throw error; } - - const elapsedTime = Date.now() - transactionStartTime; - const remainingTime = Math.max(0, 1000 - elapsedTime); - if (remainingTime > 0) { - await new Promise(resolve => setTimeout(resolve, remainingTime)); - } + } catch (error) { + console.log("Error sending transaction:", error); + throw error; } - throw new Error("Transaction timeout"); } diff --git a/test/index.ts b/test/index.ts index c6f188d..1a64ec9 100644 --- a/test/index.ts +++ b/test/index.ts @@ -36,8 +36,8 @@ const WALLET_DATA_FILE = "wallet_data.txt"; async function initializeAgent() { try { const llm = new ChatOpenAI({ - modelName: "gpt-4o", - temperature: 0.7, + modelName: "gpt-4o-mini", + // temperature: 0.7, }); let walletDataStr: string | null = null; From 1e1588b3fc324bd273af71e01460dba73a9e59a8 Mon Sep 17 00:00:00 2001 From: calintje Date: Mon, 30 Dec 2024 04:53:28 +0100 Subject: [PATCH 5/8] Finish Orca tools --- src/agent/index.ts | 46 +- src/langchain/index.ts | 137 ++++- src/tools/deploy_token.ts | 2 +- src/tools/index.ts | 3 + src/tools/orca_close_position.ts | 78 +++ src/tools/orca_create_clmm.ts | 33 +- ...orca_create_single_sided_liquidity_pool.ts | 568 +++++++++--------- src/tools/orca_fetch_positions.ts | 104 ++++ ...a_open_centered_position_with_liquidity.ts | 14 +- src/tools/orca_open_single_sided_position.ts | 169 ++++++ src/utils/send_tx.ts | 7 +- test/index.ts | 2 +- 12 files changed, 843 insertions(+), 320 deletions(-) create mode 100644 src/tools/orca_close_position.ts create mode 100644 src/tools/orca_fetch_positions.ts diff --git a/src/agent/index.ts b/src/agent/index.ts index b39eaa7..da2e489 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -27,6 +27,7 @@ import { orcaCreateSingleSidedLiquidityPool, orcaCreateCLMM, orcaOpenCenteredPositionWithLiquidity, + orcaOpenSingleSidedPosition, FEE_TIERS, fetchPrice, pythFetchPrice, @@ -37,6 +38,8 @@ import { getOwnedAllDomains, resolveAllDomains, create_gibwork_task, + orcaClosePosition, + orcaFetchPositions, } from "../tools"; import { @@ -202,23 +205,32 @@ export class SolanaAgentKit { ); } + async orcaClosePosition( + positionMintAddress: PublicKey, + ) { + return orcaClosePosition( + this, + positionMintAddress, + ); + } + async orcaCreateCLMM( - mintA: PublicKey, - mintB: PublicKey, + mintDeploy: PublicKey, + mintPair: PublicKey, initialPrice: Decimal, feeTier: keyof typeof FEE_TIERS, ) { return orcaCreateCLMM( this, - mintA, - mintB, + mintDeploy, + mintPair, initialPrice, feeTier, ); } async orcaCreateSingleSidedLiquidityPool( - depositTokenAmount: BN, + depositTokenAmount: number, depositTokenMint: PublicKey, otherTokenMint: PublicKey, initialPrice: Decimal, @@ -236,6 +248,13 @@ export class SolanaAgentKit { ); } + async orcaFetchPositions( + ) { + return orcaFetchPositions( + this, + ); + } + async orcaOpenCenteredPositionWithLiquidity( whirlpoolAddress: PublicKey, priceOffsetBps: number, @@ -251,6 +270,23 @@ export class SolanaAgentKit { ); } + async orcaOpenSingleSidedPosition( + whirlpoolAddress: PublicKey, + distanceFromCurrentPriceBps: number, + widthBps: number, + inputTokenMint: PublicKey, + inputAmount: Decimal, + ): Promise { + return orcaOpenSingleSidedPosition( + this, + whirlpoolAddress, + distanceFromCurrentPriceBps, + widthBps, + inputTokenMint, + inputAmount, + ); + } + async resolveAllDomains(domain: string): Promise { return resolveAllDomains(this, domain); } diff --git a/src/langchain/index.ts b/src/langchain/index.ts index 21da295..d627984 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -10,6 +10,7 @@ import { create_image } from "../tools/create_image"; import { BN } from "@coral-xyz/anchor"; import { FEE_TIERS } from "../tools"; import { toJSON } from "../utils/toJSON"; +import { s } from "@raydium-io/raydium-sdk-v2/lib/api-0eb57ba2"; export class SolanaBalanceTool extends Tool { name = "solana_balance"; @@ -754,13 +755,49 @@ export class SolanaCompressedAirdropTool extends Tool { } } +export class SolanaClosePostition extends Tool { + name = "orca_close_position"; + description = `Closes an existing liquidity position in an Orca Whirlpool. This function fetches the position + details using the provided mint address and closes the position with a 1% slippage. + + Inputs (JSON string): + - positionMintAddress: string, the address of the position mint that represents the liquidity position.` + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + async _call(input: string): Promise { + try { + const inputFormat = JSON.parse(input); + const positionMintAddress = new PublicKey(inputFormat.positionMintAddress); + + const txId = await this.solanaKit.orcaClosePosition( + positionMintAddress, + ); + + return JSON.stringify({ + status: "success", + message: "Liquidity position closed successfully.", + transaction: txId, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + export class SolanaOrcaCreateCLMM extends Tool { name = "orca_create_clmm"; - description = `Create a Concentrated Liquidity Market Maker (CLMM) pool on Orca, the most efficient and capital-optimized CLMM on Solana. This function initializes a CLMM pool but does not add liquidity. You can add liquidity later using a centered position or a single-sided position. Single-sided positions are ideal for directional price expectations, acting as sort of a limit call. Centered positions are ideal for generating yield. The tighter, the better. + description = `Create a Concentrated Liquidity Market Maker (CLMM) pool on Orca, the most efficient and capital-optimized CLMM on Solana. This function initializes a CLMM pool but does not add liquidity. You can add liquidity later using a centered position or a single-sided position. Inputs (JSON string): - - mintA: string, mint address of the first token, e.g., "MintAAddress" (required). - - mintB: string, mint address of the second token, e.g., "MintBAddress" (required). + - mintDeploy: string, the mint of the token you want to deploy (required). + - mintPair: string, The mint of the token you want to pair the deployed mint with (required). - initialPrice: number, initial price of mintA in terms of mintB, e.g., 0.001 (required). - feeTier: number, fee tier in bps. Options: 1, 2, 4, 5, 16, 30, 65, 100, 200 (required).`; @@ -771,8 +808,8 @@ export class SolanaOrcaCreateCLMM extends Tool { async _call(input: string): Promise { try { const inputFormat = JSON.parse(input); - const mintA = new PublicKey(inputFormat.mintA); - const mintB = new PublicKey(inputFormat.mintB); + const mintA = new PublicKey(inputFormat.mintDeploy); + const mintB = new PublicKey(inputFormat.mintPair); const initialPrice = new Decimal(inputFormat.initialPrice); const feeTier = inputFormat.feeTier; @@ -828,7 +865,7 @@ export class SolanaOrcaCreateSingleSideLiquidityPool extends Tool { async _call(input: string): Promise { try { const inputFormat = JSON.parse(input); - const depositTokenAmount = new BN(inputFormat.depositTokenAmount); + const depositTokenAmount = inputFormat.depositTokenAmount; const depositTokenMint = new PublicKey(inputFormat.depositTokenMint); const otherTokenMint = new PublicKey(inputFormat.otherTokenMint); const initialPrice = new Decimal(inputFormat.initialPrice); @@ -867,9 +904,37 @@ export class SolanaOrcaCreateSingleSideLiquidityPool extends Tool { } } +export class SolanaOrcaFetchPositions extends Tool { + name = "orca_fetch_positions"; + description = `Fetch all the liquidity positions in an Orca Whirlpool by owner. Returns an object with positiont mint addresses as keys and position status details as values.`; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + async _call(): Promise { + try { + + const txId = await this.solanaKit.orcaFetchPositions() + + return JSON.stringify({ + status: "success", + message: "Liquidity positions fetched.", + transaction: txId, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + export class SolanaOrcaOpenCenteredPosition extends Tool { name = "orca_open_centered_position_with_liquidity"; - description = `Add liquidity to a CLMM by opening a position in an Orca Whirlpool, the most efficient CLMM on Solana for precise liquidity management. A tighter range concentrates more liquidity, increasing yield potential. This function calculates the required amount of the other token based on the input token and desired offset. Centered positions are ideal for optimizing returns within defined ranges. + description = `Add liquidity to a CLMM by opening a centered position in an Orca Whirlpool, the most efficient liquidity pool on Solana. Inputs (JSON string): - whirlpoolAddress: string, address of the Orca Whirlpool (required). @@ -889,8 +954,8 @@ export class SolanaOrcaOpenCenteredPosition extends Tool { const inputTokenMint = new PublicKey(inputFormat.inputTokenMint); const inputAmount = new Decimal(inputFormat.inputAmount); - if (priceOffsetBps <= 0 || priceOffsetBps > 10_000) { - throw new Error("Invalid priceOffsetBps. It must be greater than 0 and less than or equal to 10,000."); + if (priceOffsetBps < 0 ) { + throw new Error("Invalid distanceFromCurrentPriceBps. It must be equal or greater than 0."); } const txId = await this.solanaKit.orcaOpenCenteredPositionWithLiquidity( @@ -915,6 +980,57 @@ export class SolanaOrcaOpenCenteredPosition extends Tool { } } +export class SolanaOrcaOpenSingleSidedPosition extends Tool { + name = "orca_open_single_sided_position"; + description = `Add liquidity to a CLMM by opening a single-sided position in an Orca Whirlpool, the most efficient liquidity pool on Solana. + + Inputs (JSON string): + - whirlpoolAddress: string, address of the Orca Whirlpool (required). + - distanceFromCurrentPriceBps: number, distance in basis points from the current price for the position (required). + - widthBps: number, width of the position in basis points (required). + - inputTokenMint: string, mint address of the deposit token (required). + - inputAmount: number, amount of the deposit token, e.g., 100.0 (required).`; + + constructor(private solanaKit: SolanaAgentKit) { + super(); + } + + async _call(input: string): Promise { + try { + const inputFormat = JSON.parse(input); + const whirlpoolAddress = new PublicKey(inputFormat.whirlpoolAddress); + const distanceFromCurrentPriceBps = inputFormat.distanceFromCurrentPriceBps; + const widthBps = inputFormat.widthBps; + const inputTokenMint = new PublicKey(inputFormat.inputTokenMint); + const inputAmount = new Decimal(inputFormat.inputAmount); + + if (distanceFromCurrentPriceBps < 0 || widthBps < 0) { + throw new Error("Invalid distanceFromCurrentPriceBps or width. It must be equal or greater than 0."); + } + + const txId = await this.solanaKit.orcaOpenSingleSidedPosition( + whirlpoolAddress, + distanceFromCurrentPriceBps, + widthBps, + inputTokenMint, + inputAmount, + ); + + return JSON.stringify({ + status: "success", + message: "Single-sided liquidity position opened successfully.", + transaction: txId, + }); + } catch (error: any) { + return JSON.stringify({ + status: "error", + message: error.message, + code: error.code || "UNKNOWN_ERROR", + }); + } + } +} + export class SolanaRaydiumCreateAmmV4 extends Tool { name = "raydium_create_ammV4"; description = `Raydium's Legacy AMM that requiers an OpenBook marketID @@ -1357,9 +1473,12 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) { new SolanaRaydiumCreateClmm(solanaKit), new SolanaRaydiumCreateCpmm(solanaKit), new SolanaOpenbookCreateMarket(solanaKit), + new SolanaClosePostition(solanaKit), new SolanaOrcaCreateCLMM(solanaKit), new SolanaOrcaCreateSingleSideLiquidityPool(solanaKit), + new SolanaOrcaFetchPositions(solanaKit), new SolanaOrcaOpenCenteredPosition(solanaKit), + new SolanaOrcaOpenSingleSidedPosition(solanaKit), new SolanaPythFetchPrice(solanaKit), new SolanaResolveDomainTool(solanaKit), new SolanaGetOwnedDomains(solanaKit), diff --git a/src/tools/deploy_token.ts b/src/tools/deploy_token.ts index 7532eef..798e020 100644 --- a/src/tools/deploy_token.ts +++ b/src/tools/deploy_token.ts @@ -59,7 +59,7 @@ export async function deploy_token( mint: mint.publicKey, tokenStandard: TokenStandard.Fungible, tokenOwner: fromWeb3JsPublicKey(agent.wallet_address), - amount: initialSupply, + amount: initialSupply * Math.pow(10, decimals), }), ); } diff --git a/src/tools/index.ts b/src/tools/index.ts index a87f931..863a68a 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -15,9 +15,12 @@ export * from "./get_token_data"; export * from "./stake_with_jup"; export * from "./fetch_price"; export * from "./send_compressed_airdrop"; +export * from "./orca_close_position" export * from "./orca_create_clmm"; export * from "./orca_create_single_sided_liquidity_pool"; +export * from "./orca_fetch_positions"; export * from "./orca_open_centered_position_with_liquidity"; +export * from "./orca_open_single_sided_position"; export * from "./get_all_domains_tlds"; export * from "./get_all_registered_all_domains"; export * from "./get_owned_domains_for_tld"; diff --git a/src/tools/orca_close_position.ts b/src/tools/orca_close_position.ts new file mode 100644 index 0000000..976d6fc --- /dev/null +++ b/src/tools/orca_close_position.ts @@ -0,0 +1,78 @@ +import { + Keypair, + PublicKey, + TransactionMessage, + VersionedTransaction +} from "@solana/web3.js"; +import { SolanaAgentKit } from "../agent"; +import { Wallet } from "@coral-xyz/anchor"; +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 Orca’s 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 { + 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}`); + } +} \ No newline at end of file diff --git a/src/tools/orca_create_clmm.ts b/src/tools/orca_create_clmm.ts index 4b6d465..5c6b402 100644 --- a/src/tools/orca_create_clmm.ts +++ b/src/tools/orca_create_clmm.ts @@ -1,4 +1,9 @@ -import { Keypair, PublicKey, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { + Keypair, + PublicKey, + TransactionMessage, + VersionedTransaction +} from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { Wallet } from "@coral-xyz/anchor"; import { Decimal } from "decimal.js"; @@ -9,7 +14,6 @@ import { PoolUtil, buildWhirlpoolClient, } from "@orca-so/whirlpools-sdk"; - import { sendTx } from "../utils/send_tx"; import { FEE_TIERS } from "./orca_create_single_sided_liquidity_pool"; @@ -28,9 +32,9 @@ import { FEE_TIERS } from "./orca_create_single_sided_liquidity_pool"; * adjusts the input order as needed and inverts the initial price accordingly. * * @param agent - The `SolanaAgentKit` instance representing the wallet and connection details. - * @param mintA - The mint address of the first token in the pool (e.g., SHARK). - * @param mintB - The mint address of the second token in the pool (e.g., USDC). - * @param initialPrice - The initial price of `mintA` in terms of `mintB`. + * @param mintDeploy - The mint of the token you want to deploy (e.g., SHARK). + * @param mintPair - The mint of the token you want to pair the deployed mint with (e.g., USDC). + * @param initialPrice - The initial price of `mintDeploy` in terms of `mintPair`. * @param feeTier - The fee tier bps for the pool, determining tick spacing and fee collection rates. * * @returns A promise that resolves to a transaction ID (`string`) of the transaction creating the pool. @@ -46,8 +50,8 @@ import { FEE_TIERS } from "./orca_create_single_sided_liquidity_pool"; */ export async function orcaCreateCLMM( agent: SolanaAgentKit, - mintA: PublicKey, - mintB: PublicKey, + mintDeploy: PublicKey, + mintPair: PublicKey, initialPrice: Decimal, feeTier: keyof typeof FEE_TIERS, ): Promise { @@ -70,14 +74,18 @@ export async function orcaCreateCLMM( const client = buildWhirlpoolClient(ctx) const correctTokenOrder = PoolUtil.orderMints( - mintA, - mintB, + mintDeploy, + mintPair, ).map((addr) => addr.toString()); const isCorrectMintOrder = - correctTokenOrder[0] === mintA.toString(); + correctTokenOrder[0] === mintDeploy.toString(); + let mintA; + let mintB; if (!isCorrectMintOrder) { - [mintA, mintB] = [mintB, mintA]; + [mintA, mintB] = [mintPair, mintDeploy]; initialPrice = new Decimal(1 / initialPrice.toNumber()); + } else { + [mintA, mintB] = [mintDeploy, mintPair]; } const mintAAccount = await fetcher.getMintInfo(mintA); const mintBAccount = await fetcher.getMintInfo(mintB); @@ -85,9 +93,8 @@ export async function orcaCreateCLMM( throw Error("Mint account not found"); } - const initialTick = PriceMath.priceToTickIndex(initialPrice, mintAAccount.decimals, mintBAccount.decimals) const tickSpacing = FEE_TIERS[feeTier]; - + const initialTick = PriceMath.priceToInitializableTickIndex(initialPrice, mintAAccount.decimals, mintBAccount.decimals, tickSpacing) const { poolKey, tx: txBuilder } = await client.createPool( whirlpoolsConfigAddress, mintA, diff --git a/src/tools/orca_create_single_sided_liquidity_pool.ts b/src/tools/orca_create_single_sided_liquidity_pool.ts index 932f119..68602b9 100644 --- a/src/tools/orca_create_single_sided_liquidity_pool.ts +++ b/src/tools/orca_create_single_sided_liquidity_pool.ts @@ -84,7 +84,7 @@ export const FEE_TIERS = { * be 1/`initialPrice`. This will not affect the price of the token as you intended it to be. * * @param agent - The `SolanaAgentKit` instance representing the wallet and connection details. - * @param depositTokenAmount - The amount of the deposit token (including the decimals) to contribute to the pool. + * @param depositTokenAmount - The amount of the deposit token to deposit in the pool. * @param depositTokenMint - The mint address of the token being deposited into the pool, eg. SHARK. * @param otherTokenMint - The mint address of the other token in the pool, eg. USDC. * @param initialPrice - The initial price of the deposit token in terms of the other token. @@ -103,304 +103,308 @@ export const FEE_TIERS = { */ export async function orcaCreateSingleSidedLiquidityPool( agent: SolanaAgentKit, - depositTokenAmount: BN, + depositTokenAmount: number, depositTokenMint: PublicKey, otherTokenMint: PublicKey, initialPrice: Decimal, maxPrice: Decimal, feeTierBps: keyof typeof FEE_TIERS, ): Promise { - let whirlpoolsConfigAddress: PublicKey; - if (agent.connection.rpcEndpoint.includes('mainnet')) { - whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); - } else if (agent.connection.rpcEndpoint.includes('devnet')) { - whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); - } else { - throw new Error('Unsupported network'); - } - const wallet = new Wallet(agent.wallet); - const ctx = WhirlpoolContext.from( - agent.connection, - wallet, - ORCA_WHIRLPOOL_PROGRAM_ID, - ); - const fetcher = ctx.fetcher; - - const correctTokenOrder = PoolUtil.orderMints( - otherTokenMint, - depositTokenMint, - ).map((addr) => addr.toString()); - const isCorrectMintOrder = - correctTokenOrder[0] === depositTokenMint.toString(); - let mintA, mintB; - if (isCorrectMintOrder) { - [mintA, mintB] = [depositTokenMint, otherTokenMint]; - } else { - [mintA, mintB] = [otherTokenMint, depositTokenMint]; - initialPrice = new Decimal(1 / initialPrice.toNumber()); - maxPrice = new Decimal(1 / maxPrice.toNumber()); - } - const mintAAccount = await fetcher.getMintInfo(mintA); - const mintBAccount = await fetcher.getMintInfo(mintB); - if (mintAAccount === null || mintBAccount === null) { - throw Error("Mint account not found"); - } - const tickSpacing = FEE_TIERS[feeTierBps]; - const tickIndex = PriceMath.priceToTickIndex( - initialPrice, - mintAAccount.decimals, - mintBAccount.decimals, - ); - const initialTick = TickUtil.getInitializableTickIndex( - tickIndex, - tickSpacing, - ); - - const tokenExtensionCtx: TokenExtensionContextForPool = { - ...NO_TOKEN_EXTENSION_CONTEXT, - tokenMintWithProgramA: mintAAccount, - tokenMintWithProgramB: mintBAccount, - }; - const feeTierKey = PDAUtil.getFeeTier( - ORCA_WHIRLPOOL_PROGRAM_ID, - whirlpoolsConfigAddress, - tickSpacing, - ).publicKey; - const initSqrtPrice = PriceMath.tickIndexToSqrtPriceX64(initialTick); - const tokenVaultAKeypair = Keypair.generate(); - const tokenVaultBKeypair = Keypair.generate(); - const whirlpoolPda = PDAUtil.getWhirlpool( - ORCA_WHIRLPOOL_PROGRAM_ID, - whirlpoolsConfigAddress, - mintA, - mintB, - FEE_TIERS[feeTierBps], - ); - const tokenBadgeA = PDAUtil.getTokenBadge( - ORCA_WHIRLPOOL_PROGRAM_ID, - whirlpoolsConfigAddress, - mintA, - ).publicKey; - const tokenBadgeB = PDAUtil.getTokenBadge( - ORCA_WHIRLPOOL_PROGRAM_ID, - whirlpoolsConfigAddress, - mintB, - ).publicKey; - const baseParamsPool = { - initSqrtPrice, - whirlpoolsConfig: whirlpoolsConfigAddress, - whirlpoolPda, - tokenMintA: mintA, - tokenMintB: mintB, - tokenVaultAKeypair, - tokenVaultBKeypair, - feeTierKey, - tickSpacing: tickSpacing, - funder: wallet.publicKey, - }; - const initPoolIx = !TokenExtensionUtil.isV2IxRequiredPool(tokenExtensionCtx) - ? WhirlpoolIx.initializePoolIx(ctx.program, baseParamsPool) - : WhirlpoolIx.initializePoolV2Ix(ctx.program, { - ...baseParamsPool, - tokenProgramA: tokenExtensionCtx.tokenMintWithProgramA.tokenProgram, - tokenProgramB: tokenExtensionCtx.tokenMintWithProgramB.tokenProgram, - tokenBadgeA, - tokenBadgeB, - }); - const initialTickArrayStartTick = TickUtil.getStartTickIndex( - initialTick, - tickSpacing, - ); - const initialTickArrayPda = PDAUtil.getTickArray( - ctx.program.programId, - whirlpoolPda.publicKey, - initialTickArrayStartTick, - ); - - const txBuilder = new TransactionBuilder( - ctx.provider.connection, - ctx.provider.wallet, - ctx.txBuilderOpts, - ); - txBuilder.addInstruction(initPoolIx); - txBuilder.addInstruction( - initTickArrayIx(ctx.program, { - startTick: initialTickArrayStartTick, - tickArrayPda: initialTickArrayPda, - whirlpool: whirlpoolPda.publicKey, - funder: wallet.publicKey, - }), - ); - - let tickLowerIndex, tickUpperIndex; - if (isCorrectMintOrder) { - tickLowerIndex = initialTick; - tickUpperIndex = PriceMath.priceToTickIndex( - maxPrice, - mintAAccount.decimals, - mintBAccount.decimals, + try { + let whirlpoolsConfigAddress: PublicKey; + if (agent.connection.rpcEndpoint.includes('mainnet')) { + whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); + } else if (agent.connection.rpcEndpoint.includes('devnet')) { + whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); + } else { + throw new Error('Unsupported network'); + } + const wallet = new Wallet(agent.wallet); + const ctx = WhirlpoolContext.from( + agent.connection, + wallet, + ORCA_WHIRLPOOL_PROGRAM_ID, ); - } else { - tickLowerIndex = PriceMath.priceToTickIndex( - maxPrice, - mintAAccount.decimals, - mintBAccount.decimals, - ); - tickUpperIndex = initialTick; - } - const tickLowerInitializableIndex = TickUtil.getInitializableTickIndex( - tickLowerIndex, - tickSpacing, - ); - const tickUpperInitializableIndex = TickUtil.getInitializableTickIndex( - tickUpperIndex, - tickSpacing, - ); - if ( - !TickUtil.checkTickInBounds(tickLowerInitializableIndex) || - !TickUtil.checkTickInBounds(tickUpperInitializableIndex) - ) { - throw Error("Prices out of bounds"); - } - const increasLiquidityQuoteParam: IncreaseLiquidityQuoteParam = { - inputTokenAmount: new BN(depositTokenAmount), - inputTokenMint: depositTokenMint, - tokenMintA: mintA, - tokenMintB: mintB, - tickCurrentIndex: initialTick, - sqrtPrice: initSqrtPrice, - tickLowerIndex: tickLowerInitializableIndex, - tickUpperIndex: tickUpperInitializableIndex, - tokenExtensionCtx: tokenExtensionCtx, - slippageTolerance: Percentage.fromFraction(0, 100), - }; - const liquidityInput = increaseLiquidityQuoteByInputTokenWithParams( - increasLiquidityQuoteParam, - ); - const { liquidityAmount: liquidity, tokenMaxA, tokenMaxB } = liquidityInput; + const fetcher = ctx.fetcher; - const positionMintKeypair = Keypair.generate(); - const positionMintPubkey = positionMintKeypair.publicKey; - const positionPda = PDAUtil.getPosition( - ORCA_WHIRLPOOL_PROGRAM_ID, - positionMintPubkey, - ); - const positionTokenAccountAddress = getAssociatedTokenAddressSync( - positionMintPubkey, - wallet.publicKey, - ctx.accountResolverOpts.allowPDAOwnerAddress, - TOKEN_2022_PROGRAM_ID, - ); - const params = { - funder: wallet.publicKey, - owner: wallet.publicKey, - positionPda, - positionTokenAccount: positionTokenAccountAddress, - whirlpool: whirlpoolPda.publicKey, - tickLowerIndex: tickLowerInitializableIndex, - tickUpperIndex: tickUpperInitializableIndex, - }; - const positionIx = openPositionWithTokenExtensionsIx(ctx.program, { - ...params, - positionMint: positionMintPubkey, - withTokenMetadataExtension: true, - }); - - txBuilder.addInstruction(positionIx); - txBuilder.addSigner(positionMintKeypair); - - const [ataA, ataB] = await resolveOrCreateATAs( - ctx.connection, - wallet.publicKey, - [ - { tokenMint: mintA, wrappedSolAmountIn: tokenMaxA }, - { tokenMint: mintB, wrappedSolAmountIn: tokenMaxB }, - ], - () => ctx.fetcher.getAccountRentExempt(), - wallet.publicKey, - undefined, - ctx.accountResolverOpts.allowPDAOwnerAddress, - "ata", - ); - const { address: tokenOwnerAccountA, ...tokenOwnerAccountAIx } = ataA; - const { address: tokenOwnerAccountB, ...tokenOwnerAccountBIx } = ataB; - - txBuilder.addInstruction(tokenOwnerAccountAIx); - txBuilder.addInstruction(tokenOwnerAccountBIx); - - const tickArrayLowerStartIndex = TickUtil.getStartTickIndex( - tickLowerInitializableIndex, - tickSpacing, - ); - const tickArrayUpperStartIndex = TickUtil.getStartTickIndex( - tickUpperInitializableIndex, - tickSpacing, - ); - const tickArrayLowerPda = PDAUtil.getTickArray( - ctx.program.programId, - whirlpoolPda.publicKey, - tickArrayLowerStartIndex, - ); - const tickArrayUpperPda = PDAUtil.getTickArray( - ctx.program.programId, - whirlpoolPda.publicKey, - tickArrayUpperStartIndex, - ); - if (tickArrayUpperStartIndex !== tickArrayLowerStartIndex) { + const correctTokenOrder = PoolUtil.orderMints( + otherTokenMint, + depositTokenMint, + ).map((addr) => addr.toString()); + const isCorrectMintOrder = + correctTokenOrder[0] === depositTokenMint.toString(); + let mintA, mintB; if (isCorrectMintOrder) { - txBuilder.addInstruction( - initTickArrayIx(ctx.program, { - startTick: tickArrayUpperStartIndex, - tickArrayPda: tickArrayUpperPda, - whirlpool: whirlpoolPda.publicKey, - funder: wallet.publicKey, - }), + [mintA, mintB] = [depositTokenMint, otherTokenMint]; + } else { + [mintA, mintB] = [otherTokenMint, depositTokenMint]; + initialPrice = new Decimal(1 / initialPrice.toNumber()); + maxPrice = new Decimal(1 / maxPrice.toNumber()); + } + const mintAAccount = await fetcher.getMintInfo(mintA); + const mintBAccount = await fetcher.getMintInfo(mintB); + if (mintAAccount === null || mintBAccount === null) { + throw Error("Mint account not found"); + } + const tickSpacing = FEE_TIERS[feeTierBps]; + const tickIndex = PriceMath.priceToTickIndex( + initialPrice, + mintAAccount.decimals, + mintBAccount.decimals, + ); + const initialTick = TickUtil.getInitializableTickIndex( + tickIndex, + tickSpacing, + ); + + const tokenExtensionCtx: TokenExtensionContextForPool = { + ...NO_TOKEN_EXTENSION_CONTEXT, + tokenMintWithProgramA: mintAAccount, + tokenMintWithProgramB: mintBAccount, + }; + const feeTierKey = PDAUtil.getFeeTier( + ORCA_WHIRLPOOL_PROGRAM_ID, + whirlpoolsConfigAddress, + tickSpacing, + ).publicKey; + const initSqrtPrice = PriceMath.tickIndexToSqrtPriceX64(initialTick); + const tokenVaultAKeypair = Keypair.generate(); + const tokenVaultBKeypair = Keypair.generate(); + const whirlpoolPda = PDAUtil.getWhirlpool( + ORCA_WHIRLPOOL_PROGRAM_ID, + whirlpoolsConfigAddress, + mintA, + mintB, + FEE_TIERS[feeTierBps], + ); + const tokenBadgeA = PDAUtil.getTokenBadge( + ORCA_WHIRLPOOL_PROGRAM_ID, + whirlpoolsConfigAddress, + mintA, + ).publicKey; + const tokenBadgeB = PDAUtil.getTokenBadge( + ORCA_WHIRLPOOL_PROGRAM_ID, + whirlpoolsConfigAddress, + mintB, + ).publicKey; + const baseParamsPool = { + initSqrtPrice, + whirlpoolsConfig: whirlpoolsConfigAddress, + whirlpoolPda, + tokenMintA: mintA, + tokenMintB: mintB, + tokenVaultAKeypair, + tokenVaultBKeypair, + feeTierKey, + tickSpacing: tickSpacing, + funder: wallet.publicKey, + }; + const initPoolIx = !TokenExtensionUtil.isV2IxRequiredPool(tokenExtensionCtx) + ? WhirlpoolIx.initializePoolIx(ctx.program, baseParamsPool) + : WhirlpoolIx.initializePoolV2Ix(ctx.program, { + ...baseParamsPool, + tokenProgramA: tokenExtensionCtx.tokenMintWithProgramA.tokenProgram, + tokenProgramB: tokenExtensionCtx.tokenMintWithProgramB.tokenProgram, + tokenBadgeA, + tokenBadgeB, + }); + const initialTickArrayStartTick = TickUtil.getStartTickIndex( + initialTick, + tickSpacing, + ); + const initialTickArrayPda = PDAUtil.getTickArray( + ctx.program.programId, + whirlpoolPda.publicKey, + initialTickArrayStartTick, + ); + + const txBuilder = new TransactionBuilder( + ctx.provider.connection, + ctx.provider.wallet, + ctx.txBuilderOpts, + ); + txBuilder.addInstruction(initPoolIx); + txBuilder.addInstruction( + initTickArrayIx(ctx.program, { + startTick: initialTickArrayStartTick, + tickArrayPda: initialTickArrayPda, + whirlpool: whirlpoolPda.publicKey, + funder: wallet.publicKey, + }), + ); + + let tickLowerIndex, tickUpperIndex; + if (isCorrectMintOrder) { + tickLowerIndex = initialTick; + tickUpperIndex = PriceMath.priceToTickIndex( + maxPrice, + mintAAccount.decimals, + mintBAccount.decimals, ); } else { - txBuilder.addInstruction( - initTickArrayIx(ctx.program, { - startTick: tickArrayLowerStartIndex, - tickArrayPda: tickArrayLowerPda, - whirlpool: whirlpoolPda.publicKey, - funder: wallet.publicKey, - }), + tickLowerIndex = PriceMath.priceToTickIndex( + maxPrice, + mintAAccount.decimals, + mintBAccount.decimals, ); + tickUpperIndex = initialTick; } - } + const tickLowerInitializableIndex = TickUtil.getInitializableTickIndex( + tickLowerIndex, + tickSpacing, + ); + const tickUpperInitializableIndex = TickUtil.getInitializableTickIndex( + tickUpperIndex, + tickSpacing, + ); + if ( + !TickUtil.checkTickInBounds(tickLowerInitializableIndex) || + !TickUtil.checkTickInBounds(tickUpperInitializableIndex) + ) { + throw Error("Prices out of bounds"); + } + depositTokenAmount = isCorrectMintOrder + ? depositTokenAmount * Math.pow(10, mintAAccount.decimals) + : depositTokenAmount * Math.pow(10, mintBAccount.decimals); + const increasLiquidityQuoteParam: IncreaseLiquidityQuoteParam = { + inputTokenAmount: new BN(depositTokenAmount), + inputTokenMint: depositTokenMint, + tokenMintA: mintA, + tokenMintB: mintB, + tickCurrentIndex: initialTick, + sqrtPrice: initSqrtPrice, + tickLowerIndex: tickLowerInitializableIndex, + tickUpperIndex: tickUpperInitializableIndex, + tokenExtensionCtx: tokenExtensionCtx, + slippageTolerance: Percentage.fromFraction(0, 100), + }; + const liquidityInput = increaseLiquidityQuoteByInputTokenWithParams( + increasLiquidityQuoteParam, + ); + const { liquidityAmount: liquidity, tokenMaxA, tokenMaxB } = liquidityInput; - const baseParamsLiquidity = { - liquidityAmount: liquidity, - tokenMaxA, - tokenMaxB, - whirlpool: whirlpoolPda.publicKey, - positionAuthority: wallet.publicKey, - position: positionPda.publicKey, - positionTokenAccount: positionTokenAccountAddress, - tokenOwnerAccountA, - tokenOwnerAccountB, - tokenVaultA: tokenVaultAKeypair.publicKey, - tokenVaultB: tokenVaultBKeypair.publicKey, - tickArrayLower: tickArrayLowerPda.publicKey, - tickArrayUpper: tickArrayUpperPda.publicKey, - }; + const positionMintKeypair = Keypair.generate(); + const positionMintPubkey = positionMintKeypair.publicKey; + const positionPda = PDAUtil.getPosition( + ORCA_WHIRLPOOL_PROGRAM_ID, + positionMintPubkey, + ); + const positionTokenAccountAddress = getAssociatedTokenAddressSync( + positionMintPubkey, + wallet.publicKey, + ctx.accountResolverOpts.allowPDAOwnerAddress, + TOKEN_2022_PROGRAM_ID, + ); + const params = { + funder: wallet.publicKey, + owner: wallet.publicKey, + positionPda, + positionTokenAccount: positionTokenAccountAddress, + whirlpool: whirlpoolPda.publicKey, + tickLowerIndex: tickLowerInitializableIndex, + tickUpperIndex: tickUpperInitializableIndex, + }; + const positionIx = openPositionWithTokenExtensionsIx(ctx.program, { + ...params, + positionMint: positionMintPubkey, + withTokenMetadataExtension: true, + }); - const liquidityIx = !TokenExtensionUtil.isV2IxRequiredPool(tokenExtensionCtx) - ? increaseLiquidityIx(ctx.program, baseParamsLiquidity) - : increaseLiquidityV2Ix(ctx.program, { - ...baseParamsLiquidity, - tokenMintA: mintA, - tokenMintB: mintB, - tokenProgramA: tokenExtensionCtx.tokenMintWithProgramA.tokenProgram, - tokenProgramB: tokenExtensionCtx.tokenMintWithProgramB.tokenProgram, - }); - txBuilder.addInstruction(liquidityIx); + txBuilder.addInstruction(positionIx); + txBuilder.addSigner(positionMintKeypair); - const txPayload = await txBuilder.build(); - const instructions = TransactionMessage.decompile( - (txPayload.transaction as VersionedTransaction).message, - ).instructions; + const [ataA, ataB] = await resolveOrCreateATAs( + ctx.connection, + wallet.publicKey, + [ + { tokenMint: mintA, wrappedSolAmountIn: tokenMaxA }, + { tokenMint: mintB, wrappedSolAmountIn: tokenMaxB }, + ], + () => ctx.fetcher.getAccountRentExempt(), + wallet.publicKey, + undefined, + ctx.accountResolverOpts.allowPDAOwnerAddress, + "ata", + ); + const { address: tokenOwnerAccountA, ...tokenOwnerAccountAIx } = ataA; + const { address: tokenOwnerAccountB, ...tokenOwnerAccountBIx } = ataB; - try { + txBuilder.addInstruction(tokenOwnerAccountAIx); + txBuilder.addInstruction(tokenOwnerAccountBIx); + + const tickArrayLowerStartIndex = TickUtil.getStartTickIndex( + tickLowerInitializableIndex, + tickSpacing, + ); + const tickArrayUpperStartIndex = TickUtil.getStartTickIndex( + tickUpperInitializableIndex, + tickSpacing, + ); + const tickArrayLowerPda = PDAUtil.getTickArray( + ctx.program.programId, + whirlpoolPda.publicKey, + tickArrayLowerStartIndex, + ); + const tickArrayUpperPda = PDAUtil.getTickArray( + ctx.program.programId, + whirlpoolPda.publicKey, + tickArrayUpperStartIndex, + ); + if (tickArrayUpperStartIndex !== tickArrayLowerStartIndex) { + if (isCorrectMintOrder) { + txBuilder.addInstruction( + initTickArrayIx(ctx.program, { + startTick: tickArrayUpperStartIndex, + tickArrayPda: tickArrayUpperPda, + whirlpool: whirlpoolPda.publicKey, + funder: wallet.publicKey, + }), + ); + } else { + txBuilder.addInstruction( + initTickArrayIx(ctx.program, { + startTick: tickArrayLowerStartIndex, + tickArrayPda: tickArrayLowerPda, + whirlpool: whirlpoolPda.publicKey, + funder: wallet.publicKey, + }), + ); + } + } + + const baseParamsLiquidity = { + liquidityAmount: liquidity, + tokenMaxA, + tokenMaxB, + whirlpool: whirlpoolPda.publicKey, + positionAuthority: wallet.publicKey, + position: positionPda.publicKey, + positionTokenAccount: positionTokenAccountAddress, + tokenOwnerAccountA, + tokenOwnerAccountB, + tokenVaultA: tokenVaultAKeypair.publicKey, + tokenVaultB: tokenVaultBKeypair.publicKey, + tickArrayLower: tickArrayLowerPda.publicKey, + tickArrayUpper: tickArrayUpperPda.publicKey, + }; + + const liquidityIx = !TokenExtensionUtil.isV2IxRequiredPool(tokenExtensionCtx) + ? increaseLiquidityIx(ctx.program, baseParamsLiquidity) + : increaseLiquidityV2Ix(ctx.program, { + ...baseParamsLiquidity, + tokenMintA: mintA, + tokenMintB: mintB, + tokenProgramA: tokenExtensionCtx.tokenMintWithProgramA.tokenProgram, + tokenProgramB: tokenExtensionCtx.tokenMintWithProgramB.tokenProgram, + }); + txBuilder.addInstruction(liquidityIx); + + const txPayload = await txBuilder.build(); + const instructions = TransactionMessage.decompile( + (txPayload.transaction as VersionedTransaction).message, + ).instructions; + + const txId = await sendTx( agent, instructions, diff --git a/src/tools/orca_fetch_positions.ts b/src/tools/orca_fetch_positions.ts new file mode 100644 index 0000000..647d052 --- /dev/null +++ b/src/tools/orca_fetch_positions.ts @@ -0,0 +1,104 @@ +import { SolanaAgentKit } from "../agent"; +import { Wallet } from "@coral-xyz/anchor"; +import { + ORCA_WHIRLPOOL_PROGRAM_ID, + WhirlpoolContext, + buildWhirlpoolClient, + getAllPositionAccountsByOwner, + PriceMath, +} from "@orca-so/whirlpools-sdk"; + +interface PositionInfo { + whirlpoolAddress: string; + positionInRange: boolean; + distanceFromCenterBps: number; +} + +type PositionDataMap = { + [positionMintAddress: string]: PositionInfo; +}; + +/** + * # Fetches Liquidity Position Data in Orca Whirlpools + * + * Fetches data for all liquidity positions owned by the provided wallet, including: + * - Whirlpool address. + * - Whether the position is in range. + * - Distance from the center price to the current price in basis points. + * + * ## Parameters + * - `agent`: The `SolanaAgentKit` instance representing the wallet and connection. + * + * ## Returns + * A JSON string with an object mapping position mint addresses to position details: + * ```json + * { + * "positionMintAddress1": { + * "whirlpoolAddress": "whirlpoolAddress1", + * "positionInRange": true, + * "distanceFromCenterBps": 250 + * } + * } + * ``` + * + * ## Throws + * - If positions cannot be fetched or processed. + * - If the position mint address is invalid. + * + * @param agent - The `SolanaAgentKit` instance. + * @returns A JSON string with position data. + */ +export async function orcaFetchPositions( + agent: SolanaAgentKit, +): Promise { + try { + const wallet = new Wallet(agent.wallet); + const ctx = WhirlpoolContext.from( + agent.connection, + wallet, + ORCA_WHIRLPOOL_PROGRAM_ID, + ); + const client = buildWhirlpoolClient(ctx) + + const positions = await getAllPositionAccountsByOwner({ + ctx, + owner: agent.wallet.publicKey + }) + const positionDatas = [ + ...positions.positions.entries(), + ...positions.positionsWithTokenExtensions.entries() + ]; + const result: PositionDataMap = {}; + for (const [_, positionData] of positionDatas) { + const positionMintAddress = positionData.positionMint; + const whirlpoolAddress = positionData.whirlpool; + const whirlpool = await client.getPool(whirlpoolAddress); + const whirlpoolData = whirlpool.getData(); + const sqrtPrice = whirlpoolData.sqrtPrice; + const currentTick = whirlpoolData.tickCurrentIndex; + const mintA = whirlpool.getTokenAInfo(); + const mintB = whirlpool.getTokenBInfo(); + const currentPrice = PriceMath.sqrtPriceX64ToPrice(sqrtPrice, mintA.decimals, mintB.decimals); + const lowerTick = positionData.tickLowerIndex + const upperTick = positionData.tickUpperIndex + const lowerPrice = PriceMath.tickIndexToPrice(lowerTick, mintA.decimals, mintB.decimals); + const upperPrice = PriceMath.tickIndexToPrice(upperTick, mintA.decimals, mintB.decimals); + const centerPosition = (lowerPrice.add(upperPrice)).div(2); + + const positionInRange = (currentTick > lowerTick && currentTick < upperTick) ? true : false; + const distanceFromCenterBps = Math.ceil( + currentPrice.sub(centerPosition).abs().div(centerPosition).mul(10000).toNumber() + ); + + result[positionMintAddress.toString()] = { + whirlpoolAddress: whirlpoolAddress.toString(), + positionInRange, + distanceFromCenterBps, + }; + } + return JSON.stringify(result); + } catch (error) { + console.log(error) + throw new Error(`${error}`); + } +} \ No newline at end of file diff --git a/src/tools/orca_open_centered_position_with_liquidity.ts b/src/tools/orca_open_centered_position_with_liquidity.ts index de89a28..0fe3d0d 100644 --- a/src/tools/orca_open_centered_position_with_liquidity.ts +++ b/src/tools/orca_open_centered_position_with_liquidity.ts @@ -14,6 +14,7 @@ import { import { sendTx } from "../utils/send_tx"; import { Percentage } from "@orca-so/common-sdk"; +import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; /** * # Opens a Centered Liquidity Position in an Orca Whirlpool @@ -71,18 +72,19 @@ export async function orcaOpenCenteredPositionWithLiquidity( const client = buildWhirlpoolClient(ctx) const whirlpool = await client.getPool(whirlpoolAddress); + const whirlpoolData = whirlpool.getData(); const mintInfoA = whirlpool.getTokenAInfo() const mintInfoB = whirlpool.getTokenBInfo() const price = PriceMath.sqrtPriceX64ToPrice( - whirlpool.getData().sqrtPrice, + whirlpoolData.sqrtPrice, mintInfoA.decimals, mintInfoB.decimals ) const lowerPrice = price.mul(1 - priceOffsetBps / 10000) const upperPrice = price.mul(1 + priceOffsetBps / 10000) - const lowerTick = PriceMath.priceToTickIndex(lowerPrice, mintInfoA.decimals, mintInfoB.decimals) - const upperTick = PriceMath.priceToTickIndex(upperPrice, mintInfoA.decimals, mintInfoB.decimals) + const lowerTick = PriceMath.priceToInitializableTickIndex(lowerPrice, mintInfoA.decimals, mintInfoB.decimals, whirlpoolData.tickSpacing) + const upperTick = PriceMath.priceToInitializableTickIndex(upperPrice, mintInfoA.decimals, mintInfoB.decimals, whirlpoolData.tickSpacing) const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([lowerTick, upperTick]) let instructions: TransactionInstruction[] = [] @@ -112,7 +114,11 @@ export async function orcaOpenCenteredPositionWithLiquidity( const { positionMint, tx: txBuilder } = await whirlpool.openPositionWithMetadata( lowerTick, upperTick, - increaseLiquiditQuote + increaseLiquiditQuote, + undefined, + undefined, + undefined, + TOKEN_2022_PROGRAM_ID ) const txPayload = await txBuilder.build(); diff --git a/src/tools/orca_open_single_sided_position.ts b/src/tools/orca_open_single_sided_position.ts index e69de29..1829865 100644 --- a/src/tools/orca_open_single_sided_position.ts +++ b/src/tools/orca_open_single_sided_position.ts @@ -0,0 +1,169 @@ +import { Keypair, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { SolanaAgentKit } from "../agent"; +import { Wallet } from "@coral-xyz/anchor"; +import { Decimal } from "decimal.js"; +import { + ORCA_WHIRLPOOL_PROGRAM_ID, + WhirlpoolContext, + PriceMath, + buildWhirlpoolClient, + increaseLiquidityQuoteByInputToken, + TokenExtensionContextForPool, + NO_TOKEN_EXTENSION_CONTEXT, +} from "@orca-so/whirlpools-sdk"; +import { sendTx } from "../utils/send_tx"; +import { Percentage } from "@orca-so/common-sdk"; +import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; + +/** + * # Opens a Single-Sided Liquidity Position in an Orca Whirlpool + * + * This function opens a single-sided liquidity position in a specified Orca Whirlpool. The user specifies + * a basis point (bps) offset from the current price for the lower bound and a width (bps) for the range width. + * The required amount of the other token is calculated automatically. + * + * ## Parameters + * - `agent`: The `SolanaAgentKit` instance representing the wallet and connection details. + * - `whirlpoolAddress`: The address of the Orca Whirlpool where the position will be opened. + * - `distanceFromCurrentPriceBps`: The basis point offset from the current price for the lower bound. + * - `widthBps`: The width of the range as a percentage increment from the lower bound. + * - `inputTokenMint`: The mint address of the token being deposited (e.g., USDC or another token). + * - `inputAmount`: The amount of the input token to deposit, specified as a `Decimal` value. + * + * ## Returns + * A `Promise` that resolves to the transaction ID (`string`) of the transaction that opens the position. + * + * ## Notes + * - The `distanceFromCurrentPriceBps` specifies the starting point of the range. + * - The `widthBps` determines the range size from the lower bound. + * - The specified `inputTokenMint` determines which token is deposited directly. + * + * @param agent - The `SolanaAgentKit` instance representing the wallet and connection. + * @param whirlpoolAddress - The address of the Orca Whirlpool. + * @param distanceFromCurrentPriceBps - The basis point offset from the current price for the lower bound. + * @param widthBps - The width of the range as a percentage increment from the lower bound. + * @param inputTokenMint - The mint address of the token to deposit. + * @param inputAmount - The amount of the input token to deposit. + * @returns A promise resolving to the transaction ID (`string`). + */ +export async function orcaOpenSingleSidedPosition( + agent: SolanaAgentKit, + whirlpoolAddress: PublicKey, + distanceFromCurrentPriceBps: number, + widthBps: number, + inputTokenMint: PublicKey, + inputAmount: Decimal +): Promise { + try { + const wallet = new Wallet(agent.wallet); + const ctx = WhirlpoolContext.from( + agent.connection, + wallet, + ORCA_WHIRLPOOL_PROGRAM_ID, + ); + // ctx.accountResolverOpts.createWrappedSolAccountMethod = "ata"; + const client = buildWhirlpoolClient(ctx); + + const whirlpool = await client.getPool(whirlpoolAddress); + const whirlpoolData = whirlpool.getData(); + const mintInfoA = whirlpool.getTokenAInfo(); + const mintInfoB = whirlpool.getTokenBInfo(); + const price = PriceMath.sqrtPriceX64ToPrice( + whirlpoolData.sqrtPrice, + mintInfoA.decimals, + mintInfoB.decimals + ); + + const isTokenA = inputTokenMint.equals(mintInfoA.mint); + let lowerBoundPrice; + let upperBoundPrice; + let lowerTick; + let upperTick; + if (isTokenA) { + lowerBoundPrice = price.mul(1 + distanceFromCurrentPriceBps / 10000); + upperBoundPrice = lowerBoundPrice.mul(1 + widthBps / 10000); + upperTick = PriceMath.priceToInitializableTickIndex( + upperBoundPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing + ); + lowerTick = PriceMath.priceToInitializableTickIndex( + lowerBoundPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing + ); + } else { + lowerBoundPrice = price.mul(1 - distanceFromCurrentPriceBps / 10000); + upperBoundPrice = lowerBoundPrice.mul(1 - widthBps / 10000); + lowerTick = PriceMath.priceToInitializableTickIndex( + upperBoundPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing + ); + upperTick = PriceMath.priceToInitializableTickIndex( + lowerBoundPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing + ); + } + + const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([lowerTick, upperTick]); + let txIds: string = ''; + if (txBuilderTickArrays !== null) { + const txPayloadTickArrays = await txBuilderTickArrays.build(); + const txPayloadTickArraysDecompiled = TransactionMessage.decompile((txPayloadTickArrays.transaction as VersionedTransaction).message); + const instructions = txPayloadTickArraysDecompiled.instructions; + const signers = txPayloadTickArrays.signers as Keypair[]; + + const tickArrayTxId = await sendTx(agent, instructions, signers); + txIds += tickArrayTxId + ','; + } + + const tokenExtensionCtx: TokenExtensionContextForPool = { + ...NO_TOKEN_EXTENSION_CONTEXT, + tokenMintWithProgramA: mintInfoA, + tokenMintWithProgramB: mintInfoB, + }; + const increaseLiquiditQuote = increaseLiquidityQuoteByInputToken( + inputTokenMint, + inputAmount, + lowerTick, + upperTick, + Percentage.fromFraction(1, 100), + whirlpool, + tokenExtensionCtx + ); + const { positionMint, tx: txBuilder } = await whirlpool.openPositionWithMetadata( + lowerTick, + upperTick, + increaseLiquiditQuote, + undefined, + undefined, + undefined, + TOKEN_2022_PROGRAM_ID + ); + + const txPayload = await txBuilder.build(); + const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + const instructions = txPayloadDecompiled.instructions; + const signers = txPayload.signers as Keypair[]; + for (const signer of signers) { + console.log(signer.publicKey.toBase58()); + } + + const positionTxId = await sendTx(agent, instructions, signers); + txIds += positionTxId; + + return JSON.stringify({ + transactionIds: txIds, + positionMint: positionMint.toString(), + }); + } catch (error) { + console.log(error); + throw new Error(`${error}`); + } +} diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index ad42204..d897b9a 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -54,7 +54,6 @@ export async function getComputeBudgetInstructions(agent: SolanaAgentKit, instru computeBudgetPriorityFeeInstructions }; } catch (error) { - console.error("Error getting compute budget instructions fees:", error); throw error; } } @@ -82,7 +81,7 @@ export async function sendTx( instructions: allInstructions, }).compileToV0Message(); const transaction = new VersionedTransaction(messageV0); - transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]); + transaction.sign([agent.wallet, ...(otherKeypairs ?? [])] as Signer[]); const timeoutMs = 90000; const startTime = Date.now(); @@ -94,7 +93,7 @@ export async function sendTx( transaction, { maxRetries: 0, - skipPreflight: true, + skipPreflight: false, }); const statuses = await agent.connection.getSignatureStatuses([signature]); @@ -114,11 +113,9 @@ export async function sendTx( } throw new Error("Transaction timeout"); } catch (error) { - console.log("Error sending transaction:", error); throw error; } } catch (error) { - console.log("Error sending transaction:", error); throw error; } } diff --git a/test/index.ts b/test/index.ts index 1a64ec9..6fb00f4 100644 --- a/test/index.ts +++ b/test/index.ts @@ -37,7 +37,7 @@ async function initializeAgent() { try { const llm = new ChatOpenAI({ modelName: "gpt-4o-mini", - // temperature: 0.7, + temperature: 0.3, }); let walletDataStr: string | null = null; From 3f9828d67216e6e5b9e9ada1005d74fbdf843692 Mon Sep 17 00:00:00 2001 From: calintje Date: Mon, 30 Dec 2024 05:05:02 +0100 Subject: [PATCH 6/8] Update lockfile --- pnpm-lock.yaml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 58e21d9..660a954 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -21,13 +21,8 @@ importers: specifier: ^0.1.2 version: 0.1.2(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) '@langchain/langgraph': -<<<<<<< HEAD - specifier: ^0.2.27 - version: 0.2.36(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) -======= specifier: ^0.2.34 - version: 0.2.34(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) ->>>>>>> main + version: 0.2.36(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) '@langchain/openai': specifier: ^0.3.13 version: 0.3.16(@langchain/core@0.3.26(openai@4.77.0(zod@3.24.1))) @@ -2730,7 +2725,7 @@ snapshots: '@solana/errors@2.0.0-rc.1(typescript@4.9.5)': dependencies: - chalk: 5.4.0 + chalk: 5.4.1 commander: 12.1.0 typescript: 4.9.5 From 43953f262f9151f99441f97583a18cc8832224da Mon Sep 17 00:00:00 2001 From: calintje Date: Mon, 30 Dec 2024 05:23:16 +0100 Subject: [PATCH 7/8] Update description --- src/langchain/index.ts | 2 +- src/tools/orca_open_centered_position_with_liquidity.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/langchain/index.ts b/src/langchain/index.ts index 4abf169..92f3a58 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -979,7 +979,7 @@ export class SolanaOrcaOpenCenteredPosition extends Tool { Inputs (JSON string): - whirlpoolAddress: string, address of the Orca Whirlpool (required). - - priceOffsetBps: number, bps offset (one side) from the center price, e.g., 500 for 5% (required). + - priceOffsetBps: number, bps offset (one side) from the current pool price, e.g., 500 for 5% (required). - inputTokenMint: string, mint address of the deposit token (required). - inputAmount: number, amount of the deposit token, e.g., 100.0 (required).`; diff --git a/src/tools/orca_open_centered_position_with_liquidity.ts b/src/tools/orca_open_centered_position_with_liquidity.ts index c061c8e..2213c12 100644 --- a/src/tools/orca_open_centered_position_with_liquidity.ts +++ b/src/tools/orca_open_centered_position_with_liquidity.ts @@ -20,15 +20,15 @@ import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; * # Opens a Centered Liquidity Position in an Orca Whirlpool * * This function opens a centered liquidity position in a specified Orca Whirlpool. The user defines - * a basis point (bps) offset from the center price to set the lower and upper bounds of the position. + * a basis point (bps) offset from the cuurent price of the pool to set the lower and upper bounds of the position. * The user also specifies the token mint and the amount to deposit. The required amount of the other token * is calculated automatically. * * ## Parameters * - `agent`: The `SolanaAgentKit` instance representing the wallet and connection details. * - `whirlpoolAddress`: The address of the Orca Whirlpool where the position will be opened. - * - `priceOffsetBps`: The basis point (bps) offset (on one side) from the center price. For example, - * 500 bps (5%) creates a range from 95% to 105% of the center price. + * - `priceOffsetBps`: The basis point (bps) offset (on one side) from the current price fo the pool. For example, + * 500 bps (5%) creates a range from 95% to 105% of the current pool price. * - `inputTokenMint`: The mint address of the token being deposited (e.g., USDC or another token). * - `inputAmount`: The amount of the input token to deposit, specified as a `Decimal` value. * @@ -50,7 +50,7 @@ import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; * * @param agent - The `SolanaAgentKit` instance representing the wallet and connection. * @param whirlpoolAddress - The address of the Orca Whirlpool. - * @param priceOffsetBps - The basis point offset (one side) from the center price. + * @param priceOffsetBps - The basis point offset (one side) from the current pool price. * @param inputTokenMint - The mint address of the token to deposit. * @param inputAmount - The amount of the input token to deposit. * @returns A promise resolving to the transaction ID (`string`). From 4c0124da9767dd1a91d06618d5038dc663bc9354 Mon Sep 17 00:00:00 2001 From: aryan Date: Mon, 30 Dec 2024 14:37:31 +0530 Subject: [PATCH 8/8] chore: lint --- .../tg-bot-starter/src/app/api/bot/route.ts | 57 +++--- src/agent/index.ts | 24 +-- src/langchain/index.ts | 38 ++-- src/tools/index.ts | 2 +- src/tools/orca_close_position.ts | 30 ++-- src/tools/orca_create_clmm.ts | 49 ++--- ...orca_create_single_sided_liquidity_pool.ts | 43 +++-- src/tools/orca_fetch_positions.ts | 48 +++-- ...a_open_centered_position_with_liquidity.ts | 88 +++++---- src/tools/orca_open_single_sided_position.ts | 59 +++--- src/utils/send_tx.ts | 169 +++++++++--------- 11 files changed, 345 insertions(+), 262 deletions(-) diff --git a/examples/tg-bot-starter/src/app/api/bot/route.ts b/examples/tg-bot-starter/src/app/api/bot/route.ts index ac63719..2dcdf97 100644 --- a/examples/tg-bot-starter/src/app/api/bot/route.ts +++ b/examples/tg-bot-starter/src/app/api/bot/route.ts @@ -1,8 +1,8 @@ -export const dynamic = 'force-dynamic'; -export const fetchCache = 'force-no-store'; +export const dynamic = "force-dynamic"; +export const fetchCache = "force-no-store"; export const maxDuration = 300; -import { Bot, webhookCallback } from 'grammy'; +import { Bot, webhookCallback } from "grammy"; import { SolanaAgentKit, createSolanaTools } from "solana-agent-kit"; import { ChatOpenAI } from "@langchain/openai"; import { MemorySaver } from "@langchain/langgraph"; @@ -10,10 +10,11 @@ import { createReactAgent } from "@langchain/langgraph/prebuilt"; import { HumanMessage } from "@langchain/core/messages"; const token = process.env.TELEGRAM_BOT_TOKEN; -if (!token) throw new Error('TELEGRAM_BOT_TOKEN environment variable not found.'); +if (!token) { + throw new Error("TELEGRAM_BOT_TOKEN environment variable not found."); +} const bot = new Bot(token); - async function initializeAgent(userId: string) { try { const llm = new ChatOpenAI({ @@ -24,7 +25,7 @@ async function initializeAgent(userId: string) { const solanaKit = new SolanaAgentKit( process.env.SOLANA_PRIVATE_KEY!, process.env.RPC_URL, - process.env.OPENAI_API_KEY! + process.env.OPENAI_API_KEY!, ); const tools = createSolanaTools(solanaKit); @@ -51,24 +52,40 @@ async function initializeAgent(userId: string) { } } // Telegram bot handler -bot.on('message:text', async (ctx:any) => { +bot.on("message:text", async (ctx: any) => { const userId = ctx.from?.id.toString(); - if (!userId) return; - const {agent, config} = await initializeAgent(userId); - const stream = await agent.stream({ messages: [new HumanMessage(ctx.message.text)] }, config); - const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 20000)); + if (!userId) { + return; + } + const { agent, config } = await initializeAgent(userId); + const stream = await agent.stream( + { messages: [new HumanMessage(ctx.message.text)] }, + config, + ); + const timeoutPromise = new Promise((_, reject) => + setTimeout(() => reject(new Error("Timeout")), 20000), + ); try { - for await (const chunk of await Promise.race([stream, timeoutPromise]) as AsyncIterable<{ agent?: any; tools?: any }>) { + for await (const chunk of (await Promise.race([ + stream, + timeoutPromise, + ])) as AsyncIterable<{ agent?: any; tools?: any }>) { if ("agent" in chunk) { - if (chunk.agent.messages[0].content) await ctx.reply(String(chunk.agent.messages[0].content)); - } + if (chunk.agent.messages[0].content) { + await ctx.reply(String(chunk.agent.messages[0].content)); + } + } } } catch (error: any) { - if (error.message === 'Timeout') { - await ctx.reply("I'm sorry, the operation took too long and timed out. Please try again."); + if (error.message === "Timeout") { + await ctx.reply( + "I'm sorry, the operation took too long and timed out. Please try again.", + ); } else { console.error("Error processing stream:", error); - await ctx.reply("I'm sorry, an error occurred while processing your request."); + await ctx.reply( + "I'm sorry, an error occurred while processing your request.", + ); } } }); @@ -77,10 +94,10 @@ bot.on('message:text', async (ctx:any) => { export const POST = async (req: Request) => { // Mark the function as a background function for Vercel const headers = new Headers(); - headers.set('x-vercel-background', 'true'); + headers.set("x-vercel-background", "true"); - const handler = webhookCallback(bot, 'std/http'); // Use the correct callback + const handler = webhookCallback(bot, "std/http"); // Use the correct callback // Handle the incoming webhook request return handler(req); -}; \ No newline at end of file +}; diff --git a/src/agent/index.ts b/src/agent/index.ts index f301523..461baf5 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -215,13 +215,8 @@ export class SolanaAgentKit { ); } - async orcaClosePosition( - positionMintAddress: PublicKey, - ) { - return orcaClosePosition( - this, - positionMintAddress, - ); + async orcaClosePosition(positionMintAddress: PublicKey) { + return orcaClosePosition(this, positionMintAddress); } async orcaCreateCLMM( @@ -230,13 +225,7 @@ export class SolanaAgentKit { initialPrice: Decimal, feeTier: keyof typeof FEE_TIERS, ) { - return orcaCreateCLMM( - this, - mintDeploy, - mintPair, - initialPrice, - feeTier, - ); + return orcaCreateCLMM(this, mintDeploy, mintPair, initialPrice, feeTier); } async orcaCreateSingleSidedLiquidityPool( @@ -258,11 +247,8 @@ export class SolanaAgentKit { ); } - async orcaFetchPositions( - ) { - return orcaFetchPositions( - this, - ); + async orcaFetchPositions() { + return orcaFetchPositions(this); } async orcaOpenCenteredPositionWithLiquidity( diff --git a/src/langchain/index.ts b/src/langchain/index.ts index 92f3a58..e17d25a 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -798,11 +798,11 @@ export class SolanaCompressedAirdropTool extends Tool { export class SolanaClosePostition extends Tool { name = "orca_close_position"; - description = `Closes an existing liquidity position in an Orca Whirlpool. This function fetches the position + description = `Closes an existing liquidity position in an Orca Whirlpool. This function fetches the position details using the provided mint address and closes the position with a 1% slippage. Inputs (JSON string): - - positionMintAddress: string, the address of the position mint that represents the liquidity position.` + - positionMintAddress: string, the address of the position mint that represents the liquidity position.`; constructor(private solanaKit: SolanaAgentKit) { super(); @@ -811,12 +811,12 @@ export class SolanaClosePostition extends Tool { async _call(input: string): Promise { try { const inputFormat = JSON.parse(input); - const positionMintAddress = new PublicKey(inputFormat.positionMintAddress); - - const txId = await this.solanaKit.orcaClosePosition( - positionMintAddress, + const positionMintAddress = new PublicKey( + inputFormat.positionMintAddress, ); + const txId = await this.solanaKit.orcaClosePosition(positionMintAddress); + return JSON.stringify({ status: "success", message: "Liquidity position closed successfully.", @@ -871,7 +871,8 @@ export class SolanaOrcaCreateCLMM extends Tool { return JSON.stringify({ status: "success", - message: "CLMM pool created successfully. Note: No liquidity was added.", + message: + "CLMM pool created successfully. Note: No liquidity was added.", transaction: txId, }); } catch (error: any) { @@ -884,7 +885,6 @@ export class SolanaOrcaCreateCLMM extends Tool { } } - export class SolanaOrcaCreateSingleSideLiquidityPool extends Tool { name = "orca_create_single_sided_liquidity_pool"; description = `Create a single-sided liquidity pool on Orca, the most efficient and capital-optimized CLMM platform on Solana. @@ -955,8 +955,7 @@ export class SolanaOrcaFetchPositions extends Tool { async _call(): Promise { try { - - const txId = await this.solanaKit.orcaFetchPositions() + const txId = await this.solanaKit.orcaFetchPositions(); return JSON.stringify({ status: "success", @@ -995,8 +994,10 @@ export class SolanaOrcaOpenCenteredPosition extends Tool { const inputTokenMint = new PublicKey(inputFormat.inputTokenMint); const inputAmount = new Decimal(inputFormat.inputAmount); - if (priceOffsetBps < 0 ) { - throw new Error("Invalid distanceFromCurrentPriceBps. It must be equal or greater than 0."); + if (priceOffsetBps < 0) { + throw new Error( + "Invalid distanceFromCurrentPriceBps. It must be equal or greater than 0.", + ); } const txId = await this.solanaKit.orcaOpenCenteredPositionWithLiquidity( @@ -1040,13 +1041,16 @@ export class SolanaOrcaOpenSingleSidedPosition extends Tool { try { const inputFormat = JSON.parse(input); const whirlpoolAddress = new PublicKey(inputFormat.whirlpoolAddress); - const distanceFromCurrentPriceBps = inputFormat.distanceFromCurrentPriceBps; + const distanceFromCurrentPriceBps = + inputFormat.distanceFromCurrentPriceBps; const widthBps = inputFormat.widthBps; const inputTokenMint = new PublicKey(inputFormat.inputTokenMint); const inputAmount = new Decimal(inputFormat.inputAmount); if (distanceFromCurrentPriceBps < 0 || widthBps < 0) { - throw new Error("Invalid distanceFromCurrentPriceBps or width. It must be equal or greater than 0."); + throw new Error( + "Invalid distanceFromCurrentPriceBps or width. It must be equal or greater than 0.", + ); } const txId = await this.solanaKit.orcaOpenSingleSidedPosition( @@ -1520,9 +1524,9 @@ export class SolanaRockPaperScissorsTool extends Tool { const result = await this.solanaKit.rockPaperScissors( Number(parsedInput['"amount"']), parsedInput['"choice"'].replace(/^"|"$/g, "") as - | "rock" - | "paper" - | "scissors", + | "rock" + | "paper" + | "scissors", ); return JSON.stringify({ diff --git a/src/tools/index.ts b/src/tools/index.ts index 9bdd7c5..af70276 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -16,7 +16,7 @@ export * from "./get_token_data"; export * from "./stake_with_jup"; export * from "./fetch_price"; export * from "./send_compressed_airdrop"; -export * from "./orca_close_position" +export * from "./orca_close_position"; export * from "./orca_create_clmm"; export * from "./orca_create_single_sided_liquidity_pool"; export * from "./orca_fetch_positions"; diff --git a/src/tools/orca_close_position.ts b/src/tools/orca_close_position.ts index 976d6fc..4475692 100644 --- a/src/tools/orca_close_position.ts +++ b/src/tools/orca_close_position.ts @@ -2,7 +2,7 @@ import { Keypair, PublicKey, TransactionMessage, - VersionedTransaction + VersionedTransaction, } from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { Wallet } from "@coral-xyz/anchor"; @@ -32,7 +32,7 @@ import { Percentage } from "@orca-so/common-sdk"; * - The function uses Orca’s 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. @@ -54,25 +54,29 @@ export async function orcaClosePosition( wallet, ORCA_WHIRLPOOL_PROGRAM_ID, ); - const client = buildWhirlpoolClient(ctx) + const client = buildWhirlpoolClient(ctx); - const positionAddress = PDAUtil.getPosition(ORCA_WHIRLPOOL_PROGRAM_ID, positionMintAddress); + 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 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 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 + const txId = await sendTx(agent, instructions, signers); + return txId; } catch (error) { throw new Error(`${error}`); } -} \ No newline at end of file +} diff --git a/src/tools/orca_create_clmm.ts b/src/tools/orca_create_clmm.ts index ff86fdc..9fcdc33 100644 --- a/src/tools/orca_create_clmm.ts +++ b/src/tools/orca_create_clmm.ts @@ -1,8 +1,8 @@ -import { +import { Keypair, PublicKey, TransactionMessage, - VersionedTransaction + VersionedTransaction, } from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { Wallet } from "@coral-xyz/anchor"; @@ -45,7 +45,7 @@ import { FEE_TIERS } from "./orca_create_single_sided_liquidity_pool"; * * @remarks * This function only initializes the CLMM pool and does not add liquidity. For adding liquidity, you can use - * a separate function after the pool is successfully created. + * a separate function after the pool is successfully created. * ``` */ export async function orcaCreateCLMM( @@ -57,12 +57,16 @@ export async function orcaCreateCLMM( ): Promise { try { let whirlpoolsConfigAddress: PublicKey; - if (agent.connection.rpcEndpoint.includes('mainnet')) { - whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); - } else if (agent.connection.rpcEndpoint.includes('devnet')) { - whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); + if (agent.connection.rpcEndpoint.includes("mainnet")) { + whirlpoolsConfigAddress = new PublicKey( + "2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ", + ); + } else if (agent.connection.rpcEndpoint.includes("devnet")) { + whirlpoolsConfigAddress = new PublicKey( + "FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR", + ); } else { - throw new Error('Unsupported network'); + throw new Error("Unsupported network"); } const wallet = new Wallet(agent.wallet); const ctx = WhirlpoolContext.from( @@ -71,14 +75,12 @@ export async function orcaCreateCLMM( ORCA_WHIRLPOOL_PROGRAM_ID, ); const fetcher = ctx.fetcher; - const client = buildWhirlpoolClient(ctx) + const client = buildWhirlpoolClient(ctx); - const correctTokenOrder = PoolUtil.orderMints( - mintDeploy, - mintPair, - ).map((addr) => addr.toString()); - const isCorrectMintOrder = - correctTokenOrder[0] === mintDeploy.toString(); + const correctTokenOrder = PoolUtil.orderMints(mintDeploy, mintPair).map( + (addr) => addr.toString(), + ); + const isCorrectMintOrder = correctTokenOrder[0] === mintDeploy.toString(); let mintA; let mintB; if (!isCorrectMintOrder) { @@ -94,7 +96,12 @@ export async function orcaCreateCLMM( } const tickSpacing = FEE_TIERS[feeTier]; - const initialTick = PriceMath.priceToInitializableTickIndex(initialPrice, mintAAccount.decimals, mintBAccount.decimals, tickSpacing) + const initialTick = PriceMath.priceToInitializableTickIndex( + initialPrice, + mintAAccount.decimals, + mintBAccount.decimals, + tickSpacing, + ); const { poolKey, tx: txBuilder } = await client.createPool( whirlpoolsConfigAddress, mintA, @@ -102,16 +109,18 @@ export async function orcaCreateCLMM( tickSpacing, initialTick, wallet.publicKey, - ) + ); const txPayload = await txBuilder.build(); - const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + const txPayloadDecompiled = TransactionMessage.decompile( + (txPayload.transaction as VersionedTransaction).message, + ); const instructions = txPayloadDecompiled.instructions; const txId = await sendTx( agent, instructions, - txPayload.signers as Keypair[] + txPayload.signers as Keypair[], ); return JSON.stringify({ transactionId: txId, @@ -120,4 +129,4 @@ export async function orcaCreateCLMM( } catch (error) { throw new Error(`${error}`); } -} \ No newline at end of file +} diff --git a/src/tools/orca_create_single_sided_liquidity_pool.ts b/src/tools/orca_create_single_sided_liquidity_pool.ts index 68602b9..26792b8 100644 --- a/src/tools/orca_create_single_sided_liquidity_pool.ts +++ b/src/tools/orca_create_single_sided_liquidity_pool.ts @@ -1,8 +1,8 @@ -import { - Keypair, - PublicKey, - TransactionMessage, - VersionedTransaction +import { + Keypair, + PublicKey, + TransactionMessage, + VersionedTransaction, } from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { BN, Wallet } from "@coral-xyz/anchor"; @@ -112,12 +112,16 @@ export async function orcaCreateSingleSidedLiquidityPool( ): Promise { try { let whirlpoolsConfigAddress: PublicKey; - if (agent.connection.rpcEndpoint.includes('mainnet')) { - whirlpoolsConfigAddress = new PublicKey('2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ'); - } else if (agent.connection.rpcEndpoint.includes('devnet')) { - whirlpoolsConfigAddress = new PublicKey('FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR'); + if (agent.connection.rpcEndpoint.includes("mainnet")) { + whirlpoolsConfigAddress = new PublicKey( + "2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ", + ); + } else if (agent.connection.rpcEndpoint.includes("devnet")) { + whirlpoolsConfigAddress = new PublicKey( + "FcrweFY1G9HJAHG5inkGB6pKg1HZ6x9UC2WioAfWrGkR", + ); } else { - throw new Error('Unsupported network'); + throw new Error("Unsupported network"); } const wallet = new Wallet(agent.wallet); const ctx = WhirlpoolContext.from( @@ -263,7 +267,7 @@ export async function orcaCreateSingleSidedLiquidityPool( ) { throw Error("Prices out of bounds"); } - depositTokenAmount = isCorrectMintOrder + depositTokenAmount = isCorrectMintOrder ? depositTokenAmount * Math.pow(10, mintAAccount.decimals) : depositTokenAmount * Math.pow(10, mintBAccount.decimals); const increasLiquidityQuoteParam: IncreaseLiquidityQuoteParam = { @@ -388,7 +392,9 @@ export async function orcaCreateSingleSidedLiquidityPool( tickArrayUpper: tickArrayUpperPda.publicKey, }; - const liquidityIx = !TokenExtensionUtil.isV2IxRequiredPool(tokenExtensionCtx) + const liquidityIx = !TokenExtensionUtil.isV2IxRequiredPool( + tokenExtensionCtx, + ) ? increaseLiquidityIx(ctx.program, baseParamsLiquidity) : increaseLiquidityV2Ix(ctx.program, { ...baseParamsLiquidity, @@ -404,14 +410,13 @@ export async function orcaCreateSingleSidedLiquidityPool( (txPayload.transaction as VersionedTransaction).message, ).instructions; - - const txId = await sendTx( - agent, - instructions, - [positionMintKeypair, tokenVaultAKeypair, tokenVaultBKeypair], - ); + const txId = await sendTx(agent, instructions, [ + positionMintKeypair, + tokenVaultAKeypair, + tokenVaultBKeypair, + ]); return txId; } catch (error) { throw new Error(`Failed to send transaction: ${JSON.stringify(error)}`); } -} \ No newline at end of file +} diff --git a/src/tools/orca_fetch_positions.ts b/src/tools/orca_fetch_positions.ts index dbd86c0..f90b8a0 100644 --- a/src/tools/orca_fetch_positions.ts +++ b/src/tools/orca_fetch_positions.ts @@ -58,18 +58,18 @@ export async function orcaFetchPositions( wallet, ORCA_WHIRLPOOL_PROGRAM_ID, ); - const client = buildWhirlpoolClient(ctx) + const client = buildWhirlpoolClient(ctx); const positions = await getAllPositionAccountsByOwner({ - ctx, - owner: agent.wallet.publicKey - }) + ctx, + owner: agent.wallet.publicKey, + }); const positionDatas = [ ...positions.positions.entries(), - ...positions.positionsWithTokenExtensions.entries() + ...positions.positionsWithTokenExtensions.entries(), ]; const result: PositionDataMap = {}; - for (const [_, positionData] of positionDatas) { + for (const [, positionData] of positionDatas) { const positionMintAddress = positionData.positionMint; const whirlpoolAddress = positionData.whirlpool; const whirlpool = await client.getPool(whirlpoolAddress); @@ -78,16 +78,34 @@ export async function orcaFetchPositions( const currentTick = whirlpoolData.tickCurrentIndex; const mintA = whirlpool.getTokenAInfo(); const mintB = whirlpool.getTokenBInfo(); - const currentPrice = PriceMath.sqrtPriceX64ToPrice(sqrtPrice, mintA.decimals, mintB.decimals); - const lowerTick = positionData.tickLowerIndex - const upperTick = positionData.tickUpperIndex - const lowerPrice = PriceMath.tickIndexToPrice(lowerTick, mintA.decimals, mintB.decimals); - const upperPrice = PriceMath.tickIndexToPrice(upperTick, mintA.decimals, mintB.decimals); - const centerPosition = (lowerPrice.add(upperPrice)).div(2); + const currentPrice = PriceMath.sqrtPriceX64ToPrice( + sqrtPrice, + mintA.decimals, + mintB.decimals, + ); + const lowerTick = positionData.tickLowerIndex; + const upperTick = positionData.tickUpperIndex; + const lowerPrice = PriceMath.tickIndexToPrice( + lowerTick, + mintA.decimals, + mintB.decimals, + ); + const upperPrice = PriceMath.tickIndexToPrice( + upperTick, + mintA.decimals, + mintB.decimals, + ); + const centerPosition = lowerPrice.add(upperPrice).div(2); - const positionInRange = (currentTick > lowerTick && currentTick < upperTick) ? true : false; + const positionInRange = + currentTick > lowerTick && currentTick < upperTick ? true : false; const distanceFromCenterBps = Math.ceil( - currentPrice.sub(centerPosition).abs().div(centerPosition).mul(10000).toNumber() + currentPrice + .sub(centerPosition) + .abs() + .div(centerPosition) + .mul(10000) + .toNumber(), ); result[positionMintAddress.toString()] = { @@ -100,4 +118,4 @@ export async function orcaFetchPositions( } catch (error) { throw new Error(`${error}`); } -} \ No newline at end of file +} diff --git a/src/tools/orca_open_centered_position_with_liquidity.ts b/src/tools/orca_open_centered_position_with_liquidity.ts index 2213c12..dfe8ee3 100644 --- a/src/tools/orca_open_centered_position_with_liquidity.ts +++ b/src/tools/orca_open_centered_position_with_liquidity.ts @@ -1,4 +1,10 @@ -import { Keypair, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { + Keypair, + PublicKey, + TransactionInstruction, + TransactionMessage, + VersionedTransaction, +} from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { Wallet } from "@coral-xyz/anchor"; import { Decimal } from "decimal.js"; @@ -20,7 +26,7 @@ import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; * # Opens a Centered Liquidity Position in an Orca Whirlpool * * This function opens a centered liquidity position in a specified Orca Whirlpool. The user defines - * a basis point (bps) offset from the cuurent price of the pool to set the lower and upper bounds of the position. + * a basis point (bps) offset from the cuurent price of the pool to set the lower and upper bounds of the position. * The user also specifies the token mint and the amount to deposit. The required amount of the other token * is calculated automatically. * @@ -69,29 +75,44 @@ export async function orcaOpenCenteredPositionWithLiquidity( wallet, ORCA_WHIRLPOOL_PROGRAM_ID, ); - const client = buildWhirlpoolClient(ctx) + const client = buildWhirlpoolClient(ctx); const whirlpool = await client.getPool(whirlpoolAddress); const whirlpoolData = whirlpool.getData(); - const mintInfoA = whirlpool.getTokenAInfo() - const mintInfoB = whirlpool.getTokenBInfo() + const mintInfoA = whirlpool.getTokenAInfo(); + const mintInfoB = whirlpool.getTokenBInfo(); const price = PriceMath.sqrtPriceX64ToPrice( whirlpoolData.sqrtPrice, mintInfoA.decimals, - mintInfoB.decimals - ) + mintInfoB.decimals, + ); - const lowerPrice = price.mul(1 - priceOffsetBps / 10000) - const upperPrice = price.mul(1 + priceOffsetBps / 10000) - const lowerTick = PriceMath.priceToInitializableTickIndex(lowerPrice, mintInfoA.decimals, mintInfoB.decimals, whirlpoolData.tickSpacing) - const upperTick = PriceMath.priceToInitializableTickIndex(upperPrice, mintInfoA.decimals, mintInfoB.decimals, whirlpoolData.tickSpacing) + const lowerPrice = price.mul(1 - priceOffsetBps / 10000); + const upperPrice = price.mul(1 + priceOffsetBps / 10000); + const lowerTick = PriceMath.priceToInitializableTickIndex( + lowerPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing, + ); + const upperTick = PriceMath.priceToInitializableTickIndex( + upperPrice, + mintInfoA.decimals, + mintInfoB.decimals, + whirlpoolData.tickSpacing, + ); - const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([lowerTick, upperTick]) - let instructions: TransactionInstruction[] = [] - let signers: Keypair[] = [] + const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([ + lowerTick, + upperTick, + ]); + let instructions: TransactionInstruction[] = []; + let signers: Keypair[] = []; if (txBuilderTickArrays !== null) { const txPayloadTickArrays = await txBuilderTickArrays.build(); - const txPayloadTickArraysDecompiled = TransactionMessage.decompile((txPayloadTickArrays.transaction as VersionedTransaction).message); + const txPayloadTickArraysDecompiled = TransactionMessage.decompile( + (txPayloadTickArrays.transaction as VersionedTransaction).message, + ); const instructionsTickArrays = txPayloadTickArraysDecompiled.instructions; instructions = instructions.concat(instructionsTickArrays); signers = signers.concat(txPayloadTickArrays.signers as Keypair[]); @@ -109,33 +130,32 @@ export async function orcaOpenCenteredPositionWithLiquidity( upperTick, Percentage.fromFraction(1, 100), whirlpool, - tokenExtensionCtx - ) - const { positionMint, tx: txBuilder } = await whirlpool.openPositionWithMetadata( - lowerTick, - upperTick, - increaseLiquiditQuote, - undefined, - undefined, - undefined, - TOKEN_2022_PROGRAM_ID - ) + tokenExtensionCtx, + ); + const { positionMint, tx: txBuilder } = + await whirlpool.openPositionWithMetadata( + lowerTick, + upperTick, + increaseLiquiditQuote, + undefined, + undefined, + undefined, + TOKEN_2022_PROGRAM_ID, + ); const txPayload = await txBuilder.build(); - const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + const txPayloadDecompiled = TransactionMessage.decompile( + (txPayload.transaction as VersionedTransaction).message, + ); instructions = instructions.concat(txPayloadDecompiled.instructions); signers = signers.concat(txPayload.signers as Keypair[]); - const txId = await sendTx( - agent, - instructions, - signers - ); + const txId = await sendTx(agent, instructions, signers); return JSON.stringify({ transactionId: txId, positionMint: positionMint.toString(), - }) + }); } catch (error) { throw new Error(`${error}`); } -} \ No newline at end of file +} diff --git a/src/tools/orca_open_single_sided_position.ts b/src/tools/orca_open_single_sided_position.ts index 3845f61..741069a 100644 --- a/src/tools/orca_open_single_sided_position.ts +++ b/src/tools/orca_open_single_sided_position.ts @@ -1,4 +1,9 @@ -import { Keypair, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; +import { + Keypair, + PublicKey, + TransactionMessage, + VersionedTransaction, +} from "@solana/web3.js"; import { SolanaAgentKit } from "../agent"; import { Wallet } from "@coral-xyz/anchor"; import { Decimal } from "decimal.js"; @@ -52,7 +57,7 @@ export async function orcaOpenSingleSidedPosition( distanceFromCurrentPriceBps: number, widthBps: number, inputTokenMint: PublicKey, - inputAmount: Decimal + inputAmount: Decimal, ): Promise { try { const wallet = new Wallet(agent.wallet); @@ -70,7 +75,7 @@ export async function orcaOpenSingleSidedPosition( const price = PriceMath.sqrtPriceX64ToPrice( whirlpoolData.sqrtPrice, mintInfoA.decimals, - mintInfoB.decimals + mintInfoB.decimals, ); const isTokenA = inputTokenMint.equals(mintInfoA.mint); @@ -85,13 +90,13 @@ export async function orcaOpenSingleSidedPosition( upperBoundPrice, mintInfoA.decimals, mintInfoB.decimals, - whirlpoolData.tickSpacing + whirlpoolData.tickSpacing, ); lowerTick = PriceMath.priceToInitializableTickIndex( lowerBoundPrice, mintInfoA.decimals, mintInfoB.decimals, - whirlpoolData.tickSpacing + whirlpoolData.tickSpacing, ); } else { lowerBoundPrice = price.mul(1 - distanceFromCurrentPriceBps / 10000); @@ -100,26 +105,31 @@ export async function orcaOpenSingleSidedPosition( upperBoundPrice, mintInfoA.decimals, mintInfoB.decimals, - whirlpoolData.tickSpacing + whirlpoolData.tickSpacing, ); upperTick = PriceMath.priceToInitializableTickIndex( lowerBoundPrice, mintInfoA.decimals, mintInfoB.decimals, - whirlpoolData.tickSpacing + whirlpoolData.tickSpacing, ); } - const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([lowerTick, upperTick]); - let txIds: string = ''; + const txBuilderTickArrays = await whirlpool.initTickArrayForTicks([ + lowerTick, + upperTick, + ]); + let txIds: string = ""; if (txBuilderTickArrays !== null) { const txPayloadTickArrays = await txBuilderTickArrays.build(); - const txPayloadTickArraysDecompiled = TransactionMessage.decompile((txPayloadTickArrays.transaction as VersionedTransaction).message); + const txPayloadTickArraysDecompiled = TransactionMessage.decompile( + (txPayloadTickArrays.transaction as VersionedTransaction).message, + ); const instructions = txPayloadTickArraysDecompiled.instructions; const signers = txPayloadTickArrays.signers as Keypair[]; const tickArrayTxId = await sendTx(agent, instructions, signers); - txIds += tickArrayTxId + ','; + txIds += tickArrayTxId + ","; } const tokenExtensionCtx: TokenExtensionContextForPool = { @@ -134,25 +144,28 @@ export async function orcaOpenSingleSidedPosition( upperTick, Percentage.fromFraction(1, 100), whirlpool, - tokenExtensionCtx - ); - const { positionMint, tx: txBuilder } = await whirlpool.openPositionWithMetadata( - lowerTick, - upperTick, - increaseLiquiditQuote, - undefined, - undefined, - undefined, - TOKEN_2022_PROGRAM_ID + tokenExtensionCtx, ); + const { positionMint, tx: txBuilder } = + await whirlpool.openPositionWithMetadata( + lowerTick, + upperTick, + increaseLiquiditQuote, + undefined, + undefined, + undefined, + TOKEN_2022_PROGRAM_ID, + ); const txPayload = await txBuilder.build(); - const txPayloadDecompiled = TransactionMessage.decompile((txPayload.transaction as VersionedTransaction).message); + const txPayloadDecompiled = TransactionMessage.decompile( + (txPayload.transaction as VersionedTransaction).message, + ); const instructions = txPayloadDecompiled.instructions; const signers = txPayload.signers as Keypair[]; const positionTxId = await sendTx(agent, instructions, signers); - txIds += positionTxId; + txIds += positionTxId; return JSON.stringify({ transactionIds: txIds, diff --git a/src/utils/send_tx.ts b/src/utils/send_tx.ts index d897b9a..11923b5 100644 --- a/src/utils/send_tx.ts +++ b/src/utils/send_tx.ts @@ -1,61 +1,71 @@ import { SolanaAgentKit } from "../agent"; -import { Keypair, Signer, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js"; -import { ComputeBudgetProgram, } from "@solana/web3.js"; - +import { + Keypair, + Signer, + TransactionInstruction, + TransactionMessage, + VersionedTransaction, +} from "@solana/web3.js"; +import { ComputeBudgetProgram } from "@solana/web3.js"; const feeTiers = { min: 0.01, mid: 0.5, - max: 0.95 -} + max: 0.95, +}; /** * Get priority fees for the current block * @param connection - Solana RPC connection * @returns Priority fees statistics and instructions for different fee levels */ -export async function getComputeBudgetInstructions(agent: SolanaAgentKit, instructions: TransactionInstruction[], feeTier: keyof typeof feeTiers): Promise<{ +export async function getComputeBudgetInstructions( + agent: SolanaAgentKit, + instructions: TransactionInstruction[], + feeTier: keyof typeof feeTiers, +): Promise<{ blockhash: string; computeBudgetLimitInstruction: TransactionInstruction; computeBudgetPriorityFeeInstructions: TransactionInstruction; - } > { - try { - const blockhash = (await agent.connection.getLatestBlockhash()).blockhash; - const messageV0 = new TransactionMessage({ - payerKey: agent.wallet_address, - recentBlockhash: blockhash, - instructions: instructions, - }).compileToV0Message(); - const transaction = new VersionedTransaction(messageV0); - const simulatedTx = agent.connection.simulateTransaction(transaction); - const estimatedComputeUnits = (await simulatedTx).value.unitsConsumed; - const safeComputeUnits = Math.ceil( - estimatedComputeUnits ? - Math.max(estimatedComputeUnits + 100000, estimatedComputeUnits * 1.2) - : 200000 - ); - const computeBudgetLimitInstruction = ComputeBudgetProgram.setComputeUnitLimit({ +}> { + const blockhash = (await agent.connection.getLatestBlockhash()).blockhash; + const messageV0 = new TransactionMessage({ + payerKey: agent.wallet_address, + recentBlockhash: blockhash, + instructions: instructions, + }).compileToV0Message(); + const transaction = new VersionedTransaction(messageV0); + const simulatedTx = agent.connection.simulateTransaction(transaction); + const estimatedComputeUnits = (await simulatedTx).value.unitsConsumed; + const safeComputeUnits = Math.ceil( + estimatedComputeUnits + ? Math.max(estimatedComputeUnits + 100000, estimatedComputeUnits * 1.2) + : 200000, + ); + const computeBudgetLimitInstruction = + ComputeBudgetProgram.setComputeUnitLimit({ units: safeComputeUnits, }); - const priorityFee = await agent.connection.getRecentPrioritizationFees() - .then(fees => - fees.sort((a, b) => a.prioritizationFee - b.prioritizationFee) - [Math.floor(fees.length * feeTiers[feeTier])].prioritizationFee - ); + const priorityFee = await agent.connection + .getRecentPrioritizationFees() + .then( + (fees) => + fees.sort((a, b) => a.prioritizationFee - b.prioritizationFee)[ + Math.floor(fees.length * feeTiers[feeTier]) + ].prioritizationFee, + ); - const computeBudgetPriorityFeeInstructions = ComputeBudgetProgram.setComputeUnitPrice({ + const computeBudgetPriorityFeeInstructions = + ComputeBudgetProgram.setComputeUnitPrice({ microLamports: priorityFee, }); - return { - blockhash, - computeBudgetLimitInstruction, - computeBudgetPriorityFeeInstructions - }; - } catch (error) { - throw error; - } + return { + blockhash, + computeBudgetLimitInstruction, + computeBudgetPriorityFeeInstructions, + }; } /** @@ -67,55 +77,52 @@ export async function getComputeBudgetInstructions(agent: SolanaAgentKit, instru export async function sendTx( agent: SolanaAgentKit, instructions: TransactionInstruction[], - otherKeypairs?: Keypair[] + otherKeypairs?: Keypair[], ) { - try { - const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid"); - const allInstructions = [ - ixComputeBudget.computeBudgetLimitInstruction, - ixComputeBudget.computeBudgetPriorityFeeInstructions, - ...instructions]; - const messageV0 = new TransactionMessage({ - payerKey: agent.wallet_address, - recentBlockhash: ixComputeBudget.blockhash, - instructions: allInstructions, - }).compileToV0Message(); - const transaction = new VersionedTransaction(messageV0); - transaction.sign([agent.wallet, ...(otherKeypairs ?? [])] as Signer[]); + const ixComputeBudget = await getComputeBudgetInstructions( + agent, + instructions, + "mid", + ); + const allInstructions = [ + ixComputeBudget.computeBudgetLimitInstruction, + ixComputeBudget.computeBudgetPriorityFeeInstructions, + ...instructions, + ]; + const messageV0 = new TransactionMessage({ + payerKey: agent.wallet_address, + recentBlockhash: ixComputeBudget.blockhash, + instructions: allInstructions, + }).compileToV0Message(); + const transaction = new VersionedTransaction(messageV0); + transaction.sign([agent.wallet, ...(otherKeypairs ?? [])] as Signer[]); - const timeoutMs = 90000; - const startTime = Date.now(); - try { - while (Date.now() - startTime < timeoutMs) { - const transactionStartTime = Date.now(); + const timeoutMs = 90000; + const startTime = Date.now(); + while (Date.now() - startTime < timeoutMs) { + const transactionStartTime = Date.now(); - const signature = await agent.connection.sendTransaction( - transaction, - { - maxRetries: 0, - skipPreflight: false, - }); + const signature = await agent.connection.sendTransaction(transaction, { + maxRetries: 0, + skipPreflight: false, + }); - const statuses = await agent.connection.getSignatureStatuses([signature]); - if (statuses.value[0]) { - if (!statuses.value[0].err) { - return signature; - } else { - throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`); - } - } - - const elapsedTime = Date.now() - transactionStartTime; - const remainingTime = Math.max(0, 1000 - elapsedTime); - if (remainingTime > 0) { - await new Promise(resolve => setTimeout(resolve, remainingTime)); - } + const statuses = await agent.connection.getSignatureStatuses([signature]); + if (statuses.value[0]) { + if (!statuses.value[0].err) { + return signature; + } else { + throw new Error( + `Transaction failed: ${statuses.value[0].err.toString()}`, + ); } - throw new Error("Transaction timeout"); - } catch (error) { - throw error; } - } catch (error) { - throw error; + + const elapsedTime = Date.now() - transactionStartTime; + const remainingTime = Math.max(0, 1000 - elapsedTime); + if (remainingTime > 0) { + await new Promise((resolve) => setTimeout(resolve, remainingTime)); + } } + throw new Error("Transaction timeout"); }