Skip to content

Commit 23d6c49

Browse files
committed
meteora lp
1 parent 343cf4c commit 23d6c49

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

src/agent/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import {
114114
voltrGetPositionValues,
115115
voltrDepositStrategy,
116116
voltrWithdrawStrategy,
117+
createMeteoraDlmmLpPosition,
117118
} from "../tools";
118119
import {
119120
Config,
@@ -450,6 +451,13 @@ export class SolanaAgentKit {
450451
);
451452
}
452453

454+
async createMeteoraDlmmLpPosition(
455+
pool: PublicKey,
456+
totalXAmount: BN,
457+
): Promise<string> {
458+
return createMeteoraDlmmLpPosition(this, pool, totalXAmount);
459+
}
460+
453461
async orcaClosePosition(positionMintAddress: PublicKey) {
454462
return orcaClosePosition(this, positionMintAddress);
455463
}
@@ -654,6 +662,7 @@ export class SolanaAgentKit {
654662
) {
655663
return rock_paper_scissor(this, amount, choice);
656664
}
665+
657666
async createTiplink(amount: number, splmintAddress?: PublicKey) {
658667
return create_TipLink(this, amount, splmintAddress);
659668
}
@@ -698,9 +707,11 @@ export class SolanaAgentKit {
698707
async flashCloseTrade(params: FlashCloseTradeParams): Promise<string> {
699708
return flashCloseTrade(this, params);
700709
}
710+
701711
async heliusParseTransactions(transactionId: string): Promise<any> {
702712
return parseTransaction(this, transactionId);
703713
}
714+
704715
async getAllAssetsbyOwner(owner: PublicKey, limit: number): Promise<any> {
705716
return getAssetsByOwner(this, owner, limit);
706717
}
@@ -746,6 +757,7 @@ export class SolanaAgentKit {
746757
);
747758
return `Transaction: ${tx}`;
748759
}
760+
749761
async sendTranctionWithPriority(
750762
priorityLevel: string,
751763
amount: number,
@@ -805,15 +817,18 @@ export class SolanaAgentKit {
805817
): Promise<string> {
806818
return multisig_execute_proposal(this, transactionIndex);
807819
}
820+
808821
async CreateWebhook(
809822
accountAddresses: string[],
810823
webhookURL: string,
811824
): Promise<HeliusWebhookResponse> {
812825
return create_HeliusWebhook(this, accountAddresses, webhookURL);
813826
}
827+
814828
async getWebhook(id: string): Promise<HeliusWebhookIdResponse> {
815829
return getHeliusWebhook(this, id);
816830
}
831+
817832
async deleteWebhook(webhookID: string): Promise<any> {
818833
return deleteHeliusWebhook(this, webhookID);
819834
}
@@ -839,25 +854,31 @@ export class SolanaAgentKit {
839854
async depositIntoDriftVault(amount: number, vault: string) {
840855
return await depositIntoVault(this, amount, vault);
841856
}
857+
842858
async depositToDriftUserAccount(
843859
amount: number,
844860
symbol: string,
845861
isRepayment?: boolean,
846862
) {
847863
return await depositToDriftUserAccount(this, amount, symbol, isRepayment);
848864
}
865+
849866
async deriveDriftVaultAddress(name: string) {
850867
return await getVaultAddress(this, name);
851868
}
869+
852870
async doesUserHaveDriftAccount() {
853871
return await doesUserHaveDriftAccount(this);
854872
}
873+
855874
async driftUserAccountInfo() {
856875
return await driftUserAccountInfo(this);
857876
}
877+
858878
async requestWithdrawalFromDriftVault(amount: number, vault: string) {
859879
return await requestWithdrawalFromVault(this, amount, vault);
860880
}
881+
861882
async tradeUsingDelegatedDriftVault(
862883
vault: string,
863884
amount: number,
@@ -876,6 +897,7 @@ export class SolanaAgentKit {
876897
price,
877898
);
878899
}
900+
879901
async tradeUsingDriftPerpAccount(
880902
amount: number,
881903
symbol: string,
@@ -885,6 +907,7 @@ export class SolanaAgentKit {
885907
) {
886908
return await driftPerpTrade(this, { action, amount, symbol, type, price });
887909
}
910+
888911
async updateDriftVault(
889912
vaultAddress: string,
890913
params: {
@@ -901,19 +924,23 @@ export class SolanaAgentKit {
901924
) {
902925
return await updateVault(this, vaultAddress, params);
903926
}
927+
904928
async getDriftVaultInfo(vaultName: string) {
905929
return await getVaultInfo(this, vaultName);
906930
}
931+
907932
async withdrawFromDriftAccount(
908933
amount: number,
909934
symbol: string,
910935
isBorrow?: boolean,
911936
) {
912937
return await withdrawFromDriftUserAccount(this, amount, symbol, isBorrow);
913938
}
939+
914940
async withdrawFromDriftVault(vault: string) {
915941
return await withdrawFromDriftVault(this, vault);
916942
}
943+
917944
async updateDriftVaultDelegate(vaultAddress: string, delegate: string) {
918945
return await updateVaultDelegate(this, vaultAddress, delegate);
919946
}
@@ -931,15 +958,19 @@ export class SolanaAgentKit {
931958
};
932959
}
933960
}
961+
934962
async stakeToDriftInsuranceFund(amount: number, symbol: string) {
935963
return await stakeToDriftInsuranceFund(this, amount, symbol);
936964
}
965+
937966
async requestUnstakeFromDriftInsuranceFund(amount: number, symbol: string) {
938967
return await requestUnstakeFromDriftInsuranceFund(this, amount, symbol);
939968
}
969+
940970
async unstakeFromDriftInsuranceFund(symbol: string) {
941971
return await unstakeFromDriftInsuranceFund(this, symbol);
942972
}
973+
943974
async driftSpotTokenSwap(
944975
params: {
945976
fromSymbol: string;
@@ -962,19 +993,22 @@ export class SolanaAgentKit {
962993
slippage: params.slippage,
963994
});
964995
}
996+
965997
async getPerpMarketFundingRate(
966998
symbol: `${string}-PERP`,
967999
period: "year" | "hour" = "year",
9681000
) {
9691001
return calculatePerpMarketFundingRate(this, symbol, period);
9701002
}
1003+
9711004
async getEntryQuoteOfPerpTrade(
9721005
amount: number,
9731006
symbol: `${string}-PERP`,
9741007
action: "short" | "long",
9751008
) {
9761009
return getEntryQuoteOfPerpTrade(symbol, amount, action);
9771010
}
1011+
9781012
async getLendAndBorrowAPY(symbol: string) {
9791013
return getLendingAndBorrowAPY(this, symbol);
9801014
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { PublicKey } from "@solana/web3.js";
2+
import { Tool } from "langchain/tools";
3+
import { SolanaAgentKit } from "../../agent";
4+
import { BN } from "bn.js";
5+
6+
export class SolanaMeteoraCreateDlmmLpPosition extends Tool {
7+
name = "meteora_create_dlmm_lp_position";
8+
description = `Create a Meteora DLMM LP position.
9+
10+
Inputs (JSON string):
11+
- pool: string, pool address (required).
12+
- totalXAmount: number, total X amount to deposit, e.g., 100 (required).
13+
`;
14+
15+
constructor(private solanaKit: SolanaAgentKit) {
16+
super();
17+
}
18+
19+
async _call(input: string): Promise<string> {
20+
try {
21+
interface CreateMeteoraDlmmLpPositionInput {
22+
pool: string;
23+
totalXAmount: number;
24+
}
25+
26+
const inputFormat: CreateMeteoraDlmmLpPositionInput = JSON.parse(input);
27+
28+
const pool = new PublicKey(inputFormat.pool);
29+
const totalXAmount = new BN(inputFormat.totalXAmount);
30+
31+
const txId = await this.solanaKit.createMeteoraDlmmLpPosition(
32+
pool,
33+
totalXAmount,
34+
);
35+
36+
return JSON.stringify({
37+
status: "success",
38+
message: "Meteora DLMM LP position created successfully.",
39+
transaction: txId,
40+
});
41+
} catch (error: any) {
42+
return JSON.stringify({
43+
status: "error",
44+
message: error.message,
45+
code: error.code || "UNKNOWN_ERROR",
46+
});
47+
}
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Keypair, PublicKey } from "@solana/web3.js";
2+
import DLMM, { StrategyType } from "@meteora-ag/dlmm";
3+
import BN from "bn.js";
4+
import { SolanaAgentKit } from "../../agent";
5+
import { sendTx } from "../../utils/send_tx";
6+
7+
export async function createMeteoraDlmmLpPosition(
8+
agent: SolanaAgentKit,
9+
pool: PublicKey,
10+
totalXAmount: BN,
11+
): Promise<string> {
12+
const dlmmPool = await DLMM.create(agent.connection, pool);
13+
14+
const activeBin = await dlmmPool.getActiveBin();
15+
const activeBinPriceLamport = activeBin.price;
16+
const activeBinPricePerToken = dlmmPool.fromPricePerLamport(
17+
Number(activeBin.price),
18+
);
19+
20+
const newBalancePosition = Keypair.generate();
21+
22+
const TOTAL_RANGE_INTERVAL = 10; // 10 bins on each side of the active bin
23+
const minBinId = activeBin.binId - TOTAL_RANGE_INTERVAL;
24+
const maxBinId = activeBin.binId + TOTAL_RANGE_INTERVAL;
25+
26+
const totalYAmount = totalXAmount.mul(new BN(Number(activeBinPricePerToken)));
27+
28+
const createPositionTx =
29+
await dlmmPool.initializePositionAndAddLiquidityByStrategy({
30+
positionPubKey: newBalancePosition.publicKey,
31+
user: agent.wallet.publicKey,
32+
totalXAmount,
33+
totalYAmount,
34+
strategy: {
35+
maxBinId,
36+
minBinId,
37+
strategyType: StrategyType.SpotBalanced,
38+
},
39+
});
40+
41+
try {
42+
const createBalancePositionTxHash = await sendTx(
43+
agent,
44+
createPositionTx.instructions,
45+
[agent.wallet, newBalancePosition],
46+
);
47+
48+
return createBalancePositionTxHash;
49+
} catch (error) {
50+
console.log("🚀 ~ error:", JSON.parse(JSON.stringify(error)));
51+
throw new Error("Failed to create balance position");
52+
}
53+
}

src/tools/meteora/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./create_meteora_dlmm_pool";
22
export * from "./create_meteora_dynamic_amm_pool";
3+
export * from "./create_meteora_dlmm_lp_position";

0 commit comments

Comments
 (0)