forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ERC4626.vy
244 lines (168 loc) · 6.2 KB
/
ERC4626.vy
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
#pragma version >0.3.10
# NOTE: Copied from https://github.com/fubuloubu/ERC4626/blob/1a10b051928b11eeaad15d80397ed36603c2a49b/contracts/VyperVault.vy
# example implementation of an ERC4626 vault
###########################################################################
## THIS IS EXAMPLE CODE, NOT MEANT TO BE USED IN PRODUCTION! CAVEAT EMPTOR!
###########################################################################
from ethereum.ercs import IERC20
from ethereum.ercs import IERC4626
implements: IERC20
implements: IERC4626
##### ERC20 #####
totalSupply: public(uint256)
balanceOf: public(HashMap[address, uint256])
allowance: public(HashMap[address, HashMap[address, uint256]])
NAME: constant(String[10]) = "Test Vault"
SYMBOL: constant(String[5]) = "vTEST"
DECIMALS: constant(uint8) = 18
##### ERC4626 #####
asset: public(IERC20)
@deploy
def __init__(asset: IERC20):
self.asset = asset
@view
@external
def name() -> String[10]:
return NAME
@view
@external
def symbol() -> String[5]:
return SYMBOL
@view
@external
def decimals() -> uint8:
return DECIMALS
@external
def transfer(receiver: address, amount: uint256) -> bool:
self.balanceOf[msg.sender] -= amount
self.balanceOf[receiver] += amount
log IERC20.Transfer(msg.sender, receiver, amount)
return True
@external
def approve(spender: address, amount: uint256) -> bool:
self.allowance[msg.sender][spender] = amount
log IERC20.Approval(msg.sender, spender, amount)
return True
@external
def transferFrom(sender: address, receiver: address, amount: uint256) -> bool:
self.allowance[sender][msg.sender] -= amount
self.balanceOf[sender] -= amount
self.balanceOf[receiver] += amount
log IERC20.Transfer(sender, receiver, amount)
return True
@view
@external
def totalAssets() -> uint256:
return staticcall self.asset.balanceOf(self)
@view
@internal
def _convertToAssets(shareAmount: uint256) -> uint256:
totalSupply: uint256 = self.totalSupply
if totalSupply == 0:
return 0
# NOTE: `shareAmount = 0` is extremely rare case, not optimizing for it
# NOTE: `totalAssets = 0` is extremely rare case, not optimizing for it
return shareAmount * staticcall self.asset.balanceOf(self) // totalSupply
@view
@external
def convertToAssets(shareAmount: uint256) -> uint256:
return self._convertToAssets(shareAmount)
@view
@internal
def _convertToShares(assetAmount: uint256) -> uint256:
totalSupply: uint256 = self.totalSupply
totalAssets: uint256 = staticcall self.asset.balanceOf(self)
if totalAssets == 0 or totalSupply == 0:
return assetAmount # 1:1 price
# NOTE: `assetAmount = 0` is extremely rare case, not optimizing for it
return assetAmount * totalSupply // totalAssets
@view
@external
def convertToShares(assetAmount: uint256) -> uint256:
return self._convertToShares(assetAmount)
@view
@external
def maxDeposit(owner: address) -> uint256:
return max_value(uint256)
@view
@external
def previewDeposit(assets: uint256) -> uint256:
return self._convertToShares(assets)
@external
def deposit(assets: uint256, receiver: address=msg.sender) -> uint256:
shares: uint256 = self._convertToShares(assets)
extcall self.asset.transferFrom(msg.sender, self, assets)
self.totalSupply += shares
self.balanceOf[receiver] += shares
log IERC4626.Deposit(msg.sender, receiver, assets, shares)
return shares
@view
@external
def maxMint(owner: address) -> uint256:
return max_value(uint256)
@view
@external
def previewMint(shares: uint256) -> uint256:
assets: uint256 = self._convertToAssets(shares)
# NOTE: Vyper does lazy eval on `and`, so this avoids SLOADs most of the time
if assets == 0 and staticcall self.asset.balanceOf(self) == 0:
return shares # NOTE: Assume 1:1 price if nothing deposited yet
return assets
@external
def mint(shares: uint256, receiver: address=msg.sender) -> uint256:
assets: uint256 = self._convertToAssets(shares)
if assets == 0 and staticcall self.asset.balanceOf(self) == 0:
assets = shares # NOTE: Assume 1:1 price if nothing deposited yet
extcall self.asset.transferFrom(msg.sender, self, assets)
self.totalSupply += shares
self.balanceOf[receiver] += shares
log IERC4626.Deposit(msg.sender, receiver, assets, shares)
return assets
@view
@external
def maxWithdraw(owner: address) -> uint256:
return max_value(uint256) # real max is `self.asset.balanceOf(self)`
@view
@external
def previewWithdraw(assets: uint256) -> uint256:
shares: uint256 = self._convertToShares(assets)
# NOTE: Vyper does lazy eval on and, so this avoids SLOADs most of the time
if shares == assets and self.totalSupply == 0:
return 0 # NOTE: Nothing to redeem
return shares
@external
def withdraw(assets: uint256, receiver: address=msg.sender, owner: address=msg.sender) -> uint256:
shares: uint256 = self._convertToShares(assets)
# NOTE: Vyper does lazy eval on `and`, so this avoids SLOADs most of the time
if shares == assets and self.totalSupply == 0:
raise # Nothing to redeem
if owner != msg.sender:
self.allowance[owner][msg.sender] -= shares
self.totalSupply -= shares
self.balanceOf[owner] -= shares
extcall self.asset.transfer(receiver, assets)
log IERC4626.Withdraw(msg.sender, receiver, owner, assets, shares)
return shares
@view
@external
def maxRedeem(owner: address) -> uint256:
return max_value(uint256) # real max is `self.totalSupply`
@view
@external
def previewRedeem(shares: uint256) -> uint256:
return self._convertToAssets(shares)
@external
def redeem(shares: uint256, receiver: address=msg.sender, owner: address=msg.sender) -> uint256:
if owner != msg.sender:
self.allowance[owner][msg.sender] -= shares
assets: uint256 = self._convertToAssets(shares)
self.totalSupply -= shares
self.balanceOf[owner] -= shares
extcall self.asset.transfer(receiver, assets)
log IERC4626.Withdraw(msg.sender, receiver, owner, assets, shares)
return assets
@external
def DEBUG_steal_tokens(amount: uint256):
# NOTE: This is the primary method of mocking share price changes
# do not put in production code!!!
extcall self.asset.transfer(msg.sender, amount)