feat:parsing transactions and getting all Assets

This commit is contained in:
shivaji43
2025-01-07 15:53:20 +05:30
parent 58a9edd7e2
commit 418f55178a
4 changed files with 1462 additions and 0 deletions

1349
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -594,6 +594,12 @@ export class SolanaAgentKit {
async flashCloseTrade(params: FlashCloseTradeParams): Promise<string> {
return flashCloseTrade(this, params);
}
async heliusParseTransactions(transactionId: string): Promise<any> {
return parseTransaction(this, transactionId);
}
async getAllAssetsbyOwner(owner: PublicKey, limit: number): Promise<any> {
return getAssetsByOwner(this, owner, limit);
}
async create3LandCollection(
optionsWithBase58: StoreInitOptions,

View File

@@ -0,0 +1,60 @@
import { SolanaAgentKit } from "../index";
import { PublicKey } from "@solana/web3.js";
import * as dotenv from "dotenv";
dotenv.config();
/**
* Fetch assets by owner using the Helius Digital Asset Standard (DAS) API
* @param agent SolanaAgentKit instance
* @param ownerPublicKey Owner's Solana wallet PublicKey
* @param limit Number of assets to retrieve per request
* @returns Assets owned by the specified address
*/
export async function getAssetsByOwner(
agent: SolanaAgentKit,
ownerPublicKey: PublicKey,
limit: number,
): Promise<any> {
try {
const apiKey = process.env.HELIUS_API_KEY;
if (!apiKey) {
throw new Error("HELIUS_API_KEY not found in environment variables");
}
const url = `https://mainnet.helius-rpc.com/?api-key=${apiKey}`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "get-assets",
method: "getAssetsByOwner",
params: {
ownerAddress: ownerPublicKey.toString(),
page: 3,
limit: limit,
displayOptions: {
showFungible: true,
},
},
}),
});
if (!response.ok) {
throw new Error(
`Failed to fetch: ${response.status} - ${response.statusText}`,
);
}
const data = await response.json();
console.log("Assets by Owner: ", data.result.items);
return data.result.items;
} catch (error: any) {
console.error("Error retrieving assets: ", error.message);
throw new Error(`Assets retrieval failed: ${error.message}`);
}
}

View File

@@ -0,0 +1,47 @@
import { SolanaAgentKit } from "../index";
import * as dotenv from "dotenv";
dotenv.config();
/**
* Parse a Solana transaction using the Helius Enhanced Transactions API
* @param agent SolanaAgentKit instance
* @param transactionId The transaction ID to parse
* @returns Parsed transaction data
*/
export async function parseTransaction(
agent: SolanaAgentKit,
transactionId: string,
): Promise<any> {
try {
const apiKey = process.env.HELIUS_API_KEY;
if (!apiKey) {
throw new Error("HELIUS_API_KEY not found in environment variables");
}
const url = `https://api.helius.xyz/v0/transactions/?api-key=${apiKey}`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
transactions: [transactionId],
}),
});
if (!response.ok) {
throw new Error(
`Failed to fetch: ${response.status} - ${response.statusText}`,
);
}
const data = await response.json();
console.log("Parsed transaction: ", data);
return data;
} catch (error: any) {
console.error("Error parsing transaction: ", error.message);
throw new Error(`Transaction parsing failed: ${error.message}`);
}
}