|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.0; |
| 4 | + |
| 5 | +library BankPrecompile { |
| 6 | + error BankError(bytes); |
| 7 | + |
| 8 | + enum BankMethod { |
| 9 | + NAME, |
| 10 | + SYMBOL, |
| 11 | + DECIMALS, |
| 12 | + TOTAL_SUPPLY, |
| 13 | + BALANCE_OF, |
| 14 | + TRANSFER_FROM |
| 15 | + } |
| 16 | + |
| 17 | + function name(address bank, string memory denom) internal view returns (string memory) { |
| 18 | + bytes memory result = _staticcall_bank(bank, abi.encodePacked(uint8(BankMethod.NAME), denom)); |
| 19 | + return string(result); |
| 20 | + } |
| 21 | + |
| 22 | + function symbol(address bank, string memory denom) internal view returns (string memory) { |
| 23 | + bytes memory result = _staticcall_bank(bank, abi.encodePacked(uint8(BankMethod.SYMBOL), denom)); |
| 24 | + return string(result); |
| 25 | + } |
| 26 | + |
| 27 | + function decimals(address bank, string memory denom) internal view returns (uint8) { |
| 28 | + bytes memory data = _staticcall_bank(bank, abi.encodePacked(uint8(BankMethod.DECIMALS), denom)); |
| 29 | + |
| 30 | + uint8 result; |
| 31 | + assembly { |
| 32 | + result := byte(0, mload(add(data, 0x20))) |
| 33 | + } |
| 34 | + return result; |
| 35 | + } |
| 36 | + |
| 37 | + function totalSupply(address bank, string memory denom) internal view returns (uint256) { |
| 38 | + bytes memory data = _staticcall_bank(bank, abi.encodePacked(uint8(BankMethod.TOTAL_SUPPLY), denom)); |
| 39 | + |
| 40 | + uint256 result; |
| 41 | + assembly { |
| 42 | + result := mload(add(data, 0x20)) |
| 43 | + } |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + function balanceOf(address bank, address account, string memory denom) internal view returns (uint256) { |
| 48 | + bytes memory data = _staticcall_bank(bank, abi.encodePacked(uint8(BankMethod.BALANCE_OF), account, denom)); |
| 49 | + |
| 50 | + uint256 result; |
| 51 | + assembly { |
| 52 | + result := mload(add(data, 0x20)) |
| 53 | + } |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + function transferFrom(address bank, address from, address to, uint256 amount, string memory denom) internal returns (bool) { |
| 58 | + _call_bank(bank, abi.encodePacked(uint8(BankMethod.TRANSFER_FROM), from, to, amount, denom)); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + function _staticcall_bank(address bank, bytes memory _calldata) internal view returns (bytes memory) { |
| 63 | + (bool success, bytes memory data) = bank.staticcall(_calldata); |
| 64 | + if (!success) { |
| 65 | + revert BankError(data); |
| 66 | + } |
| 67 | + |
| 68 | + return data; |
| 69 | + } |
| 70 | + |
| 71 | + function _call_bank(address bank, bytes memory _calldata) internal returns (bytes memory) { |
| 72 | + (bool success, bytes memory data) = bank.call(_calldata); |
| 73 | + if (!success) { |
| 74 | + revert BankError(data); |
| 75 | + } |
| 76 | + |
| 77 | + return data; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +interface IERC20 { |
| 82 | + event Transfer(address indexed from, address indexed to, uint256 value); |
| 83 | + event Approval(address indexed owner, address indexed spender, uint256 value); |
| 84 | + function totalSupply() external view returns (uint256); |
| 85 | + function balanceOf(address account) external view returns (uint256); |
| 86 | + function transfer(address to, uint256 value) external returns (bool); |
| 87 | + function allowance(address owner, address spender) external view returns (uint256); |
| 88 | + function approve(address spender, uint256 value) external returns (bool); |
| 89 | + function transferFrom(address from, address to, uint256 value) external returns (bool); |
| 90 | +} |
| 91 | + |
| 92 | +interface IERC20Metadata is IERC20 { |
| 93 | + function name() external view returns (string memory); |
| 94 | + function symbol() external view returns (string memory); |
| 95 | + function decimals() external view returns (uint8); |
| 96 | +} |
| 97 | + |
| 98 | +interface IERC20Errors { |
| 99 | + error ERC20InvalidSender(address sender); |
| 100 | + error ERC20InvalidReceiver(address receiver); |
| 101 | + error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed); |
| 102 | + error ERC20InvalidApprover(address approver); |
| 103 | + error ERC20InvalidSpender(address spender); |
| 104 | +} |
| 105 | + |
| 106 | +contract ERC20 is IERC20, IERC20Metadata, IERC20Errors { |
| 107 | + using BankPrecompile for address; |
| 108 | + |
| 109 | + string public denom; |
| 110 | + mapping(address account => mapping(address spender => uint256)) public allowance; |
| 111 | + |
| 112 | + address public immutable bank; |
| 113 | + |
| 114 | + constructor(string memory denom_, address bank_) { |
| 115 | + denom = denom_; |
| 116 | + bank = bank_; |
| 117 | + } |
| 118 | + |
| 119 | + function name() public view returns (string memory) { |
| 120 | + return bank.name(denom); |
| 121 | + } |
| 122 | + |
| 123 | + function symbol() public view returns (string memory) { |
| 124 | + return bank.symbol(denom); |
| 125 | + } |
| 126 | + |
| 127 | + function decimals() public view returns (uint8) { |
| 128 | + return bank.decimals(denom); |
| 129 | + } |
| 130 | + |
| 131 | + function totalSupply() public view returns (uint256) { |
| 132 | + return bank.totalSupply(denom); |
| 133 | + } |
| 134 | + |
| 135 | + function balanceOf(address account) public view returns (uint256) { |
| 136 | + return bank.balanceOf(account, denom); |
| 137 | + } |
| 138 | + |
| 139 | + function transfer(address to, uint256 value) public returns (bool) { |
| 140 | + _transfer(msg.sender, to, value); |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + function approve(address spender, uint256 value) public returns (bool) { |
| 145 | + _approve(msg.sender, spender, value); |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + function transferFrom(address from, address to, uint256 value) public returns (bool) { |
| 150 | + address spender = msg.sender; |
| 151 | + _spendAllowance(from, spender, value); |
| 152 | + _transfer(from, to, value); |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + function _transfer(address from, address to, uint256 value) internal { |
| 157 | + if (from == address(0)) { |
| 158 | + revert ERC20InvalidSender(address(0)); |
| 159 | + } |
| 160 | + if (to == address(0)) { |
| 161 | + revert ERC20InvalidReceiver(address(0)); |
| 162 | + } |
| 163 | + |
| 164 | + bank.transferFrom(from, to, value, denom); |
| 165 | + emit Transfer(from, to, value); |
| 166 | + } |
| 167 | + |
| 168 | + function _approve(address owner, address spender, uint256 value) internal { |
| 169 | + _approve(owner, spender, value, true); |
| 170 | + } |
| 171 | + |
| 172 | + function _approve(address owner, address spender, uint256 value, bool emitEvent) internal virtual { |
| 173 | + if (owner == address(0)) { |
| 174 | + revert ERC20InvalidApprover(address(0)); |
| 175 | + } |
| 176 | + if (spender == address(0)) { |
| 177 | + revert ERC20InvalidSpender(address(0)); |
| 178 | + } |
| 179 | + allowance[owner][spender] = value; |
| 180 | + if (emitEvent) { |
| 181 | + emit Approval(owner, spender, value); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + function _spendAllowance(address owner, address spender, uint256 value) internal virtual { |
| 186 | + uint256 currentAllowance = allowance[owner][spender]; |
| 187 | + if (currentAllowance < type(uint256).max) { |
| 188 | + if (currentAllowance < value) { |
| 189 | + revert ERC20InsufficientAllowance(spender, currentAllowance, value); |
| 190 | + } |
| 191 | + unchecked { |
| 192 | + _approve(owner, spender, currentAllowance - value, false); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments