Skip to content

Commit

Permalink
Original codex answers
Browse files Browse the repository at this point in the history
  • Loading branch information
montyly committed Dec 7, 2022
1 parent 3a752b8 commit d8e4942
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/auth/Auth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ abstract contract Auth {
_;
}

/**
* @notice Checks if the caller is authorized to call the given function.
* @dev This function checks if the caller is the owner or if the authority has granted the caller permission to call the given function.
*/
function isAuthorized(address user, bytes4 functionSig) internal view virtual returns (bool) {
Authority auth = authority; // Memoizing authority saves us a warm SLOAD, around 100 gas.

Expand All @@ -45,6 +49,10 @@ abstract contract Auth {
emit AuthorityUpdated(msg.sender, newAuthority);
}

/**
* @notice This function allows the current owner to transfer ownership of the contract to a new owner.
* @dev The new owner must be provided as an address parameter. The function emits an OwnershipTransferred event.
*/
function transferOwnership(address newOwner) public virtual requiresAuth {
owner = newOwner;

Expand Down
9 changes: 9 additions & 0 deletions src/auth/Owned.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ abstract contract Owned {
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/

/**
* @notice This function sets the owner of the contract to the address passed in as an argument.
* @dev The OwnershipTransferred event is emitted with the address 0 as the previous owner and the address passed in as the new owner.
*/
constructor(address _owner) {
owner = _owner;

Expand All @@ -36,6 +40,11 @@ abstract contract Owned {
OWNERSHIP LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice Transfers ownership of the contract to a new owner.
* @dev Only the current owner can call this function.
* @param newOwner The address of the new owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
owner = newOwner;

Expand Down
8 changes: 8 additions & 0 deletions src/auth/authorities/MultiRolesAuthority.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ contract MultiRolesAuthority is Auth, Authority {
CUSTOM TARGET AUTHORITY CONFIGURATION LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice Sets the custom authority for a given target address.
* @dev This function sets the custom authority for a given target address. It requires authentication and emits an event when the target custom authority is updated.
*/
function setTargetCustomAuthority(address target, Authority customAuthority) public virtual requiresAuth {
getTargetCustomAuthority[target] = customAuthority;

Expand Down Expand Up @@ -107,6 +111,10 @@ contract MultiRolesAuthority is Auth, Authority {
ROLE CAPABILITY CONFIGURATION LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice setRoleCapability() sets the capability of a role to execute a function.
* @dev This function requires authentication and sets the capability of a role to execute a function. It takes three parameters: role, functionSig, and enabled. If enabled is true, the role is given the capability to execute the function. Otherwise, the role is not given the capability to execute the function.
*/
function setRoleCapability(
uint8 role,
bytes4 functionSig,
Expand Down
4 changes: 4 additions & 0 deletions src/auth/authorities/RolesAuthority.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ contract RolesAuthority is Auth, Authority {

mapping(address => mapping(bytes4 => bytes32)) public getRolesWithCapability;

/**
* @notice This function checks if a user has a certain role.
* @dev This function takes an address of a user and a uint8 role as parameters and returns a boolean value. It uses the getUserRoles mapping to check if the user has the role. It shifts the uint256 value of the mapping to the right by the role parameter and checks if the result is not equal to 0.
*/
function doesUserHaveRole(address user, uint8 role) public view virtual returns (bool) {
return (uint256(getUserRoles[user]) >> role) & 1 != 0;
}
Expand Down
33 changes: 33 additions & 0 deletions src/mixins/ERC4626.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ abstract contract ERC4626 is ERC20 {
ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function returns the total assets of the contract.
* @dev This function is a public view virtual function that returns the total assets of the contract.
*/
function totalAssets() public view virtual returns (uint256);

/**
* @notice This function converts a given amount of assets to the corresponding amount of shares.
* @dev The function takes in a uint256 representing the amount of assets and returns a uint256 representing the amount of shares. The conversion is done by dividing the total assets by the total supply.
*/
function convertToShares(uint256 assets) public view virtual returns (uint256) {
uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.

Expand All @@ -133,10 +141,20 @@ abstract contract ERC4626 is ERC20 {
return supply == 0 ? shares : shares.mulDivDown(totalAssets(), supply);
}

/**
* @notice This function allows users to preview the amount of shares they will receive when depositing a certain amount of assets.
* @dev This function takes in an amount of assets and returns the amount of shares that will be received when depositing that amount of assets.
*/
function previewDeposit(uint256 assets) public view virtual returns (uint256) {
return convertToShares(assets);
}

/**
* @notice Calculates the amount of tokens that will be minted when the given number of shares is minted.
* @dev This function is view and virtual, meaning that it does not modify the state of the contract and can be used to simulate the minting of tokens.
* @param shares The number of shares to be minted.
* @return The amount of tokens that will be minted when the given number of shares is minted.
*/
function previewMint(uint256 shares) public view virtual returns (uint256) {
uint256 supply = totalSupply; // Saves an extra SLOAD if totalSupply is non-zero.

Expand All @@ -149,6 +167,10 @@ abstract contract ERC4626 is ERC20 {
return supply == 0 ? assets : assets.mulDivUp(supply, totalAssets());
}

/**
* @notice This function allows users to preview the amount of assets they will receive when redeeming their shares.
* @dev This function takes in the number of shares as an argument and returns the amount of assets that will be received when redeeming the shares.
*/
function previewRedeem(uint256 shares) public view virtual returns (uint256) {
return convertToAssets(shares);
}
Expand All @@ -161,14 +183,25 @@ abstract contract ERC4626 is ERC20 {
return type(uint256).max;
}

/**
* @notice This function returns the maximum value of a uint256 type.
* @dev This function is used to return the maximum value of a uint256 type. It takes an address as an argument and returns the maximum value of a uint256 type.*/
function maxMint(address) public view virtual returns (uint256) {
return type(uint256).max;
}

/**
* @notice This function allows the owner to withdraw the maximum amount of assets from their account.
* @dev This function takes in the address of the owner and returns the maximum amount of assets that can be withdrawn.
*/
function maxWithdraw(address owner) public view virtual returns (uint256) {
return convertToAssets(balanceOf[owner]);
}

/**
* @notice This function allows the owner to redeem the maximum amount of tokens from their balance.
* @dev The function takes in the address of the owner and returns the amount of tokens in their balance.
*/
function maxRedeem(address owner) public view virtual returns (uint256) {
return balanceOf[owner];
}
Expand Down
27 changes: 27 additions & 0 deletions src/tokens/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ abstract contract ERC1155 {
METADATA LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function returns a string representation of the given id.
* @dev This function is used to convert a uint256 id into a string representation. It is used to create a unique identifier for a given id.*/
function uri(uint256 id) public view virtual returns (string memory);

/*//////////////////////////////////////////////////////////////
Expand All @@ -52,6 +55,13 @@ abstract contract ERC1155 {
emit ApprovalForAll(msg.sender, operator, approved);
}

/**
* @notice This function allows a user to transfer a specific token from one address to another.
* @dev The function requires that the sender is either the from address or is approved for all of the from address.
* The balance of the from address is decreased by the amount and the balance of the to address is increased by the amount.
* The TransferSingle event is emitted.
* The function requires that the to address is either a valid address or an ERC1155TokenReceiver contract that implements the onERC1155Received function.
*/
function safeTransferFrom(
address from,
address to,
Expand Down Expand Up @@ -149,6 +159,10 @@ abstract contract ERC1155 {
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function mints a new token to the specified address.
* @dev This function mints a new token to the specified address. It also emits a TransferSingle event and checks that the recipient is a valid ERC1155TokenReceiver.
*/
function _mint(
address to,
uint256 id,
Expand Down Expand Up @@ -235,6 +249,10 @@ abstract contract ERC1155 {
/// @notice A generic interface for a contract which properly accepts ERC1155 tokens.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC1155.sol)
abstract contract ERC1155TokenReceiver {
/**
* @notice This function is triggered when an ERC1155 token is received.
* @dev This function is used to receive ERC1155 tokens. It takes in two addresses, two uint256 values, and a bytes calldata. It returns a bytes4 value.
*/
function onERC1155Received(
address,
address,
Expand All @@ -245,6 +263,15 @@ abstract contract ERC1155TokenReceiver {
return ERC1155TokenReceiver.onERC1155Received.selector;
}

/**
* @dev Function to receive a batch of ERC1155 tokens.
* @param _operator The address which called the function.
* @param _from The address of the sender.
* @param _ids An array of the ids of the tokens being sent.
* @param _values An array of the values of the tokens being sent.
* @param _data Any extra data with the transaction.
* @return The function selector of the ERC1155TokenReceiver.onERC1155BatchReceived function.
*/
function onERC1155BatchReceived(
address,
address,
Expand Down
23 changes: 23 additions & 0 deletions src/tokens/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ abstract contract ERC20 {
return true;
}

/**
* @notice This function transfers an amount of tokens from the sender to the recipient.
* @dev The sender's balance is decreased and the recipient's balance is increased.
*/
function transfer(address to, uint256 amount) public virtual returns (bool) {
balanceOf[msg.sender] -= amount;

Expand Down Expand Up @@ -113,6 +117,10 @@ abstract contract ERC20 {
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function permits a spender to withdraw from the owner's account, up to the given amount.
* @dev The function requires the owner to sign the transaction with their private key, and the signature is verified using the ecrecover function. The allowance is then set to the given value.
*/
function permit(
address owner,
address spender,
Expand Down Expand Up @@ -159,10 +167,21 @@ abstract contract ERC20 {
emit Approval(owner, spender, value);
}

/**
* DOMAIN_SEPARATOR
*
* @dev Returns the domain separator for the current chain. If the chainid is equal to the initial chain id, the initial domain separator is returned. Otherwise, the computeDomainSeparator() function is called.
*
* @return bytes32 The domain separator for the current chain.
*/
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}

/**
* @notice Computes the domain separator for the EIP712 domain
* @dev This function computes the domain separator for the EIP712 domain. It takes in the name, version, chainId, and verifyingContract and returns the domain separator as a bytes32.
*/
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
Expand Down Expand Up @@ -192,6 +211,10 @@ abstract contract ERC20 {
emit Transfer(address(0), to, amount);
}

/**
* @notice This function burns a given amount of tokens from a given address.
* @dev The function subtracts the given amount from the balance of the given address and from the total supply.
*/
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;

Expand Down
45 changes: 45 additions & 0 deletions src/tokens/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ abstract contract ERC721 {

string public symbol;

/**
* @notice This function returns a string memory associated with a given uint256 id.
* @dev This function is a public view virtual function that returns a string memory associated with a given uint256 id.
*/
function tokenURI(uint256 id) public view virtual returns (string memory);

/*//////////////////////////////////////////////////////////////
Expand All @@ -32,6 +36,11 @@ abstract contract ERC721 {

mapping(address => uint256) internal _balanceOf;

/**
* @notice ownerOf() function allows to retrieve the owner of a given token ID.
* @dev The function requires that the given token ID is minted and returns the address of the owner.
* If the token ID is not minted, an error message "NOT_MINTED" is thrown.
*/
function ownerOf(uint256 id) public view virtual returns (address owner) {
require((owner = _ownerOf[id]) != address(0), "NOT_MINTED");
}
Expand Down Expand Up @@ -63,6 +72,11 @@ abstract contract ERC721 {
ERC721 LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function allows the owner of an id to approve a spender to transfer the id.
* @dev The function requires that the msg.sender is either the owner of the id or is approved for all by the owner.
* If the requirements are met, the spender is set as the approved address for the id and an Approval event is emitted.
*/
function approve(address spender, uint256 id) public virtual {
address owner = _ownerOf[id];

Expand All @@ -79,6 +93,13 @@ abstract contract ERC721 {
emit ApprovalForAll(msg.sender, operator, approved);
}

/**
* @notice Transfers an NFT from one address to another.
* @dev The function requires that the sender is the owner of the NFT, or is approved by the owner.
* @param from The address of the current owner of the NFT.
* @param to The address of the new owner of the NFT.
* @param id The ID of the NFT to be transferred.
*/
function transferFrom(
address from,
address to,
Expand Down Expand Up @@ -108,6 +129,10 @@ abstract contract ERC721 {
emit Transfer(from, to, id);
}

/**
* @notice This function transfers an ERC721 token from one address to another.
* @dev This function requires that the recipient of the token is a valid ERC721 receiver. If the recipient is not a valid ERC721 receiver, the transfer will not be completed and an error will be thrown.
*/
function safeTransferFrom(
address from,
address to,
Expand All @@ -123,6 +148,14 @@ abstract contract ERC721 {
);
}

/**
* @notice This function transfers an ERC721 token from one address to another.
* @dev The function requires that the recipient of the token is a valid ERC721 receiver.
* @param from The address of the sender.
* @param to The address of the recipient.
* @param id The ID of the ERC721 token.
* @param data Optional data to be passed to the recipient.
*/
function safeTransferFrom(
address from,
address to,
Expand Down Expand Up @@ -154,6 +187,10 @@ abstract contract ERC721 {
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function is used to mint a new token with a given id.
* @dev The function requires that the recipient address is not 0, and that the token has not already been minted. The balance of the recipient is incremented, and the owner of the token is set to the recipient. A Transfer event is emitted.
*/
function _mint(address to, uint256 id) internal virtual {
require(to != address(0), "INVALID_RECIPIENT");

Expand All @@ -169,6 +206,10 @@ abstract contract ERC721 {
emit Transfer(address(0), to, id);
}

/**
* @notice This function is used to burn a token with the given id.
* @dev This function requires that the owner of the token is not address(0). The balance of the owner is decreased by 1 and the owner of the token is set to address(0). The Transfer event is emitted.
*/
function _burn(uint256 id) internal virtual {
address owner = _ownerOf[id];

Expand All @@ -190,6 +231,10 @@ abstract contract ERC721 {
INTERNAL SAFE MINT LOGIC
//////////////////////////////////////////////////////////////*/

/**
* @notice This function mints a token to a specified address.
* @dev This function requires that the recipient of the token is a valid ERC721 token receiver.
*/
function _safeMint(address to, uint256 id) internal virtual {
_mint(to, id);

Expand Down
8 changes: 8 additions & 0 deletions src/tokens/WETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ contract WETH is ERC20("Wrapped Ether", "WETH", 18) {

event Withdrawal(address indexed to, uint256 amount);

/**
* @notice This function allows users to deposit funds into the contract.
* @dev The function will mint the amount of tokens equal to the amount of funds deposited and emit a Deposit event.
*/
function deposit() public payable virtual {
_mint(msg.sender, msg.value);

emit Deposit(msg.sender, msg.value);
}

/**
* @notice This function allows a user to withdraw a specified amount of ETH from the contract.
* @dev The function first burns the amount of ETH from the user's balance, then emits a Withdrawal event, and finally sends the amount of ETH to the user's address.
*/
function withdraw(uint256 amount) public virtual {
_burn(msg.sender, amount);

Expand Down
Loading

0 comments on commit d8e4942

Please sign in to comment.