mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-22 15:10:43 +00:00
Merge branch 'main' into combine
This commit is contained in:
68
src/actions/flashCloseTrade.ts
Normal file
68
src/actions/flashCloseTrade.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Action } from "../types/action";
|
||||
import { SolanaAgentKit } from "../agent";
|
||||
import { z } from "zod";
|
||||
import { flashCloseTrade } from "../tools";
|
||||
|
||||
const flashCloseTradeAction: Action = {
|
||||
name: "FLASH_CLOSE_TRADE",
|
||||
similes: [
|
||||
"close trade",
|
||||
"close leveraged trade",
|
||||
"exit position",
|
||||
"close position",
|
||||
"exit trade",
|
||||
"close long",
|
||||
"close short",
|
||||
"take profit",
|
||||
"stop loss",
|
||||
],
|
||||
description:
|
||||
"Close an existing leveraged trading position on Flash.Trade protocol",
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
token: "SOL",
|
||||
side: "long",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
signature: "4xKpN2...",
|
||||
message: "Successfully closed long position on SOL",
|
||||
},
|
||||
explanation: "Close an existing long position on SOL",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
token: z
|
||||
.string()
|
||||
.describe("Token symbol of the position to close (e.g. SOL, ETH)"),
|
||||
side: z
|
||||
.enum(["long", "short"])
|
||||
.describe("Position side to close - long or short"),
|
||||
}),
|
||||
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
|
||||
try {
|
||||
const params = {
|
||||
token: input.token as string,
|
||||
side: input.side as "long" | "short",
|
||||
};
|
||||
|
||||
const response = await flashCloseTrade(agent, params);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
signature: response,
|
||||
message: `Successfully closed ${params.side} position on ${params.token}`,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
status: "error",
|
||||
message: `Flash trade close failed: ${error.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default flashCloseTradeAction;
|
||||
78
src/actions/flashOpenTrade.ts
Normal file
78
src/actions/flashOpenTrade.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { Action } from "../types/action";
|
||||
import { SolanaAgentKit } from "../agent";
|
||||
import { z } from "zod";
|
||||
import { flashOpenTrade } from "../tools";
|
||||
|
||||
const flashOpenTradeAction: Action = {
|
||||
name: "FLASH_OPEN_TRADE",
|
||||
similes: [
|
||||
"open trade",
|
||||
"open leveraged trade",
|
||||
"start trading position",
|
||||
"open position",
|
||||
"long position",
|
||||
"short position",
|
||||
"leverage trade",
|
||||
"margin trade",
|
||||
],
|
||||
description: "Open a leveraged trading position on Flash.Trade protocol",
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
token: "SOL",
|
||||
side: "long",
|
||||
collateralUsd: 100,
|
||||
leverage: 5,
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
signature: "4xKpN2...",
|
||||
message:
|
||||
"Successfully opened 5x long position on SOL with $100 collateral",
|
||||
},
|
||||
explanation:
|
||||
"Open a 5x leveraged long position on SOL using $100 as collateral",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
token: z.string().describe("Token symbol to trade (e.g. SOL, ETH)"),
|
||||
side: z
|
||||
.enum(["long", "short"])
|
||||
.describe("Trading direction - long or short"),
|
||||
collateralUsd: z
|
||||
.number()
|
||||
.positive()
|
||||
.describe("Amount of collateral in USD"),
|
||||
leverage: z
|
||||
.number()
|
||||
.positive()
|
||||
.describe("Leverage multiplier for the trade"),
|
||||
}),
|
||||
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
|
||||
try {
|
||||
const params = {
|
||||
token: input.token as string,
|
||||
side: input.side as "long" | "short",
|
||||
collateralUsd: input.collateralUsd as number,
|
||||
leverage: input.leverage as number,
|
||||
};
|
||||
|
||||
const response = await flashOpenTrade(agent, params);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
signature: response,
|
||||
message: `Successfully opened ${params.leverage}x ${params.side} position on ${params.token} with $${params.collateralUsd} collateral`,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
status: "error",
|
||||
message: `Flash trade failed: ${error.message}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default flashOpenTradeAction;
|
||||
@@ -28,6 +28,8 @@ import raydiumCreateAmmV4Action from "./raydiumCreateAmmV4";
|
||||
import createOrcaSingleSidedWhirlpoolAction from "./createOrcaSingleSidedWhirlpool";
|
||||
import launchPumpfunTokenAction from "./launchPumpfunToken";
|
||||
import getWalletAddressAction from "./getWalletAddress";
|
||||
import flashOpenTradeAction from "./flashOpenTrade";
|
||||
import flashCloseTradeAction from "./flashCloseTrade";
|
||||
|
||||
export const ACTIONS = {
|
||||
WALLET_ADDRESS_ACTION: getWalletAddressAction,
|
||||
@@ -61,6 +63,8 @@ export const ACTIONS = {
|
||||
CREATE_ORCA_SINGLE_SIDED_WHIRLPOOL_ACTION:
|
||||
createOrcaSingleSidedWhirlpoolAction,
|
||||
LAUNCH_PUMPFUN_TOKEN_ACTION: launchPumpfunTokenAction,
|
||||
FLASH_OPEN_TRADE_ACTION: flashOpenTradeAction,
|
||||
FLASH_CLOSE_TRADE_ACTION: flashCloseTradeAction,
|
||||
};
|
||||
|
||||
export type { Action, ActionExample, Handler } from "../types/action";
|
||||
|
||||
@@ -60,6 +60,8 @@ import {
|
||||
fetchTokenDetailedReport,
|
||||
fetchPythPrice,
|
||||
fetchPythPriceFeedID,
|
||||
flashOpenTrade,
|
||||
flashCloseTrade,
|
||||
} from "../tools";
|
||||
import {
|
||||
CollectionDeployment,
|
||||
@@ -70,6 +72,8 @@ import {
|
||||
PumpfunLaunchResponse,
|
||||
PumpFunTokenOptions,
|
||||
OrderParams,
|
||||
FlashTradeParams,
|
||||
FlashCloseTradeParams,
|
||||
} from "../types";
|
||||
|
||||
/**
|
||||
@@ -541,4 +545,22 @@ export class SolanaAgentKit {
|
||||
async fetchTokenDetailedReport(mint: string): Promise<TokenCheck> {
|
||||
return fetchTokenDetailedReport(mint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a new trading position on Flash.Trade
|
||||
* @param params Flash trade parameters including market, side, collateral, leverage, and pool name
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
async flashOpenTrade(params: FlashTradeParams): Promise<string> {
|
||||
return flashOpenTrade(this, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes an existing trading position on Flash.Trade
|
||||
* @param params Flash trade close parameters
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
async flashCloseTrade(params: FlashCloseTradeParams): Promise<string> {
|
||||
return flashCloseTrade(this, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -774,6 +774,131 @@ export class SolanaGetWalletAddressTool extends Tool {
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaFlashOpenTrade extends Tool {
|
||||
name = "solana_flash_open_trade";
|
||||
description = `This tool can be used to open a new leveraged trading position on Flash.Trade exchange.
|
||||
|
||||
Inputs ( input is a JSON string ):
|
||||
token: string, eg "SOL", "BTC", "ETH" (required)
|
||||
type: string, eg "long", "short" (required)
|
||||
collateral: number, eg 10, 100, 1000 (required)
|
||||
leverage: number, eg 5, 10, 20 (required)
|
||||
|
||||
Example prompt is Open a 20x leveraged trade for SOL on long side using flash trade with 500 USD as collateral`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
// Validate input parameters
|
||||
if (!parsedInput.token) {
|
||||
throw new Error("Token is required, received: " + parsedInput.token);
|
||||
}
|
||||
if (!["SOL", "BTC", "ETH", "USDC"].includes(parsedInput.token)) {
|
||||
throw new Error(
|
||||
'Token must be one of ["SOL", "BTC", "ETH", "USDC"], received: ' +
|
||||
parsedInput.token,
|
||||
);
|
||||
}
|
||||
if (!["long", "short"].includes(parsedInput.type)) {
|
||||
throw new Error(
|
||||
'Type must be either "long" or "short", received: ' +
|
||||
parsedInput.type,
|
||||
);
|
||||
}
|
||||
if (!parsedInput.collateral || parsedInput.collateral <= 0) {
|
||||
throw new Error(
|
||||
"Collateral amount must be positive, received: " +
|
||||
parsedInput.collateral,
|
||||
);
|
||||
}
|
||||
if (!parsedInput.leverage || parsedInput.leverage <= 0) {
|
||||
throw new Error(
|
||||
"Leverage must be positive, received: " + parsedInput.leverage,
|
||||
);
|
||||
}
|
||||
|
||||
const tx = await this.solanaKit.flashOpenTrade({
|
||||
token: parsedInput.token,
|
||||
side: parsedInput.type,
|
||||
collateralUsd: parsedInput.collateral,
|
||||
leverage: parsedInput.leverage,
|
||||
});
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Flash trade position opened successfully",
|
||||
transaction: tx,
|
||||
token: parsedInput.token,
|
||||
side: parsedInput.type,
|
||||
collateral: parsedInput.collateral,
|
||||
leverage: parsedInput.leverage,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaFlashCloseTrade extends Tool {
|
||||
name = "solana_flash_close_trade";
|
||||
description = `Close an existing leveraged trading position on Flash.Trade exchange.
|
||||
|
||||
Inputs ( input is a JSON string ):
|
||||
token: string, eg "SOL", "BTC", "ETH" (required)
|
||||
side: string, eg "long", "short" (required)
|
||||
|
||||
Example prompt is Close a 20x leveraged trade for SOL on long side`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
// Validate input parameters
|
||||
if (!parsedInput.token) {
|
||||
throw new Error("Token is required");
|
||||
}
|
||||
if (!["SOL", "BTC", "ETH"].includes(parsedInput.token)) {
|
||||
throw new Error('Token must be one of ["SOL", "BTC", "ETH"]');
|
||||
}
|
||||
if (!["long", "short"].includes(parsedInput.side)) {
|
||||
throw new Error('Side must be either "long" or "short"');
|
||||
}
|
||||
|
||||
const tx = await this.solanaKit.flashCloseTrade({
|
||||
token: parsedInput.token,
|
||||
side: parsedInput.side,
|
||||
});
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Flash trade position closed successfully",
|
||||
transaction: tx,
|
||||
token: parsedInput.token,
|
||||
side: parsedInput.side,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaPumpfunTokenLaunchTool extends Tool {
|
||||
name = "solana_launch_pumpfun_token";
|
||||
|
||||
@@ -2179,5 +2304,7 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaFetchTokenDetailedReportTool(solanaKit),
|
||||
new SolanaPerpOpenTradeTool(solanaKit),
|
||||
new SolanaPerpCloseTradeTool(solanaKit),
|
||||
new SolanaFlashOpenTrade(solanaKit),
|
||||
new SolanaFlashCloseTrade(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
117
src/tools/flash_close_trade.ts
Normal file
117
src/tools/flash_close_trade.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { ComputeBudgetProgram } from "@solana/web3.js";
|
||||
import { PoolConfig, Privilege, Side } from "flash-sdk";
|
||||
import { BN } from "@coral-xyz/anchor";
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import {
|
||||
CLOSE_POSITION_CU,
|
||||
marketSdkInfo,
|
||||
marketTokenMap,
|
||||
getNftTradingAccountInfo,
|
||||
fetchOraclePrice,
|
||||
createPerpClient,
|
||||
} from "../utils/flashUtils";
|
||||
import { FlashCloseTradeParams } from "../types";
|
||||
|
||||
/**
|
||||
* Closes an existing position on Flash.Trade
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param params Trade parameters
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
export async function flashCloseTrade(
|
||||
agent: SolanaAgentKit,
|
||||
params: FlashCloseTradeParams,
|
||||
): Promise<string> {
|
||||
try {
|
||||
const { token, side } = params;
|
||||
|
||||
// Get market ID from token and side using marketTokenMap
|
||||
const tokenMarkets = marketTokenMap[token];
|
||||
if (!tokenMarkets) {
|
||||
throw new Error(`Token ${token} not supported for trading`);
|
||||
}
|
||||
|
||||
const sideEntry = tokenMarkets[side];
|
||||
if (!sideEntry) {
|
||||
throw new Error(`${side} side not available for ${token}`);
|
||||
}
|
||||
|
||||
const market = sideEntry.marketID;
|
||||
|
||||
// Validate market data using marketSdkInfo
|
||||
const marketData = marketSdkInfo[market];
|
||||
if (!marketData) {
|
||||
throw new Error(`Invalid market configuration for ${token}/${side}`);
|
||||
}
|
||||
|
||||
// Get token information
|
||||
const [targetSymbol, collateralSymbol] = marketData.tokenPair.split("/");
|
||||
|
||||
// Fetch oracle prices
|
||||
const [targetPrice] = await Promise.all([
|
||||
fetchOraclePrice(targetSymbol),
|
||||
fetchOraclePrice(collateralSymbol),
|
||||
]);
|
||||
|
||||
// Initialize pool configuration and perpClient
|
||||
const poolConfig = PoolConfig.fromIdsByName(
|
||||
marketData.pool,
|
||||
"mainnet-beta",
|
||||
);
|
||||
const perpClient = createPerpClient(agent.connection, agent.wallet);
|
||||
|
||||
// Calculate price after slippage
|
||||
const slippageBpsBN = new BN(100); // 1% slippage
|
||||
const sideEnum = side === "long" ? Side.Long : Side.Short;
|
||||
const priceWithSlippage = perpClient.getPriceAfterSlippage(
|
||||
false, // isEntry = false for closing position
|
||||
slippageBpsBN,
|
||||
targetPrice.price,
|
||||
sideEnum,
|
||||
);
|
||||
|
||||
// Get NFT trading account info
|
||||
const tradingAccounts = await getNftTradingAccountInfo(
|
||||
agent.wallet_address,
|
||||
perpClient,
|
||||
poolConfig,
|
||||
collateralSymbol,
|
||||
);
|
||||
|
||||
if (
|
||||
!tradingAccounts.nftTradingAccountPk ||
|
||||
!tradingAccounts.nftReferralAccountPK ||
|
||||
!tradingAccounts.nftOwnerRebateTokenAccountPk
|
||||
) {
|
||||
throw new Error("Required NFT trading accounts not found");
|
||||
}
|
||||
|
||||
// Build and send transaction
|
||||
const { instructions, additionalSigners } = await perpClient.closePosition(
|
||||
targetSymbol,
|
||||
collateralSymbol,
|
||||
priceWithSlippage,
|
||||
sideEnum,
|
||||
poolConfig,
|
||||
Privilege.Referral,
|
||||
tradingAccounts.nftTradingAccountPk,
|
||||
tradingAccounts.nftReferralAccountPK,
|
||||
tradingAccounts.nftOwnerRebateTokenAccountPk,
|
||||
);
|
||||
|
||||
const computeBudgetIx = ComputeBudgetProgram.setComputeUnitLimit({
|
||||
units: CLOSE_POSITION_CU,
|
||||
});
|
||||
|
||||
return await perpClient.sendTransaction(
|
||||
[computeBudgetIx, ...instructions],
|
||||
{
|
||||
additionalSigners: additionalSigners,
|
||||
alts: perpClient.addressLookupTables,
|
||||
prioritizationFee: 5000000,
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error(`Flash trade close failed: ${error}`);
|
||||
}
|
||||
}
|
||||
251
src/tools/flash_open_trade.ts
Normal file
251
src/tools/flash_open_trade.ts
Normal file
@@ -0,0 +1,251 @@
|
||||
import { ComputeBudgetProgram } from "@solana/web3.js";
|
||||
import {
|
||||
PerpetualsClient,
|
||||
OraclePrice,
|
||||
PoolConfig,
|
||||
Privilege,
|
||||
Side,
|
||||
CustodyAccount,
|
||||
Custody,
|
||||
} from "flash-sdk";
|
||||
import { BN } from "@coral-xyz/anchor";
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import {
|
||||
ALL_TOKENS,
|
||||
marketSdkInfo,
|
||||
marketTokenMap,
|
||||
getNftTradingAccountInfo,
|
||||
OPEN_POSITION_CU,
|
||||
fetchOraclePrice,
|
||||
createPerpClient,
|
||||
} from "../utils/flashUtils";
|
||||
import { FlashTradeParams } from "../types";
|
||||
|
||||
/**
|
||||
* Opens a new position on Flash.Trade
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param params Trade parameters
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
export async function flashOpenTrade(
|
||||
agent: SolanaAgentKit,
|
||||
params: FlashTradeParams,
|
||||
): Promise<string> {
|
||||
try {
|
||||
const { token, side, collateralUsd, leverage } = params;
|
||||
|
||||
// Get market ID from token and side using marketTokenMap
|
||||
const tokenMarkets = marketTokenMap[token];
|
||||
if (!tokenMarkets) {
|
||||
throw new Error(`Token ${token} not supported for trading`);
|
||||
}
|
||||
|
||||
const sideEntry = tokenMarkets[side];
|
||||
if (!sideEntry) {
|
||||
throw new Error(`${side} side not available for ${token}`);
|
||||
}
|
||||
|
||||
const market = sideEntry.marketID;
|
||||
|
||||
// Validate market data using marketSdkInfo
|
||||
const marketData = marketSdkInfo[market];
|
||||
if (!marketData) {
|
||||
throw new Error(`Invalid market configuration for ${token}/${side}`);
|
||||
}
|
||||
|
||||
// Get token information
|
||||
const [targetSymbol, collateralSymbol] = marketData.tokenPair.split("/");
|
||||
const targetToken = ALL_TOKENS.find((t) => t.symbol === targetSymbol);
|
||||
const collateralToken = ALL_TOKENS.find(
|
||||
(t) => t.symbol === collateralSymbol,
|
||||
);
|
||||
|
||||
if (!targetToken || !collateralToken) {
|
||||
throw new Error(`Token not found for pair ${marketData.tokenPair}`);
|
||||
}
|
||||
|
||||
// Fetch oracle prices
|
||||
const [targetPrice, collateralPrice] = await Promise.all([
|
||||
fetchOraclePrice(targetSymbol),
|
||||
fetchOraclePrice(collateralSymbol),
|
||||
]);
|
||||
|
||||
// Initialize pool configuration and perpClient
|
||||
const poolConfig = PoolConfig.fromIdsByName(
|
||||
marketData.pool,
|
||||
"mainnet-beta",
|
||||
);
|
||||
const perpClient = createPerpClient(agent.connection, agent.wallet);
|
||||
|
||||
// Calculate position parameters
|
||||
const leverageBN = new BN(leverage);
|
||||
const collateralTokenPrice = convertPriceToNumber(collateralPrice.price);
|
||||
const collateralAmount = calculateCollateralAmount(
|
||||
collateralUsd,
|
||||
collateralTokenPrice,
|
||||
collateralToken.decimals,
|
||||
);
|
||||
|
||||
// Get custody accounts
|
||||
const { targetCustody, collateralCustody } = await fetchCustodyAccounts(
|
||||
perpClient,
|
||||
poolConfig,
|
||||
targetSymbol,
|
||||
collateralSymbol,
|
||||
);
|
||||
|
||||
// Calculate position size
|
||||
const positionSize = calculatePositionSize(
|
||||
perpClient,
|
||||
collateralAmount,
|
||||
leverageBN,
|
||||
targetToken,
|
||||
collateralToken,
|
||||
side,
|
||||
targetPrice.price,
|
||||
collateralPrice.price,
|
||||
targetCustody,
|
||||
collateralCustody,
|
||||
);
|
||||
|
||||
// Get NFT trading account info
|
||||
const tradingAccounts = await getNftTradingAccountInfo(
|
||||
agent.wallet_address,
|
||||
perpClient,
|
||||
poolConfig,
|
||||
collateralSymbol,
|
||||
);
|
||||
|
||||
if (
|
||||
!tradingAccounts.nftTradingAccountPk ||
|
||||
!tradingAccounts.nftReferralAccountPK
|
||||
) {
|
||||
throw new Error("Required NFT trading accounts not found");
|
||||
}
|
||||
|
||||
// Prepare transaction
|
||||
const slippageBps = new BN(1000);
|
||||
const priceWithSlippage = perpClient.getPriceAfterSlippage(
|
||||
true,
|
||||
slippageBps,
|
||||
targetPrice.price,
|
||||
side === "long" ? Side.Long : Side.Short,
|
||||
);
|
||||
|
||||
// Build and send transaction
|
||||
const { instructions, additionalSigners } = await perpClient.openPosition(
|
||||
targetSymbol,
|
||||
collateralSymbol,
|
||||
priceWithSlippage,
|
||||
collateralAmount,
|
||||
positionSize,
|
||||
side === "long" ? Side.Long : Side.Short,
|
||||
poolConfig,
|
||||
Privilege.Referral,
|
||||
tradingAccounts.nftTradingAccountPk,
|
||||
tradingAccounts.nftReferralAccountPK,
|
||||
tradingAccounts.nftOwnerRebateTokenAccountPk!,
|
||||
false,
|
||||
);
|
||||
|
||||
const computeBudgetIx = ComputeBudgetProgram.setComputeUnitLimit({
|
||||
units: OPEN_POSITION_CU,
|
||||
});
|
||||
|
||||
return await perpClient.sendTransaction(
|
||||
[computeBudgetIx, ...instructions],
|
||||
{
|
||||
additionalSigners: additionalSigners,
|
||||
alts: perpClient.addressLookupTables,
|
||||
prioritizationFee: 5000000,
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error(`Flash trade failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
function convertPriceToNumber(oraclePrice: OraclePrice): number {
|
||||
const price = parseInt(oraclePrice.price.toString("hex"), 16);
|
||||
const exponent = parseInt(oraclePrice.exponent.toString("hex"), 16);
|
||||
return price * Math.pow(10, exponent);
|
||||
}
|
||||
|
||||
function calculateCollateralAmount(
|
||||
usdAmount: number,
|
||||
tokenPrice: number,
|
||||
decimals: number,
|
||||
): BN {
|
||||
return new BN((usdAmount / tokenPrice) * Math.pow(10, decimals));
|
||||
}
|
||||
|
||||
async function fetchCustodyAccounts(
|
||||
perpClient: PerpetualsClient,
|
||||
poolConfig: PoolConfig,
|
||||
targetSymbol: string,
|
||||
collateralSymbol: string,
|
||||
) {
|
||||
const targetConfig = poolConfig.custodies.find(
|
||||
(c) => c.symbol === targetSymbol,
|
||||
);
|
||||
const collateralConfig = poolConfig.custodies.find(
|
||||
(c) => c.symbol === collateralSymbol,
|
||||
);
|
||||
|
||||
if (!targetConfig || !collateralConfig) {
|
||||
throw new Error("Custody configuration not found");
|
||||
}
|
||||
|
||||
const accounts = await perpClient.provider.connection.getMultipleAccountsInfo(
|
||||
[targetConfig.custodyAccount, collateralConfig.custodyAccount],
|
||||
);
|
||||
|
||||
if (!accounts[0] || !accounts[1]) {
|
||||
throw new Error("Failed to fetch custody accounts");
|
||||
}
|
||||
|
||||
return {
|
||||
targetCustody: CustodyAccount.from(
|
||||
targetConfig.custodyAccount,
|
||||
perpClient.program.coder.accounts.decode<Custody>(
|
||||
"custody",
|
||||
accounts[0].data,
|
||||
),
|
||||
),
|
||||
collateralCustody: CustodyAccount.from(
|
||||
collateralConfig.custodyAccount,
|
||||
perpClient.program.coder.accounts.decode<Custody>(
|
||||
"custody",
|
||||
accounts[1].data,
|
||||
),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
function calculatePositionSize(
|
||||
perpClient: PerpetualsClient,
|
||||
collateralAmount: BN,
|
||||
leverage: BN,
|
||||
targetToken: any,
|
||||
collateralToken: any,
|
||||
side: "long" | "short",
|
||||
targetPrice: OraclePrice,
|
||||
collateralPrice: OraclePrice,
|
||||
targetCustody: CustodyAccount,
|
||||
collateralCustody: CustodyAccount,
|
||||
): BN {
|
||||
return perpClient.getSizeAmountFromLeverageAndCollateral(
|
||||
collateralAmount,
|
||||
leverage.toString(),
|
||||
targetToken,
|
||||
collateralToken,
|
||||
side === "long" ? Side.Long : Side.Short,
|
||||
targetPrice,
|
||||
targetPrice,
|
||||
targetCustody,
|
||||
collateralPrice,
|
||||
collateralPrice,
|
||||
collateralCustody,
|
||||
);
|
||||
}
|
||||
@@ -43,3 +43,5 @@ export * from "./stake_with_solayer";
|
||||
export * from "./tensor_trade";
|
||||
export * from "./trade";
|
||||
export * from "./transfer";
|
||||
export * from "./flash_open_trade";
|
||||
export * from "./flash_close_trade";
|
||||
|
||||
@@ -224,3 +224,15 @@ export interface BatchOrderPattern {
|
||||
numberOfOrders?: number;
|
||||
individualQuantity?: number;
|
||||
}
|
||||
|
||||
export interface FlashTradeParams {
|
||||
token: string;
|
||||
side: "long" | "short";
|
||||
collateralUsd: number;
|
||||
leverage: number;
|
||||
}
|
||||
|
||||
export interface FlashCloseTradeParams {
|
||||
token: string;
|
||||
side: "long" | "short";
|
||||
}
|
||||
|
||||
280
src/utils/flashUtils.ts
Normal file
280
src/utils/flashUtils.ts
Normal file
@@ -0,0 +1,280 @@
|
||||
import { HermesClient } from "@pythnetwork/hermes-client";
|
||||
import { OraclePrice } from "flash-sdk";
|
||||
import { AnchorProvider, BN, Wallet } from "@coral-xyz/anchor";
|
||||
import { PoolConfig, Token, Referral, PerpetualsClient } from "flash-sdk";
|
||||
import { Cluster, PublicKey, Connection, Keypair } from "@solana/web3.js";
|
||||
import { getAssociatedTokenAddressSync } from "@solana/spl-token";
|
||||
|
||||
const POOL_NAMES = [
|
||||
"Crypto.1",
|
||||
"Virtual.1",
|
||||
"Governance.1",
|
||||
"Community.1",
|
||||
"Community.2",
|
||||
"Community.3",
|
||||
];
|
||||
|
||||
const DEFAULT_CLUSTER: Cluster = "mainnet-beta";
|
||||
export const POOL_CONFIGS = POOL_NAMES.map((f) =>
|
||||
PoolConfig.fromIdsByName(f, DEFAULT_CLUSTER),
|
||||
);
|
||||
|
||||
const DUPLICATE_TOKENS = POOL_CONFIGS.map((f) => f.tokens).flat();
|
||||
const tokenMap = new Map();
|
||||
for (const token of DUPLICATE_TOKENS) {
|
||||
tokenMap.set(token.symbol, token);
|
||||
}
|
||||
export const ALL_TOKENS: Token[] = Array.from(tokenMap.values());
|
||||
export const ALL_CUSTODIES = POOL_CONFIGS.map((f) => f.custodies).flat();
|
||||
const PROGRAM_ID = POOL_CONFIGS[0].programId;
|
||||
|
||||
// CU for trade instructions
|
||||
export const OPEN_POSITION_CU = 150_000;
|
||||
export const CLOSE_POSITION_CU = 180_000;
|
||||
|
||||
const HERMES_URL = "https://hermes.pyth.network"; // Replace with the actual Hermes URL if different
|
||||
|
||||
// Create a map of symbol to Pyth price ID
|
||||
const PRICE_FEED_IDS = ALL_TOKENS.reduce(
|
||||
(acc, token) => {
|
||||
acc[token.symbol] = token.pythPriceId;
|
||||
return acc;
|
||||
},
|
||||
{} as { [key: string]: string },
|
||||
);
|
||||
|
||||
const hermesClient = new HermesClient(HERMES_URL, {});
|
||||
|
||||
export interface PythPriceEntry {
|
||||
price: OraclePrice;
|
||||
emaPrice: OraclePrice;
|
||||
isStale: boolean;
|
||||
status: PriceStatus;
|
||||
}
|
||||
|
||||
export enum PriceStatus {
|
||||
Trading,
|
||||
Unknown,
|
||||
Halted,
|
||||
Auction,
|
||||
}
|
||||
|
||||
export const fetchOraclePrice = async (
|
||||
symbol: string,
|
||||
): Promise<PythPriceEntry> => {
|
||||
const priceFeedId = PRICE_FEED_IDS[symbol];
|
||||
if (!priceFeedId) {
|
||||
throw new Error(`Price feed ID not found for symbol: ${symbol}`);
|
||||
}
|
||||
|
||||
try {
|
||||
const hermesPriceFeed = await hermesClient.getPriceFeeds({
|
||||
query: symbol,
|
||||
filter: "crypto",
|
||||
});
|
||||
|
||||
if (!hermesPriceFeed || hermesPriceFeed.length === 0) {
|
||||
throw new Error(`No price feed received for ${symbol}`);
|
||||
}
|
||||
|
||||
const hemrmesPriceUdpate = await hermesClient.getLatestPriceUpdates(
|
||||
[priceFeedId],
|
||||
{
|
||||
encoding: "hex",
|
||||
parsed: true,
|
||||
},
|
||||
);
|
||||
|
||||
if (!hemrmesPriceUdpate.parsed) {
|
||||
throw new Error(`No price feed received for ${symbol}`);
|
||||
}
|
||||
const hermesEma = hemrmesPriceUdpate.parsed[0].ema_price;
|
||||
const hermesPrice = hemrmesPriceUdpate.parsed[0].price;
|
||||
|
||||
const hermesPriceOracle = new OraclePrice({
|
||||
price: new BN(hermesPrice.price),
|
||||
exponent: new BN(hermesPrice.expo),
|
||||
confidence: new BN(hermesPrice.conf),
|
||||
timestamp: new BN(hermesPrice.publish_time),
|
||||
});
|
||||
|
||||
const hermesEmaOracle = new OraclePrice({
|
||||
price: new BN(hermesEma.price),
|
||||
exponent: new BN(hermesEma.expo),
|
||||
confidence: new BN(hermesEma.conf),
|
||||
timestamp: new BN(hermesEma.publish_time),
|
||||
});
|
||||
|
||||
const token = ALL_TOKENS.find((t) => t.pythPriceId === priceFeedId);
|
||||
if (!token) {
|
||||
throw new Error(`Token not found for price feed ID: ${priceFeedId}`);
|
||||
}
|
||||
|
||||
const status = !token.isVirtual ? PriceStatus.Trading : PriceStatus.Unknown;
|
||||
|
||||
const pythPriceEntry: PythPriceEntry = {
|
||||
price: hermesPriceOracle,
|
||||
emaPrice: hermesEmaOracle,
|
||||
isStale: false,
|
||||
status: status,
|
||||
};
|
||||
|
||||
return pythPriceEntry;
|
||||
} catch (error) {
|
||||
console.error(`Error in fetchOraclePrice for ${symbol}:`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export interface MarketInfo {
|
||||
[key: string]: {
|
||||
tokenPair: string;
|
||||
token: string;
|
||||
side: string;
|
||||
pool: string;
|
||||
};
|
||||
}
|
||||
|
||||
const marketSdkInfo: MarketInfo = {};
|
||||
|
||||
// Loop through POOL_CONFIGS to process each market
|
||||
POOL_CONFIGS.forEach((poolConfig) => {
|
||||
poolConfig.markets.forEach((market) => {
|
||||
const targetToken = ALL_TOKENS.find(
|
||||
(token) => token.mintKey.toString() === market.targetMint.toString(),
|
||||
);
|
||||
|
||||
// Find collateral token by matching mintKey
|
||||
const collateralToken = ALL_TOKENS.find(
|
||||
(token) => token.mintKey.toString() === market.collateralMint.toString(),
|
||||
);
|
||||
|
||||
if (targetToken?.symbol && collateralToken?.symbol) {
|
||||
marketSdkInfo[market.marketAccount.toString()] = {
|
||||
tokenPair: `${targetToken.symbol}/${collateralToken.symbol}`,
|
||||
token: targetToken.symbol,
|
||||
side: Object.keys(market.side)[0],
|
||||
pool: poolConfig.poolName,
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export { marketSdkInfo };
|
||||
|
||||
export interface MarketTokenSides {
|
||||
[token: string]: {
|
||||
long?: { marketID: string };
|
||||
short?: { marketID: string };
|
||||
};
|
||||
}
|
||||
|
||||
const marketTokenMap: MarketTokenSides = {};
|
||||
|
||||
// Convert marketSdkInfo into marketTokenMap
|
||||
Object.entries(marketSdkInfo).forEach(([marketID, info]) => {
|
||||
if (!marketTokenMap[info.token]) {
|
||||
marketTokenMap[info.token] = {};
|
||||
}
|
||||
|
||||
marketTokenMap[info.token][info.side.toLowerCase() as "long" | "short"] = {
|
||||
marketID,
|
||||
};
|
||||
});
|
||||
|
||||
export { marketTokenMap };
|
||||
|
||||
interface TradingAccountResult {
|
||||
nftReferralAccountPK: PublicKey | null;
|
||||
nftTradingAccountPk: PublicKey | null;
|
||||
nftOwnerRebateTokenAccountPk: PublicKey | null;
|
||||
}
|
||||
|
||||
export async function getNftTradingAccountInfo(
|
||||
userPublicKey: PublicKey,
|
||||
perpClient: PerpetualsClient,
|
||||
poolConfig: PoolConfig,
|
||||
collateralCustodySymbol: string,
|
||||
): Promise<TradingAccountResult> {
|
||||
const getNFTReferralAccountPK = (publicKey: PublicKey) => {
|
||||
return PublicKey.findProgramAddressSync(
|
||||
[Buffer.from("referral"), publicKey.toBuffer()],
|
||||
PROGRAM_ID,
|
||||
)[0];
|
||||
};
|
||||
const nftReferralAccountPK = getNFTReferralAccountPK(userPublicKey);
|
||||
const nftReferralAccountInfo =
|
||||
await perpClient.provider.connection.getAccountInfo(nftReferralAccountPK);
|
||||
|
||||
let nftTradingAccountPk: PublicKey | null = null;
|
||||
let nftOwnerRebateTokenAccountPk: PublicKey | null = null;
|
||||
|
||||
if (nftReferralAccountInfo) {
|
||||
const nftReferralAccountData = perpClient.program.coder.accounts.decode(
|
||||
"referral",
|
||||
nftReferralAccountInfo.data,
|
||||
) as Referral;
|
||||
|
||||
nftTradingAccountPk = nftReferralAccountData.refererTradingAccount;
|
||||
|
||||
if (nftTradingAccountPk) {
|
||||
const nftTradingAccountInfo =
|
||||
await perpClient.provider.connection.getAccountInfo(
|
||||
nftTradingAccountPk,
|
||||
);
|
||||
if (nftTradingAccountInfo) {
|
||||
const nftTradingAccount = perpClient.program.coder.accounts.decode(
|
||||
"trading",
|
||||
nftTradingAccountInfo.data,
|
||||
) as { owner: PublicKey };
|
||||
|
||||
nftOwnerRebateTokenAccountPk = getAssociatedTokenAddressSync(
|
||||
poolConfig.getTokenFromSymbol(collateralCustodySymbol).mintKey,
|
||||
nftTradingAccount.owner,
|
||||
);
|
||||
// Check if the account exists
|
||||
const accountExists =
|
||||
await perpClient.provider.connection.getAccountInfo(
|
||||
nftOwnerRebateTokenAccountPk,
|
||||
);
|
||||
if (!accountExists) {
|
||||
console.error(
|
||||
"NFT owner rebate token account does not exist and may need to be created",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
nftReferralAccountPK,
|
||||
nftTradingAccountPk,
|
||||
nftOwnerRebateTokenAccountPk,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PerpetualsClient instance with the given connection and wallet
|
||||
* @param connection Solana connection
|
||||
* @param wallet Solana wallet
|
||||
* @returns PerpetualsClient instance
|
||||
*/
|
||||
export function createPerpClient(
|
||||
connection: Connection,
|
||||
wallet: Keypair,
|
||||
): PerpetualsClient {
|
||||
const provider = new AnchorProvider(connection, new Wallet(wallet), {
|
||||
commitment: "confirmed",
|
||||
preflightCommitment: "confirmed",
|
||||
skipPreflight: true,
|
||||
});
|
||||
|
||||
return new PerpetualsClient(
|
||||
provider,
|
||||
POOL_CONFIGS[0].programId,
|
||||
POOL_CONFIGS[0].perpComposibilityProgramId,
|
||||
POOL_CONFIGS[0].fbNftRewardProgramId,
|
||||
POOL_CONFIGS[0].rewardDistributionProgram.programId,
|
||||
{},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user