How to write a burn function in smart contract for NFT? - solidity

I am learning how to write a smart contract for NFT collections and the following is the example function given by the tutorial I read:
function _burn(uint256 tokenId) internal virtual override {
super._burn(tokenId);
if (bytes(_tokenURIs[tokenId]).length != 0) {
delete _tokenURIs[tokenId];
}
}
I recognise that this function will not remove the token from the blockchain entirely. Instead, it will remove the URI of the token (regardless of who owns it). As a result, the token will still in the collection and be displayed on trading platforms, but the metadata will be gone (but it may take time to be in effect as platforms are not refreshing metadata frequently).
I wonder if this is the right practice for burn function. It would be greatly helpful for me if someone can provide me an example of how burn function is achieved on other NFT smart contracts.

Here is the easiest way to add burn function to an NFT.
GO to Openzepplin Wizard
Click ERC721
Give your token a name and symbol.
Click on mintable and burnable, and you would get mintable and burnable NFT token contract.
Here is a sample:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC721, ERC721Burnable, Ownable {
constructor() ERC721("MyToken", "MTK") {}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
}
The corresponding OZ wizard interface would look like this:
You would get the following public burn function:
From Openzepplin ERC721 burnable contract
Update
You can make the contract both enumarable and burnable:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC721, ERC721Enumerable, ERC721Burnable, Ownable {
constructor() ERC721("MyToken", "MTK") {}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
// The following functions are overrides required by Solidity.
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}

Related

How to pass a function as an parameter to a contract in solidity

I'm trying to make a dynamic rule builder in solidity, I have a role manager contract that looks like so:
import "#openzeppelin/contracts/access/Ownable.sol";
// The Ownable contract to manage the contract owner
contract RoleManager is Ownable {
mapping(bytes32 => function(bytes32, address, address[] memory, uint256[] memory, bytes[] memory, string memory) external view returns (bool)) public rules;
// Set the rule for the given role
function setRule(bytes32 role, function(bytes32, address, address[] memory, uint256[] memory, bytes[] memory, string memory) external view returns (bool) rule) public onlyOwner {
rules[role] = rule;
}
//...
}
Maybe my understanding of what function parameter is wrong, I would like to build a dynamic function, How to I generate a function on ethers to pass to this ?
Use interface :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/access/Ownable.sol";
interface IRule {
function rule (bytes32 data, address addr,
address[] memory addrArr, uint256[] memory uintArr,
bytes[] memory dataArr, string memory str)
external view returns (bool);
}
contract Demo is Ownable {
mapping(bytes32 => IRule) public rules;
function setRule(bytes32 role, IRule rule) public onlyOwner {
rules[role] = rule;
}
}

Inheriting multiple ERC721 extensions

I would like to use both ERC721URIStorage for setting token URI easily but I would also like the methods in ERC721Enumberable to fetch the number of tokens made by an address.
I get this error when trying to use both :
Derived contract must override function "_beforeTokenTransfer". Two or more base classes define function with same name and parameter types.
Derived contract must override function "_burn". Two or more base classes define function with same name and parameter types.
Derived contract must override function "supportsInterface". Two or more base classes define function with same name and parameter types.
Derived contract must override function "tokenURI". Two or more base classes define function with same name and parameter types.
is this just not possible at all or is there a way to Override the duplicated functions?
Below code overrides the overlapping functions from the two extensions
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract NameOfContract is ERC721URIStorage, ERC721Enumerable {
// Contract Code ....
function _beforeTokenTransfer(address from, address to, uint256 tokenId)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId);
}
function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
super._burn(tokenId);
}
function tokenURI(uint256 tokenId)
public
view
override(ERC721, ERC721URIStorage)
returns (string memory)
{
return super.tokenURI(tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721, ERC721Enumerable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
Update
_beforeTokenTransfer has a new parameter and argument
Now it is like
function _beforeTokenTransfer(address from, address to, uint256 tokenId, uint256 batchSize)
internal
override(ERC721, ERC721Enumerable)
{
super._beforeTokenTransfer(from, to, tokenId, batchSize);
}

This contract may be abstract, not implement an abstract parent's methods completely or not invoke an inherited contract's constructor correctly

When I tried to deploy this ERC20 smart contract in RemixIDE, It give warning: This contract may be abstract, not implement an abstract parent's methods completely or not invoke an inherited contract's constructor correctly. and not let me deploy the contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
constructor() ERC20("MyToken", "MTK") {}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}

The constructor should be payable if you send value

I'm running this code on remix IDE. Everything is working fine except the function transfertocontracts(uint amount) public. I'm trying to transfer some ethers for e.g. 10 to my contract and then later using this function function Transfer_Contract_Amount() public I will transfer all the amount of contract to specific address.
The problem is that when I run function transfertocontracts(uint amount) public I'm getting this Error:
Note: The constructor should be payable if you send value. debug the transaction to get more information.
contract SLA {
address seller;
event DepositFunds(address from, uint amount);
constructor() payable public {
seller = msg.sender;
}
function transfertocontracts(uint amount) public {
address(this).transfer(amount);
}
function seePerson_Amount() public view returns(uint) {
return seller.balance;
}
function seeContract_Amount() public view returns(uint) {
return address(this).balance;
}
function Transfer_Contract_Amount() public {
seller.transfer(address(this).balance);
}
}
You can send ether only by using the value attribute which you can set while interacting with the Smart Contract. Below is the working code
pragma solidity >=0.4.22 <0.6.0;
contract SLA{
address payable seller;
event DepositFunds(address from, uint amount);
constructor() payable public {
seller = msg.sender;
}
function transfertocontracts(uint amount) payable public{
}
function seePerson_Amount() public view returns(uint){
return seller.balance;
}
function seeContract_Amount() public view returns(uint){
return address(this).balance;
}
function Transfer_Contract_Amount() payable public{
seller.transfer(address(this).balance);
}
}
To check you can run it on remix and on the run tab on the left side you can putt the value of ether to send and run the transfertocontracts function further to store the value its a good idea to use the msg.value.

Interface function in Ethereum contract

I was looking at the code on the Ethereum website, what does this code do?
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData); }
Available at: https://ethereum.org/token#full-coin-code
Thanks!
It does nothing as it's an interface. An interface is a way to define constraints so that you can communicate with any object that implements this and know that the functions defined in the interface will existing in the implementation.
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
}
One example of this would be a way from one contract to communicate to another and looks like an ERC223 implementation for a recipient
In contract you want to communicate to you have to have implemented the tokenRecipient e.g.
pragma solidity ^0.4.0;
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
}
contract MyContract is tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) {
// functionality
}
}
So if we now implement a contract me know that we can interact with any contract that has tokenRecipient implemented e.g.
pragma solidity ^0.4.0;
interface tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
}
contract RemoteContract {
function func(address _addr, uint _value) {
tokenRecipient _tokenRecipient = tokenRecipient(_addr);
_tokenRecipient.receiveApproval(msg.sender, _value, address(this), empty);
}
}
It will error if the call fails which is what we need to happen to prevent things such as a token being sent to a contract which cannot do anything with it. As Solidity is atom and works in a transactional way if the contract cannot receive it then it will rollback all functionality already executed.