-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniswapV2RouterWithRewards.rb
360 lines (281 loc) · 10.1 KB
/
UniswapV2RouterWithRewards.rb
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
pragma :rubidity, "1.0.0"
import 'Upgradeable'
import 'UniswapV2Router'
contract :UniswapV2RouterWithRewards, is: [:UniswapV2Router, :Upgradeable], upgradeable: true do
event :FeeAccrued, {
total: :uint256,
toStakers: :uint256,
toSwappers: :uint256,
toProtocol: :uint256
}
address :public, :feeAdmin
uint256 :public, :feeBPS
uint256 :public, :swapperFeePct
uint256 :public, :stakerFeePct
uint256 :public, :protocolFeePct
mapping ({ address: :uint256 }), :public, :stakerRewardsPool
uint256 :public, :swapperRewardsPool
uint256 :public, :protocolFeePool
mapping ({ address: mapping(address: :uint256) }), :public, :stakedLP
mapping ({ address: mapping(address: :uint256) }), :public, :rewardDebt
mapping ({ address: :uint256 }), :public, :totalStakedLP
mapping ({ address: :uint256 }), :public, :accRewardsPerShare
array :address, :public, :topFiveSwappers, initial_length: 5
mapping ({ address: :uint256 }), :public, :feesGeneratedBySwapper
mapping ({ address: :uint256 }), :public, :swapperRewards
uint256 :public, :decimals
constructor(
_factory: :address,
_WETH: :address,
feeBPS: :uint256,
swapperFeePct: :uint256,
stakerFeePct: :uint256,
protocolFeePct: :uint256,
feeAdmin: :address
) {
updateFees(
feeBPS: feeBPS,
swapperFeePct: swapperFeePct,
stakerFeePct: stakerFeePct,
protocolFeePct: protocolFeePct,
feeAdmin: feeAdmin
)
s.decimals = 18
UniswapV2Router.constructor(_factory: _factory, _WETH: _WETH)
Upgradeable.constructor(upgradeAdmin: msg.sender)
}
function :updateFees, {
feeBPS: :uint256,
swapperFeePct: :uint256,
stakerFeePct: :uint256,
protocolFeePct: :uint256,
feeAdmin: :address
}, :public do
require(msg.sender == s.feeAdmin || s.feeAdmin == address(0), 'Only fee admin can update fees')
require(feeBPS <= 10000, 'Fee cannot be greater than 100%')
require(
swapperFeePct + stakerFeePct + protocolFeePct == 100,
'Fees must add up to 100%'
)
require(feeAdmin != address(0), 'Fee admin cannot be address(0)')
s.feeBPS = feeBPS
s.stakerFeePct = stakerFeePct
s.swapperFeePct = swapperFeePct
s.protocolFeePct = protocolFeePct
s.feeAdmin = feeAdmin
nil
end
function :stakeLP, { lpToken: :address, amount: :uint256 }, :public, returns: :bool do
lpPair = UniswapV2Pair(lpToken)
token0 = lpPair.token0()
token1 = lpPair.token1()
require(token0 == s.WETH || token1 == s.WETH, 'One of the tokens must be WETH')
require(amount > 0, 'Stake amount must be greater than 0')
updateStakeAmount(lpToken: lpToken, amount: amount, isStaking: true, user: msg.sender)
ERC20(lpToken).transferFrom(msg.sender, address(this), amount)
end
function :unstakeLP, { lpToken: :address, amount: :uint256 }, :public, returns: :bool do
require(s.stakedLP[msg.sender][lpToken] >= amount, 'Insufficient staked amount')
require(amount > 0, 'Unstake amount must be greater than 0')
updateStakeAmount(lpToken: lpToken, amount: amount, isStaking: false, user: msg.sender)
ERC20(lpToken).transfer(msg.sender, amount)
end
function :withdrawStakingRewards, { lpToken: :address }, :public do
updateStakeAmount(lpToken: lpToken, amount: 0, isStaking: true, user: msg.sender)
end
function :updateStakeAmount, {
lpToken: :address,
amount: :uint256,
isStaking: :bool,
user: :address
}, :internal do
updateStakingRewards(lpToken: lpToken)
pending = pendingStakingRewards(user: user, lpToken: lpToken)
require(pending > 0 || amount > 0, 'Nothing to do')
if pending > 0
ERC20(s.WETH).transfer(user, pending)
end
if isStaking
s.stakedLP[user][lpToken] += amount
s.totalStakedLP[lpToken] += amount
else
s.stakedLP[user][lpToken] -= amount
s.totalStakedLP[lpToken] -= amount
end
unScaledDebt = s.stakedLP[user][lpToken] * s.accRewardsPerShare[lpToken]
s.rewardDebt[user][lpToken] = unScaledDebt.div(1.ether)
nil
end
function :swapExactTokensForTokens, {
amountIn: :uint256,
amountOutMin: :uint256,
path: [:address],
to: :address,
deadline: :uint256
}, :public, :virtual, :override, returns: [:uint256] do
amounts = UniswapV2Router.swapExactTokensForTokens(
amountIn: amountIn - calculateFeeAmount(amountIn),
amountOutMin: amountOutMin,
path: path,
to: to,
deadline: deadline
)
feeInWETH = chargeFeeInWETH(amountIn, path[0])
lpToken = pairFor(factory, path[0], path[1])
s.stakerRewardsPool[lpToken] += (feeInWETH * s.stakerFeePct).div(100)
s.protocolFeePool += (feeInWETH * s.protocolFeePct).div(100)
updateTopFiveSwappers(
currentSwapper: msg.sender,
currentFee: feeInWETH
)
emit :FeeAccrued, {
total: feeInWETH,
toStakers: (feeInWETH * s.stakerFeePct).div(100),
toSwappers: (feeInWETH * s.swapperFeePct).div(100),
toProtocol: (feeInWETH * s.protocolFeePct).div(100)
}
amounts
end
function :swapTokensForExactTokens, {
amountOut: :uint256,
amountInMax: :uint256,
path: [:address],
to: :address,
deadline: :uint256
}, :public, :virtual, :override, returns: [:uint256] do
amounts = UniswapV2Router.swapTokensForExactTokens(
amountOut: amountOut + calculateFeeAmount(amountOut),
amountInMax: amountInMax,
path: path,
to: to,
deadline: deadline
)
feeInWETH = chargeFeeInWETH(amountOut, path[1])
lpToken = pairFor(factory, path[0], path[1])
s.stakerRewardsPool[lpToken] += (feeInWETH * s.stakerFeePct).div(100)
s.protocolFeePool += (feeInWETH * s.protocolFeePct).div(100)
updateTopFiveSwappers(
currentSwapper: msg.sender,
currentFee: feeInWETH
)
emit :FeeAccrued, {
total: feeInWETH,
toStakers: (feeInWETH * s.stakerFeePct).div(100),
toSwappers: (feeInWETH * s.swapperFeePct).div(100),
toProtocol: (feeInWETH * s.protocolFeePct).div(100)
}
amounts
end
function :calculateFeeAmount, { amount: :uint256 }, :public, :view, returns: :uint256 do
return (amount * s.feeBPS).div(10000)
end
function :calculateAccRewardsPerShare, { lpToken: :address }, :internal, :view, returns: :uint256 do
accRewardsPerShare = s.accRewardsPerShare[lpToken]
if s.totalStakedLP[lpToken] > 0 && s.stakerRewardsPool[lpToken] > 0
accRewardPerShareIncrement = (s.stakerRewardsPool[lpToken] * 1.ether).div(s.totalStakedLP[lpToken])
accRewardsPerShare += accRewardPerShareIncrement
end
return accRewardsPerShare
end
function :updateStakingRewards, { lpToken: :address }, :internal do
s.accRewardsPerShare[lpToken] = calculateAccRewardsPerShare(lpToken: lpToken)
s.stakerRewardsPool[lpToken] = 0
nil
end
function :pendingStakingRewards, { user: :address, lpToken: :address }, :public, :view, returns: :uint256 do
accRewardsPerShare = calculateAccRewardsPerShare(lpToken: lpToken)
topLine = (s.stakedLP[user][lpToken] * accRewardsPerShare).div(1.ether)
return topLine - s.rewardDebt[user][lpToken]
end
function :chargeFeeInWETH, {
amount: :uint256,
token: :address
}, :internal, :virtual, returns: :uint256 do
feeAmount = calculateFeeAmount(amount)
if token == s.WETH
ERC20(token).transferFrom(
from: msg.sender,
to: address(this),
amount: feeAmount
)
return feeAmount
end
path = array(:address, 2)
path[0] = token
path[1] = s.WETH
feeInWETH = UniswapV2Router.swapExactTokensForTokens(
amountIn: feeAmount,
amountOutMin: 0,
path: path,
to: address(this),
deadline: block.timestamp + 1
)[1]
return feeInWETH
end
function :updateSwapperRewards, :internal do
nonNullSwapperCount = 0
forLoop(
condition: ->(i) { i < s.topFiveSwappers.length },
max_iterations: 5
) do |i|
if s.topFiveSwappers[i] != address(0)
nonNullSwapperCount += 1
end
end
return if nonNullSwapperCount == 0
individualSwapperReward = s.swapperRewardsPool.div(nonNullSwapperCount)
forLoop(
condition: ->(i) { i < s.topFiveSwappers.length },
max_iterations: 5
) do |i|
swapper = s.topFiveSwappers[i]
if swapper != address(0)
s.swapperRewards[swapper] += individualSwapperReward
end
end
s.swapperRewardsPool = 0
nil
end
function :withdrawProtocolRewards, { to: :address }, :public, returns: :bool do
require(msg.sender == s.feeAdmin, "Only fee admin can withdraw protocol rewards")
amount = s.protocolFeePool
require(amount > 0, "No rewards to withdraw")
s.protocolFeePool = 0
ERC20(s.WETH).transfer(to, amount)
end
function :withdrawSwapperRewards, :public, returns: :bool do
updateSwapperRewards()
amount = s.swapperRewards[msg.sender]
require(amount > 0, "No rewards to withdraw")
s.swapperRewards[msg.sender] = 0
ERC20(s.WETH).transfer(msg.sender, amount)
end
function :updateTopFiveSwappers, { currentSwapper: :address, currentFee: :uint256 }, :internal do
updateSwapperRewards()
s.feesGeneratedBySwapper[currentSwapper] += currentFee
s.swapperRewardsPool += (currentFee * s.swapperFeePct).div(100)
newTotal = s.feesGeneratedBySwapper[currentSwapper]
forLoop(
condition: ->(i) { i < s.topFiveSwappers.length },
max_iterations: 5
) do |i|
return if s.topFiveSwappers[i] == currentSwapper
end
minFee = 2 ** 256 - 1
minIndex = 0
forLoop(
condition: -> i { i < s.topFiveSwappers.length },
max_iterations: 5
) do |i|
swapper = s.topFiveSwappers[i]
if swapper == address(0) || s.feesGeneratedBySwapper[swapper] < minFee
minFee = s.feesGeneratedBySwapper[swapper]
minIndex = i
end
end
if newTotal > minFee
s.topFiveSwappers[minIndex] = currentSwapper
end
nil
end
end