nft-minter.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import "../libraries/spl_token.sol";
  2. import "../libraries/mpl_metadata.sol";
  3. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  4. contract nft_minter {
  5. @payer(payer)
  6. constructor() {}
  7. @mutableSigner(payer) // payer account
  8. @mutableSigner(mint) // mint account to be created
  9. @mutableAccount(metadata) // metadata account to be created
  10. @signer(mintAuthority) // mint authority for the mint account
  11. @account(metadataProgramId)
  12. @account(rentAddress)
  13. function createTokenMint(
  14. address freezeAuthority, // freeze authority for the mint account
  15. uint8 decimals, // decimals for the mint account
  16. string name, // name for the metadata account
  17. string symbol, // symbol for the metadata account
  18. string uri // uri for the metadata account
  19. ) external {
  20. // Invoke System Program to create a new account for the mint account and,
  21. // Invoke Token Program to initialize the mint account
  22. // Set mint authority, freeze authority, and decimals for the mint account
  23. SplToken.create_mint(
  24. tx.accounts.payer.key, // payer account
  25. tx.accounts.mint.key, // mint account
  26. tx.accounts.mintAuthority.key, // mint authority
  27. freezeAuthority, // freeze authority
  28. decimals // decimals
  29. );
  30. // Invoke Metadata Program to create a new account for the metadata account
  31. MplMetadata.create_metadata_account(
  32. tx.accounts.metadata.key, // metadata account
  33. tx.accounts.mint.key, // mint account
  34. tx.accounts.mintAuthority.key, // mint authority
  35. tx.accounts.payer.key, // payer
  36. tx.accounts.payer.key, // update authority (of the metadata account)
  37. name, // name
  38. symbol, // symbol
  39. uri, // uri (off-chain metadata json)
  40. tx.accounts.metadataProgramId.key,
  41. tx.accounts.rentAddress.key
  42. );
  43. }
  44. @mutableSigner(payer)
  45. @mutableAccount(tokenAccount)
  46. @account(owner)
  47. @mutableAccount(mint)
  48. function mintTo() external {
  49. // Create an associated token account for the owner to receive the minted token
  50. SplToken.create_associated_token_account(
  51. tx.accounts.payer.key, // payer account
  52. tx.accounts.tokenAccount.key, // associated token account address
  53. tx.accounts.mint.key, // mint account
  54. tx.accounts.owner.key // owner account
  55. );
  56. // Mint 1 token to the associated token account
  57. SplToken.mint_to(
  58. tx.accounts.mint.key, // mint account
  59. tx.accounts.tokenAccount.key, // token account
  60. tx.accounts.payer.key, // mint authority
  61. 1 // amount
  62. );
  63. // Remove mint authority from mint account
  64. SplToken.remove_mint_authority(
  65. tx.accounts.mint.key, // mint
  66. tx.accounts.payer.key // mint authority
  67. );
  68. }
  69. }