-
Notifications
You must be signed in to change notification settings - Fork 7
/
market.cairo
406 lines (347 loc) · 12.8 KB
/
market.cairo
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
mod view;
mod external;
mod internal;
mod traits;
mod errors;
mod storage;
struct UpdatedAccumulators {
lending_accumulator: felt252,
debt_accumulator: felt252
}
#[starknet::contract]
mod Market {
use starknet::{ClassHash, ContractAddress};
// Hack to simulate the `crate` keyword
use super::super as crate;
use crate::interfaces::{IMarket, MarketReserveData};
use super::{external, view};
#[storage]
struct Storage {
oracle: ContractAddress,
treasury: ContractAddress,
reserves: LegacyMap::<ContractAddress, MarketReserveData>,
reserve_count: felt252,
// index -> token
reserve_tokens: LegacyMap::<felt252, ContractAddress>,
// token -> index
reserve_indices: LegacyMap::<ContractAddress, felt252>,
/// Bit 0: whether reserve #0 is used as collateral
/// Bit 1: whether user has debt in reserve #0
/// Bit 2: whether reserve #1 is used as collateral
/// Bit 3: whether user has debt in reserve #1
/// ...
user_flags: LegacyMap::<ContractAddress, felt252>,
// (user, token) -> debt
raw_user_debts: LegacyMap::<(ContractAddress, ContractAddress), felt252>,
// This weird naming is to maintain backward compatibility with the Cairo 0 version
Ownable_owner: ContractAddress,
// Used in `reentrancy_guard`
entered: bool
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
NewReserve: NewReserve,
TreasuryUpdate: TreasuryUpdate,
AccumulatorsSync: AccumulatorsSync,
InterestRatesSync: InterestRatesSync,
InterestRateModelUpdate: InterestRateModelUpdate,
CollateralFactorUpdate: CollateralFactorUpdate,
BorrowFactorUpdate: BorrowFactorUpdate,
ReserveFactorUpdate: ReserveFactorUpdate,
DebtLimitUpdate: DebtLimitUpdate,
DepositLimitUpdate: DepositLimitUpdate,
Deposit: Deposit,
Withdrawal: Withdrawal,
Borrowing: Borrowing,
Repayment: Repayment,
Liquidation: Liquidation,
FlashLoan: FlashLoan,
CollateralEnabled: CollateralEnabled,
CollateralDisabled: CollateralDisabled,
ContractUpgraded: ContractUpgraded,
OwnershipTransferred: OwnershipTransferred
}
#[derive(Drop, PartialEq, starknet::Event)]
struct NewReserve {
token: ContractAddress,
z_token: ContractAddress,
decimals: felt252,
interest_rate_model: ContractAddress,
collateral_factor: felt252,
borrow_factor: felt252,
reserve_factor: felt252,
flash_loan_fee: felt252,
liquidation_bonus: felt252,
}
#[derive(Drop, PartialEq, starknet::Event)]
struct TreasuryUpdate {
new_treasury: ContractAddress
}
#[derive(Drop, PartialEq, starknet::Event)]
struct AccumulatorsSync {
token: ContractAddress,
lending_accumulator: felt252,
debt_accumulator: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct InterestRatesSync {
token: ContractAddress,
lending_rate: felt252,
borrowing_rate: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct InterestRateModelUpdate {
token: ContractAddress,
interest_rate_model: ContractAddress
}
#[derive(Drop, PartialEq, starknet::Event)]
struct CollateralFactorUpdate {
token: ContractAddress,
collateral_factor: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct BorrowFactorUpdate {
token: ContractAddress,
borrow_factor: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct ReserveFactorUpdate {
token: ContractAddress,
reserve_factor: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct DebtLimitUpdate {
token: ContractAddress,
limit: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct DepositLimitUpdate {
token: ContractAddress,
limit: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct Deposit {
user: ContractAddress,
token: ContractAddress,
face_amount: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct Withdrawal {
user: ContractAddress,
token: ContractAddress,
face_amount: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct Borrowing {
user: ContractAddress,
token: ContractAddress,
raw_amount: felt252,
face_amount: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct Repayment {
repayer: ContractAddress,
beneficiary: ContractAddress,
token: ContractAddress,
raw_amount: felt252,
face_amount: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct Liquidation {
liquidator: ContractAddress,
user: ContractAddress,
debt_token: ContractAddress,
debt_raw_amount: felt252,
debt_face_amount: felt252,
collateral_token: ContractAddress,
collateral_amount: felt252,
}
/// `fee` indicates the actual fee paid back, which could be higher than the minimum required.
#[derive(Drop, PartialEq, starknet::Event)]
struct FlashLoan {
initiator: ContractAddress,
receiver: ContractAddress,
token: ContractAddress,
amount: felt252,
fee: felt252
}
#[derive(Drop, PartialEq, starknet::Event)]
struct CollateralEnabled {
user: ContractAddress,
token: ContractAddress
}
#[derive(Drop, PartialEq, starknet::Event)]
struct CollateralDisabled {
user: ContractAddress,
token: ContractAddress
}
#[derive(Drop, PartialEq, starknet::Event)]
struct ContractUpgraded {
new_class_hash: ClassHash
}
#[derive(Drop, PartialEq, starknet::Event)]
struct OwnershipTransferred {
previous_owner: ContractAddress,
new_owner: ContractAddress,
}
#[constructor]
fn constructor(ref self: ContractState, owner: ContractAddress, oracle: ContractAddress) {
external::initializer(ref self, owner, oracle)
}
#[abi(embed_v0)]
impl IMarketImpl of IMarket<ContractState> {
fn get_reserve_data(self: @ContractState, token: ContractAddress) -> MarketReserveData {
view::get_reserve_data(self, token)
}
fn get_lending_accumulator(self: @ContractState, token: ContractAddress) -> felt252 {
view::get_lending_accumulator(self, token)
}
fn get_debt_accumulator(self: @ContractState, token: ContractAddress) -> felt252 {
view::get_debt_accumulator(self, token)
}
// WARN: this must be run BEFORE adjusting the accumulators (otherwise always returns 0)
fn get_pending_treasury_amount(self: @ContractState, token: ContractAddress) -> felt252 {
view::get_pending_treasury_amount(self, token)
}
fn get_total_debt_for_token(self: @ContractState, token: ContractAddress) -> felt252 {
view::get_total_debt_for_token(self, token)
}
fn get_user_debt_for_token(
self: @ContractState, user: ContractAddress, token: ContractAddress
) -> felt252 {
view::get_user_debt_for_token(self, user, token)
}
/// Returns a bitmap of user flags.
fn get_user_flags(self: @ContractState, user: ContractAddress) -> felt252 {
view::get_user_flags(self, user)
}
fn is_user_undercollateralized(
self: @ContractState, user: ContractAddress, apply_borrow_factor: bool
) -> bool {
view::is_user_undercollateralized(self, user, apply_borrow_factor)
}
fn is_collateral_enabled(
self: @ContractState, user: ContractAddress, token: ContractAddress
) -> bool {
view::is_collateral_enabled(self, user, token)
}
fn user_has_debt(self: @ContractState, user: ContractAddress) -> bool {
view::user_has_debt(self, user)
}
fn deposit(ref self: ContractState, token: ContractAddress, amount: felt252) {
external::deposit(ref self, token, amount)
}
fn withdraw(ref self: ContractState, token: ContractAddress, amount: felt252) {
external::withdraw(ref self, token, amount)
}
fn withdraw_all(ref self: ContractState, token: ContractAddress) {
external::withdraw_all(ref self, token)
}
fn borrow(ref self: ContractState, token: ContractAddress, amount: felt252) {
external::borrow(ref self, token, amount)
}
fn repay(ref self: ContractState, token: ContractAddress, amount: felt252) {
external::repay(ref self, token, amount)
}
fn repay_for(
ref self: ContractState,
token: ContractAddress,
amount: felt252,
beneficiary: ContractAddress
) {
external::repay_for(ref self, token, amount, beneficiary)
}
fn repay_all(ref self: ContractState, token: ContractAddress) {
external::repay_all(ref self, token)
}
fn enable_collateral(ref self: ContractState, token: ContractAddress) {
external::enable_collateral(ref self, token)
}
fn disable_collateral(ref self: ContractState, token: ContractAddress) {
external::disable_collateral(ref self, token)
}
/// With the current design, liquidators are responsible for calculating the maximum amount allowed.
/// We simply check collteralization factor is below one after liquidation.
/// TODO: calculate max amount on-chain because compute is cheap on StarkNet.
fn liquidate(
ref self: ContractState,
user: ContractAddress,
debt_token: ContractAddress,
amount: felt252,
collateral_token: ContractAddress
) {
external::liquidate(ref self, user, debt_token, amount, collateral_token)
}
fn flash_loan(
ref self: ContractState,
receiver: ContractAddress,
token: ContractAddress,
amount: felt252,
calldata: Span::<felt252>
) {
external::flash_loan(ref self, receiver, token, amount, calldata)
}
fn upgrade(ref self: ContractState, new_implementation: ClassHash) {
external::upgrade(ref self, new_implementation)
}
fn add_reserve(
ref self: ContractState,
token: ContractAddress,
z_token: ContractAddress,
interest_rate_model: ContractAddress,
collateral_factor: felt252,
borrow_factor: felt252,
reserve_factor: felt252,
flash_loan_fee: felt252,
liquidation_bonus: felt252
) {
external::add_reserve(
ref self,
token,
z_token,
interest_rate_model,
collateral_factor,
borrow_factor,
reserve_factor,
flash_loan_fee,
liquidation_bonus
)
}
fn set_treasury(ref self: ContractState, new_treasury: ContractAddress) {
external::set_treasury(ref self, new_treasury)
}
fn set_interest_rate_model(
ref self: ContractState, token: ContractAddress, interest_rate_model: ContractAddress
) {
external::set_interest_rate_model(ref self, token, interest_rate_model)
}
fn set_collateral_factor(
ref self: ContractState, token: ContractAddress, collateral_factor: felt252
) {
external::set_collateral_factor(ref self, token, collateral_factor)
}
fn set_borrow_factor(
ref self: ContractState, token: ContractAddress, borrow_factor: felt252
) {
external::set_borrow_factor(ref self, token, borrow_factor)
}
fn set_reserve_factor(
ref self: ContractState, token: ContractAddress, reserve_factor: felt252
) {
external::set_reserve_factor(ref self, token, reserve_factor)
}
fn set_debt_limit(ref self: ContractState, token: ContractAddress, limit: felt252) {
external::set_debt_limit(ref self, token, limit)
}
fn set_deposit_limit(ref self: ContractState, token: ContractAddress, limit: felt252) {
external::set_deposit_limit(ref self, token, limit)
}
fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) {
external::transfer_ownership(ref self, new_owner)
}
fn renounce_ownership(ref self: ContractState) {
external::renounce_ownership(ref self)
}
}
}