|
| 1 | +from ethereum.ercs import IERC20 |
| 2 | + |
| 3 | +implements: IERC20 |
| 4 | + |
| 5 | +totalSupply: public(uint256) |
| 6 | +balanceOf: public(HashMap[address, uint256]) |
| 7 | +allowance: public(HashMap[address, HashMap[address, uint256]]) |
| 8 | + |
| 9 | + |
| 10 | +@deploy |
| 11 | +def __init__(): |
| 12 | + self.totalSupply = 100 * 10 ** 18 |
| 13 | + self.balanceOf[msg.sender] = 100 * 10 ** 18 |
| 14 | + |
| 15 | + |
| 16 | +@external |
| 17 | +def transfer(receiver: address, amount: uint256) -> bool: |
| 18 | + self.balanceOf[msg.sender] -= amount |
| 19 | + self.balanceOf[receiver] += amount |
| 20 | + log IERC20.Transfer(sender=msg.sender, receiver=receiver, value=amount) |
| 21 | + return True |
| 22 | + |
| 23 | + |
| 24 | +@external |
| 25 | +def approve(spender: address, amount: uint256) -> bool: |
| 26 | + self.allowance[msg.sender][spender] = amount |
| 27 | + log IERC20.Approval(owner=msg.sender, spender=spender, value=amount) |
| 28 | + return True |
| 29 | + |
| 30 | + |
| 31 | +@external |
| 32 | +def transferFrom(owner: address, receiver: address, amount: uint256) -> bool: |
| 33 | + self.allowance[owner][msg.sender] -= amount |
| 34 | + self.balanceOf[owner] -= amount |
| 35 | + self.balanceOf[receiver] += amount |
| 36 | + log IERC20.Transfer(sender=owner, receiver=receiver, value=amount) |
| 37 | + return True |
| 38 | + |
| 39 | + |
| 40 | +@external |
| 41 | +def mint(receiver: address, amount: uint256): |
| 42 | + self.totalSupply += amount |
| 43 | + self.balanceOf[receiver] += amount |
| 44 | + log IERC20.Transfer(sender=empty(address), receiver=receiver, value=amount) |
| 45 | + |
| 46 | + |
| 47 | +interface IERC3156FlashBorrower: |
| 48 | + def onFlashLoan( |
| 49 | + initiator: address, |
| 50 | + token: IERC20, |
| 51 | + amount: uint256, |
| 52 | + fee: uint256, |
| 53 | + data: Bytes[65535], |
| 54 | + ) -> bytes32: nonpayable |
| 55 | + |
| 56 | + |
| 57 | +interface IERC3156: |
| 58 | + def maxFlashLoan(token: IERC20) -> uint256: view |
| 59 | + def flashFee(token: IERC20, amount: uint256) -> uint256: view |
| 60 | + def flashLoan( |
| 61 | + receiver: IERC3156FlashBorrower, |
| 62 | + token: IERC20, |
| 63 | + amount: uint256, |
| 64 | + data: Bytes[65535], |
| 65 | + ) -> bool: nonpayable |
| 66 | + |
| 67 | + |
| 68 | +implements: IERC3156 |
| 69 | + |
| 70 | + |
| 71 | +@view |
| 72 | +@external |
| 73 | +def maxFlashLoan(token: IERC20) -> uint256: |
| 74 | + if token.address == self: |
| 75 | + return max_value(uint256) - self.totalSupply |
| 76 | + |
| 77 | + return 0 |
| 78 | + |
| 79 | + |
| 80 | +@view |
| 81 | +@external |
| 82 | +def flashFee(token: IERC20, amount: uint256) -> uint256: |
| 83 | + return 0 |
| 84 | + |
| 85 | + |
| 86 | +@external |
| 87 | +def flashLoan( |
| 88 | + receiver: IERC3156FlashBorrower, |
| 89 | + token: IERC20, |
| 90 | + amount: uint256, |
| 91 | + data: Bytes[65535], |
| 92 | +) -> bool: |
| 93 | + assert token.address == self |
| 94 | + |
| 95 | + # Send our tokens to receiver |
| 96 | + self.totalSupply += amount |
| 97 | + self.balanceOf[receiver.address] += amount |
| 98 | + log IERC20.Transfer(sender=empty(address), receiver=receiver.address, value=amount) |
| 99 | + |
| 100 | + assert ( |
| 101 | + # NOTE: `msg.sender` is original caller of delegatecall |
| 102 | + extcall receiver.onFlashLoan(msg.sender, IERC20(self), amount, 0, data) |
| 103 | + # NOTE: Magic value per ERC-3156 |
| 104 | + == keccak256("ERC3156FlashBorrower.onFlashLoan") |
| 105 | + ), "Flashloan receiver not valid" |
| 106 | + |
| 107 | + # Get our tokens back (mimic a real flash lender) |
| 108 | + self.allowance[receiver.address][self] -= amount |
| 109 | + self.totalSupply -= amount |
| 110 | + self.balanceOf[receiver.address] -= amount |
| 111 | + log IERC20.Transfer(sender=receiver.address, receiver=empty(address), value=amount) |
| 112 | + |
| 113 | + return True |
0 commit comments