minting.move 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. module mint_nft::minting {
  2. use std::signer;
  3. use std::string::{Self, String};
  4. use std::vector;
  5. use aptos_framework::account;
  6. use aptos_framework::event::EventHandle;
  7. use aptos_framework::coin;
  8. use aptos_framework::aptos_coin;
  9. use pyth::pyth;
  10. use pyth::price_identifier;
  11. use pyth::i64;
  12. use pyth::price::{Self,Price};
  13. use aptos_std::math64::pow;
  14. use aptos_token::token::{Self, TokenDataId};
  15. // For the entire list of price_ids head to https://pyth.network/developers/price-feed-ids/#pyth-cross-chain-testnet
  16. const APTOS_USD_PRICE_FEED_IDENTIFIER : vector<u8> = x"44a93dddd8effa54ea51076c4e851b6cbbfd938e82eb90197de38fe8876bb66e";
  17. // This event stores the receiver of the NFT and the TokenDataId of the NFT
  18. struct TokenMintingEvent has drop, store {
  19. token_receiver_address: address,
  20. token_data_id: TokenDataId,
  21. }
  22. // This struct stores an NFT relevant information and the signer capability required to mint the NFT
  23. struct CollectionTokenMinter has key {
  24. token_data_id: TokenDataId,
  25. token_minting_events: EventHandle<TokenMintingEvent>,
  26. signer_cap: account::SignerCapability
  27. }
  28. /// Octas per aptos coin
  29. const OCTAS_PER_APTOS: u64 = 100000000;
  30. /// Initialize this module: create a resource account, a collection, and a token data id, all the NFTs minted are editions of the same TokenDataId
  31. fun init_module(resource_account: &signer) {
  32. let collection_name = string::utf8(b"Pythians");
  33. let description = string::utf8(b"Pythians");
  34. let collection_uri = string::utf8(b"https://pyth.network/");
  35. let token_name = string::utf8(b"Pythian by @EgorNaive");
  36. let token_uri = string::utf8(b"https://pbs.twimg.com/media/FeVw9JPWYAAsiI6?format=jpg&name=medium");
  37. // Create the resource account that we'll use to create tokens
  38. let (resource_signer, resource_signer_cap) = account::create_resource_account(resource_account, b"candy-machine");
  39. // Create the nft collection
  40. let maximum_supply = 1; // There's only 1 NFT in the collection
  41. let mutate_setting = vector<bool>[ false, false, false ];
  42. let resource_account_address = signer::address_of(&resource_signer);
  43. token::create_collection(&resource_signer, collection_name, description, collection_uri, maximum_supply, mutate_setting);
  44. // Create a token data id to specify which token will be minted
  45. let token_data_id = token::create_tokendata(
  46. &resource_signer,
  47. collection_name,
  48. token_name,
  49. string::utf8(b""),
  50. 0, // 0 means the supply is infinite
  51. token_uri,
  52. resource_account_address,
  53. 0,
  54. 0,
  55. // We don't allow any mutation to the token
  56. token::create_token_mutability_config(
  57. &vector<bool>[ false, false, false, false, true ]
  58. ),
  59. vector::empty<String>(),
  60. vector::empty<vector<u8>>(),
  61. vector::empty<String>(),
  62. );
  63. move_to(resource_account, CollectionTokenMinter {
  64. token_data_id,
  65. token_minting_events: account::new_event_handle<TokenMintingEvent>(resource_account),
  66. signer_cap : resource_signer_cap
  67. });
  68. }
  69. /// Mint an edition of the Pythian NFT pay 1 USD in native APT
  70. public entry fun mint_nft(receiver : &signer, vaas : vector<vector<u8>>) acquires CollectionTokenMinter{
  71. // Fetch the signer capability to mint the NFT
  72. let collection_token_minter = borrow_global_mut<CollectionTokenMinter>(@mint_nft);
  73. let resource_signer = account::create_signer_with_capability(&collection_token_minter.signer_cap);
  74. let token_id = token::mint_token(&resource_signer, collection_token_minter.token_data_id, 1); // Mint the NFT
  75. token::direct_transfer(&resource_signer, receiver, token_id, 1); // Transfer the NFT to the caller
  76. let price = update_and_fetch_price(receiver, vaas);
  77. let price_positive = i64::get_magnitude_if_positive(&price::get_price(&price)); // This will fail if the price is negative
  78. let expo_magnitude = i64::get_magnitude_if_negative(&price::get_expo(&price)); // This will fail if the exponent is positive
  79. let price_in_aptos_coin = (OCTAS_PER_APTOS * pow(10, expo_magnitude)) / price_positive; // 1 USD in APT
  80. coin::transfer<aptos_coin::AptosCoin>(receiver, @mint_nft, price_in_aptos_coin); // Pay for the NFT
  81. }
  82. /// Please read https://docs.pyth.network/consume-data/best-practices before using a `Price` in your application
  83. fun update_and_fetch_price(receiver : &signer, vaas : vector<vector<u8>>) : Price {
  84. let coins = coin::withdraw<aptos_coin::AptosCoin>(receiver, pyth::get_update_fee(&vaas)); // Get coins to pay for the update
  85. pyth::update_price_feeds(vaas, coins); // Update price feed with the provided vaas
  86. pyth::get_price(price_identifier::from_byte_vec(APTOS_USD_PRICE_FEED_IDENTIFIER)) // Get recent price (will fail if price is too old)
  87. }
  88. }