Merge branch 'main' into manifest

This commit is contained in:
DonDuala
2024-12-30 12:50:13 -04:00
committed by GitHub
52 changed files with 6579 additions and 1040 deletions

63
SECURITY.md Normal file
View File

@@ -0,0 +1,63 @@
# Security Policy
## Reporting a Vulnerability
We take the security of our software seriously. If you believe you have found a security vulnerability, please report it to us following these guidelines:
### Reporting Process
1. **DO NOT** create a public GitHub issue for the vulnerability
2. Email your findings to:
- Primary: aryan@sendai.fun
- Secondary: dev@sendai.fun
### What to Include
Please include the following information in your report:
- A clear description of the vulnerability
- Steps to reproduce the issue
- Affected versions
- Any potential impacts
- Optional: Suggested fixes or mitigations
### Response Timeline
- We will acknowledge receipt of your vulnerability report within 48 hours
- We aim to send a more detailed response within 5 business days
- We will keep you informed of our progress throughout the process
### Security Updates
Security updates will be released as soon as possible after we have confirmed and fixed the vulnerability. Updates will be published through:
- GitHub releases
- Security advisories
- Email notifications to affected parties (if applicable)
## Supported Versions
As an open-source project under the Apache 2.0 license, we focus our security updates on the latest stable release. While you're free to use any version as per the Apache 2.0 license terms, we strongly recommend using the most recent version for the best security posture.
| Version | Security Updates |
| ------- | --------------- |
| Latest Release | ✅ Active |
| Previous Releases | ⚠️ Use at your own risk |
Note: The Apache 2.0 license comes with NO WARRANTIES or CONDITIONS of any kind, either express or implied. Users are responsible for their own security assessment when using any version of this software.
## Security Best Practices
When using this software, please follow these security best practices:
- Keep your private keys secure and never share them
- Regularly update to the latest version
- Review transaction details before signing
- Use appropriate access controls in production environments
## Bug Bounty Program
Currently, we do not offer a bug bounty program. However, we greatly appreciate responsible disclosure of security vulnerabilities.
## License
This security policy is part of our project licensed under [Apache 2.0](LICENSE).

View File

@@ -1 +1 @@
window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAAE42TwU4DIRCG34VzY2OjjemtcbMmWrWpezMeEKcuWZYhMEQb03cXt8a6lp164QAf3w/D8PghCN5JzMQDGmnl/BUs3WgSI+Ek1WleGRkChHF//aSm1iSo0fZFzE4nF9vRj+kSjQFFGm0BzuCmTVv2Pm0J/FqqpMyBffHkfJoV37uvMRyxflOs0oMk9HnRbo3bXgKpeum1ghUEl7IgazrEOOmVfn5D33TxUMnQrGBYPQRzAdfR6SSosAFbSJJZ8V+IEy6iwblSGC0VQFKbwJZjGOdCbpNh/7Z3ZcVmDNJcxDK2bh3tQkaralafJY+py2i7cnLdm+FY7YbqfzZhHuXkqmuq3cevEM2vE6e7d4UN4wOob5yebZ8+AWWuiIJlBAAA"
window.navigationData = "data:application/octet-stream;base64,H4sIAAAAAAAAE42TwU7DMAyG3yXniooJJtTbRFUkGDCN3hCHEFwaNXWixBFMaO+O6BCsLPW45OIv3x/ZzuOHIHgnUYgHayTKxSsg3WgSmXCSWlEIZWQIEPJx/aSl3ohMdBpfRHE6u9hmP6ZLawwo0hZLcMZuesA9n0YC30gFIU+BY/HsfJ4U37uvMxyxflOs0oMk69OiXY27XgGpduW1gjUEZzFA0nSIcdIr/fxmfTfEQy1Dt4Zp9RTMBVxHpwl8bTvAUpJMiv9CnHAZjV0oZSNSCSS1CWw7pnEu5FYj/c72rqrZjEmai1jF3jURlzKiall9kjymriIO7eS2N8Gx2g21/1zCNMrJ1bBUu49fW2v2XtxEHBob8gNobJyfbZ8+AWWuiIJlBAAA"

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,4 @@
OPENAI_API_KEY=
RPC_URL=
SOLANA_PRIVATE_KEY=
TELEGRAM_BOT_TOKEN=

41
examples/tg-bot-starter/.gitignore vendored Normal file
View File

@@ -0,0 +1,41 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# env files (can opt-in for committing if needed)
.env
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts

View File

@@ -0,0 +1,22 @@
# Telegram Bot Starter with Solana Agent Kit
This example showcases how we can make a telegram bot with the Solana Agent Kit by Send AI.
## How to get the telegram bot token
You can check [here](https://help.zoho.com/portal/en/kb/desk/support-channels/instant-messaging/telegram/articles/telegram-integration-with-zoho-desk#How_to_find_a_token_for_an_existing_Telegram_Bot) how you can obtain a bot token for your telegram bot.
## How to setup the project
- Set env variables
- Run ``` pnpm install ```
- Run ``` pnpm run dev ```
- Run ``` ngrok http 3000 ```
- With the URL you got from ngrok, where your bot is hosted at https://yourUrl.app/api/bot
- Set the webhook by using this command ``` curl https://api.telegram.org/bot<telegram_bot_token>/setWebhook?url=https://<your-deployment-url>.app/api/bot ``` or simply clicking on that link.
- You can host it on Vercel too as we have used NextJs in this.
- Once the URL is set successfully, you will see this ``` {"ok":true,"result":true,"description":"Webhook was set"} ```
Done!!! Congrtulations you just hosted Solana Agent Kit on a Telegram bot.

View File

@@ -0,0 +1,7 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
/* config options here */
};
export default nextConfig;

View File

@@ -0,0 +1,31 @@
{
"name": "tg-bot-starter",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@langchain/core": "^0.3.26",
"@langchain/langgraph": "^0.2.36",
"@langchain/openai": "^0.3.16",
"grammy": "^1.33.0",
"messages": "link:@langchain/core/messages",
"next": "15.1.3",
"prebuilt": "link:@langchain/langgraph/prebuilt",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"solana-agent-kit": "^1.3.0"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
}

4296
examples/tg-bot-starter/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
/** @type {import('postcss-load-config').Config} */
const config = {
plugins: {
tailwindcss: {},
},
};
export default config;

View File

@@ -0,0 +1 @@
<svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>

After

Width:  |  Height:  |  Size: 391 B

View File

@@ -0,0 +1 @@
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -0,0 +1 @@
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>

After

Width:  |  Height:  |  Size: 128 B

View File

@@ -0,0 +1 @@
<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>

After

Width:  |  Height:  |  Size: 385 B

View File

@@ -0,0 +1,103 @@
export const dynamic = "force-dynamic";
export const fetchCache = "force-no-store";
export const maxDuration = 300;
import { Bot, webhookCallback } from "grammy";
import { SolanaAgentKit, createSolanaTools } from "solana-agent-kit";
import { ChatOpenAI } from "@langchain/openai";
import { MemorySaver } from "@langchain/langgraph";
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.");
}
const bot = new Bot(token);
async function initializeAgent(userId: string) {
try {
const llm = new ChatOpenAI({
modelName: "gpt-4o-mini",
temperature: 0.7,
});
const solanaKit = new SolanaAgentKit(
process.env.SOLANA_PRIVATE_KEY!,
process.env.RPC_URL,
process.env.OPENAI_API_KEY!,
);
const tools = createSolanaTools(solanaKit);
const memory = new MemorySaver();
const config = { configurable: { thread_id: userId } };
const agent = createReactAgent({
llm,
tools,
checkpointSaver: memory,
messageModifier: `
You are a helpful agent that can interact onchain using the Solana Agent Kit. You are
empowered to interact onchain using your tools. If you ever need funds, you can request them from the
faucet. If not, you can provide your wallet details and request funds from the user. If there is a 5XX
(internal) HTTP error code, ask the user to try again later. If someone asks you to do something you
can't do with your currently available tools, you must say so, and encourage them to implement it
themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. Be
concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.
`,
});
return { agent, config };
} catch (error) {
console.error("Failed to initialize agent:", error);
throw error;
}
}
// Telegram bot handler
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),
);
try {
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));
}
}
}
} catch (error: any) {
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.",
);
}
}
});
// Export webhook handler
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");
const handler = webhookCallback(bot, "std/http"); // Use the correct callback
// Handle the incoming webhook request
return handler(req);
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -0,0 +1,21 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
:root {
--background: #ffffff;
--foreground: #171717;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
body {
color: var(--foreground);
background: var(--background);
font-family: Arial, Helvetica, sans-serif;
}

View File

@@ -0,0 +1,34 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";
const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});
const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});
export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
</body>
</html>
);
}

View File

@@ -0,0 +1,101 @@
import Image from "next/image";
export default function Home() {
return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
<main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ol className="list-inside list-decimal text-sm text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
<li className="mb-2">
Get started by editing{" "}
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold">
src/app/page.tsx
</code>
.
</li>
<li>Save and see your changes instantly.</li>
</ol>
<div className="flex gap-4 items-center flex-col sm:flex-row">
<a
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="/vercel.svg"
alt="Vercel logomark"
width={20}
height={20}
/>
Deploy now
</a>
<a
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:min-w-44"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Read our docs
</a>
</div>
</main>
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/file.svg"
alt="File icon"
width={16}
height={16}
/>
Learn
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/window.svg"
alt="Window icon"
width={16}
height={16}
/>
Examples
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/globe.svg"
alt="Globe icon"
width={16}
height={16}
/>
Go to nextjs.org
</a>
</footer>
</div>
);
}

View File

@@ -0,0 +1,18 @@
import type { Config } from "tailwindcss";
export default {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
"./src/app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {
colors: {
background: "var(--background)",
foreground: "var(--foreground)",
},
},
},
plugins: [],
} satisfies Config;

View File

@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}

View File

@@ -1,6 +1,6 @@
{
"name": "solana-agent-kit",
"version": "1.3.0",
"version": "1.3.1",
"description": "connect any ai agents to solana protocols",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@@ -14,7 +14,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
},
"engines": {
"node": ">=23.1.0",
"node": ">=22.0.0",
"pnpm": ">=8.0.0"
},
"keywords": [],
@@ -26,8 +26,8 @@
"@coral-xyz/anchor": "0.29",
"@langchain/core": "^0.3.26",
"@langchain/groq": "^0.1.2",
"@langchain/langgraph": "^0.2.34",
"@langchain/openai": "^0.3.13",
"@langchain/langgraph": "^0.2.36",
"@langchain/openai": "^0.3.16",
"@lightprotocol/compressed-token": "^0.17.1",
"@lightprotocol/stateless.js": "^0.17.1",
"@metaplex-foundation/mpl-core": "^1.1.1",
@@ -42,28 +42,28 @@
"@pythnetwork/price-service-client": "^1.9.0",
"@raydium-io/raydium-sdk-v2": "0.1.95-alpha",
"@solana/spl-token": "^0.4.9",
"@solana/web3.js": "^1.95.4",
"@solana/web3.js": "^1.98.0",
"@tiplink/api": "^0.3.1",
"bn.js": "^5.2.1",
"bs58": "^6.0.0",
"chai": "^5.1.2",
"decimal.js": "^10.4.3",
"dotenv": "^16.4.5",
"dotenv": "^16.4.7",
"form-data": "^4.0.1",
"langchain": "^0.3.6",
"openai": "^4.75.0",
"typedoc": "^0.26.11"
"langchain": "^0.3.8",
"openai": "^4.77.0",
"typedoc": "^0.27.6"
},
"devDependencies": {
"@types/bn.js": "^5.1.5",
"@types/bn.js": "^5.1.6",
"@types/chai": "^5.0.1",
"@types/node": "^22.9.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"@types/node": "^22.10.2",
"@typescript-eslint/eslint-plugin": "^8.18.2",
"@typescript-eslint/parser": "^8.18.2",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.5",
"eslint-plugin-prettier": "^5.2.1",
"prettier": "^3.4.2",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
}

560
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,7 @@ import {
deploy_collection,
deploy_token,
get_balance,
get_balance_other,
getTPS,
resolveSolDomain,
getPrimaryDomain,
@@ -26,10 +27,13 @@ import {
getTokenDataByTicker,
stakeWithJup,
sendCompressedAirdrop,
createOrcaSingleSidedWhirlpool,
orcaCreateSingleSidedLiquidityPool,
orcaCreateCLMM,
orcaOpenCenteredPositionWithLiquidity,
orcaOpenSingleSidedPosition,
FEE_TIERS,
fetchPrice,
pythFetchPrice,
FEE_TIERS,
getAllDomainsTLDs,
getAllRegisteredAllDomains,
getOwnedDomainsForTLD,
@@ -37,9 +41,12 @@ import {
getOwnedAllDomains,
resolveAllDomains,
create_gibwork_task,
orcaClosePosition,
orcaFetchPositions,
rock_paper_scissor,
create_TipLink,
} from "../tools";
import {
CollectionDeployment,
CollectionOptions,
@@ -64,12 +71,12 @@ export class SolanaAgentKit {
public connection: Connection;
public wallet: Keypair;
public wallet_address: PublicKey;
public openai_api_key: string;
public openai_api_key: string | null;
constructor(
private_key: string,
rpc_url = "https://api.mainnet-beta.solana.com",
openai_api_key: string,
openai_api_key: string | null = null,
) {
this.connection = new Connection(rpc_url);
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));
@@ -102,6 +109,13 @@ export class SolanaAgentKit {
return get_balance(this, token_address);
}
async getBalanceOther(
walletAddress: PublicKey,
tokenAddress?: PublicKey,
): Promise<number> {
return get_balance_other(this, walletAddress, tokenAddress);
}
async mintNFT(
collectionMint: PublicKey,
metadata: Parameters<typeof mintCollectionNFT>[2],
@@ -212,15 +226,28 @@ export class SolanaAgentKit {
);
}
async createOrcaSingleSidedWhirlpool(
depositTokenAmount: BN,
async orcaClosePosition(positionMintAddress: PublicKey) {
return orcaClosePosition(this, positionMintAddress);
}
async orcaCreateCLMM(
mintDeploy: PublicKey,
mintPair: PublicKey,
initialPrice: Decimal,
feeTier: keyof typeof FEE_TIERS,
) {
return orcaCreateCLMM(this, mintDeploy, mintPair, initialPrice, feeTier);
}
async orcaCreateSingleSidedLiquidityPool(
depositTokenAmount: number,
depositTokenMint: PublicKey,
otherTokenMint: PublicKey,
initialPrice: Decimal,
maxPrice: Decimal,
feeTier: keyof typeof FEE_TIERS,
) {
return createOrcaSingleSidedWhirlpool(
return orcaCreateSingleSidedLiquidityPool(
this,
depositTokenAmount,
depositTokenMint,
@@ -231,6 +258,42 @@ export class SolanaAgentKit {
);
}
async orcaFetchPositions() {
return orcaFetchPositions(this);
}
async orcaOpenCenteredPositionWithLiquidity(
whirlpoolAddress: PublicKey,
priceOffsetBps: number,
inputTokenMint: PublicKey,
inputAmount: Decimal,
) {
return orcaOpenCenteredPositionWithLiquidity(
this,
whirlpoolAddress,
priceOffsetBps,
inputTokenMint,
inputAmount,
);
}
async orcaOpenSingleSidedPosition(
whirlpoolAddress: PublicKey,
distanceFromCurrentPriceBps: number,
widthBps: number,
inputTokenMint: PublicKey,
inputAmount: Decimal,
): Promise<string> {
return orcaOpenSingleSidedPosition(
this,
whirlpoolAddress,
distanceFromCurrentPriceBps,
widthBps,
inputTokenMint,
inputAmount,
);
}
async resolveAllDomains(domain: string): Promise<PublicKey | undefined> {
return resolveAllDomains(this, domain);
}

View File

@@ -9,7 +9,6 @@ import {
import { create_image } from "../tools/create_image";
import { BN } from "@coral-xyz/anchor";
import { FEE_TIERS } from "../tools";
import { toJSON } from "../utils/toJSON";
export class SolanaBalanceTool extends Tool {
name = "solana_balance";
@@ -18,7 +17,7 @@ export class SolanaBalanceTool extends Tool {
If you want to get the balance of your wallet, you don't need to provide the tokenAddress.
If no tokenAddress is provided, the balance will be in SOL.
Inputs:
Inputs ( input is a JSON string ):
tokenAddress: string, eg "So11111111111111111111111111111111111111112" (optional)`;
constructor(private solanaKit: SolanaAgentKit) {
@@ -32,7 +31,7 @@ export class SolanaBalanceTool extends Tool {
return JSON.stringify({
status: "success",
balance: balance,
balance,
token: input || "SOL",
});
} catch (error: any) {
@@ -45,6 +44,49 @@ export class SolanaBalanceTool extends Tool {
}
}
export class SolanaBalanceOtherTool extends Tool {
name = "solana_balance_other";
description = `Get the balance of a Solana wallet or token account different from the agent's wallet.
If no tokenAddress is provided, the SOL balance of the wallet will be returned.
Inputs ( input is a JSON string ):
walletAddress: string, eg "GDEkQF7UMr7RLv1KQKMtm8E2w3iafxJLtyXu3HVQZnME" (required)
tokenAddress: string, eg "SENDdRQtYMWaQrBroBrJ2Q53fgVuq95CV9UPGEvpCxa" (optional)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
protected async _call(input: string): Promise<string> {
try {
const { walletAddress, tokenAddress } = JSON.parse(input);
const tokenPubKey = tokenAddress
? new PublicKey(tokenAddress)
: undefined;
const balance = await this.solanaKit.getBalanceOther(
new PublicKey(walletAddress),
tokenPubKey,
);
return JSON.stringify({
status: "success",
balance,
wallet: walletAddress,
token: tokenAddress || "SOL",
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
export class SolanaTransferTool extends Tool {
name = "solana_transfer";
description = `Transfer tokens or SOL to another address ( also called as wallet address ).
@@ -362,7 +404,7 @@ export class SolanaRegisterDomainTool extends Tool {
protected async _call(input: string): Promise<string> {
try {
const parsedInput = toJSON(input);
const parsedInput = JSON.parse(input);
this.validateInput(parsedInput);
const tx = await this.solanaKit.registerDomain(
@@ -599,7 +641,7 @@ export class SolanaLendAssetTool extends Tool {
status: "success",
message: "Asset lent successfully",
transaction: tx,
amount: amount,
amount,
});
} catch (error: any) {
return JSON.stringify({
@@ -713,7 +755,7 @@ export class SolanaTokenDataTool extends Tool {
return JSON.stringify({
status: "success",
tokenData: tokenData,
tokenData,
});
} catch (error: any) {
return JSON.stringify({
@@ -742,7 +784,7 @@ export class SolanaTokenDataByTickerTool extends Tool {
const tokenData = await this.solanaKit.getTokenDataByTicker(ticker);
return JSON.stringify({
status: "success",
tokenData: tokenData,
tokenData,
});
} catch (error: any) {
return JSON.stringify({
@@ -798,17 +840,13 @@ export class SolanaCompressedAirdropTool extends Tool {
}
}
export class SolanaCreateSingleSidedWhirlpoolTool extends Tool {
name = "create_orca_single_sided_whirlpool";
description = `Create a single-sided Whirlpool with liquidity.
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)`;
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();
@@ -817,7 +855,102 @@ export class SolanaCreateSingleSidedWhirlpoolTool extends Tool {
async _call(input: string): Promise<string> {
try {
const inputFormat = JSON.parse(input);
const depositTokenAmount = new BN(inputFormat.depositTokenAmount);
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.
Inputs (JSON string):
- 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).`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
const inputFormat = JSON.parse(input);
const mintA = new PublicKey(inputFormat.mintDeploy);
const mintB = new PublicKey(inputFormat.mintPair);
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();
}
async _call(input: string): Promise<string> {
try {
const inputFormat = JSON.parse(input);
const depositTokenAmount = inputFormat.depositTokenAmount;
const depositTokenMint = new PublicKey(inputFormat.depositTokenMint);
const otherTokenMint = new PublicKey(inputFormat.otherTokenMint);
const initialPrice = new Decimal(inputFormat.initialPrice);
@@ -832,7 +965,7 @@ export class SolanaCreateSingleSidedWhirlpoolTool extends Tool {
);
}
const txId = await this.solanaKit.createOrcaSingleSidedWhirlpool(
const txId = await this.solanaKit.orcaCreateSingleSidedLiquidityPool(
depositTokenAmount,
depositTokenMint,
otherTokenMint,
@@ -856,6 +989,137 @@ export class SolanaCreateSingleSidedWhirlpoolTool 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<string> {
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 centered position in an Orca Whirlpool, the most efficient liquidity pool on Solana.
Inputs (JSON string):
- whirlpoolAddress: string, address of the Orca Whirlpool (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).`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
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) {
throw new Error(
"Invalid distanceFromCurrentPriceBps. It must be equal or greater than 0.",
);
}
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 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<string> {
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
@@ -1087,7 +1351,7 @@ export class SolanaPythFetchPrice extends Tool {
const response: PythFetchPriceResponse = {
status: "success",
priceFeedID: input,
price: price,
price,
};
return JSON.stringify(response);
} catch (error: any) {
@@ -1161,7 +1425,7 @@ export class SolanaGetOwnedDomains extends Tool {
return JSON.stringify({
status: "success",
message: "Owned domains fetched successfully",
domains: domains,
domains,
});
} catch (error: any) {
return JSON.stringify({
@@ -1191,7 +1455,7 @@ export class SolanaGetOwnedTldDomains extends Tool {
return JSON.stringify({
status: "success",
message: "TLD domains fetched successfully",
domains: domains,
domains,
});
} catch (error: any) {
return JSON.stringify({
@@ -1218,7 +1482,7 @@ export class SolanaGetAllTlds extends Tool {
return JSON.stringify({
status: "success",
message: "TLDs fetched successfully",
tlds: tlds,
tlds,
});
} catch (error: any) {
return JSON.stringify({
@@ -1337,7 +1601,7 @@ export class SolanaRockPaperScissorsTool extends Tool {
protected async _call(input: string): Promise<string> {
try {
const parsedInput = toJSON(input);
const parsedInput = JSON.parse(input);
this.validateInput(parsedInput);
const result = await this.solanaKit.rockPaperScissors(
Number(parsedInput['"amount"']),
@@ -1411,6 +1675,7 @@ export class SolanaTipLinkTool extends Tool {
export function createSolanaTools(solanaKit: SolanaAgentKit) {
return [
new SolanaBalanceTool(solanaKit),
new SolanaBalanceOtherTool(solanaKit),
new SolanaTransferTool(solanaKit),
new SolanaDeployTokenTool(solanaKit),
new SolanaDeployCollectionTool(solanaKit),
@@ -1436,6 +1701,12 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaManifestCreateMarket(solanaKit),
new SolanaLimitOrderTool(solanaKit),
new SolanaCreateSingleSidedWhirlpoolTool(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),

View File

@@ -1,431 +0,0 @@
import { Keypair, PublicKey, Transaction } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { BN, Wallet } from "@coral-xyz/anchor";
import { Decimal } from "decimal.js";
import {
PDAUtil,
ORCA_WHIRLPOOL_PROGRAM_ID,
ORCA_WHIRLPOOLS_CONFIG,
WhirlpoolContext,
TickUtil,
PriceMath,
PoolUtil,
TokenExtensionContextForPool,
NO_TOKEN_EXTENSION_CONTEXT,
TokenExtensionUtil,
WhirlpoolIx,
IncreaseLiquidityQuoteParam,
increaseLiquidityQuoteByInputTokenWithParams,
} from "@orca-so/whirlpools-sdk";
import {
Percentage,
resolveOrCreateATAs,
TransactionBuilder,
} from "@orca-so/common-sdk";
import {
increaseLiquidityIx,
increaseLiquidityV2Ix,
initTickArrayIx,
openPositionWithTokenExtensionsIx,
} from "@orca-so/whirlpools-sdk/dist/instructions";
import {
getAssociatedTokenAddressSync,
TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";
import { sendTx } from "../utils/send_tx";
/**
* Maps fee tier percentages 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
* the granularity of price ranges for liquidity positions.
*
* For more details, refer to:
* - [Whirlpool Fees](https://orca-so.github.io/whirlpools/Architecture%20Overview/Whirlpool%20Fees)
* - [Whirlpool Parameters](https://orca-so.github.io/whirlpools/Architecture%20Overview/Whirlpool%20Parameters)
*
* @example
* const tickSpacing = FEE_TIERS[0.30]; // Returns 64
*/
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,
} as const;
/**
* # Creates a single-sided Whirlpool.
*
* This function initializes a new Whirlpool (liquidity pool) on Orca and seeds it with liquidity from a single token.
*
* ## Example Usage:
* You created a new token called SHARK, and you want to set the initial price to 0.001 USDC.
* You set `depositTokenMint` to SHARK's mint address and `otherTokenMint` to USDC's mint address.
* You can minimize price impact for buyers in a few ways:
* 1. Increase the amount of tokens you deposit
* 2. Set the initial price very low
* 3. Set the maximum price closer to the initial price
*
* ### Note for experts:
* The Wrhirlpool program initializes the Whirlpool with the in a specific order. This might not be
* the order you expect, so the function checks the order and adjusts the inverts the prices. This means that
* on-chain the Whirlpool might be configured as USDC/SHARK instead of SHARK/USDC, and the on-chain price will
* 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 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.
* @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.
*
* @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.
* - Prices are out of bounds.
*
* @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.30;
*
* const txId = await createOrcaSingleSidedWhirlpool(
* agent,
* depositAmount,
* depositTokenMint,
* otherTokenMint,
* initialPrice,
* maxPrice,
* feeTier,
* );
* console.log(`Single sided whirlpool created in transaction: ${txId}`);
* ```
*/
export async function createOrcaSingleSidedWhirlpool(
agent: SolanaAgentKit,
depositTokenAmount: BN,
depositTokenMint: PublicKey,
otherTokenMint: PublicKey,
initialPrice: Decimal,
maxPrice: Decimal,
feeTier: keyof typeof FEE_TIERS,
): Promise<string> {
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[feeTier];
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,
ORCA_WHIRLPOOLS_CONFIG,
tickSpacing,
).publicKey;
const initSqrtPrice = PriceMath.tickIndexToSqrtPriceX64(initialTick);
const tokenVaultAKeypair = Keypair.generate();
const tokenVaultBKeypair = Keypair.generate();
const whirlpoolPda = PDAUtil.getWhirlpool(
ORCA_WHIRLPOOL_PROGRAM_ID,
ORCA_WHIRLPOOLS_CONFIG,
mintA,
mintB,
FEE_TIERS[feeTier],
);
const tokenBadgeA = PDAUtil.getTokenBadge(
ORCA_WHIRLPOOL_PROGRAM_ID,
ORCA_WHIRLPOOLS_CONFIG,
mintA,
).publicKey;
const tokenBadgeB = PDAUtil.getTokenBadge(
ORCA_WHIRLPOOL_PROGRAM_ID,
ORCA_WHIRLPOOLS_CONFIG,
mintB,
).publicKey;
const baseParamsPool = {
initSqrtPrice,
whirlpoolsConfig: ORCA_WHIRLPOOLS_CONFIG,
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 {
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 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,
ctx.accountResolverOpts.createWrappedSolAccountMethod,
);
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) {
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({
maxSupportedTransactionVersion: "legacy",
});
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");
}
}

View File

@@ -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),
}),
);
}

View File

@@ -0,0 +1,50 @@
import {
LAMPORTS_PER_SOL,
ParsedAccountData,
PublicKey,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
/**
* Get the balance of SOL or an SPL token for the specified wallet address (other than the agent's wallet)
* @param agent - SolanaAgentKit instance
* @param wallet_address - Public key of the wallet to check balance for
* @param token_address - Optional SPL token mint address. If not provided, returns SOL balance
* @returns Promise resolving to the balance as a number (in UI units) or 0 if account doesn't exist
*/
export async function get_balance_other(
agent: SolanaAgentKit,
wallet_address: PublicKey,
token_address?: PublicKey,
): Promise<number> {
try {
if (!token_address) {
return (
(await agent.connection.getBalance(wallet_address)) / LAMPORTS_PER_SOL
);
}
const tokenAccounts = await agent.connection.getTokenAccountsByOwner(
wallet_address,
{ mint: token_address },
);
if (tokenAccounts.value.length === 0) {
console.warn(
`No token accounts found for wallet ${wallet_address.toString()} and token ${token_address.toString()}`,
);
return 0;
}
const tokenAccount = await agent.connection.getParsedAccountInfo(
tokenAccounts.value[0].pubkey,
);
const tokenData = tokenAccount.value?.data as ParsedAccountData;
return tokenData.parsed?.info?.tokenAmount?.uiAmount || 0;
} catch (error) {
throw new Error(
`Error fetching on-chain balance for ${token_address?.toString()}: ${error}`,
);
}
}

View File

@@ -2,6 +2,7 @@ export * from "./request_faucet_funds";
export * from "./deploy_token";
export * from "./deploy_collection";
export * from "./get_balance";
export * from "./get_balance_other";
export * from "./mint_nft";
export * from "./transfer";
export * from "./trade";
@@ -16,7 +17,12 @@ 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_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";

View File

@@ -0,0 +1,82 @@
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 Orcas SDK to interact with the specified Whirlpool and close the liquidity position.
* - A maximum slippage of 1% is assumed for liquidity provision during the position closing.
* - The function automatically fetches the associated Whirlpool address and position details using the provided mint address.
*
* ## Throws
* An error will be thrown if:
* - The specified position mint address is invalid or inaccessible.
* - The transaction fails to send.
* - Any required position or Whirlpool data cannot be fetched.
*
* @param agent - The `SolanaAgentKit` instance representing the wallet and connection.
* @param positionMintAddress - The mint address of the liquidity position to close.
* @returns A promise resolving to the transaction ID (`string`).
*/
export async function orcaClosePosition(
agent: SolanaAgentKit,
positionMintAddress: PublicKey,
): Promise<string> {
try {
const wallet = new Wallet(agent.wallet);
const ctx = WhirlpoolContext.from(
agent.connection,
wallet,
ORCA_WHIRLPOOL_PROGRAM_ID,
);
const client = buildWhirlpoolClient(ctx);
const positionAddress = PDAUtil.getPosition(
ORCA_WHIRLPOOL_PROGRAM_ID,
positionMintAddress,
);
const position = await client.getPosition(positionAddress.publicKey);
const whirlpoolAddress = position.getData().whirlpool;
const whirlpool = await client.getPool(whirlpoolAddress);
const txBuilder = await whirlpool.closePosition(
positionAddress.publicKey,
Percentage.fromFraction(1, 100),
);
const txPayload = await txBuilder[0].build();
const txPayloadDecompiled = TransactionMessage.decompile(
(txPayload.transaction as VersionedTransaction).message,
);
const instructions = txPayloadDecompiled.instructions;
const signers = txPayload.signers as Keypair[];
const txId = await sendTx(agent, instructions, signers);
return txId;
} catch (error) {
throw new Error(`${error}`);
}
}

View File

@@ -0,0 +1,132 @@
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 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.
*
* @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,
mintDeploy: PublicKey,
mintPair: PublicKey,
initialPrice: Decimal,
feeTier: keyof typeof FEE_TIERS,
): Promise<string> {
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(mintDeploy, mintPair).map(
(addr) => addr.toString(),
);
const isCorrectMintOrder = correctTokenOrder[0] === mintDeploy.toString();
let mintA;
let mintB;
if (!isCorrectMintOrder) {
[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);
if (mintAAccount === null || mintBAccount === null) {
throw Error("Mint account not found");
}
const tickSpacing = FEE_TIERS[feeTier];
const initialTick = PriceMath.priceToInitializableTickIndex(
initialPrice,
mintAAccount.decimals,
mintBAccount.decimals,
tickSpacing,
);
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) {
throw new Error(`${error}`);
}
}

View File

@@ -0,0 +1,422 @@
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";
import {
PDAUtil,
ORCA_WHIRLPOOL_PROGRAM_ID,
WhirlpoolContext,
TickUtil,
PriceMath,
PoolUtil,
TokenExtensionContextForPool,
NO_TOKEN_EXTENSION_CONTEXT,
TokenExtensionUtil,
WhirlpoolIx,
IncreaseLiquidityQuoteParam,
increaseLiquidityQuoteByInputTokenWithParams,
} from "@orca-so/whirlpools-sdk";
import {
Percentage,
resolveOrCreateATAs,
TransactionBuilder,
} from "@orca-so/common-sdk";
import {
increaseLiquidityIx,
increaseLiquidityV2Ix,
initTickArrayIx,
openPositionWithTokenExtensionsIx,
} from "@orca-so/whirlpools-sdk/dist/instructions";
import {
getAssociatedTokenAddressSync,
TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";
import { sendTx } from "../utils/send_tx";
/**
* 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
* the granularity of price ranges for liquidity positions.
*
* For more details, refer to:
* - [Whirlpool Fees](https://orca-so.github.io/whirlpools/Architecture%20Overview/Whirlpool%20Fees)
* - [Whirlpool Parameters](https://orca-so.github.io/whirlpools/Architecture%20Overview/Whirlpool%20Parameters)
*
* @example
* const tickSpacing = FEE_TIERS[1]; // returns 1
*/
export const FEE_TIERS = {
1: 1,
2: 2,
4: 4,
5: 8,
16: 16,
30: 64,
65: 96,
100: 128,
200: 256,
} as const;
/**
* # 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.
*
* ## Example Usage:
* You created a new token called SHARK, and you want to set the initial price to 0.001 USDC.
* You set `depositTokenMint` to SHARK's mint address and `otherTokenMint` to USDC's mint address.
* You can minimize price impact for buyers in a few ways:
* 1. Increase the amount of tokens you deposit
* 2. Set the initial price very low
* 3. Set the maximum price closer to the initial price
*
* ### Note for experts:
* The Wrhirlpool program initializes the Whirlpool with the in a specific order. This might not be
* the order you expect, so the function checks the order and adjusts the inverts the prices. This means that
* on-chain the Whirlpool might be configured as USDC/SHARK instead of SHARK/USDC, and the on-chain price will
* 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 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.
* @param maxPrice - The maximum price at which liquidity is added.
* @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.
* - Prices are out of bounds.
*
* @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.
*/
export async function orcaCreateSingleSidedLiquidityPool(
agent: SolanaAgentKit,
depositTokenAmount: number,
depositTokenMint: PublicKey,
otherTokenMint: PublicKey,
initialPrice: Decimal,
maxPrice: Decimal,
feeTierBps: keyof typeof FEE_TIERS,
): Promise<string> {
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 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,
);
} 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");
}
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 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) {
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, [
positionMintKeypair,
tokenVaultAKeypair,
tokenVaultBKeypair,
]);
return txId;
} catch (error) {
throw new Error(`Failed to send transaction: ${JSON.stringify(error)}`);
}
}

View File

@@ -0,0 +1,121 @@
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<string> {
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) {
throw new Error(`${error}`);
}
}

View File

@@ -0,0 +1,161 @@
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 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.
* 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 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.
*
* ## 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 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`).
*/
export async function orcaOpenCenteredPositionWithLiquidity(
agent: SolanaAgentKit,
whirlpoolAddress: PublicKey,
priceOffsetBps: number,
inputTokenMint: PublicKey,
inputAmount: Decimal,
): Promise<string> {
try {
const wallet = new Wallet(agent.wallet);
const ctx = WhirlpoolContext.from(
agent.connection,
wallet,
ORCA_WHIRLPOOL_PROGRAM_ID,
);
const client = buildWhirlpoolClient(ctx);
const 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 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[] = [];
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,
undefined,
undefined,
undefined,
TOKEN_2022_PROGRAM_ID,
);
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) {
throw new Error(`${error}`);
}
}

View File

@@ -0,0 +1,177 @@
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,
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<string> {
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 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[];
const positionTxId = await sendTx(agent, instructions, signers);
txIds += positionTxId;
return JSON.stringify({
transactionIds: txIds,
positionMint: positionMint.toString(),
});
} catch (error) {
throw new Error(`${error}`);
}
}

View File

@@ -1,70 +1,71 @@
import { SolanaAgentKit } from "../agent";
import { Transaction, Keypair, TransactionInstruction } from "@solana/web3.js";
import { Connection, 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,
};
/**
* 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);
const computeBudgetPriorityFeeInstructions =
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: priorityFee,
});
// 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,
});
};
return {
min,
median,
max,
// Return instructions for different fee levels
instructions: {
low: createPriorityFeeIx(min),
medium: createPriorityFeeIx(median),
high: createPriorityFeeIx(max),
},
};
} catch (error) {
console.error("Error getting priority fees:", error);
throw error;
}
return {
blockhash,
computeBudgetLimitInstruction,
computeBudgetPriorityFeeInstructions,
};
}
/**
@@ -75,23 +76,53 @@ 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!);
}
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[]);
tx.sign(agent.wallet, ...(otherKeypairs ?? []));
const 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 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 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");
}

View File

@@ -1,26 +0,0 @@
export const toJSON = (str: string): any => {
try {
// Remove curly braces and split by comma
const pairs = str.trim().slice(1, -1).split(",");
// Convert to object with explicit type
const obj: Record<string, any> = {};
pairs.forEach((pair) => {
const [key, value] = pair
.trim()
.split(":")
.map((s) => s.trim());
if (!key || value === undefined) {
throw new Error("Invalid key-value pair format");
}
obj[key] = isNaN(Number(value)) ? value : Number(value);
});
return JSON.parse(JSON.stringify(obj));
} catch (error) {
throw new Error(`Failed to parse string to JSON: ${error}`);
}
};

View File

@@ -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;