Merge branch 'main' into feature/totalbalance

This commit is contained in:
Michael Essiet
2025-01-07 09:12:32 +01:00
committed by GitHub
83 changed files with 32124 additions and 819 deletions

View File

@@ -0,0 +1,506 @@
import {
PublicKey,
SystemProgram,
TransactionInstruction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { TOKENS, DEFAULT_OPTIONS } from "../constants";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { BN } from "@coral-xyz/anchor";
import AdrenaClient from "../utils/AdrenaClient";
import { sendTx } from "../utils/send_tx";
const PRICE_DECIMALS = 10;
const ADRENA_PROGRAM_ID = new PublicKey(
"13gDzEXCdocbj8iAiqrScGo47NiSuYENGsRqi3SEAwet",
);
// i.e percentage = -2 (for -2%)
// i.e percentage = 5 (for 5%)
function applySlippage(nb: BN, percentage: number): BN {
const negative = percentage < 0 ? true : false;
// Do x10_000 so percentage can be up to 4 decimals
const percentageBN = new BN(
(negative ? percentage * -1 : percentage) * 10_000,
);
const delta = nb.mul(percentageBN).divRound(new BN(10_000 * 100));
return negative ? nb.sub(delta) : nb.add(delta);
}
/**
* Close short trade on Adrena
* @returns Transaction signature
*/
export async function closePerpTradeShort({
agent,
price,
tradeMint,
}: {
agent: SolanaAgentKit;
price: number;
tradeMint: PublicKey;
}) {
const client = await AdrenaClient.load(agent);
const owner = agent.wallet.publicKey;
const custody = client.getCustodyByMint(tradeMint);
const collateralCustody = client.getCustodyByMint(TOKENS.USDC);
const stakingRewardTokenCustodyAccount = client.getCustodyByMint(
AdrenaClient.stakingRewardTokenMint,
);
const stakingRewardTokenCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(
AdrenaClient.stakingRewardTokenMint,
);
const position = AdrenaClient.findPositionAddress(
owner,
custody.pubkey,
"long",
);
const userProfilePda = AdrenaClient.getUserProfilePda(owner);
const userProfile =
await client.program.account.userProfile.fetchNullable(userProfilePda);
const receivingAccount = AdrenaClient.findATAAddressSync(
owner,
collateralCustody.mint,
);
const preInstructions: TransactionInstruction[] = [];
const collateralCustodyOracle = collateralCustody.oracle;
const collateralCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(collateralCustody.mint);
if (
!(await AdrenaClient.isAccountInitialized(
agent.connection,
receivingAccount,
))
) {
preInstructions.push(
AdrenaClient.createATAInstruction({
ataAddress: receivingAccount,
mint: collateralCustody.mint,
owner,
}),
);
}
const instruction = await client.program.methods
.closePositionShort({
price: new BN(price * 10 ** PRICE_DECIMALS),
})
.accountsStrict({
owner,
receivingAccount,
transferAuthority: AdrenaClient.transferAuthority,
pool: AdrenaClient.mainPool,
position: position,
custody: custody.pubkey,
custodyTradeOracle: custody.tradeOracle,
tokenProgram: TOKEN_PROGRAM_ID,
lmStaking: AdrenaClient.lmStaking,
lpStaking: AdrenaClient.lpStaking,
cortex: AdrenaClient.cortex,
stakingRewardTokenCustody: stakingRewardTokenCustodyAccount.pubkey,
stakingRewardTokenCustodyOracle: stakingRewardTokenCustodyAccount.oracle,
stakingRewardTokenCustodyTokenAccount,
lmStakingRewardTokenVault: AdrenaClient.lmStakingRewardTokenVault,
lpStakingRewardTokenVault: AdrenaClient.lpStakingRewardTokenVault,
lpTokenMint: AdrenaClient.lpTokenMint,
protocolFeeRecipient: client.cortex.protocolFeeRecipient,
adrenaProgram: AdrenaClient.programId,
userProfile: userProfile ? userProfilePda : null,
caller: owner,
collateralCustody: collateralCustody.pubkey,
collateralCustodyOracle,
collateralCustodyTokenAccount,
})
.instruction();
return sendTx(agent, [...preInstructions, instruction]);
}
/**
* Close long trade on Adrena
* @returns Transaction signature
*/
export async function closePerpTradeLong({
agent,
price,
tradeMint,
}: {
agent: SolanaAgentKit;
price: number;
tradeMint: PublicKey;
}) {
const client = await AdrenaClient.load(agent);
const owner = agent.wallet.publicKey;
const custody = client.getCustodyByMint(tradeMint);
const custodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(tradeMint);
const stakingRewardTokenCustodyAccount = client.getCustodyByMint(
AdrenaClient.stakingRewardTokenMint,
);
const stakingRewardTokenCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(
AdrenaClient.stakingRewardTokenMint,
);
const position = AdrenaClient.findPositionAddress(
owner,
custody.pubkey,
"long",
);
const userProfilePda = AdrenaClient.getUserProfilePda(owner);
const userProfile =
await client.program.account.userProfile.fetchNullable(userProfilePda);
const receivingAccount = AdrenaClient.findATAAddressSync(owner, custody.mint);
const preInstructions: TransactionInstruction[] = [];
if (
!(await AdrenaClient.isAccountInitialized(
agent.connection,
receivingAccount,
))
) {
preInstructions.push(
AdrenaClient.createATAInstruction({
ataAddress: receivingAccount,
mint: custody.mint,
owner,
}),
);
}
const instruction = await client.program.methods
.closePositionLong({
price: new BN(price * 10 ** PRICE_DECIMALS),
})
.accountsStrict({
owner,
receivingAccount,
transferAuthority: AdrenaClient.transferAuthority,
pool: AdrenaClient.mainPool,
position: position,
custody: custody.pubkey,
custodyTokenAccount,
custodyOracle: custody.oracle,
custodyTradeOracle: custody.tradeOracle,
tokenProgram: TOKEN_PROGRAM_ID,
lmStaking: AdrenaClient.lmStaking,
lpStaking: AdrenaClient.lpStaking,
cortex: AdrenaClient.cortex,
stakingRewardTokenCustody: stakingRewardTokenCustodyAccount.pubkey,
stakingRewardTokenCustodyOracle: stakingRewardTokenCustodyAccount.oracle,
stakingRewardTokenCustodyTokenAccount,
lmStakingRewardTokenVault: AdrenaClient.lmStakingRewardTokenVault,
lpStakingRewardTokenVault: AdrenaClient.lpStakingRewardTokenVault,
lpTokenMint: AdrenaClient.lpTokenMint,
protocolFeeRecipient: client.cortex.protocolFeeRecipient,
adrenaProgram: AdrenaClient.programId,
userProfile: userProfile ? userProfilePda : null,
caller: owner,
})
.instruction();
return sendTx(agent, [...preInstructions, instruction]);
}
/**
* Open long trade on Adrena
*
* Note: provide the same token as collateralMint and as tradeMint to avoid swap
* @returns Transaction signature
*/
export async function openPerpTradeLong({
agent,
price,
collateralAmount,
collateralMint = TOKENS.jitoSOL,
leverage = DEFAULT_OPTIONS.LEVERAGE_BPS,
tradeMint = TOKENS.jitoSOL,
slippage = 0.3,
}: {
agent: SolanaAgentKit;
price: number;
collateralAmount: number;
collateralMint?: PublicKey;
leverage?: number;
tradeMint?: PublicKey;
slippage?: number;
}): Promise<string> {
const client = await AdrenaClient.load(agent);
const owner = agent.wallet.publicKey;
const collateralAccount = AdrenaClient.findATAAddressSync(owner, tradeMint);
const fundingAccount = AdrenaClient.findATAAddressSync(owner, collateralMint);
const receivingCustody = AdrenaClient.findCustodyAddress(collateralMint);
const receivingCustodyOracle = client.getCustodyByMint(collateralMint).oracle;
const receivingCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(collateralMint);
// Principal custody is the custody of the targeted token
// i.e open a 1 ETH long position, principal custody is ETH
const principalCustody = AdrenaClient.findCustodyAddress(tradeMint);
const principalCustodyAccount = client.getCustodyByMint(tradeMint);
const principalCustodyOracle = principalCustodyAccount.oracle;
const principalCustodyTradeOracle = principalCustodyAccount.tradeOracle;
const principalCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(tradeMint);
const stakingRewardTokenCustodyAccount = client.getCustodyByMint(
AdrenaClient.stakingRewardTokenMint,
);
const stakingRewardTokenCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(
AdrenaClient.stakingRewardTokenMint,
);
const position = AdrenaClient.findPositionAddress(
owner,
principalCustody,
"long",
);
const userProfilePda = AdrenaClient.getUserProfilePda(owner);
const userProfile =
await client.program.account.userProfile.fetchNullable(userProfilePda);
const priceWithSlippage = applySlippage(
new BN(price * 10 ** PRICE_DECIMALS),
slippage,
);
const scaledCollateralAmount = new BN(
collateralAmount *
Math.pow(10, client.getCustodyByMint(collateralMint).decimals),
);
const preInstructions: TransactionInstruction[] = [];
if (
!(await AdrenaClient.isAccountInitialized(
agent.connection,
collateralAccount,
))
) {
preInstructions.push(
AdrenaClient.createATAInstruction({
ataAddress: collateralAccount,
mint: tradeMint,
owner,
}),
);
}
const instruction = await client.program.methods
.openOrIncreasePositionWithSwapLong({
price: priceWithSlippage,
collateral: scaledCollateralAmount,
leverage,
referrer: null,
})
.accountsStrict({
owner,
payer: owner,
fundingAccount,
collateralAccount,
receivingCustody,
receivingCustodyOracle,
receivingCustodyTokenAccount,
principalCustody,
principalCustodyOracle,
principalCustodyTradeOracle,
principalCustodyTokenAccount,
transferAuthority: AdrenaClient.transferAuthority,
cortex: AdrenaClient.cortex,
lmStaking: AdrenaClient.lmStaking,
lpStaking: AdrenaClient.lpStaking,
pool: AdrenaClient.mainPool,
position,
stakingRewardTokenCustody: stakingRewardTokenCustodyAccount.pubkey,
stakingRewardTokenCustodyOracle: stakingRewardTokenCustodyAccount.oracle,
stakingRewardTokenCustodyTokenAccount,
lmStakingRewardTokenVault: AdrenaClient.lmStakingRewardTokenVault,
lpStakingRewardTokenVault: AdrenaClient.lpStakingRewardTokenVault,
lpTokenMint: AdrenaClient.lpTokenMint,
userProfile: userProfile ? userProfilePda : null,
protocolFeeRecipient: client.cortex.protocolFeeRecipient,
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
adrenaProgram: ADRENA_PROGRAM_ID,
})
.instruction();
return sendTx(agent, [...preInstructions, instruction]);
}
/**
* Open short trade on Adrena
*
* Note: provide USDC as collateralMint to avoid swap
* @returns Transaction signature
*/
export async function openPerpTradeShort({
agent,
price,
collateralAmount,
collateralMint = TOKENS.USDC,
leverage = DEFAULT_OPTIONS.LEVERAGE_BPS,
tradeMint = TOKENS.jitoSOL,
slippage = 0.3,
}: {
agent: SolanaAgentKit;
price: number;
collateralAmount: number;
collateralMint?: PublicKey;
leverage?: number;
tradeMint?: PublicKey;
slippage?: number;
}): Promise<string> {
const client = await AdrenaClient.load(agent);
const owner = agent.wallet.publicKey;
const collateralAccount = AdrenaClient.findATAAddressSync(owner, tradeMint);
const fundingAccount = AdrenaClient.findATAAddressSync(owner, collateralMint);
const receivingCustody = AdrenaClient.findCustodyAddress(collateralMint);
const receivingCustodyOracle = client.getCustodyByMint(collateralMint).oracle;
const receivingCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(collateralMint);
// Principal custody is the custody of the targeted token
// i.e open a 1 BTC short position, principal custody is BTC
const principalCustody = AdrenaClient.findCustodyAddress(tradeMint);
const principalCustodyAccount = client.getCustodyByMint(tradeMint);
const principalCustodyTradeOracle = principalCustodyAccount.tradeOracle;
const principalCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(tradeMint);
const usdcAta = AdrenaClient.findATAAddressSync(owner, TOKENS.USDC);
const preInstructions: TransactionInstruction[] = [];
if (!(await AdrenaClient.isAccountInitialized(agent.connection, usdcAta))) {
preInstructions.push(
AdrenaClient.createATAInstruction({
ataAddress: usdcAta,
mint: TOKENS.USDC,
owner,
}),
);
}
// Custody used to provide collateral when opening the position
// Should be a stable token, by default, use USDC
const instructionCollateralMint = TOKENS.USDC;
const collateralCustody = AdrenaClient.findCustodyAddress(
instructionCollateralMint,
);
const collateralCustodyOracle = client.getCustodyByMint(
instructionCollateralMint,
).oracle;
const collateralCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(instructionCollateralMint);
const stakingRewardTokenCustodyAccount = client.getCustodyByMint(
AdrenaClient.stakingRewardTokenMint,
);
const stakingRewardTokenCustodyTokenAccount =
AdrenaClient.findCustodyTokenAccountAddress(
AdrenaClient.stakingRewardTokenMint,
);
const position = AdrenaClient.findPositionAddress(
owner,
principalCustody,
"long",
);
const userProfilePda = AdrenaClient.getUserProfilePda(owner);
const userProfile =
await client.program.account.userProfile.fetchNullable(userProfilePda);
const priceWithSlippage = applySlippage(
new BN(price * 10 ** PRICE_DECIMALS),
slippage,
);
const scaledCollateralAmount = new BN(
collateralAmount *
Math.pow(10, client.getCustodyByMint(collateralMint).decimals),
);
const instruction = await client.program.methods
.openOrIncreasePositionWithSwapShort({
price: priceWithSlippage,
collateral: scaledCollateralAmount,
leverage,
referrer: null,
})
.accountsStrict({
owner,
payer: owner,
fundingAccount,
collateralAccount,
receivingCustody,
receivingCustodyOracle,
receivingCustodyTokenAccount,
principalCustody,
principalCustodyTradeOracle,
principalCustodyTokenAccount,
collateralCustody,
collateralCustodyOracle,
collateralCustodyTokenAccount,
transferAuthority: AdrenaClient.transferAuthority,
cortex: AdrenaClient.cortex,
lmStaking: AdrenaClient.lmStaking,
lpStaking: AdrenaClient.lpStaking,
pool: AdrenaClient.mainPool,
position,
stakingRewardTokenCustody: stakingRewardTokenCustodyAccount.pubkey,
stakingRewardTokenCustodyOracle: stakingRewardTokenCustodyAccount.oracle,
stakingRewardTokenCustodyTokenAccount,
lmStakingRewardTokenVault: AdrenaClient.lmStakingRewardTokenVault,
lpStakingRewardTokenVault: AdrenaClient.lpStakingRewardTokenVault,
lpTokenMint: AdrenaClient.lpTokenMint,
userProfile: userProfile ? userProfilePda : null,
protocolFeeRecipient: client.cortex.protocolFeeRecipient,
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
adrenaProgram: ADRENA_PROGRAM_ID,
})
.instruction();
return sendTx(agent, [...preInstructions, instruction]);
}

View File

@@ -1,37 +0,0 @@
import {
PublicKey,
sendAndConfirmTransaction,
Transaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { ManifestClient } from "@cks-systems/manifest-sdk";
/**
* Cancels all orders from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function cancelAllOrders(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const cancelAllOrdersIx = await mfxClient.cancelAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(cancelAllOrdersIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Cancel all orders failed: ${error.message}`);
}
}

View 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}`);
}
}

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

View File

@@ -16,6 +16,7 @@ export async function getMainAllDomainsDomain(
mainDomain = await _getFavoriteDomain(agent.connection, owner);
return mainDomain.stale ? null : mainDomain.reverse;
} catch (error: any) {
console.error(error);
return null;
}
}

View File

@@ -29,7 +29,8 @@ export async function getPrimaryDomain(
);
}
return reverse;
} catch (error) {
} catch (error: any) {
console.error(error);
throw new Error(
`Failed to get primary domain for account: ${account.toBase58()}`,
);

View File

@@ -0,0 +1,10 @@
import { SolanaAgentKit } from "../agent";
/**
* Get the agents wallet address
* @param agent - SolanaAgentKit instance
* @returns string
*/
export function get_wallet_address(agent: SolanaAgentKit) {
return agent.wallet_address.toBase58();
}

View File

@@ -1,54 +1,47 @@
export * from "./request_faucet_funds";
export * from "./deploy_token";
export * from "./adrena_perp_trading";
export * from "./create_gibwork_task";
export * from "./create_image";
export * from "./create_tiplinks";
export * from "./deploy_collection";
export * from "./deploy_token";
export * from "./fetch_price";
export * from "./get_all_domains_tlds";
export * from "./get_all_registered_all_domains";
export * from "./get_balance";
export * from "./get_balance_other";
export * from "./mint_nft";
export * from "./transfer";
export * from "./trade";
export * from "./limit_order";
export * from "./cancel_all_orders";
export * from "./withdraw_all";
export * from "./register_domain";
export * from "./resolve_sol_domain";
export * from "./get_main_all_domains_domain";
export * from "./get_owned_all_domains";
export * from "./get_owned_domains_for_tld";
export * from "./get_primary_domain";
export * from "./get_token_data";
export * from "./get_tps";
export * from "./get_wallet_address";
export * from "./launch_pumpfun_token";
export * from "./lend";
export * from "./get_tps";
export * from "./get_token_data";
export * from "./stake_with_jup";
export * from "./fetch_price";
export * from "./send_compressed_airdrop";
export * from "./manifest_trade";
export * from "./mint_nft";
export * from "./openbook_create_market";
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";
export * from "./get_main_all_domains_domain";
export * from "./get_owned_all_domains";
export * from "./resolve_domain";
export * from "./get_all_domains_tlds";
export * from "./get_all_registered_all_domains";
export * from "./get_owned_domains_for_tld";
export * from "./get_main_all_domains_domain";
export * from "./get_owned_all_domains";
export * from "./resolve_domain";
export * from "./pyth_fetch_price";
export * from "./raydium_create_ammV4";
export * from "./raydium_create_clmm";
export * from "./raydium_create_cpmm";
export * from "./openbook_create_market";
export * from "./manifest_create_market";
export * from "./pyth_fetch_price";
export * from "./create_gibwork_task";
export * from "./register_domain";
export * from "./request_faucet_funds";
export * from "./resolve_domain";
export * from "./resolve_sol_domain";
export * from "./rock_paper_scissor";
export * from "./create_tiplinks";
export * from "./rugcheck";
export * from "./send_compressed_airdrop";
export * from "./stake_with_jup";
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";

View File

@@ -1,61 +0,0 @@
import {
PublicKey,
Transaction,
sendAndConfirmTransaction,
TransactionInstruction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import {
ManifestClient,
WrapperPlaceOrderParamsExternal,
} from "@cks-systems/manifest-sdk";
import { OrderType } from "@cks-systems/manifest-sdk/client/ts/src/wrapper/types/OrderType";
/**
* Place limit orders using Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @param quantity Amount to trade in tokens
* @param side Buy or Sell
* @param price Price in tokens ie. SOL/USDC
* @returns Transaction signature
*/
export async function limitOrder(
agent: SolanaAgentKit,
marketId: PublicKey,
quantity: number,
side: string,
price: number,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const orderParams: WrapperPlaceOrderParamsExternal = {
numBaseTokens: quantity,
tokenPrice: price,
isBid: side === "Buy",
lastValidSlot: 0,
orderType: OrderType.Limit,
clientOrderId: Number(Math.random() * 1000),
};
const depositPlaceOrderIx: TransactionInstruction[] =
await mfxClient.placeOrderWithRequiredDepositIx(
agent.wallet.publicKey,
orderParams,
);
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(...depositPlaceOrderIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Limit Order failed: ${error.message}`);
}
}

View File

@@ -1,43 +0,0 @@
import { ManifestClient } from "@cks-systems/manifest-sdk";
import {
Keypair,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
export async function manifestCreateMarket(
agent: SolanaAgentKit,
baseMint: PublicKey,
quoteMint: PublicKey,
): Promise<string[]> {
const marketKeypair: Keypair = Keypair.generate();
const FIXED_MANIFEST_HEADER_SIZE: number = 256;
const createAccountIx: TransactionInstruction = SystemProgram.createAccount({
fromPubkey: agent.wallet.publicKey,
newAccountPubkey: marketKeypair.publicKey,
space: FIXED_MANIFEST_HEADER_SIZE,
lamports: await agent.connection.getMinimumBalanceForRentExemption(
FIXED_MANIFEST_HEADER_SIZE,
),
programId: new PublicKey("MNFSTqtC93rEfYHB6hF82sKdZpUDFWkViLByLd1k1Ms"),
});
const createMarketIx = ManifestClient["createMarketIx"](
agent.wallet.publicKey,
baseMint,
quoteMint,
marketKeypair.publicKey,
);
const tx: Transaction = new Transaction();
tx.add(createAccountIx);
tx.add(createMarketIx);
const signature = await sendAndConfirmTransaction(agent.connection, tx, [
agent.wallet,
marketKeypair,
]);
return [signature, marketKeypair.publicKey.toBase58()];
}

295
src/tools/manifest_trade.ts Normal file
View File

@@ -0,0 +1,295 @@
import {
ManifestClient,
OrderType,
WrapperPlaceOrderParamsExternal,
} from "@cks-systems/manifest-sdk";
import {
Keypair,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
TransactionInstruction,
} from "@solana/web3.js";
import { BatchOrderPattern, OrderParams, SolanaAgentKit } from "../index";
export async function manifestCreateMarket(
agent: SolanaAgentKit,
baseMint: PublicKey,
quoteMint: PublicKey,
): Promise<string[]> {
const marketKeypair: Keypair = Keypair.generate();
const FIXED_MANIFEST_HEADER_SIZE: number = 256;
const createAccountIx: TransactionInstruction = SystemProgram.createAccount({
fromPubkey: agent.wallet.publicKey,
newAccountPubkey: marketKeypair.publicKey,
space: FIXED_MANIFEST_HEADER_SIZE,
lamports: await agent.connection.getMinimumBalanceForRentExemption(
FIXED_MANIFEST_HEADER_SIZE,
),
programId: new PublicKey("MNFSTqtC93rEfYHB6hF82sKdZpUDFWkViLByLd1k1Ms"),
});
const createMarketIx = ManifestClient["createMarketIx"](
agent.wallet.publicKey,
baseMint,
quoteMint,
marketKeypair.publicKey,
);
const tx: Transaction = new Transaction();
tx.add(createAccountIx);
tx.add(createMarketIx);
const signature = await sendAndConfirmTransaction(agent.connection, tx, [
agent.wallet,
marketKeypair,
]);
return [signature, marketKeypair.publicKey.toBase58()];
}
/**
* Place limit orders using Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @param quantity Amount to trade in tokens
* @param side Buy or Sell
* @param price Price in tokens ie. SOL/USDC
* @returns Transaction signature
*/
export async function limitOrder(
agent: SolanaAgentKit,
marketId: PublicKey,
quantity: number,
side: string,
price: number,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const orderParams: WrapperPlaceOrderParamsExternal = {
numBaseTokens: quantity,
tokenPrice: price,
isBid: side === "Buy",
lastValidSlot: 0,
orderType: OrderType.Limit,
clientOrderId: Number(Math.random() * 1000),
};
const depositPlaceOrderIx: TransactionInstruction[] =
await mfxClient.placeOrderWithRequiredDepositIx(
agent.wallet.publicKey,
orderParams,
);
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(...depositPlaceOrderIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Limit Order failed: ${error.message}`);
}
}
/**
* Cancels all orders from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function cancelAllOrders(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const cancelAllOrdersIx = await mfxClient.cancelAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(cancelAllOrdersIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Cancel all orders failed: ${error.message}`);
}
}
/**
* Withdraws all funds from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function withdrawAll(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const withdrawAllIx = await mfxClient.withdrawAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(...withdrawAllIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Withdraw all failed: ${error.message}`);
}
}
/**
* Generates an array of orders based on the specified pattern
*/
export function generateOrdersfromPattern(
pattern: BatchOrderPattern,
): OrderParams[] {
const orders: OrderParams[] = [];
// Random number of orders if not specified, max of 8
const numOrders = pattern.numberOfOrders || Math.ceil(Math.random() * 8);
// Calculate price points
const prices: number[] = [];
if (pattern.priceRange) {
const { min, max } = pattern.priceRange;
if (min && max) {
// Generate evenly spaced prices
for (let i = 0; i < numOrders; i++) {
if (pattern.spacing?.type === "percentage") {
const factor = 1 + pattern.spacing.value / 100;
prices.push(min * Math.pow(factor, i));
} else {
const step = (max - min) / (numOrders - 1);
prices.push(min + step * i);
}
}
} else if (min) {
// Generate prices starting from min with specified spacing
for (let i = 0; i < numOrders; i++) {
if (pattern.spacing?.type === "percentage") {
const factor = 1 + pattern.spacing.value / 100;
prices.push(min * Math.pow(factor, i));
} else {
prices.push(min + (pattern.spacing?.value || 0.01) * i);
}
}
}
}
// Calculate quantities
let quantities: number[] = [];
if (pattern.totalQuantity) {
const individualQty = pattern.totalQuantity / numOrders;
quantities = Array(numOrders).fill(individualQty);
} else if (pattern.individualQuantity) {
quantities = Array(numOrders).fill(pattern.individualQuantity);
}
// Generate orders
for (let i = 0; i < numOrders; i++) {
orders.push({
side: pattern.side,
price: prices[i],
quantity: quantities[i],
});
}
return orders;
}
/**
* Validates that sell orders are not priced below buy orders
* @param orders Array of order parameters to validate
* @throws Error if orders are crossed
*/
function validateNoCrossedOrders(orders: OrderParams[]): void {
// Find lowest sell and highest buy prices
let lowestSell = Number.MAX_SAFE_INTEGER;
let highestBuy = 0;
orders.forEach((order) => {
if (order.side === "Sell" && order.price < lowestSell) {
lowestSell = order.price;
}
if (order.side === "Buy" && order.price > highestBuy) {
highestBuy = order.price;
}
});
// Check if orders cross
if (lowestSell <= highestBuy) {
throw new Error(
`Invalid order prices: Sell order at ${lowestSell} is lower than or equal to Buy order at ${highestBuy}. Orders cannot cross.`,
);
}
}
/**
* Place batch orders using Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @param quantity Amount to trade in tokens
* @param side Buy or Sell
* @param price Price in tokens ie. SOL/USDC
* @returns Transaction signature
*/
export async function batchOrder(
agent: SolanaAgentKit,
marketId: PublicKey,
orders: OrderParams[],
): Promise<string> {
try {
validateNoCrossedOrders(orders);
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const placeParams: WrapperPlaceOrderParamsExternal[] = orders.map(
(order) => ({
numBaseTokens: order.quantity,
tokenPrice: order.price,
isBid: order.side === "Buy",
lastValidSlot: 0,
orderType: OrderType.Limit,
clientOrderId: Number(Math.random() * 10000),
}),
);
const batchOrderIx: TransactionInstruction = await mfxClient.batchUpdateIx(
placeParams,
[],
true,
);
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(batchOrderIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Batch Order failed: ${error.message}`);
}
}

View File

@@ -26,7 +26,7 @@ import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
* # Opens a Centered Liquidity Position in an Orca Whirlpool
*
* This function opens a centered liquidity position in a specified Orca Whirlpool. The user defines
* a basis point (bps) offset from the cuurent price of the pool to set the lower and upper bounds of the position.
* a basis point (bps) offset from the current 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.
*

View File

@@ -1,38 +1,91 @@
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
import BN from "bn.js";
import { PythPriceFeedIDItem } from "../types";
/**
* Fetch the price feed ID for a given token symbol from Pyth
* @param tokenSymbol Token symbol
* @returns Price feed ID
*/
export async function fetchPythPriceFeedID(
tokenSymbol: string,
): Promise<string> {
try {
const stableHermesServiceUrl: string = "https://hermes.pyth.network";
const response = await fetch(
`${stableHermesServiceUrl}/v2/price_feeds?query=${tokenSymbol}&asset_type=crypto`,
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.length === 0) {
throw new Error(`No price feed found for ${tokenSymbol}`);
}
if (data.length > 1) {
const filteredData = data.filter(
(item: PythPriceFeedIDItem) =>
item.attributes.base.toLowerCase() === tokenSymbol.toLowerCase(),
);
if (filteredData.length === 0) {
throw new Error(`No price feed found for ${tokenSymbol}`);
}
return filteredData[0].id;
}
return data[0].id;
} catch (error: any) {
throw new Error(
`Fetching price feed ID from Pyth failed: ${error.message}`,
);
}
}
/**
* Fetch the price of a given price feed from Pyth
* @param agent SolanaAgentKit instance
* @param priceFeedID Price feed ID
* @returns Latest price value from feed
*
* You can find priceFeedIDs here: https://www.pyth.network/developers/price-feed-ids#stable
*/
export async function pythFetchPrice(priceFeedID: string): Promise<string> {
// get Hermes service URL from https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes
const stableHermesServiceUrl: string = "https://hermes.pyth.network";
const connection = new PriceServiceConnection(stableHermesServiceUrl);
const feeds = [priceFeedID];
export async function fetchPythPrice(feedID: string): Promise<string> {
try {
const currentPrice = await connection.getLatestPriceFeeds(feeds);
const stableHermesServiceUrl: string = "https://hermes.pyth.network";
if (currentPrice === undefined) {
throw new Error("Price data not available for the given token.");
const response = await fetch(
`${stableHermesServiceUrl}/v2/updates/price/latest?ids[]=${feedID}`,
);
const data = await response.json();
const parsedData = data.parsed;
if (parsedData.length === 0) {
throw new Error(`No price data found for ${feedID}`);
}
if (currentPrice.length === 0) {
throw new Error("Price data not available for the given token.");
const price = new BN(parsedData[0].price.price);
const exponent = parsedData[0].price.expo;
if (exponent < 0) {
const adjustedPrice = price.mul(new BN(100));
const divisor = new BN(10).pow(new BN(-exponent));
const scaledPrice = adjustedPrice.div(divisor);
const priceStr = scaledPrice.toString();
const formattedPrice = `${priceStr.slice(0, -2)}.${priceStr.slice(-2)}`;
return formattedPrice.startsWith(".")
? `0${formattedPrice}`
: formattedPrice;
}
// get price and exponent from price feed
const price = new BN(currentPrice[0].getPriceUnchecked().price);
const exponent = new BN(currentPrice[0].getPriceUnchecked().expo);
// convert to scaled price
const scaledPrice = price.div(new BN(10).pow(exponent));
const scaledPrice = price.div(new BN(10).pow(new BN(exponent)));
return scaledPrice.toString();
} catch (error: any) {
throw new Error(`Fetching price from Pyth failed: ${error.message}`);

View File

@@ -24,7 +24,8 @@ export async function resolveSolDomain(
try {
return await resolve(agent.connection, domain);
} catch (error) {
} catch (error: any) {
console.error(error);
throw new Error(`Failed to resolve domain: ${domain}`);
}
}

53
src/tools/rugcheck.ts Normal file
View File

@@ -0,0 +1,53 @@
import { TokenCheck } from "../types";
const BASE_URL = "https://api.rugcheck.xyz/v1";
/**
* Fetches a summary report for a specific token.
* @async
* @param {string} mint - The mint address of the token.
* @returns {Promise<TokenCheck>} The token summary report.
* @throws {Error} If the API call fails.
*/
export async function fetchTokenReportSummary(
mint: string,
): Promise<TokenCheck> {
try {
const response = await fetch(`${BASE_URL}/tokens/${mint}/report/summary`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error: any) {
console.error(
`Error fetching report summary for token ${mint}:`,
error.message,
);
throw new Error(`Failed to fetch report summary for token ${mint}.`);
}
}
/**
* Fetches a detailed report for a specific token.
* @async
* @param {string} mint - The mint address of the token.
* @returns {Promise<TokenCheck>} The detailed token report.
* @throws {Error} If the API call fails.
*/
export async function fetchTokenDetailedReport(
mint: string,
): Promise<TokenCheck> {
try {
const response = await fetch(`${BASE_URL}/tokens/${mint}/report`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error: any) {
console.error(
`Error fetching detailed report for token ${mint}:`,
error.message,
);
throw new Error(`Failed to fetch detailed report for token ${mint}.`);
}
}

View File

@@ -88,6 +88,7 @@ export async function sendCompressedAirdrop(
agent.wallet.publicKey,
);
} catch (error) {
console.error(error);
throw new Error(
"Source token account not found and failed to create it. Please add funds to your wallet and try again.",
);

View File

@@ -0,0 +1,64 @@
import { VersionedTransaction } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
/**
* Stake SOL with Solayer
* @param agent SolanaAgentKit instance
* @param amount Amount of SOL to stake
* @returns Transaction signature
*/
export async function stakeWithSolayer(
agent: SolanaAgentKit,
amount: number,
): Promise<string> {
try {
const response = await fetch(
`https://app.solayer.org/api/action/restake/ssol?amount=${amount}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || "Staking request failed");
}
const data = await response.json();
// Deserialize and prepare transaction
const txn = VersionedTransaction.deserialize(
Buffer.from(data.transaction, "base64"),
);
// Update blockhash
const { blockhash } = await agent.connection.getLatestBlockhash();
txn.message.recentBlockhash = blockhash;
// Sign and send transaction
txn.sign([agent.wallet]);
const signature = await agent.connection.sendTransaction(txn, {
preflightCommitment: "confirmed",
maxRetries: 3,
});
// Wait for confirmation
const latestBlockhash = await agent.connection.getLatestBlockhash();
await agent.connection.confirmTransaction({
signature,
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
});
return signature;
} catch (error: any) {
console.error(error);
throw new Error(`Solayer sSOL staking failed: ${error.message}`);
}
}

View File

@@ -33,6 +33,7 @@ export async function listNFTForSale(
throw new Error(`You don't own this NFT (${nftMint.toString()})`);
}
} catch (error: any) {
console.error(error);
throw new Error(
`No token account found for mint ${nftMint.toString()}. Make sure you own this NFT.`,
);

View File

@@ -1,37 +0,0 @@
import {
PublicKey,
sendAndConfirmTransaction,
Transaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { ManifestClient } from "@cks-systems/manifest-sdk";
/**
* Withdraws all funds from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function withdrawAll(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const withdrawAllIx = await mfxClient.withdrawAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(...withdrawAllIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Withdraw all failed: ${error.message}`);
}
}