From b0883477870fe2f733cf9cf94b1d35d0542faec1 Mon Sep 17 00:00:00 2001 From: michaelhly Date: Sun, 29 Aug 2021 19:12:43 -0400 Subject: [PATCH 1/2] token-utils: Add helper to mint a NFT --- packages/token-utils/src/instructions/nft.ts | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 packages/token-utils/src/instructions/nft.ts diff --git a/packages/token-utils/src/instructions/nft.ts b/packages/token-utils/src/instructions/nft.ts new file mode 100644 index 000000000..38ba9468c --- /dev/null +++ b/packages/token-utils/src/instructions/nft.ts @@ -0,0 +1,55 @@ +import type { Provider, TransactionEnvelope } from "@saberhq/solana-contrib"; +import { Token as SPLToken, TOKEN_PROGRAM_ID, u64 } from "@solana/spl-token"; +import type { PublicKey, Signer } from "@solana/web3.js"; + +import { createInitMintInstructions, getOrCreateATA } from "."; + +export const mintNFT = async ( + provider: Provider, + mintKP: Signer, + owner: PublicKey = provider.wallet.publicKey +): Promise => { + // Temporary mint authority + const mintAuthority = provider.wallet.publicKey; + // Mint for the NFT + const tx = await createInitMintInstructions({ + provider, + mintKP, + decimals: 0, + mintAuthority, + }); + // Token account for the NFT + const { address, instruction } = await getOrCreateATA({ + provider, + mint: mintKP.publicKey, + owner: owner, + payer: provider.wallet.publicKey, + }); + if (instruction) { + tx.instructions.push(instruction); + } + // Mint to owner's ATA + tx.instructions.push( + SPLToken.createMintToInstruction( + TOKEN_PROGRAM_ID, + mintKP.publicKey, + address, + mintAuthority, + [], + new u64(1) + ) + ); + // Set mint authority of the NFT to NULL + tx.instructions.push( + SPLToken.createSetAuthorityInstruction( + TOKEN_PROGRAM_ID, + mintKP.publicKey, + null, + "MintTokens", + provider.wallet.publicKey, + [] + ) + ); + + return tx; +}; From d42f42a6edbde1040497ca2e1011349c5cf92729 Mon Sep 17 00:00:00 2001 From: michaelhly Date: Sun, 29 Aug 2021 19:20:37 -0400 Subject: [PATCH 2/2] mintAuthority -> tempMintAuthority --- packages/token-utils/src/instructions/nft.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/token-utils/src/instructions/nft.ts b/packages/token-utils/src/instructions/nft.ts index 38ba9468c..774bf4947 100644 --- a/packages/token-utils/src/instructions/nft.ts +++ b/packages/token-utils/src/instructions/nft.ts @@ -10,13 +10,13 @@ export const mintNFT = async ( owner: PublicKey = provider.wallet.publicKey ): Promise => { // Temporary mint authority - const mintAuthority = provider.wallet.publicKey; + const tempMintAuthority = provider.wallet.publicKey; // Mint for the NFT const tx = await createInitMintInstructions({ provider, mintKP, decimals: 0, - mintAuthority, + mintAuthority: tempMintAuthority, }); // Token account for the NFT const { address, instruction } = await getOrCreateATA({ @@ -34,7 +34,7 @@ export const mintNFT = async ( TOKEN_PROGRAM_ID, mintKP.publicKey, address, - mintAuthority, + tempMintAuthority, [], new u64(1) ) @@ -46,7 +46,7 @@ export const mintNFT = async ( mintKP.publicKey, null, "MintTokens", - provider.wallet.publicKey, + tempMintAuthority, [] ) );