mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-13 23:16:55 +00:00
Implement getAsset tool
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
"@langchain/openai": "^0.3.16",
|
||||
"@lightprotocol/compressed-token": "^0.17.1",
|
||||
"@lightprotocol/stateless.js": "^0.17.1",
|
||||
"@metaplex-foundation/digital-asset-standard-api": "^1.0.4",
|
||||
"@metaplex-foundation/mpl-core": "^1.1.1",
|
||||
"@metaplex-foundation/mpl-token-metadata": "^3.3.0",
|
||||
"@metaplex-foundation/mpl-toolbox": "^0.9.4",
|
||||
|
||||
528
pnpm-lock.yaml
generated
528
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -59,6 +59,7 @@ import withdrawFromDriftAccountAction from "./drift/withdrawFromDriftAccount";
|
||||
import driftUserAccountInfoAction from "./drift/driftUserAccountInfo";
|
||||
import deriveDriftVaultAddressAction from "./drift/deriveVaultAddress";
|
||||
import updateDriftVaultDelegateAction from "./drift/updateDriftVaultDelegate";
|
||||
import getAssetAction from "./metaplex/getAsset";
|
||||
|
||||
export const ACTIONS = {
|
||||
WALLET_ADDRESS_ACTION: getWalletAddressAction,
|
||||
@@ -123,6 +124,7 @@ export const ACTIONS = {
|
||||
DRIFT_USER_ACCOUNT_INFO_ACTION: driftUserAccountInfoAction,
|
||||
DERIVE_DRIFT_VAULT_ADDRESS_ACTION: deriveDriftVaultAddressAction,
|
||||
UPDATE_DRIFT_VAULT_DELEGATE_ACTION: updateDriftVaultDelegateAction,
|
||||
GET_ASSET_ACTION: getAssetAction,
|
||||
};
|
||||
|
||||
export type { Action, ActionExample, Handler } from "../types/action";
|
||||
|
||||
51
src/actions/metaplex/getAsset.ts
Normal file
51
src/actions/metaplex/getAsset.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { Action } from "../../types/action";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
import { z } from "zod";
|
||||
import { get_asset } from "../../tools/metaplex";
|
||||
|
||||
const getAssetAction: Action = {
|
||||
name: "GET_ASSET",
|
||||
similes: [
|
||||
"fetch asset",
|
||||
"retrieve asset",
|
||||
"get asset details",
|
||||
"fetch asset details",
|
||||
],
|
||||
description: `Fetch asset details using the Metaplex DAS API.`,
|
||||
examples: [
|
||||
[
|
||||
{
|
||||
input: {
|
||||
assetId: "Asset ID",
|
||||
},
|
||||
output: {
|
||||
status: "success",
|
||||
message: "Asset retrieved successfully",
|
||||
result: {
|
||||
// Example asset details
|
||||
name: "Example Asset",
|
||||
symbol: "EXA",
|
||||
uri: "https://example.com/asset.json",
|
||||
},
|
||||
},
|
||||
explanation: "Fetch details of an asset using its ID",
|
||||
},
|
||||
],
|
||||
],
|
||||
schema: z.object({
|
||||
assetId: z.string().min(1, "Asset ID is required"),
|
||||
}),
|
||||
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
|
||||
const assetId = input.assetId;
|
||||
|
||||
const result = await get_asset(agent, assetId);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
message: "Asset retrieved successfully",
|
||||
result,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default getAssetAction;
|
||||
@@ -98,6 +98,7 @@ import {
|
||||
withdrawFromDriftVault,
|
||||
updateVaultDelegate,
|
||||
get_token_balance,
|
||||
get_asset,
|
||||
} from "../tools";
|
||||
import {
|
||||
Config,
|
||||
@@ -115,6 +116,7 @@ import {
|
||||
HeliusWebhookIdResponse,
|
||||
HeliusWebhookResponse,
|
||||
} from "../types";
|
||||
import { DasApiAsset } from "@metaplex-foundation/digital-asset-standard-api";
|
||||
|
||||
/**
|
||||
* Main class for interacting with Solana blockchain
|
||||
@@ -821,4 +823,8 @@ export class SolanaAgentKit {
|
||||
async updateDriftVaultDelegate(vaultAddress: string, delegate: string) {
|
||||
return await updateVaultDelegate(this, vaultAddress, delegate);
|
||||
}
|
||||
|
||||
async getAsset(assetId: string): Promise<DasApiAsset> {
|
||||
return get_asset(this, assetId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ import {
|
||||
SolanaWithdrawFromDriftAccountTool,
|
||||
SolanaWithdrawFromDriftVaultTool,
|
||||
} from "./index";
|
||||
import { SolanaGetAssetTool } from "./metaplex/get_asset";
|
||||
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
@@ -208,5 +209,6 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
new SolanaDriftVaultInfoTool(solanaKit),
|
||||
new SolanaWithdrawFromDriftAccountTool(solanaKit),
|
||||
new SolanaWithdrawFromDriftVaultTool(solanaKit),
|
||||
new SolanaGetAssetTool(solanaKit),
|
||||
];
|
||||
}
|
||||
|
||||
34
src/langchain/metaplex/get_asset.ts
Normal file
34
src/langchain/metaplex/get_asset.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Tool } from "langchain/tools";
|
||||
import { SolanaAgentKit } from "../../agent";
|
||||
|
||||
export class SolanaGetAssetTool extends Tool {
|
||||
name = "solana_get_asset";
|
||||
description = `Fetch asset details using the Metaplex DAS API.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
assetId: string, eg "Asset ID" (required)`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
const result = await this.solanaKit.getAsset(parsedInput.assetId);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Asset retrieved successfully",
|
||||
result,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/tools/metaplex/get_asset.ts
Normal file
31
src/tools/metaplex/get_asset.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { SolanaAgentKit } from "../../index";
|
||||
import { publicKey } from "@metaplex-foundation/umi";
|
||||
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
|
||||
import {
|
||||
dasApi,
|
||||
DasApiAsset,
|
||||
} from "@metaplex-foundation/digital-asset-standard-api";
|
||||
|
||||
/**
|
||||
* Fetch asset details using the Metaplex DAS API
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param assetId ID of the asset to fetch
|
||||
* @returns Asset details
|
||||
*/
|
||||
export async function get_asset(
|
||||
agent: SolanaAgentKit,
|
||||
assetId: string,
|
||||
): Promise<DasApiAsset> {
|
||||
try {
|
||||
const endpoint = agent.connection.rpcEndpoint;
|
||||
const umi = createUmi(endpoint).use(dasApi());
|
||||
const assetPublicKey = publicKey(assetId);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
return await umi.rpc.getAsset(assetPublicKey);
|
||||
} catch (error: any) {
|
||||
console.error("Error retrieving asset: ", error.message);
|
||||
throw new Error(`Asset retrieval failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./deploy_collection";
|
||||
export * from "./mint_nft";
|
||||
export * from "./deploy_token";
|
||||
export * from "./get_asset";
|
||||
|
||||
Reference in New Issue
Block a user