Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,16 +28,19 @@ 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);

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;
Expand All @@ -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;
Expand All @@ -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);
}
}
19 changes: 11 additions & 8 deletions contracts/ERC721.sol
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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");

Expand All @@ -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, "")
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
16 changes: 16 additions & 0 deletions contracts/Ownable.sol
Original file line number Diff line number Diff line change
@@ -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!");
_;
}

}