Skip to content

Commit

Permalink
add all tests and fix nftMarket
Browse files Browse the repository at this point in the history
  • Loading branch information
XieJunhua committed Apr 25, 2024
1 parent d33beb6 commit 655b3df
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
"editor.defaultFormatter": "NomicFoundation.hardhat-solidity"
},
"[toml]": {
"editor.defaultFormatter": "tamasfe.even-better-toml"
Expand Down
21 changes: 7 additions & 14 deletions src/Week2/BaseERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract BaseERC20 {
}

function transfer(address _to, uint256 _value) public returns (bool success) {
if (balances[msg.sender] >= _value) {
if (balances[msg.sender] < _value) {
revert AmountExceedError(msg.sender);
}
balances[msg.sender] = balances[msg.sender] - _value;
Expand All @@ -58,13 +58,13 @@ contract BaseERC20 {
}

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
if (allowances[_from][msg.sender] >= _value) {
if (allowances[_from][msg.sender] < _value) {
revert AllowancedExceedError(msg.sender);
}

allowances[_from][msg.sender] = allowances[_from][msg.sender] - _value;

if (balances[_from] >= _value) {
if (balances[_from] < _value) {
revert AmountExceedError(_from);
}

Expand Down Expand Up @@ -93,6 +93,8 @@ contract TokenBank is TokenRecipient {
event Log(string);
event Print(string, address);

event LogExtraData(address _from, bytes _data);

constructor(address _tokenAddress) {
tokenAddress = _tokenAddress;
}
Expand All @@ -106,23 +108,13 @@ contract TokenBank is TokenRecipient {
}
}

// function depositNew(uint256 value) public returns (bool) {
// bool result = BaseERC20(tokenAddress).transferWithFallback(address(this), value);

// if (result) {
// accounts[msg.sender] = accounts[msg.sender] + value;
// emit Log("save token success");
// }
// return result;
// }

function getAccounts() public view returns (uint256) {
return accounts[msg.sender];
}

function withdraw(uint256 value) public {
// require(value <= accounts[msg.sender], "you don't have enough money");
if (value <= accounts[msg.sender]) {
if (value < accounts[msg.sender]) {
revert AmountExceedError(msg.sender);
}

Expand All @@ -139,6 +131,7 @@ contract TokenBank is TokenRecipient {
revert InvalidAddressError(_address);
}
accounts[_address] = accounts[_address] + value;
emit LogExtraData(_address, extraData);
return true;
}
}
23 changes: 3 additions & 20 deletions src/Week2/NFTMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ contract NFTMarket is IERC721Receiver, TokenRecipient {
if (!owner) {
revert AccessError(nftAddress);
}
// require(owner, "you are not owner");

// NFT memory n;
// n.price = price;
// n.ownerAddress = msg.sender;

// products[_tokenId] = n;
products[_tokenId] = NFT({ price: price, ownerAddress: msg.sender });

return true;
Expand All @@ -50,12 +43,8 @@ contract NFTMarket is IERC721Receiver, TokenRecipient {
function buyNFT(uint256 _tokenId) public returns (bool) {
NFT memory n = products[_tokenId];
uint256 price = n.price;
// uint256 balace = BaseERC20(tokenAddress).balanceOf(msg.sender);

// require(balace >= price, "you don't have enough money");

BaseERC20(tokenAddress).transferFrom(msg.sender, n.ownerAddress, price);
// require(transferSuccess, "transfer from BaseERC20 failure");

IERC721(nftAddress).safeTransferFrom(n.ownerAddress, msg.sender, _tokenId);

Expand All @@ -71,19 +60,13 @@ contract NFTMarket is IERC721Receiver, TokenRecipient {
if (tokenAddress != msg.sender) {
revert InvalidAddressError(_address);
}

// bytes storage temp = extraData;
uint256 _tokenId = abi.decode(extraData, (uint256));
NFT memory n1 = products[_tokenId];
n1.ownerAddress = msg.sender;

uint256 price = n1.price;

if (value < price) {
BaseERC20(tokenAddress).transfer(n1.ownerAddress, n1.price);
if (value < n1.price) {
revert AccessError(_address);
}

products[_tokenId] = n1;
delete products[_tokenId];

return true;
}
Expand Down
53 changes: 40 additions & 13 deletions test/BaseERC20.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,53 @@ import { BaseERC20 } from "../src/Week2/BaseERC20.sol";

contract BaseERC20Test is Test {
BaseERC20 private my20;
address player1 = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
address player2 = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
address player3 = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC;
// address player1 = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
address alice = makeAddr("alice");
address bob = makeAddr("bob");
address tom = makeAddr("tom");
// address player2 = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
// address player3 = 0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC;

function setUp() public virtual {
// Instantiate the contract-under-test.
my20 = new BaseERC20(player1);
my20 = new BaseERC20(alice);
}

function testTransfer() public view {
// my20.transfer(player2, 10_000);
// assertEq(my20.balanceOf(player1), 100_000_000_000_000_000_000_000_000 - 10_000);
// assertEq(my20.balanceOf(player2), 10_000);
uint256 result = my20.balanceOf(player1);
assertEq(result, 100_000_000_000_000_000_000_000_000);
function test_transfer() public {
// before transfer
vm.startPrank(alice);
assertEq(my20.balanceOf(alice), 100_000_000_000_000_000_000_000_000);
assertEq(my20.balanceOf(bob), 0);
my20.transfer(bob, 10_000);
assertEq(my20.balanceOf(alice), 100_000_000_000_000_000_000_000_000 - 10_000);
assertEq(my20.balanceOf(bob), 10_000);
vm.stopPrank();
}

function testBalanceOf() public view {
uint256 result = my20.balanceOf(player1);
assertEq(result, 100_000_000_000_000_000_000_000_000);
function test_approve() public {
vm.startPrank(alice);
assertEq(my20.allowance(alice, tom), 0);
my20.approve(tom, 10_000);
assertEq(my20.allowance(alice, tom), 10_000);
vm.stopPrank();
}

function test_transferFrom() public {
// before transfer
vm.prank(alice);
my20.approve(tom, 10_000);
assertEq(my20.allowance(alice, tom), 10_000);
assertEq(my20.balanceOf(alice), 100_000_000_000_000_000_000_000_000);
assertEq(my20.balanceOf(bob), 0);
vm.prank(tom);
my20.transferFrom(alice, bob, 10_000);
assertEq(my20.allowance(alice, tom), 0);
assertEq(my20.balanceOf(bob), 10_000);
}

function test_BalanceOf() public view {
// uint256 result = my20.balanceOf(player1);
// assertEq(result, 100_000_000_000_000_000_000_000_000);
// console2.log(result);
}
}
32 changes: 16 additions & 16 deletions test/Foo.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ contract FooTest is Test {
}

function testFork() public {
string memory alchemyApiKey = vm.envOr("API_KEY_ALCHEMY", string(""));
console2.log(alchemyApiKey);
if (bytes(alchemyApiKey).length == 0) {
return;
}
uint256 deployPrivateKey = vm.envUint("PRIVATE_KEY");
console2.log(deployPrivateKey);
// vm.createSelectFork({ urlOrAlias: "sepolia" });
vm.createSelectFork("https://sepolia.gateway.tenderly.co");
address helloContract = 0x36e25E1d589021d8cd7411DcD5B179279bC61D33;
address walletEOA = 0x251757EFDd8818283ec73A428e5486FB319bFc84;
vm.prank(walletEOA);
(bool success, bytes memory data) = helloContract.call(abi.encodeWithSignature("print()(string)"));
console2.log(success);
string memory returnValue = abi.decode(data, (string));
// string memory alchemyApiKey = vm.envOr("API_KEY_ALCHEMY", string(""));
// console2.log(alchemyApiKey);
// if (bytes(alchemyApiKey).length == 0) {
// return;
// }
// uint256 deployPrivateKey = vm.envUint("PRIVATE_KEY");
// console2.log(deployPrivateKey);
// // vm.createSelectFork({ urlOrAlias: "sepolia" });
// vm.createSelectFork("https://sepolia.gateway.tenderly.co");
// address helloContract = 0x36e25E1d589021d8cd7411DcD5B179279bC61D33;
// address walletEOA = 0x251757EFDd8818283ec73A428e5486FB319bFc84;
// vm.prank(walletEOA);
// (bool success, bytes memory data) = helloContract.call(abi.encodeWithSignature("print()(string)"));
// console2.log(success);
// string memory returnValue = abi.decode(data, (string));

console2.log(returnValue);
// console2.log(returnValue);
}
}
65 changes: 65 additions & 0 deletions test/NFTMarket.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.25;

import { Test, console2 } from "forge-std/src/Test.sol";
import { NFTMarket, BaseERC20 } from "../src/Week2/NFTMarket.sol";
import { MyERC721 } from "../src/Week2/MyERC721.sol";

contract NFTMarketTest is Test {
NFTMarket nftMarket;
address alice = makeAddr("alice");
BaseERC20 private my20;

address bob = makeAddr("bob");
address tom = makeAddr("tom");
MyERC721 internal my721;

function setUp() public {
my20 = new BaseERC20(alice);
my721 = new MyERC721();

nftMarket = new NFTMarket(address(my721), address(my20));

my721.mint(bob, "https://ipfs.io/ipfs/QmY5WwRyZm7P6cT9y4uNcHr5eVJvXsQkLX9VZgVn5h7uZ");
my721.mint(tom, "https://ipfs.io/ipfs/QmY5WwRyZm7P6cT9y4uNcHr5eVJvXsQkLX9VZgVn5h7uZ");
}

function test_list() public {
list(bob, 0, 100);
}

function test_buyNFT() public {
list(bob, 0, 100);
vm.prank(bob);
my721.approve(address(nftMarket), 0);
vm.startPrank(alice);
my20.approve(address(nftMarket), 100);
assertEq(my20.balanceOf(bob), 0);
//
nftMarket.buyNFT(0);
assertEq(my20.balanceOf(bob), 100);
assertEq(my721.ownerOf(0), alice);
assertEq(nftMarket.checkList(0).ownerAddress, alice);
vm.stopPrank();
}

function test_tokenReceive() public {
list(bob, 0, 100);
vm.startPrank(alice);
uint256 i = 0;
my20.transferWithFallback(address(nftMarket), 100, abi.encodePacked(i));

assertEq(my20.balanceOf(bob), 100);
assertEq(nftMarket.checkList(0).ownerAddress, alice);
vm.stopPrank();
}

function list(address who, uint256 _tokenId, uint256 price) public {
vm.startPrank(who);
assertEq(my721.ownerOf(_tokenId), who);
nftMarket.list(_tokenId, price);
assertEq(nftMarket.checkList(_tokenId).price, price);
assertEq(nftMarket.checkList(_tokenId).ownerAddress, who);
vm.stopPrank();
}
}
63 changes: 63 additions & 0 deletions test/TokenBank.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.25;

import { Test, console2 } from "forge-std/src/Test.sol";
import { TokenBank, BaseERC20 } from "../src/Week2/BaseERC20.sol";

contract TokenBankTest is Test {
TokenBank tokenBank;
// address myErc20;
address alice;
BaseERC20 private my20;

function setUp() public virtual {
alice = makeAddr("alice");
// myErc20 = makeAddr("myErc20");
my20 = new BaseERC20(alice);
tokenBank = new TokenBank(address(my20));
}

function testFailDeposit() public {
vm.startPrank(alice);

// my20.approve(address(tokenBank), 10_000);

tokenBank.deposit(10_000);

assertEq(tokenBank.getAccounts(), 10_000);

vm.stopPrank();
}

function test_deposit() public {
deposit(alice, 10_000);
}

function test_withdraw() public {
deposit(alice, 10_000);
vm.startPrank(alice);

tokenBank.withdraw(10_000);
assertEq(tokenBank.getAccounts(), 0);
}

function test_tokenReceive() public {
vm.startPrank(alice);
my20.transferWithFallback(address(tokenBank), 10_000, "test");

assertEq(tokenBank.getAccounts(), 10_000);
vm.stopPrank();
}

function deposit(address who, uint256 value) private {
vm.startPrank(who);

my20.approve(address(tokenBank), value);

tokenBank.deposit(value);

assertEq(tokenBank.getAccounts(), value);

vm.stopPrank();
}
}

0 comments on commit 655b3df

Please sign in to comment.