Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
token-utils: Add helper to mint a NFT (#217)
* token-utils: Add helper to mint a NFT * mintAuthority -> tempMintAuthority
- Loading branch information
1 parent
0c5e0de
commit 51390aa
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
51390aa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: