Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-utils: Add helper to mint a NFT #217

Merged
merged 2 commits into from Aug 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions 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<TransactionEnvelope> => {
// Temporary mint authority
const tempMintAuthority = provider.wallet.publicKey;
// Mint for the NFT
const tx = await createInitMintInstructions({
provider,
mintKP,
decimals: 0,
mintAuthority: tempMintAuthority,
});
// 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,
tempMintAuthority,
[],
new u64(1)
)
);
// Set mint authority of the NFT to NULL
tx.instructions.push(
SPLToken.createSetAuthorityInstruction(
TOKEN_PROGRAM_ID,
mintKP.publicKey,
null,
"MintTokens",
tempMintAuthority,
[]
)
);

return tx;
};