diff --git a/contracts/ERC20.sol b/contracts/ERC20.sol index 06e1ed09..ee3d0de4 100644 --- a/contracts/ERC20.sol +++ b/contracts/ERC20.sol @@ -1,19 +1,24 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.24; +pragma solidity >=0.7.0 <0.9.0 ; import "./IERC20.sol"; +import "./Ownable.sol"; -contract ERC20 is IERC20 { +contract ERC20 is IERC20, Ownable { event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); + event Mint(address indexed to, uint256 amount); + event Burn(address indexed from, uint256 amount); + uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; string public name; string public symbol; + uint8 public decimals; constructor(string memory _name, string memory _symbol, uint8 _decimals) { name = _name; @@ -23,8 +28,11 @@ contract ERC20 is IERC20 { function transfer(address recipient, uint256 amount) external + onlyOwner returns (bool) { + require(recipient != address(0), "A Transfer to Address Zero!"); + require(amount <= balanceOf[msg.sender]); balanceOf[msg.sender] -= amount; balanceOf[recipient] += amount; emit Transfer(msg.sender, recipient, amount); @@ -32,7 +40,7 @@ contract ERC20 is IERC20 { return true; } - function approve(address spender, uint256 amount) external returns (bool) { + function approve(address spender, uint256 amount) external onlyOwner returns (bool) { allowance[msg.sender][spender] = amount; emit Approval(msg.sender, spender, amount); return true; @@ -42,6 +50,8 @@ contract ERC20 is IERC20 { external returns (bool) { + require(amount <= allowance[sender][msg.sender], "Transfer amount exceeds allowance"); + require(amount <= balanceOf[msg.sender]); require(msg.sender != recipient, "cannot transfer to self"); allowance[sender][msg.sender] -= amount; balanceOf[sender] -= amount; @@ -50,23 +60,25 @@ contract ERC20 is IERC20 { return true; } - function _mint(address to, uint256 amount) internal { + function _mint(address to, uint256 amount) onlyOwner internal { + require(to != address(0), "You can't mint to Address Zero!"); balanceOf[to] += amount; totalSupply += amount; - emit Transfer(address(0), to, amount); + emit Mint(to, amount); } - function _burn(address from, uint256 amount) internal { + function _burn(address from, uint256 amount) onlyOwner internal { balanceOf[from] -= amount; totalSupply -= amount; - emit Transfer(from, address(0), amount); + emit Burn(from, amount); + } - function mint(address to, uint256 amount) external { + function mint(address to, uint256 amount) onlyOwner external { _mint(to, amount); } - function burn(address from, uint256 amount) external { + function burn(address from, uint256 amount) onlyOwner external { _burn(from, amount); } } diff --git a/contracts/ERC721.sol b/contracts/ERC721.sol index b53814c2..54c371e8 100644 --- a/contracts/ERC721.sol +++ b/contracts/ERC721.sol @@ -1,5 +1,7 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.24; +pragma solidity >=0.7.0 <0.9.0 ; +import "contracts/Ownable.sol"; + interface IERC165 { function supportsInterface(bytes4 interfaceID) @@ -41,7 +43,7 @@ interface IERC721Receiver { ) external returns (bytes4); } -contract ERC721 is IERC721 { +contract ERC721 is IERC721, Ownable { event Transfer( address indexed from, address indexed to, uint256 indexed id ); @@ -88,7 +90,7 @@ contract ERC721 is IERC721 { emit ApprovalForAll(msg.sender, operator, approved); } - function approve(address spender, uint256 id) external { + function approve(address spender, uint256 id) external onlyOwner { address owner = _ownerOf[id]; require( msg.sender == owner || isApprovedForAll[owner][msg.sender], @@ -117,6 +119,7 @@ contract ERC721 is IERC721 { } function transferFrom(address from, address to, uint256 id) public { + require(_ownerOf[id] != address(0), "token doesn't exist"); require(from == _ownerOf[id], "from != owner"); require(to != address(0), "transfer to zero address"); @@ -133,7 +136,7 @@ contract ERC721 is IERC721 { function safeTransferFrom(address from, address to, uint256 id) external { transferFrom(from, to, id); - + require(_isApprovedOrOwner(from, msg.sender, id), "not authorized"); require( to.code.length == 0 || IERC721Receiver(to).onERC721Received(msg.sender, from, id, "") @@ -149,7 +152,7 @@ contract ERC721 is IERC721 { bytes calldata data ) external { transferFrom(from, to, id); - + require(_isApprovedOrOwner(from, msg.sender, id), "not authorized"); require( to.code.length == 0 || IERC721Receiver(to).onERC721Received(msg.sender, from, id, data) @@ -182,12 +185,12 @@ contract ERC721 is IERC721 { } contract MyNFT is ERC721 { - function mint(address to, uint256 id) external { + function mint(address to, uint256 id) external onlyOwner { _mint(to, id); } - function burn(uint256 id) external { + function burn(uint256 id) external onlyOwner { require(msg.sender == _ownerOf[id], "not owner"); _burn(id); } -} +} \ No newline at end of file diff --git a/contracts/Ownable.sol b/contracts/Ownable.sol new file mode 100644 index 00000000..c0c5a2a5 --- /dev/null +++ b/contracts/Ownable.sol @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.7.0 <0.9.0 ; + +contract Ownable{ + address public owner; + + constructor() { + owner = msg.sender; + } + + modifier onlyOwner () { + require(msg.sender == owner, "You're not the Owner!"); + _; + } + +} \ No newline at end of file