feat: langchain tool

This commit is contained in:
aryan
2024-12-19 06:27:59 +05:30
parent 1b7f657734
commit 6ab20f85cc
4 changed files with 1430 additions and 1177 deletions

2514
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -49,13 +49,13 @@ export class SolanaAgentKit {
}
async deployToken(
decimals: number = DEFAULT_OPTIONS.TOKEN_DECIMALS,
name: string,
uri: string,
symbol: string,
decimals: number = DEFAULT_OPTIONS.TOKEN_DECIMALS,
initialSupply?: number,
) {
return deploy_token(this, decimals, name, uri, symbol, initialSupply);
return deploy_token(this, name, uri, symbol, decimals, initialSupply);
}
async deployCollection(options: CollectionOptions) {

View File

@@ -87,38 +87,32 @@ export class SolanaTransferTool extends Tool {
export class SolanaDeployTokenTool extends Tool {
name = "solana_deploy_token";
description =
"Deploy a new SPL token. Input should be JSON string with: {decimals?: number, initialSupply?: number}";
description = `Deploy a new token on Solana blockchain.
Inputs (input is a JSON string):
name: string, eg "My Token" (required)
uri: string, eg "https://example.com/token.json" (required)
symbol: string, eg "MTK" (required)
decimals?: number, eg 9 (optional, defaults to 9)
initialSupply?: number, eg 1000000 (optional)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
private validateInput(input: any): void {
if (
input.decimals !== undefined &&
(typeof input.decimals !== "number" ||
input.decimals < 0 ||
input.decimals > 9)
) {
throw new Error(
"decimals must be a number between 0 and 9 when provided"
);
}
if (
input.initialSupply !== undefined &&
(typeof input.initialSupply !== "number" || input.initialSupply <= 0)
) {
throw new Error("initialSupply must be a positive number when provided");
}
}
protected async _call(input: string): Promise<string> {
try {
const parsedInput = toJSON(input);
this.validateInput(parsedInput);
const parsedInput = JSON.parse(input);
const result = await this.solanaKit.deployToken(parsedInput.decimals);
console.log(parsedInput);
const result = await this.solanaKit.deployToken(
parsedInput.name,
parsedInput.uri,
parsedInput.symbol,
parsedInput.decimals,
parsedInput.initialSupply
);
return JSON.stringify({
status: "success",

View File

@@ -1,10 +1,22 @@
import { SolanaAgentKit } from "../index";
import { PublicKey } from "@solana/web3.js";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import { generateSigner } from "@metaplex-foundation/umi";
import { createFungible, mintV1, TokenStandard } from "@metaplex-foundation/mpl-token-metadata";
import { fromWeb3JsPublicKey, toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
import {
generateSigner,
keypairIdentity,
signerIdentity,
} from "@metaplex-foundation/umi";
import {
createFungible,
mintV1,
TokenStandard,
} from "@metaplex-foundation/mpl-token-metadata";
import {
fromWeb3JsKeypair,
fromWeb3JsPublicKey,
toWeb3JsPublicKey,
} from "@metaplex-foundation/umi-web3js-adapters";
import bs58 from "bs58";
/**
* Deploy a new SPL token
* @param agent SolanaAgentKit instance
@@ -17,29 +29,28 @@ import { fromWeb3JsPublicKey, toWeb3JsPublicKey } from "@metaplex-foundation/umi
*/
export async function deploy_token(
agent: SolanaAgentKit,
decimals: number = 9,
name: string,
uri: string,
symbol: string,
initialSupply?: number,
decimals: number = 9,
initialSupply?: number
): Promise<{ mint: PublicKey }> {
try {
// Create UMI instance from agent
const umi = createUmi(agent.connection.rpcEndpoint)
const umi = createUmi(agent.connection.rpcEndpoint).use(
keypairIdentity(fromWeb3JsKeypair(agent.wallet))
);
// Create new token mint
const mint = generateSigner(umi);
console.log("Mint address: ", mint.publicKey.toString());
console.log("Agent address: ", agent.wallet_address.toString());
let builder = createFungible(umi, {
name,
uri,
symbol,
sellerFeeBasisPoints: {
basisPoints: 0n,
identifier: '%',
identifier: "%",
decimals: 2,
},
decimals,
@@ -47,12 +58,14 @@ export async function deploy_token(
});
if (initialSupply) {
builder = builder.add(mintV1(umi, {
mint: mint.publicKey,
tokenStandard: TokenStandard.Fungible,
tokenOwner: fromWeb3JsPublicKey(agent.wallet_address),
amount: initialSupply,
}));
builder = builder.add(
mintV1(umi, {
mint: mint.publicKey,
tokenStandard: TokenStandard.Fungible,
tokenOwner: fromWeb3JsPublicKey(agent.wallet_address),
amount: initialSupply,
})
);
}
builder.sendAndConfirm(umi);