From ae73107d235e0628df98fac3252b83e8fbdfa7c2 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 13 Oct 2022 17:11:51 +0000 Subject: [PATCH] refactor: do not receive token id as mint argument --- --- a/pallets/nonfungible/src/erc.rs +++ b/pallets/nonfungible/src/erc.rs @@ -193,7 +193,7 @@ } #[derive(ToLog)] -pub enum ERC721MintableEvents { +pub enum ERC721UniqueMintableEvents { #[allow(dead_code)] MintingFinished {}, } @@ -431,19 +431,33 @@ } /// @title ERC721 minting logic. -#[solidity_interface(name = ERC721Mintable, events(ERC721MintableEvents))] +#[solidity_interface(name = ERC721UniqueMintable, events(ERC721UniqueMintableEvents))] impl NonfungibleHandle { fn minting_finished(&self) -> Result { Ok(false) } /// @notice Function to mint token. + /// @param to The new owner + /// @return uint256 The id of the newly minted token + #[weight(>::create_item())] + fn mint(&mut self, caller: caller, to: address) -> Result { + let token_id: uint256 = >::get(self.id) + .checked_add(1) + .ok_or("item id overflow")? + .into(); + self.mint_check_id(caller, to, token_id)?; + Ok(token_id) + } + + /// @notice Function to mint token. /// @dev `tokenId` should be obtained with `nextTokenId` method, /// unlike standard, you can't specify it manually /// @param to The new owner /// @param tokenId ID of the minted NFT + #[solidity(hide, rename_selector = "mint")] #[weight(>::create_item())] - fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result { + fn mint_check_id(&mut self, caller: caller, to: address, token_id: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller); let to = T::CrossAccountId::from_eth(to); let token_id: u32 = token_id.try_into()?; @@ -474,14 +488,34 @@ } /// @notice Function to mint token with the given tokenUri. + /// @param to The new owner + /// @param tokenUri Token URI that would be stored in the NFT properties + /// @return uint256 The id of the newly minted token + #[solidity(rename_selector = "mintWithTokenURI")] + #[weight(>::create_item())] + fn mint_with_token_uri( + &mut self, + caller: caller, + to: address, + token_uri: string, + ) -> Result { + let token_id: uint256 = >::get(self.id) + .checked_add(1) + .ok_or("item id overflow")? + .into(); + self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?; + Ok(token_id) + } + + /// @notice Function to mint token with the given tokenUri. /// @dev `tokenId` should be obtained with `nextTokenId` method, /// unlike standard, you can't specify it manually /// @param to The new owner /// @param tokenId ID of the minted NFT /// @param tokenUri Token URI that would be stored in the NFT properties - #[solidity(rename_selector = "mintWithTokenURI")] + #[solidity(hide, rename_selector = "mintWithTokenURI")] #[weight(>::create_item())] - fn mint_with_token_uri( + fn mint_with_token_uri_check_id( &mut self, caller: caller, to: address, @@ -637,6 +671,7 @@ /// should be obtained with `nextTokenId` method /// @param to The new owner /// @param tokenIds IDs of the minted NFTs + #[solidity(hide)] #[weight(>::create_multiple_items(token_ids.len() as u32))] fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec) -> Result { let caller = T::CrossAccountId::from_eth(caller); @@ -673,7 +708,7 @@ /// numbers and first number should be obtained with `nextTokenId` method /// @param to The new owner /// @param tokens array of pairs of token ID and token URI for minted tokens - #[solidity(rename_selector = "mintBulkWithTokenURI")] + #[solidity(hide, rename_selector = "mintBulkWithTokenURI")] #[weight(>::create_multiple_items(tokens.len() as u32))] fn mint_bulk_with_token_uri( &mut self, @@ -728,7 +763,7 @@ ERC721, ERC721Enumerable, ERC721UniqueExtensions, - ERC721Mintable, + ERC721UniqueMintable, ERC721Burnable, ERC721Metadata(if(this.flags.erc721metadata)), Collection(via(common_mut returns CollectionHandle)), --- a/pallets/refungible/src/erc.rs +++ b/pallets/refungible/src/erc.rs @@ -187,7 +187,7 @@ } #[derive(ToLog)] -pub enum ERC721MintableEvents { +pub enum ERC721UniqueMintableEvents { /// @dev Not supported #[allow(dead_code)] MintingFinished {}, @@ -449,19 +449,33 @@ } /// @title ERC721 minting logic. -#[solidity_interface(name = ERC721Mintable, events(ERC721MintableEvents))] +#[solidity_interface(name = ERC721UniqueMintable, events(ERC721UniqueMintableEvents))] impl RefungibleHandle { fn minting_finished(&self) -> Result { Ok(false) } /// @notice Function to mint token. + /// @param to The new owner + /// @return uint256 The id of the newly minted token + #[weight(>::create_item())] + fn mint(&mut self, caller: caller, to: address) -> Result { + let token_id: uint256 = >::get(self.id) + .checked_add(1) + .ok_or("item id overflow")? + .into(); + self.mint_check_id(caller, to, token_id)?; + Ok(token_id) + } + + /// @notice Function to mint token. /// @dev `tokenId` should be obtained with `nextTokenId` method, /// unlike standard, you can't specify it manually /// @param to The new owner /// @param tokenId ID of the minted RFT + #[solidity(hide, rename_selector = "mint")] #[weight(>::create_item())] - fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result { + fn mint_check_id(&mut self, caller: caller, to: address, token_id: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller); let to = T::CrossAccountId::from_eth(to); let token_id: u32 = token_id.try_into()?; @@ -497,14 +511,34 @@ } /// @notice Function to mint token with the given tokenUri. + /// @param to The new owner + /// @param tokenUri Token URI that would be stored in the NFT properties + /// @return uint256 The id of the newly minted token + #[solidity(rename_selector = "mintWithTokenURI")] + #[weight(>::create_item())] + fn mint_with_token_uri( + &mut self, + caller: caller, + to: address, + token_uri: string, + ) -> Result { + let token_id: uint256 = >::get(self.id) + .checked_add(1) + .ok_or("item id overflow")? + .into(); + self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?; + Ok(token_id) + } + + /// @notice Function to mint token with the given tokenUri. /// @dev `tokenId` should be obtained with `nextTokenId` method, /// unlike standard, you can't specify it manually /// @param to The new owner /// @param tokenId ID of the minted RFT /// @param tokenUri Token URI that would be stored in the RFT properties - #[solidity(rename_selector = "mintWithTokenURI")] + #[solidity(hide, rename_selector = "mintWithTokenURI")] #[weight(>::create_item())] - fn mint_with_token_uri( + fn mint_with_token_uri_check_id( &mut self, caller: caller, to: address, @@ -671,6 +705,7 @@ /// should be obtained with `nextTokenId` method /// @param to The new owner /// @param tokenIds IDs of the minted RFTs + #[solidity(hide)] #[weight(>::create_multiple_items(token_ids.len() as u32))] fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec) -> Result { let caller = T::CrossAccountId::from_eth(caller); @@ -713,7 +748,7 @@ /// numbers and first number should be obtained with `nextTokenId` method /// @param to The new owner /// @param tokens array of pairs of token ID and token URI for minted tokens - #[solidity(rename_selector = "mintBulkWithTokenURI")] + #[solidity(hide, rename_selector = "mintBulkWithTokenURI")] #[weight(>::create_multiple_items(tokens.len() as u32))] fn mint_bulk_with_token_uri( &mut self, @@ -784,7 +819,7 @@ ERC721, ERC721Enumerable, ERC721UniqueExtensions, - ERC721Mintable, + ERC721UniqueMintable, ERC721Burnable, ERC721Metadata(if(this.flags.erc721metadata)), Collection(via(common_mut returns CollectionHandle)), -- gitstuff