Skip to content

Commit

Permalink
add weird erc20: return bool on transfer token
Browse files Browse the repository at this point in the history
  • Loading branch information
arbazkiraak committed Nov 20, 2023
1 parent 73b1083 commit d929445
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/weird-erc20/ERC20-base.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
pragma solidity ^0.8.0;

import "forge-std/interfaces/IERC20.sol";

contract ERC20Token is IERC20 {
string private _name = "ERC20Token";
string private _symbol = "ERC";
uint8 private _decimals = 18;

uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

function name() public view returns (string memory) {
return _name;
}

function symbol() public view returns (string memory) {
return _symbol;
}

function decimals() public view returns (uint8) {
return _decimals;
}

function approve(address spender, uint256 amount) public returns (bool) {
_approve(msg.sender,spender,amount);
return true;
}

function _approve(address owner,address spender,uint256 amount) internal virtual {
require(owner != address(0),"ERC20: approve from the zero address");
require(spender != address(0),"ERC20: approve to the zero address");

allowance[owner][spender] = amount;
emit Approval(owner,spender,amount);
}

function transfer(address to,uint256 amount) public virtual returns (bool) {
_transfer(msg.sender,to,amount);
return true;
}

function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) {
_transfer(from,to,amount);
_approve(from,msg.sender,allowance[from][msg.sender] - amount);
return true;
}

function _transfer(address sender, address recipient, uint256 amount) internal {
require(sender != address(0),"ERC20: transfer from the zero address");
require(recipient != address(0),"ERC20: transfer to the zero address");
require(balanceOf[sender] >= amount,"ERC20: insufficient balance");

balanceOf[sender] -= amount;
balanceOf[recipient] += amount;

emit Transfer(sender,recipient,amount);
}
}



48 changes: 48 additions & 0 deletions src/weird-erc20/ERC20-bool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pragma solidity ^0.8.0;

import "forge-std/interfaces/IERC20.sol";
import {ERC20Token} from "./ERC20-base.sol";

contract ERC20Bool is ERC20Token {
constructor(uint256 initialSupply) {
totalSupply = initialSupply;
balanceOf[msg.sender] = initialSupply;

emit Transfer(address(0),msg.sender,initialSupply);
}

function transfer(address to,uint256 amount) public override returns (bool) {
unchecked {
if (
balanceOf[msg.sender] >= amount &&
balanceOf[to] + amount >= balanceOf[to]
) {
balanceOf[to] += amount;
balanceOf[msg.sender] -= amount;
emit Transfer(msg.sender,to,amount);
return true;
} else {
return false;
}
}
}

function transferFrom(address from,address to,uint256 amount) public override returns (bool) {
unchecked {
if (
balanceOf[from] >= amount &&
allowance[from][msg.sender] >= amount &&
balanceOf[to] + amount >= balanceOf[to]
) {
balanceOf[to] += amount;
balanceOf[from] -= amount;
emit Transfer(from,to,amount);
allowance[from][msg.sender] -= amount;
emit Approval(from,msg.sender,allowance[from][msg.sender]);
return true;
} else {
return false;
}
}
}
}
37 changes: 37 additions & 0 deletions test/examples/ERC20Example.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "forge-std/interfaces/IERC20.sol";
import "../../src/weird-erc20/ERC20-bool.sol";

contract ERC20Test is Test {
IERC20 public boolToken;
uint256 immutable initialSupply = 1000000000;
address alice = vm.addr(1);
address bob = vm.addr(2);

function setUp() public {
boolToken = new ERC20Bool(initialSupply);
assertEq(boolToken.balanceOf(address(this)),initialSupply);
}

function testBoolTokenTransfer() public {
assertEq(boolToken.balanceOf(alice),0);

bool retVal = boolToken.transfer(alice,100);
assertEq(retVal,true);

assertEq(boolToken.balanceOf(alice),100);
assertEq(boolToken.balanceOf(address(this)),initialSupply - 100);
}

function testBoolTokenTransferFail() public {
assertEq(boolToken.balanceOf(alice),0);

vm.startPrank(alice);
bool retVal = boolToken.transfer(bob,100);
assertEq(retVal,false);

assertEq(boolToken.balanceOf(bob),0);
}
}

0 comments on commit d929445

Please sign in to comment.