1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | 4× 4× 4× 4× 4× 1× 1× 1× 16× 18× 18× 18× 18× 6× 6× 4× 4× 4× 26× 26× 25× 25× 25× 17× 27× 27× 26× 26× 26× 18× 42× 28× 28× 15× 14× 14× 14× 41× 41× 40× 40× 40× 28× 21× 21× 21× 106× 106× 106× 106× 106× 106× 106× 106× 106× 24× 24× 24× 24× 24× 22× 18× 194× 194× 194× 138× 138× 138× 138× 194× 194× 194× 192× 192× 186× 180× | // SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2015, 2016, 2017 Dapphub // Adapted by Ethereum Community 2021 pragma solidity 0.8.6; import "./interfaces/IWETH10.sol"; import "./interfaces/IERC3156FlashBorrower.sol"; import "./interfaces/ITransferReceiver.sol"; import "./interfaces/IApprovalReceiver.sol"; /// @dev Wrapped Ether v10 (WETH10) is an Ether (ETH) ERC-20 wrapper. You can `deposit` ETH and obtain a WETH10 balance which can then be operated as an ERC-20 token. You can /// `withdraw` ETH from WETH10, which will then burn WETH10 token in your wallet. The amount of WETH10 token in any wallet is always identical to the /// balance of ETH deposited minus the ETH withdrawn with that specific wallet. contract WETH10 is IWETH10 { string public constant name = "Wrapped Ether v10"; string public constant symbol = "WETH10"; uint8 public constant decimals = 18; bytes32 public immutable CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan"); bytes32 public immutable PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); uint256 public immutable deploymentChainId; bytes32 private immutable _DOMAIN_SEPARATOR; /// @dev Records amount of WETH10 token owned by account. mapping (address => uint256) public override balanceOf; /// @dev Records current ERC2612 nonce for account. This value must be included whenever signature is generated for {permit}. /// Every successful call to {permit} increases account's nonce by one. This prevents signature from being used multiple times. mapping (address => uint256) public override nonces; /// @dev Records number of WETH10 token that account (second) will be allowed to spend on behalf of another account (first) through {transferFrom}. mapping (address => mapping (address => uint256)) public override allowance; /// @dev Current amount of flash-minted WETH10 token. uint256 public override flashMinted; constructor() { uint256 chainId; assembly {chainId := chainid()} deploymentChainId = chainId; _DOMAIN_SEPARATOR = _calculateDomainSeparator(chainId); } /// @dev Calculate the DOMAIN_SEPARATOR. function _calculateDomainSeparator(uint256 chainId) private view returns (bytes32) { return keccak256( abi.encode( keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), keccak256(bytes(name)), keccak256(bytes("1")), chainId, address(this) ) ); } /// @dev Return the DOMAIN_SEPARATOR. function DOMAIN_SEPARATOR() external view override returns (bytes32) { uint256 chainId; assembly {chainId := chainid()} return chainId == deploymentChainId ? _DOMAIN_SEPARATOR : _calculateDomainSeparator(chainId); } /// @dev Returns the total supply of WETH10 token as the ETH held in this contract. function totalSupply() external view override returns (uint256) { return address(this).balance + flashMinted; } /// @dev Fallback, `msg.value` of ETH sent to this contract grants caller account a matching increase in WETH10 token balance. /// Emits {Transfer} event to reflect WETH10 token mint of `msg.value` from `address(0)` to caller account. receive() external payable { // _mintTo(msg.sender, msg.value); balanceOf[msg.sender] += msg.value; emit Transfer(address(0), msg.sender, msg.value); } /// @dev `msg.value` of ETH sent to this contract grants caller account a matching increase in WETH10 token balance. /// Emits {Transfer} event to reflect WETH10 token mint of `msg.value` from `address(0)` to caller account. function deposit() external override payable { // _mintTo(msg.sender, msg.value); balanceOf[msg.sender] += msg.value; emit Transfer(address(0), msg.sender, msg.value); } /// @dev `msg.value` of ETH sent to this contract grants `to` account a matching increase in WETH10 token balance. /// Emits {Transfer} event to reflect WETH10 token mint of `msg.value` from `address(0)` to `to` account. function depositTo(address to) external override payable { // _mintTo(to, msg.value); balanceOf[to] += msg.value; emit Transfer(address(0), to, msg.value); } /// @dev `msg.value` of ETH sent to this contract grants `to` account a matching increase in WETH10 token balance, /// after which a call is executed to an ERC677-compliant contract with the `data` parameter. /// Emits {Transfer} event. /// Returns boolean value indicating whether operation succeeded. /// For more information on {transferAndCall} format, see https://github.com/ethereum/EIPs/issues/677. function depositToAndCall(address to, bytes calldata data) external override payable returns (bool success) { // _mintTo(to, msg.value); balanceOf[to] += msg.value; emit Transfer(address(0), to, msg.value); return ITransferReceiver(to).onTokenTransfer(msg.sender, msg.value, data); } /// @dev Return the amount of WETH10 token that can be flash-lent. function maxFlashLoan(address token) external view override returns (uint256) { return token == address(this) ? type(uint112).max - flashMinted : 0; // Can't underflow } /// @dev Return the fee (zero) for flash lending an amount of WETH10 token. function flashFee(address token, uint256) external view override returns (uint256) { require(token == address(this), "WETH: flash mint only WETH10"); return 0; } /// @dev Flash lends `value` WETH10 token to the receiver address. /// By the end of the transaction, `value` WETH10 token will be burned from the receiver. /// The flash-minted WETH10 token is not backed by real ETH, but can be withdrawn as such up to the ETH balance of this contract. /// Arbitrary data can be passed as a bytes calldata parameter. /// Emits {Approval} event to reflect reduced allowance `value` for this contract to spend from receiver account (`receiver`), /// unless allowance is set to `type(uint256).max` /// Emits two {Transfer} events for minting and burning of the flash-minted amount. /// Returns boolean value indicating whether operation succeeded. /// Requirements: /// - `value` must be less or equal to type(uint112).max. /// - The total of all flash loans in a tx must be less or equal to type(uint112).max. function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 value, bytes calldata data) external override returns (bool) { require(token == address(this), "WETH: flash mint only WETH10"); require(value <= type(uint112).max, "WETH: individual loan limit exceeded"); flashMinted = flashMinted + value; require(flashMinted <= type(uint112).max, "WETH: total loan limit exceeded"); // _mintTo(address(receiver), value); balanceOf[address(receiver)] += value; emit Transfer(address(0), address(receiver), value); require( receiver.onFlashLoan(msg.sender, address(this), value, 0, data) == CALLBACK_SUCCESS, "WETH: flash loan failed" ); // _decreaseAllowance(address(receiver), address(this), value); uint256 allowed = allowance[address(receiver)][address(this)]; if (allowed != type(uint256).max) { require(allowed >= value, "WETH: request exceeds allowance"); uint256 reduced = allowed - value; allowance[address(receiver)][address(this)] = reduced; emit Approval(address(receiver), address(this), reduced); } // _burnFrom(address(receiver), value); uint256 balance = balanceOf[address(receiver)]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[address(receiver)] = balance - value; emit Transfer(address(receiver), address(0), value); flashMinted = flashMinted - value; return true; } /// @dev Burn `value` WETH10 token from caller account and withdraw matching ETH to the same. /// Emits {Transfer} event to reflect WETH10 token burn of `value` to `address(0)` from caller account. /// Requirements: /// - caller account must have at least `value` balance of WETH10 token. function withdraw(uint256 value) external override { // _burnFrom(msg.sender, value); uint256 balance = balanceOf[msg.sender]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[msg.sender] = balance - value; emit Transfer(msg.sender, address(0), value); // _transferEther(msg.sender, value); (bool success, ) = msg.sender.call{value: value}(""); require(success, "WETH: ETH transfer failed"); } /// @dev Burn `value` WETH10 token from caller account and withdraw matching ETH to account (`to`). /// Emits {Transfer} event to reflect WETH10 token burn of `value` to `address(0)` from caller account. /// Requirements: /// - caller account must have at least `value` balance of WETH10 token. function withdrawTo(address payable to, uint256 value) external override { // _burnFrom(msg.sender, value); uint256 balance = balanceOf[msg.sender]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[msg.sender] = balance - value; emit Transfer(msg.sender, address(0), value); // _transferEther(to, value); (bool success, ) = to.call{value: value}(""); require(success, "WETH: ETH transfer failed"); } /// @dev Burn `value` WETH10 token from account (`from`) and withdraw matching ETH to account (`to`). /// Emits {Approval} event to reflect reduced allowance `value` for caller account to spend from account (`from`), /// unless allowance is set to `type(uint256).max` /// Emits {Transfer} event to reflect WETH10 token burn of `value` to `address(0)` from account (`from`). /// Requirements: /// - `from` account must have at least `value` balance of WETH10 token. /// - `from` account must have approved caller to spend at least `value` of WETH10 token, unless `from` and caller are the same account. function withdrawFrom(address from, address payable to, uint256 value) external override { if (from != msg.sender) { // _decreaseAllowance(from, msg.sender, value); uint256 allowed = allowance[from][msg.sender]; if (allowed != type(uint256).max) { require(allowed >= value, "WETH: request exceeds allowance"); uint256 reduced = allowed - value; allowance[from][msg.sender] = reduced; emit Approval(from, msg.sender, reduced); } } // _burnFrom(from, value); uint256 balance = balanceOf[from]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[from] = balance - value; emit Transfer(from, address(0), value); // _transferEther(to, value); (bool success, ) = to.call{value: value}(""); require(success, "WETH: Ether transfer failed"); } /// @dev Sets `value` as allowance of `spender` account over caller account's WETH10 token. /// Emits {Approval} event. /// Returns boolean value indicating whether operation succeeded. function approve(address spender, uint256 value) external override returns (bool) { // _approve(msg.sender, spender, value); allowance[msg.sender][spender] = value; emit Approval(msg.sender, spender, value); return true; } /// @dev Sets `value` as allowance of `spender` account over caller account's WETH10 token, /// after which a call is executed to an ERC677-compliant contract with the `data` parameter. /// Emits {Approval} event. /// Returns boolean value indicating whether operation succeeded. /// For more information on {approveAndCall} format, see https://github.com/ethereum/EIPs/issues/677. function approveAndCall(address spender, uint256 value, bytes calldata data) external override returns (bool) { // _approve(msg.sender, spender, value); allowance[msg.sender][spender] = value; emit Approval(msg.sender, spender, value); return IApprovalReceiver(spender).onTokenApproval(msg.sender, value, data); } /// @dev Sets `value` as allowance of `spender` account over `owner` account's WETH10 token, given `owner` account's signed approval. /// Emits {Approval} event. /// Requirements: /// - `deadline` must be timestamp in future. /// - `v`, `r` and `s` must be valid `secp256k1` signature from `owner` account over EIP712-formatted function arguments. /// - the signature must use `owner` account's current nonce (see {nonces}). /// - the signer cannot be `address(0)` and must be `owner` account. /// For more information on signature format, see https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP section]. /// WETH10 token implementation adapted from https://github.com/albertocuestacanada/ERC20Permit/blob/master/contracts/ERC20Permit.sol. function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external override { Erequire(block.timestamp <= deadline, "WETH: Expired permit"); uint256 chainId; assembly {chainId := chainid()} bytes32 hashStruct = keccak256( abi.encode( PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline)); bytes32 hash = keccak256( abi.encodePacked( "\x19\x01", chainId == deploymentChainId ? _DOMAIN_SEPARATOR : _calculateDomainSeparator(chainId), hashStruct)); address signer = ecrecover(hash, v, r, s); Erequire(signer != address(0) && signer == owner, "WETH: invalid permit"); // _approve(owner, spender, value); allowance[owner][spender] = value; emit Approval(owner, spender, value); } /// @dev Moves `value` WETH10 token from caller's account to account (`to`). /// A transfer to `address(0)` triggers an ETH withdraw matching the sent WETH10 token in favor of caller. /// Emits {Transfer} event. /// Returns boolean value indicating whether operation succeeded. /// Requirements: /// - caller account must have at least `value` WETH10 token. function transfer(address to, uint256 value) external override returns (bool) { // _transferFrom(msg.sender, to, value); Eif (to != address(0)) { // Transfer uint256 balance = balanceOf[msg.sender]; Erequire(balance >= value, "WETH: transfer amount exceeds balance"); balanceOf[msg.sender] = balance - value; balanceOf[to] += value; emit Transfer(msg.sender, to, value); } else { // Withdraw uint256 balance = balanceOf[msg.sender]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[msg.sender] = balance - value; emit Transfer(msg.sender, address(0), value); (bool success, ) = msg.sender.call{value: value}(""); require(success, "WETH: ETH transfer failed"); } return true; } /// @dev Moves `value` WETH10 token from account (`from`) to account (`to`) using allowance mechanism. /// `value` is then deducted from caller account's allowance, unless set to `type(uint256).max`. /// A transfer to `address(0)` triggers an ETH withdraw matching the sent WETH10 token in favor of caller. /// Emits {Approval} event to reflect reduced allowance `value` for caller account to spend from account (`from`), /// unless allowance is set to `type(uint256).max` /// Emits {Transfer} event. /// Returns boolean value indicating whether operation succeeded. /// Requirements: /// - `from` account must have at least `value` balance of WETH10 token. /// - `from` account must have approved caller to spend at least `value` of WETH10 token, unless `from` and caller are the same account. function transferFrom(address from, address to, uint256 value) external override returns (bool) { Eif (from != msg.sender) { // _decreaseAllowance(from, msg.sender, value); uint256 allowed = allowance[from][msg.sender]; if (allowed != type(uint256).max) { Erequire(allowed >= value, "WETH: request exceeds allowance"); uint256 reduced = allowed - value; allowance[from][msg.sender] = reduced; emit Approval(from, msg.sender, reduced); } } // _transferFrom(from, to, value); Eif (to != address(0)) { // Transfer uint256 balance = balanceOf[from]; require(balance >= value, "WETH: transfer amount exceeds balance"); balanceOf[from] = balance - value; balanceOf[to] += value; emit Transfer(from, to, value); } else { // Withdraw uint256 balance = balanceOf[from]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[from] = balance - value; emit Transfer(from, address(0), value); (bool success, ) = msg.sender.call{value: value}(""); require(success, "WETH: ETH transfer failed"); } return true; } /// @dev Moves `value` WETH10 token from caller's account to account (`to`), /// after which a call is executed to an ERC677-compliant contract with the `data` parameter. /// A transfer to `address(0)` triggers an ETH withdraw matching the sent WETH10 token in favor of caller. /// Emits {Transfer} event. /// Returns boolean value indicating whether operation succeeded. /// Requirements: /// - caller account must have at least `value` WETH10 token. /// For more information on {transferAndCall} format, see https://github.com/ethereum/EIPs/issues/677. function transferAndCall(address to, uint value, bytes calldata data) external override returns (bool) { // _transferFrom(msg.sender, to, value); if (to != address(0)) { // Transfer uint256 balance = balanceOf[msg.sender]; require(balance >= value, "WETH: transfer amount exceeds balance"); balanceOf[msg.sender] = balance - value; balanceOf[to] += value; emit Transfer(msg.sender, to, value); } else { // Withdraw uint256 balance = balanceOf[msg.sender]; require(balance >= value, "WETH: burn amount exceeds balance"); balanceOf[msg.sender] = balance - value; emit Transfer(msg.sender, address(0), value); (bool success, ) = msg.sender.call{value: value}(""); require(success, "WETH: ETH transfer failed"); } return ITransferReceiver(to).onTokenTransfer(msg.sender, value, data); } } |