|
| 1 | +pragma solidity 0.8.25; |
| 2 | + |
| 3 | +import {IERC20Metadata} from |
| 4 | + "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; |
| 5 | +import {SafeERC20} from |
| 6 | + "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 7 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 8 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 9 | + |
| 10 | +contract Vault is Ownable { |
| 11 | + using SafeERC20 for IERC20; |
| 12 | + |
| 13 | + /// @notice Mapping of authorized tokens |
| 14 | + mapping(address => bool) public authorizedToken; |
| 15 | + |
| 16 | + /// @notice User's balance of all tokens deposited in the vault |
| 17 | + mapping(address => uint256) public balanceOf; |
| 18 | + |
| 19 | + /// @notice Total amount of tokens supplied to the vault |
| 20 | + /// |
| 21 | + /// invariants: |
| 22 | + /// totalSupplied = sum(balanceOf all users) |
| 23 | + /// sum(balanceOf(vault) authorized tokens) >= totalSupplied |
| 24 | + /// |
| 25 | + uint256 public totalSupplied; |
| 26 | + |
| 27 | + /// @notice Deposit event |
| 28 | + /// @param token The token deposited |
| 29 | + /// @param sender The address that deposited the token |
| 30 | + /// @param amount The amount deposited |
| 31 | + event Deposit( |
| 32 | + address indexed token, address indexed sender, uint256 amount |
| 33 | + ); |
| 34 | + |
| 35 | + /// @notice Withdraw event |
| 36 | + /// @param token The token withdrawn |
| 37 | + /// @param sender The address that withdrew the token |
| 38 | + /// @param amount The amount withdrawn |
| 39 | + event Withdraw( |
| 40 | + address indexed token, address indexed sender, uint256 amount |
| 41 | + ); |
| 42 | + |
| 43 | + event TokenAdded(address indexed token); |
| 44 | + |
| 45 | + /// @notice Construct the vault with a list of authorized tokens |
| 46 | + /// @param _tokens The list of authorized tokens |
| 47 | + constructor(address[] memory _tokens, address _owner) Ownable(_owner) { |
| 48 | + for (uint256 i = 0; i < _tokens.length; i++) { |
| 49 | + require( |
| 50 | + IERC20Metadata(_tokens[i]).decimals() <= 18, |
| 51 | + "Vault: unsupported decimals" |
| 52 | + ); |
| 53 | + |
| 54 | + authorizedToken[_tokens[i]] = true; |
| 55 | + |
| 56 | + emit TokenAdded(_tokens[i]); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// ------------------------------------------------------------- |
| 61 | + /// ------------------------------------------------------------- |
| 62 | + /// -------------------- ONLY OWNER FUNCTION -------------------- |
| 63 | + /// ------------------------------------------------------------- |
| 64 | + /// ------------------------------------------------------------- |
| 65 | + |
| 66 | + /// @notice Add a token to the list of authorized tokens |
| 67 | + /// only callable by the owner |
| 68 | + /// @param token to add |
| 69 | + function addToken(address token) external onlyOwner { |
| 70 | + require( |
| 71 | + IERC20Metadata(token).decimals() <= 18, |
| 72 | + "Vault: unsupported decimals" |
| 73 | + ); |
| 74 | + require(!authorizedToken[token], "Vault: token already authorized"); |
| 75 | + |
| 76 | + authorizedToken[token] = true; |
| 77 | + |
| 78 | + emit TokenAdded(token); |
| 79 | + } |
| 80 | + |
| 81 | + /// ------------------------------------------------------------- |
| 82 | + /// ------------------------------------------------------------- |
| 83 | + /// ----------------- PUBLIC MUTATIVE FUNCTIONS ----------------- |
| 84 | + /// ------------------------------------------------------------- |
| 85 | + /// ------------------------------------------------------------- |
| 86 | + |
| 87 | + /// @notice Deposit tokens into the vault |
| 88 | + /// @param token The token to deposit, only authorized tokens allowed |
| 89 | + /// @param amount The amount to deposit |
| 90 | + function deposit(address token, uint256 amount) external { |
| 91 | + require(authorizedToken[token], "Vault: token not authorized"); |
| 92 | + |
| 93 | + uint256 normalizedAmount = getNormalizedAmount(token, amount); |
| 94 | + |
| 95 | + /// save on gas by using unchecked, no need to check for overflow |
| 96 | + /// as all deposited tokens are whitelisted |
| 97 | + unchecked { |
| 98 | + balanceOf[msg.sender] += normalizedAmount; |
| 99 | + } |
| 100 | + |
| 101 | + totalSupplied += normalizedAmount; |
| 102 | + |
| 103 | + IERC20(token).safeTransferFrom(msg.sender, address(this), amount); |
| 104 | + |
| 105 | + emit Deposit(token, msg.sender, amount); |
| 106 | + } |
| 107 | + |
| 108 | + /// @notice Withdraw tokens from the vault |
| 109 | + /// @param token The token to withdraw, only authorized tokens are allowed |
| 110 | + /// this is implicitly checked because a user can only have a balance of an |
| 111 | + /// authorized token |
| 112 | + /// @param amount The amount to withdraw |
| 113 | + function withdraw(address token, uint256 amount) external { |
| 114 | + require(authorizedToken[token], "Vault: token not authorized"); |
| 115 | + |
| 116 | + uint256 normalizedAmount = getNormalizedAmount(token, amount); |
| 117 | + |
| 118 | + /// both a check and an effect, ensures user has sufficient funds for |
| 119 | + /// withdrawal |
| 120 | + /// must be checked for underflow as a user can only withdraw what they |
| 121 | + /// have deposited |
| 122 | + balanceOf[msg.sender] -= normalizedAmount; |
| 123 | + |
| 124 | + /// save on gas by using unchecked, no need to check for underflow |
| 125 | + /// as all deposited tokens are whitelisted, plus we know our invariant |
| 126 | + /// always holds |
| 127 | + unchecked { |
| 128 | + totalSupplied -= normalizedAmount; |
| 129 | + } |
| 130 | + |
| 131 | + IERC20(token).safeTransfer(msg.sender, amount); |
| 132 | + |
| 133 | + emit Withdraw(token, msg.sender, amount); |
| 134 | + } |
| 135 | + |
| 136 | + /// -------------------------------------------------------- |
| 137 | + /// -------------------------------------------------------- |
| 138 | + /// ----------------- PUBLIC VIEW FUNCTION ----------------- |
| 139 | + /// -------------------------------------------------------- |
| 140 | + /// -------------------------------------------------------- |
| 141 | + |
| 142 | + /// @notice public for testing purposes, returns the normalized amount of |
| 143 | + /// tokens scaled to 18 decimals |
| 144 | + /// @param token The token to deposit |
| 145 | + /// @param amount The amount to deposit |
| 146 | + function getNormalizedAmount(address token, uint256 amount) |
| 147 | + public |
| 148 | + view |
| 149 | + returns (uint256 normalizedAmount) |
| 150 | + { |
| 151 | + uint8 decimals = IERC20Metadata(token).decimals(); |
| 152 | + normalizedAmount = amount; |
| 153 | + if (decimals < 18) { |
| 154 | + normalizedAmount = amount * (10 ** (18 - decimals)); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments