Skip to content

Commit

Permalink
Merge pull request #1360 from DawnBlackA/task03
Browse files Browse the repository at this point in the history
task3: DawnBlackA
  • Loading branch information
linghuccc authored Aug 8, 2024
2 parents cc90e7c + d161cb2 commit dec95ed
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
93 changes: 93 additions & 0 deletions members/DawnBlackA/task3/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.10;

import "src/interface/IERC20.sol";
import "forge-std/console.sol";

contract ERC20 is IERC20 {

mapping(address => uint256) public override balanceOf;

mapping(address => mapping(address => uint256)) public override allowance;

mapping (address => bool) admin;

uint256 public override totalSupply;

string public name;
string public symbol;

uint8 public decimals = 18;

address private owner;
// address private admin;

constructor(string memory _name,string memory _symbol) {
name = _name;
_symbol = symbol;
owner = msg.sender;
admin[msg.sender] = true;
_mint(msg.sender,21000000*10**18);
}

function setAdmin(address _admin) external {
require(msg.sender == owner,"contract:::ERC20.sol::ERC20:You must be owner.");
// admin = _admin;
admin[_admin] = true;
}

modifier adminOnly {
require(admin[msg.sender],"contract:::ERC20.sol::ERC20:You must be admin.");
_;
}

function transfer(address to,uint256 amount) public override returns (bool) {
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;

emit Transfer(msg.sender, to, amount);
return true;
}

function approve(address spender,uint256 amount) public override returns (bool) {
console.log(spender);
allowance[msg.sender][spender] = amount;
emit Approval(owner, spender, amount);
return true;
}

function transferFrom(address from,address to,uint256 amount) public override returns (bool) {
allowance[from][msg.sender] -= amount;
balanceOf[from] -= amount;
balanceOf[to] += amount;
emit Transfer(from, to, amount);
return true;
}

function _mint(address account,uint256 amount) internal {
balanceOf[account] += amount;
totalSupply += amount;
emit Transfer(address(0), account, amount);
}

function mint(uint256 amount) external {
_mint(msg.sender, amount);
}

function _burn(address account,uint amount) internal {
balanceOf[account] -= amount;
totalSupply -= amount;
emit Transfer(account, address(0), amount);
}

function burn(uint amount) external adminOnly() {
_burn(msg.sender, amount);
}

// function request() external adminOnly(){
// balanceOf[address(this)] -= 1000;
// balanceOf[msg.sender] += 1000;
// emit Transfer(address(this), msg.sender, 1000);
// }

}
104 changes: 104 additions & 0 deletions members/DawnBlackA/task3/Market.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.13;


import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "src/contract/ERC20.sol";
import "src/contract/NFTDemo.sol";

contract Market{

address internal erc20;
uint uploadCount = 0;
mapping (address => mapping (uint => uint)) nftID;
mapping (uint => address) ownerAddress;
mapping (uint => address) nftAddress;
mapping (uint => uint) nftTokenId;
mapping (uint => uint) price;
mapping (uint => string) tokenURI;
mapping (uint => bool) inSelling;

struct NFT {
uint NFTId;
uint TokenId;
address Address;
address Owner;
uint Price;
string TokenURI;
bool InSelling;
}

NFT[] public NFTs;

constructor(address _ERC20) {
erc20 = _ERC20;
// nftDemo = _NFTDemo;
}

function upload(uint tokenId,uint _price,address nftDemo) external {

require(NFTDemo(nftDemo).ownerOf(tokenId) == msg.sender,"contract:::Market::upload:You need to be the Owner of the token.");
require(!inSelling[nftID[nftDemo][tokenId]],"contract:::Market::upload:NFT has been uploaded.");


uint nftId = uploadCount++;
nftID[nftDemo][tokenId] = nftId;
ownerAddress[nftId] = NFTDemo(nftDemo).ownerOf(tokenId);
nftAddress[nftId] = nftDemo;
nftTokenId[nftId] = tokenId;
price[nftId] = _price;
tokenURI[nftId] = NFTDemo(nftDemo).tokenURI(tokenId);
inSelling[nftId] = true;

NFTs.push(NFT({NFTId: nftId,TokenId: nftTokenId[nftId],Address: nftAddress[nftId],Owner: ownerAddress[nftId],Price: price[nftId],TokenURI: tokenURI[nftId],InSelling: inSelling[nftId]}));
}

function download(uint _nftId) external {
require(ownerAddress[_nftId] == msg.sender,"contract:::Market::download:You need to be the Owner of the token.");
inSelling[_nftId] = false;
NFTs[_nftId].InSelling = inSelling[_nftId];
}

function buy(uint _nftId) external {
require(inSelling[_nftId],"contract:::Market::buy:The token isn't in selling.");
require(ownerAddress[_nftId] != msg.sender, "contract:::Market::buy:You are the owner of the NFT.");
address owner = ownerAddress[_nftId];

// ERC20(erc20).approve(address(this), price[_nftId]);
ERC20(erc20).transferFrom(msg.sender, owner, price[_nftId]);

NFTDemo(nftAddress[_nftId]).safeTransferFrom(owner, msg.sender, nftTokenId[_nftId]);
inSelling[_nftId] = false;
NFTs[_nftId].InSelling = inSelling[_nftId];
}

function getAllNFTs() external view returns (NFT[] memory) {
return NFTs;
}


// function getNFTokenId(uint _nftId) external view returns(uint) {
// return nftTokenId[_nftId];
// }

// function getInSelling(uint _nftId) external view returns(bool) {
// return inSelling[_nftId];
// }

// function getNFTAddress(uint _nftId) external view returns(address) {
// return nftAddress[_nftId];
// }

// function getPrice(uint _nftId) external view returns(uint) {
// return price[_nftId];
// }

// function getOwner(uint _nftId) external view returns(address) {
// return ownerAddress[_nftId];
// }

function getUploadCount() external view returns (uint) {
return uploadCount;
}

}
56 changes: 56 additions & 0 deletions members/DawnBlackA/task3/NFTDemo.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.13;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
// import "forge-std/console.sol";

contract NFTDemo is ERC721 {

address internal owner;
string public baseTokenURI;

constructor(string memory _name,string memory _symbol) ERC721(_name,_symbol){
owner = msg.sender;
}

mapping (address => bool) admin;

uint public Max = 10000;

function setAdmin(address to) external {
require(msg.sender == owner,"contract:::NFTDemo::NFTDemo:Owner Only.");
admin[to] = true;
}

modifier adminOnly {
require(admin[msg.sender],"contract:::NFTDemo::NFTDemo:Admin Only.");
_;
}

function _baseURI() internal view override returns (string memory) {
return baseTokenURI;
}

function mint(uint tokenId,string memory _baseTokenURI) external {
require(tokenId>0 && tokenId<Max,"contract:::NFTDemo::NFTDemo:tokenId out of range");
baseTokenURI = _baseTokenURI;
// _baseURI();
_mint(msg.sender, tokenId);
}

function getOwner(uint256 tokenId) public view returns (address) {
// console.log("12");
// return address(msg.sender);
return ownerOf(tokenId);
// return _baseURI();
}

function safeTransferFrom(address from,address to,uint256 tokenId) public override {
// console.log("1");
// console.log("2");
// console.log(ownerOf(1));
// console.log("3");
super.safeTransferFrom(from,to,tokenId);
}

}
15 changes: 15 additions & 0 deletions members/DawnBlackA/task3/task3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ERC20: 0x638a441770C5484Bf0125CA7a1af30E802022ee1

0x4142495ddda20abe630f6add75ede37cf9cf1b592065f0444d18de6d2b3ce362

NFTDemo: 0xcf7bedda290c41865abbd535e85d601a94602880

0x6e2601a40ef67c33ffe983a8fff59b6dda4242d1a9841566c018280c9d60869b

Market: 0xfffdb34db968B2ea6ba0975cF64985890d25F10f

0xcc7ba454d50565ba06c99c36597468674c252af687ab010459d74f7bc0bf303b

上架NFT:0x1c29e3b09e4f949112f88a143907f8b62f55df23904edb149cc255b2fb7b1971

购买NFT:0xc02ad9c17324403ac500ce4684504e77882364f1d1f2757889e04606bb7a9b2f

0 comments on commit dec95ed

Please sign in to comment.