mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-13 23:16:55 +00:00
Updating NFTs to Core and adding token metadata.
This commit is contained in:
3
.env.example
Normal file
3
.env.example
Normal file
@@ -0,0 +1,3 @@
|
||||
OPENAI_API_KEY=your_openai_api_key_here
|
||||
RPC_URL=your_rpc_url_here
|
||||
SOLANA_PRIVATE_KEY=your_solana_private_key_here
|
||||
10
README.md
10
README.md
@@ -112,25 +112,32 @@ const signature = await lendAsset(
|
||||
|
||||
### Core Functions
|
||||
|
||||
#### `deploy_token(agent, decimals?, initialSupply?)`
|
||||
#### `deploy_token(agent, decimals, name, uri, symbol, initialSupply?)`
|
||||
|
||||
Deploy a new SPL token with optional initial supply.
|
||||
|
||||
#### `deploy_collection(agent, options)`
|
||||
|
||||
Create a new NFT collection with customizable metadata and royalties.
|
||||
|
||||
#### `mintCollectionNFT(agent, collectionMint, metadata, recipient?)`
|
||||
|
||||
Mint a new NFT as part of an existing collection.
|
||||
|
||||
#### `transfer(agent, to, amount, mint?)`
|
||||
|
||||
Transfer SOL or SPL tokens to a recipient.
|
||||
|
||||
#### `trade(agent, outputMint, inputAmount, inputMint?, slippageBps?)`
|
||||
|
||||
Swap tokens using Jupiter Exchange integration.
|
||||
|
||||
#### `get_balance(agent, token_address)`
|
||||
|
||||
Check SOL or token balance for the agent's wallet.
|
||||
|
||||
#### `lendAsset(agent, assetMint, amount, apiKey)`
|
||||
|
||||
Lend idle assets to earn interest with Lulo.
|
||||
|
||||
## Dependencies
|
||||
@@ -140,6 +147,7 @@ The toolkit relies on several key Solana and Metaplex libraries:
|
||||
- @solana/web3.js
|
||||
- @solana/spl-token
|
||||
- @metaplex-foundation/mpl-token-metadata
|
||||
- @metaplex-foundation/mpl-core
|
||||
- @metaplex-foundation/umi
|
||||
|
||||
## Contributing
|
||||
|
||||
2618
pnpm-lock.yaml
generated
2618
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -49,9 +49,12 @@ export class SolanaAgentKit {
|
||||
|
||||
async deployToken(
|
||||
decimals: number = DEFAULT_OPTIONS.TOKEN_DECIMALS,
|
||||
// initialSupply?: number
|
||||
name: string,
|
||||
uri: string,
|
||||
symbol: string,
|
||||
initialSupply?: number,
|
||||
) {
|
||||
return deploy_token(this, decimals);
|
||||
return deploy_token(this, decimals, name, uri, symbol, initialSupply);
|
||||
}
|
||||
|
||||
async deployCollection(options: CollectionOptions) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import { createUmi, generateSigner, publicKey } from "@metaplex-foundation/umi";
|
||||
import { createCollection, ruleSet } from "@metaplex-foundation/mpl-core";
|
||||
import { mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata";
|
||||
import { generateSigner, publicKey } from "@metaplex-foundation/umi";
|
||||
import { createCollection, mplCore, ruleSet } from "@metaplex-foundation/mpl-core";
|
||||
import { CollectionOptions, CollectionDeployment } from "../types";
|
||||
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
||||
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
|
||||
|
||||
/**
|
||||
* Deploy a new NFT collection
|
||||
@@ -17,7 +17,7 @@ export async function deploy_collection(
|
||||
): Promise<CollectionDeployment> {
|
||||
try {
|
||||
// Initialize Umi
|
||||
const umi = createUmi().use(mplTokenMetadata());
|
||||
const umi = createUmi(agent.connection.rpcEndpoint).use(mplCore());
|
||||
|
||||
// Generate collection signer
|
||||
const collectionSigner = generateSigner(umi);
|
||||
@@ -27,11 +27,11 @@ export async function deploy_collection(
|
||||
address: publicKey(creator.address),
|
||||
percentage: creator.percentage,
|
||||
})) || [
|
||||
{
|
||||
address: publicKey(agent.wallet_address.toString()),
|
||||
percentage: 100,
|
||||
},
|
||||
];
|
||||
{
|
||||
address: publicKey(agent.wallet_address.toString()),
|
||||
percentage: 100,
|
||||
},
|
||||
];
|
||||
|
||||
// Create collection
|
||||
const tx = await createCollection(umi, {
|
||||
|
||||
@@ -1,57 +1,61 @@
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import {
|
||||
createInitializeMint2Instruction,
|
||||
MINT_SIZE,
|
||||
getMinimumBalanceForRentExemptAccount,
|
||||
TOKEN_PROGRAM_ID,
|
||||
} from "@solana/spl-token";
|
||||
import { Keypair, SystemProgram, Transaction } from "@solana/web3.js";
|
||||
import { sendTx } from "../utils/send_tx";
|
||||
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";
|
||||
|
||||
/**
|
||||
* Deploy a new SPL token
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param decimals Number of decimals for the token (default: 9)
|
||||
* @param name Name of the token
|
||||
* @param uri URI for the token metadata
|
||||
* @param symbol Symbol of the token
|
||||
* @param initialSupply Initial supply to mint (optional)
|
||||
* @returns Object containing token mint address and initial account (if supply was minted)
|
||||
*/
|
||||
export async function deploy_token(
|
||||
agent: SolanaAgentKit,
|
||||
decimals: number = 9
|
||||
// initialSupply?: number
|
||||
) {
|
||||
decimals: number = 9,
|
||||
name: string,
|
||||
uri: string,
|
||||
symbol: string,
|
||||
initialSupply?: number,
|
||||
): Promise<{ mint: PublicKey }> {
|
||||
try {
|
||||
// Create new token mint
|
||||
const lamports = await getMinimumBalanceForRentExemptAccount(
|
||||
agent.connection
|
||||
);
|
||||
// Create UMI instance from agent
|
||||
const umi = createUmi(agent.connection.rpcEndpoint)
|
||||
|
||||
const mint = Keypair.generate();
|
||||
// Create new token mint
|
||||
const mint = generateSigner(umi);
|
||||
|
||||
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,
|
||||
lamports,
|
||||
space: MINT_SIZE,
|
||||
programId: TOKEN_PROGRAM_ID,
|
||||
let builder = createFungible(umi, {
|
||||
name,
|
||||
uri,
|
||||
symbol,
|
||||
sellerFeeBasisPoints: {
|
||||
basisPoints: 0n,
|
||||
identifier: '%',
|
||||
decimals: 2,
|
||||
},
|
||||
decimals,
|
||||
mint,
|
||||
});
|
||||
|
||||
let create_mint_ix = createInitializeMint2Instruction(
|
||||
mint.publicKey,
|
||||
decimals,
|
||||
agent.wallet_address,
|
||||
agent.wallet_address,
|
||||
TOKEN_PROGRAM_ID
|
||||
);
|
||||
if (initialSupply) {
|
||||
builder = builder.add(mintV1(umi, {
|
||||
mint: mint.publicKey,
|
||||
tokenStandard: TokenStandard.Fungible,
|
||||
tokenOwner: fromWeb3JsPublicKey(agent.wallet_address),
|
||||
amount: initialSupply,
|
||||
}));
|
||||
}
|
||||
|
||||
let tx = new Transaction().add(account_create_ix, create_mint_ix);
|
||||
|
||||
let hash = await sendTx(agent, tx, [mint]);
|
||||
|
||||
console.log("Transaction hash: ", hash);
|
||||
builder.sendAndConfirm(umi);
|
||||
|
||||
console.log(
|
||||
"Token deployed successfully. Mint address: ",
|
||||
@@ -59,7 +63,7 @@ export async function deploy_token(
|
||||
);
|
||||
|
||||
return {
|
||||
mint: mint.publicKey,
|
||||
mint: toWeb3JsPublicKey(mint.publicKey),
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log(error);
|
||||
|
||||
@@ -32,11 +32,11 @@ export async function mintCollectionNFT(
|
||||
): Promise<MintCollectionNFTResponse> {
|
||||
try {
|
||||
// Create UMI instance from agent
|
||||
const umi = createUmi(agent.connection)
|
||||
const umi = createUmi(agent.connection.rpcEndpoint)
|
||||
|
||||
// Convert collection mint to UMI format
|
||||
const umiCollectionMint = fromWeb3JsPublicKey(collectionMint);
|
||||
|
||||
|
||||
// Fetch the existing collection
|
||||
const collection = await fetchCollection(umi, umiCollectionMint);
|
||||
|
||||
@@ -48,7 +48,7 @@ export async function mintCollectionNFT(
|
||||
asset: assetSigner,
|
||||
collection: collection,
|
||||
name: metadata.name,
|
||||
uri: metadata.uri,
|
||||
uri: metadata.uri,
|
||||
owner: fromWeb3JsPublicKey(recipient!)
|
||||
}).sendAndConfirm(umi);
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@ dotenv.config();
|
||||
|
||||
function validateEnvironment(): void {
|
||||
const missingVars: string[] = [];
|
||||
const requiredVars = ["OPENAI_API_KEY", "HELIUS_API_KEY", "SOLANA_PRIVATE_KEY"];
|
||||
|
||||
const requiredVars = ["OPENAI_API_KEY", "RPC_URL", "SOLANA_PRIVATE_KEY"];
|
||||
|
||||
requiredVars.forEach(varName => {
|
||||
if (!process.env[varName]) {
|
||||
missingVars.push(varName);
|
||||
@@ -52,7 +52,7 @@ async function initializeAgent() {
|
||||
|
||||
const solanaKit = new SolanaAgentKit(
|
||||
process.env.SOLANA_PRIVATE_KEY!,
|
||||
`https://mainnet.helius-rpc.com/?api-key=${process.env.HELIUS_API_KEY}`,
|
||||
process.env.RPC_URL,
|
||||
process.env.OPENAI_API_KEY!
|
||||
);
|
||||
|
||||
@@ -176,7 +176,7 @@ async function chooseMode(): Promise<"chat" | "auto"> {
|
||||
.trim();
|
||||
|
||||
rl.close();
|
||||
|
||||
|
||||
if (choice === "1" || choice === "chat") {
|
||||
return "chat";
|
||||
} else if (choice === "2" || choice === "auto") {
|
||||
|
||||
Reference in New Issue
Block a user