Updating NFTs to Core and adding token metadata.

This commit is contained in:
Blockiosaurus
2024-12-15 19:24:42 -05:00
parent ff77c6a5aa
commit 9f308de533
8 changed files with 1245 additions and 1499 deletions

View File

@@ -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) {

View File

@@ -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, {

View File

@@ -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);

View File

@@ -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);