Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Jan 3, 2023
1 parent 34023b3 commit 93ee8de
Show file tree
Hide file tree
Showing 62 changed files with 4,764 additions and 4,752 deletions.
6 changes: 0 additions & 6 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ singleQuote: false
tabWidth: 2
trailingComma: "all"
useTabs: false

overrides:
- files: "*.sol"
options:
compiler: "0.8.17"
tabWidth: 4
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// editor settings
// "editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"solidity.formatter": "prettier", // This is the default so it might be missing.
"solidity.formatter": "forge", // This is the default so it might be missing.
"[solidity]": {
"editor.defaultFormatter": "JuanBlanco.solidity"
}
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Find safe transfer library
find fixed point math lib that supports powu
Find safe transfer library find fixed point math lib that supports powu
11 changes: 11 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ test = "test"
fuzz = { runs = 1_000 }
verbosity = 4

[fmt]
bracket_spacing = true
int_types = "long"
line_length = 120
multiline_func_header = "all"
number_underscore = "thousands"
quote_style = "double"
tab_width = 2
variable_override_spacing = false
wrap_comments = true

[rpc_endpoints]
arbitrum = "${RPC_URL_ARBITRUM}"
goerli = "${RPC_URL_GOERLI}"
Expand Down
247 changes: 123 additions & 124 deletions src/core/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,189 +3,188 @@ pragma solidity >=0.8.0;

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Kyle Scott (https://github.com/numoen/core/blob/master/src/ERC20.sol)
/// @author Modified from Solmate v6 (https://github.com/transmissions11/solmate/blob/a9e3ea26a2dc73bfa87f0cb189687d029028e0c5/src/tokens/ERC20.sol)
/// @author Modified from Solmate v6
/// (https://github.com/transmissions11/solmate/blob/a9e3ea26a2dc73bfa87f0cb189687d029028e0c5/src/tokens/ERC20.sol)
/// and Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/

event Transfer(address indexed from, address indexed to, uint256 amount);
event Transfer(address indexed from, address indexed to, uint256 amount);

event Approval(address indexed owner, address indexed spender, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);

/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
METADATA STORAGE
//////////////////////////////////////////////////////////////*/

string public constant name = "Numoen Replicating Derivative";
string public constant name = "Numoen Replicating Derivative";

string public constant symbol = "NRD";
string public constant symbol = "NRD";

uint8 public constant decimals = 18;
uint8 public constant decimals = 18;

/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
ERC20 STORAGE
//////////////////////////////////////////////////////////////*/

uint256 public totalSupply;
uint256 public totalSupply;

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

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

/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
EIP-2612 STORAGE
//////////////////////////////////////////////////////////////*/

bytes32 public constant PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 public constant PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");

uint256 internal immutable INITIAL_CHAIN_ID;
uint256 internal immutable INITIAL_CHAIN_ID;

bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

mapping(address => uint256) public nonces;
mapping(address => uint256) public nonces;

/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/

constructor() {
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
constructor() {
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}

/*///////////////////////////////////////////////////////////////
/*///////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/

/// @dev changed visibility to external
function approve(address spender, uint256 amount) external virtual returns (bool) {
allowance[msg.sender][spender] = amount;

emit Approval(msg.sender, spender, amount);

return true;
}
/// @dev changed visibility to external
function approve(address spender, uint256 amount) external virtual returns (bool) {
allowance[msg.sender][spender] = amount;

/// @dev changed visibility to external
function transfer(address to, uint256 amount) external virtual returns (bool) {
balanceOf[msg.sender] -= amount;
emit Approval(msg.sender, spender, amount);

// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
return true;
}

emit Transfer(msg.sender, to, amount);
/// @dev changed visibility to external
function transfer(address to, uint256 amount) external virtual returns (bool) {
balanceOf[msg.sender] -= amount;

return true;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}

/// @dev changed visibility to external
function transferFrom(
address from,
address to,
uint256 amount
) external virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.
emit Transfer(msg.sender, to, amount);

if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
return true;
}

balanceOf[from] -= amount;
/// @dev changed visibility to external
function transferFrom(address from, address to, uint256 amount) external virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

emit Transfer(from, to, amount);
balanceOf[from] -= amount;

return true;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}

/*///////////////////////////////////////////////////////////////
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/
emit Transfer(from, to, amount);

function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

// Unchecked because the only math done is incrementing
// the owner's nonce which cannot realistically overflow.
unchecked {
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
)
);

address recoveredAddress = ecrecover(digest, v, r, s);

require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

allowance[recoveredAddress][spender] = value;
}

emit Approval(owner, spender, value);
}
return true;
}

function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
/*///////////////////////////////////////////////////////////////
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/

function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
)
external
virtual
{
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

// Unchecked because the only math done is incrementing
// the owner's nonce which cannot realistically overflow.
unchecked {
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
)
);

address recoveredAddress = ecrecover(digest, v, r, s);

require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

allowance[recoveredAddress][spender] = value;
}

/*///////////////////////////////////////////////////////////////
emit Approval(owner, spender, value);
}

function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}

function computeDomainSeparator() internal view virtual returns (bytes32) {
return keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}

/*///////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/

function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;

// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;

emit Transfer(address(0), to, amount);
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}

function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
emit Transfer(address(0), to, amount);
}

// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;

emit Transfer(from, address(0), amount);
// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}

emit Transfer(from, address(0), amount);
}
}
Loading

0 comments on commit 93ee8de

Please sign in to comment.