Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add meteora lp #227

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import {
voltrGetPositionValues,
voltrDepositStrategy,
voltrWithdrawStrategy,
createMeteoraDlmmLpPosition,
} from "../tools";
import {
Config,
Expand Down Expand Up @@ -450,6 +451,13 @@ export class SolanaAgentKit {
);
}

async createMeteoraDlmmLpPosition(
pool: PublicKey,
totalXAmount: BN,
): Promise<string> {
return createMeteoraDlmmLpPosition(this, pool, totalXAmount);
}

async orcaClosePosition(positionMintAddress: PublicKey) {
return orcaClosePosition(this, positionMintAddress);
}
Expand Down Expand Up @@ -654,6 +662,7 @@ export class SolanaAgentKit {
) {
return rock_paper_scissor(this, amount, choice);
}

async createTiplink(amount: number, splmintAddress?: PublicKey) {
return create_TipLink(this, amount, splmintAddress);
}
Expand Down Expand Up @@ -698,9 +707,11 @@ export class SolanaAgentKit {
async flashCloseTrade(params: FlashCloseTradeParams): Promise<string> {
return flashCloseTrade(this, params);
}

async heliusParseTransactions(transactionId: string): Promise<any> {
return parseTransaction(this, transactionId);
}

async getAllAssetsbyOwner(owner: PublicKey, limit: number): Promise<any> {
return getAssetsByOwner(this, owner, limit);
}
Expand Down Expand Up @@ -746,6 +757,7 @@ export class SolanaAgentKit {
);
return `Transaction: ${tx}`;
}

async sendTranctionWithPriority(
priorityLevel: string,
amount: number,
Expand Down Expand Up @@ -805,15 +817,18 @@ export class SolanaAgentKit {
): Promise<string> {
return multisig_execute_proposal(this, transactionIndex);
}

async CreateWebhook(
accountAddresses: string[],
webhookURL: string,
): Promise<HeliusWebhookResponse> {
return create_HeliusWebhook(this, accountAddresses, webhookURL);
}

async getWebhook(id: string): Promise<HeliusWebhookIdResponse> {
return getHeliusWebhook(this, id);
}

async deleteWebhook(webhookID: string): Promise<any> {
return deleteHeliusWebhook(this, webhookID);
}
Expand All @@ -839,25 +854,31 @@ export class SolanaAgentKit {
async depositIntoDriftVault(amount: number, vault: string) {
return await depositIntoVault(this, amount, vault);
}

async depositToDriftUserAccount(
amount: number,
symbol: string,
isRepayment?: boolean,
) {
return await depositToDriftUserAccount(this, amount, symbol, isRepayment);
}

async deriveDriftVaultAddress(name: string) {
return await getVaultAddress(this, name);
}

async doesUserHaveDriftAccount() {
return await doesUserHaveDriftAccount(this);
}

async driftUserAccountInfo() {
return await driftUserAccountInfo(this);
}

async requestWithdrawalFromDriftVault(amount: number, vault: string) {
return await requestWithdrawalFromVault(this, amount, vault);
}

async tradeUsingDelegatedDriftVault(
vault: string,
amount: number,
Expand All @@ -876,6 +897,7 @@ export class SolanaAgentKit {
price,
);
}

async tradeUsingDriftPerpAccount(
amount: number,
symbol: string,
Expand All @@ -885,6 +907,7 @@ export class SolanaAgentKit {
) {
return await driftPerpTrade(this, { action, amount, symbol, type, price });
}

async updateDriftVault(
vaultAddress: string,
params: {
Expand All @@ -901,19 +924,23 @@ export class SolanaAgentKit {
) {
return await updateVault(this, vaultAddress, params);
}

async getDriftVaultInfo(vaultName: string) {
return await getVaultInfo(this, vaultName);
}

async withdrawFromDriftAccount(
amount: number,
symbol: string,
isBorrow?: boolean,
) {
return await withdrawFromDriftUserAccount(this, amount, symbol, isBorrow);
}

async withdrawFromDriftVault(vault: string) {
return await withdrawFromDriftVault(this, vault);
}

async updateDriftVaultDelegate(vaultAddress: string, delegate: string) {
return await updateVaultDelegate(this, vaultAddress, delegate);
}
Expand All @@ -931,15 +958,19 @@ export class SolanaAgentKit {
};
}
}

async stakeToDriftInsuranceFund(amount: number, symbol: string) {
return await stakeToDriftInsuranceFund(this, amount, symbol);
}

async requestUnstakeFromDriftInsuranceFund(amount: number, symbol: string) {
return await requestUnstakeFromDriftInsuranceFund(this, amount, symbol);
}

async unstakeFromDriftInsuranceFund(symbol: string) {
return await unstakeFromDriftInsuranceFund(this, symbol);
}

async driftSpotTokenSwap(
params: {
fromSymbol: string;
Expand All @@ -962,19 +993,22 @@ export class SolanaAgentKit {
slippage: params.slippage,
});
}

async getPerpMarketFundingRate(
symbol: `${string}-PERP`,
period: "year" | "hour" = "year",
) {
return calculatePerpMarketFundingRate(this, symbol, period);
}

async getEntryQuoteOfPerpTrade(
amount: number,
symbol: `${string}-PERP`,
action: "short" | "long",
) {
return getEntryQuoteOfPerpTrade(symbol, amount, action);
}

async getLendAndBorrowAPY(symbol: string) {
return getLendingAndBorrowAPY(this, symbol);
}
Expand Down
49 changes: 49 additions & 0 deletions src/langchain/meteora/meteora_dlmm_lp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { PublicKey } from "@solana/web3.js";
import { Tool } from "langchain/tools";
import { SolanaAgentKit } from "../../agent";
import { BN } from "bn.js";

export class SolanaMeteoraCreateDlmmLpPosition extends Tool {
name = "meteora_create_dlmm_lp_position";
description = `Create a Meteora DLMM LP position.

Inputs (JSON string):
- pool: string, pool address (required).
- totalXAmount: number, total X amount to deposit, e.g., 100 (required).
`;

constructor(private solanaKit: SolanaAgentKit) {
super();
}

async _call(input: string): Promise<string> {
try {
interface CreateMeteoraDlmmLpPositionInput {
pool: string;
totalXAmount: number;
}

const inputFormat: CreateMeteoraDlmmLpPositionInput = JSON.parse(input);

const pool = new PublicKey(inputFormat.pool);
const totalXAmount = new BN(inputFormat.totalXAmount);

const txId = await this.solanaKit.createMeteoraDlmmLpPosition(
pool,
totalXAmount,
);

return JSON.stringify({
status: "success",
message: "Meteora DLMM LP position created successfully.",
transaction: txId,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
53 changes: 53 additions & 0 deletions src/tools/meteora/create_meteora_dlmm_lp_position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Keypair, PublicKey } from "@solana/web3.js";
import DLMM, { StrategyType } from "@meteora-ag/dlmm";
import BN from "bn.js";
import { SolanaAgentKit } from "../../agent";
import { sendTx } from "../../utils/send_tx";

export async function createMeteoraDlmmLpPosition(
agent: SolanaAgentKit,
pool: PublicKey,
totalXAmount: BN,
): Promise<string> {
const dlmmPool = await DLMM.create(agent.connection, pool);

const activeBin = await dlmmPool.getActiveBin();
const activeBinPriceLamport = activeBin.price;

Check warning on line 15 in src/tools/meteora/create_meteora_dlmm_lp_position.ts

View workflow job for this annotation

GitHub Actions / check

'activeBinPriceLamport' is assigned a value but never used
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
Number(activeBin.price),
);

const newBalancePosition = Keypair.generate();

const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
const minBinId = activeBin.binId - TOTAL_RANGE_INTERVAL;
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL;

const totalYAmount = totalXAmount.mul(new BN(Number(activeBinPricePerToken)));

const createPositionTx =
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
positionPubKey: newBalancePosition.publicKey,
user: agent.wallet.publicKey,
totalXAmount,
totalYAmount,
strategy: {
maxBinId,
minBinId,
strategyType: StrategyType.SpotBalanced,
},
});

try {
const createBalancePositionTxHash = await sendTx(
agent,
createPositionTx.instructions,
[agent.wallet, newBalancePosition],
);

return createBalancePositionTxHash;
} catch (error) {
console.log("🚀 ~ error:", JSON.parse(JSON.stringify(error)));

Check warning on line 50 in src/tools/meteora/create_meteora_dlmm_lp_position.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected console statement
throw new Error("Failed to create balance position");
}
}
1 change: 1 addition & 0 deletions src/tools/meteora/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./create_meteora_dlmm_pool";
export * from "./create_meteora_dynamic_amm_pool";
export * from "./create_meteora_dlmm_lp_position";
Loading