Merge branch 'main' into jup-list

This commit is contained in:
aryan
2024-12-19 05:45:01 +05:30
30 changed files with 3384 additions and 89 deletions

View File

@@ -14,6 +14,7 @@ import {
getTPS,
getTokenDataByAddress,
getTokenDataByTicker,
stakeWithJup,
} from "../tools";
import { CollectionOptions, PumpFunTokenOptions } from "../types";
import { DEFAULT_OPTIONS } from "../constants";
@@ -121,4 +122,10 @@ export class SolanaAgentKit {
options
);
}
async stake(
amount: number,
) {
return stakeWithJup(this, amount);
}
}

View File

@@ -3,6 +3,7 @@ import { SolanaAgentKit } from "../index";
import { PublicKey } from "@solana/web3.js";
import { toJSON } from "../utils/toJSON";
import { create_image } from "../tools/create_image";
import { fetchPrice } from "../tools/fetch_price";
export class SolanaBalanceTool extends Tool {
name = "solana_balance";
@@ -567,6 +568,71 @@ export class SolanaTPSCalculatorTool extends Tool {
}
}
export class SolanaStakeTool extends Tool {
name = "solana_stake";
description = `This tool can be used to stake your SOL (Solana), also called as SOL staking or liquid staking.
Inputs ( input is a JSON string ):
amount: number, eg 1 or 0.01 (required)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
protected async _call(input: string): Promise<string> {
try {
const parsedInput = JSON.parse(input) || Number(input);
const tx = await this.solanaKit.stake(parsedInput.amount);
return JSON.stringify({
status: "success",
message: "Staked successfully",
transaction: tx,
amount: parsedInput.amount,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
/**
* Tool to fetch the price of a token in USDC
*/
export class SolanaFetchPriceTool extends Tool {
name = "solana_fetch_price";
description = `Fetch the price of a given token in USDC.
Inputs:
- tokenId: string, the mint address of the token, e.g., "JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN"`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
async _call(input: string): Promise<string> {
try {
const price = await fetchPrice(this.solanaKit, input.trim());
return JSON.stringify({
status: "success",
tokenId: input.trim(),
priceInUSDC: price,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
export class SolanaTokenDataTool extends Tool {
name = "solana_token_data";
description = `Get the token data for a given token mint address
@@ -644,6 +710,8 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaCreateImageTool(solanaKit),
new SolanaLendAssetTool(solanaKit),
new SolanaTPSCalculatorTool(solanaKit),
new SolanaStakeTool(solanaKit),
new SolanaFetchPriceTool(solanaKit),
new SolanaTokenDataTool(solanaKit),
new SolanaTokenDataByTickerTool(solanaKit),
];

View File

@@ -27,10 +27,6 @@ export async function deploy_token(
);
const mint = Keypair.generate();
console.log("Mint address: ", mint.publicKey.toString());
console.log("Agent address: ", agent.wallet_address.toString());
let account_create_ix = SystemProgram.createAccount({
fromPubkey: agent.wallet_address,
newAccountPubkey: mint.publicKey,
@@ -51,13 +47,6 @@ export async function deploy_token(
let hash = await sendTx(agent, tx, [mint]);
console.log("Transaction hash: ", hash);
console.log(
"Token deployed successfully. Mint address: ",
mint.publicKey.toString()
);
return {
mint: mint.publicKey,
};

35
src/tools/fetch_price.ts Normal file
View File

@@ -0,0 +1,35 @@
import { SolanaAgentKit } from "../index";
import { Tool } from "langchain/tools";
/**
* Fetch the price of a given token in USDC using Jupiter API
* @param agent SolanaAgentKit instance
* @param tokenId The token mint address
* @returns The price of the token in USDC
*/
export async function fetchPrice(
agent: SolanaAgentKit,
tokenId: string
): Promise<string> {
try {
const response = await fetch(
`https://api.jup.ag/price/v2?ids=${tokenId}`
);
if (!response.ok) {
throw new Error(`Failed to fetch price: ${response.statusText}`);
}
const data = await response.json();
const price = data.data[tokenId]?.price;
if (!price) {
throw new Error("Price data not available for the given token.");
}
return price;
} catch (error: any) {
throw new Error(`Price fetch failed: ${error.message}`);
}
}

View File

@@ -10,3 +10,5 @@ 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";

View File

@@ -38,7 +38,6 @@ async function uploadMetadata(
finalFormData.append('file', files.file);
}
console.log("Final form data:", finalFormData);
const metadataResponse = await fetch("https://pump.fun/api/ipfs", {
method: "POST",
@@ -46,7 +45,6 @@ async function uploadMetadata(
});
if (!metadataResponse.ok) {
console.log("Metadata response:", await metadataResponse.json());
throw new Error(`Metadata upload failed: ${metadataResponse.statusText}`);
}
@@ -152,30 +150,14 @@ export async function launchPumpFunToken(
options?: PumpFunTokenOptions
) {
try {
// TBD : Remove clgs after approval
console.log("Starting token launch process...");
// Generate mint keypair
const mintKeypair = Keypair.generate();
console.log("Mint public key:", mintKeypair.publicKey.toBase58());
// Upload metadata
console.log("Uploading metadata to IPFS...");
const metadataResponse = await uploadMetadata(tokenName, tokenTicker, description, imageUrl, options);
console.log("Metadata response:", metadataResponse);
// Create token transaction
console.log("Creating token transaction...");
const response = await createTokenTransaction(agent, mintKeypair, metadataResponse, options);
const transactionData = await response.arrayBuffer();
const tx = VersionedTransaction.deserialize(new Uint8Array(transactionData));
// Send transaction with proper blockhash handling
console.log("Sending transaction...");
const signature = await signAndSendTransaction(agent, tx, mintKeypair);
console.log("Token launch successful!");
return {
signature,
mint: mintKeypair.publicKey.toBase58(),

View File

@@ -0,0 +1,56 @@
import { VersionedTransaction } from "@solana/web3.js";
import { SolanaAgentKit } from "../agent";
/**
* Stake SOL with Jup validator
* @param agent SolanaAgentKit instance
* @param amount Amount of SOL to stake
* @returns Transaction signature
*/
export async function stakeWithJup(
agent: SolanaAgentKit,
amount: number,
): Promise<string> {
try {
const res = await fetch(
`https://worker.jup.ag/blinks/swap/So11111111111111111111111111111111111111112/jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v/${amount}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
account: agent.wallet.publicKey.toBase58(),
}),
},
);
const data = await res.json();
const txn = VersionedTransaction.deserialize(
Buffer.from(data.transaction, "base64"),
);
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,
});
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(`jupSOL staking failed: ${error.message}`);
}
}

View File

@@ -19,8 +19,6 @@ export async function trade(
slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS,
): Promise<string> {
try {
// Get quote for the swap
console.log(inputMint.toString(), outputMint.toString(), inputAmount, slippageBps);
const quoteResponse = await (
await fetch(
`${JUP_API}/quote?` +

View File

@@ -69,3 +69,11 @@ export interface JupiterTokenData {
coingeckoId?: string;
};
}
export interface FetchPriceResponse {
status: "success" | "error";
tokenId?: string;
priceInUSDC?: string;
message?: string;
code?: string;
}