-
Notifications
You must be signed in to change notification settings - Fork 1
/
SiennaLend.ts
475 lines (412 loc) · 16.2 KB
/
SiennaLend.ts
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import {
Address,
Client,
Contract,
ContractLink,
Decimal256,
Deployment,
Fee,
PaginatedResponse,
Pagination,
Permit,
Signer,
Snip20,
TokenInfo,
Uint128,
Uint256,
VersionedDeployment,
ViewingKey,
ViewingKeyClient,
randomBase64,
} from './Core'
import type { AuthStrategy, AuthMethod } from './Auth'
import { LendVersion } from './Versions'
import TGEDeployment from './SiennaTGE'
export default class LendDeployment extends VersionedDeployment<LendVersion> {
/** The names of contract in a Lend group. */
names = {
interestModel: `Lend[${this.version}].InterestModel`,
oracle: `Lend[${this.version}].Oracle`,
mockOracle: `Lend[${this.version}].MockOracle`,
overseer: `Lend[${this.version}].Overseer`,
rewardToken: `Lend[${this.version}].Placeholder.SIENNA`,
}
/** The lend interest model contract. */
interestModel = this.contract({ name: this.names.interestModel, client: LendInterestModel })
/** The lend overseer factory. */
overseer = this.contract({ name: this.names.overseer, client: LendOverseer })
/** Configure the overseer whitelist. */
whitelist = async () => {
const MARKET_INITIAL_EXCHANGE_RATE = "0.2";
const MARKET_RESERVE_FACTOR = "1";
const MARKET_SEIZE_FACTOR = "0.9";
const MARKET_LTV_RATIO = "0.7";
const MARKET_TOKEN_SYMBOL = "SSCRT";
const overseer = await this.overseer()
const interestModel = await this.interestModel()
const config = {
initial_exchange_rate: MARKET_INITIAL_EXCHANGE_RATE,
reserve_factor: MARKET_RESERVE_FACTOR,
seize_factor: MARKET_SEIZE_FACTOR,
}
const underlying_asset = {
address: "",
code_hash: "",
}
await overseer.execute({
whitelist: {
config: {
entropy: randomBase64(36),
prng_seed: randomBase64(36),
interest_model_contract: interestModel.asLink,
ltv_ratio: MARKET_LTV_RATIO,
token_symbol: MARKET_TOKEN_SYMBOL,
config,
underlying_asset,
},
},
})
}
/** The known lend markets. */
markets: Promise<LendMarket[]> = Promise.resolve([])
/** The lend oracle. */
oracle?: Promise<LendOracle> = undefined
/** The lend mock oracle. */
mockOracle?: Promise<MockOracle> = undefined
/** The TGE containing the token and RPT used by the deployment. */
tge = new TGEDeployment(this as Deployment)
/** The reward token for Lend. Defaults to SIENNA. */
get rewardToken () { return this.tge.token }
showStatus = async () => {}
}
export type LendAuthStrategy = AuthStrategy
export type LendAuthMethod<T> = AuthMethod<T>
export interface LendMarketState {
/** Block height that the interest was last accrued at */
accrual_block: number,
/** Accumulator of the total earned interest rate since the opening of the market */
borrow_index: Decimal256,
/** Total amount of outstanding borrows of the underlying in this market */
total_borrows: Uint256,
/** Total amount of reserves of the underlying held in this market */
total_reserves: Uint256,
/** Total number of tokens in circulation */
total_supply: Uint256,
/** The amount of the underlying token that the market has. */
underlying_balance: Uint128,
/** Values in the contract that rarely change. */
config: LendMarketConfig
}
export interface LendMarketConfig {
/** Initial exchange rate used when minting the first slTokens (used when totalSupply = 0) */
initial_exchange_rate: Decimal256,
/** Fraction of interest currently set aside for reserves */
reserve_factor: Decimal256,
/** Share of seized collateral that is added to reserves */
seize_factor: Decimal256
}
export interface LendMarketAccount {
/** Amount of slToken that this account has. */
sl_token_balance: Uint256,
/** How much the account has borrowed. */
borrow_balance: Uint256,
/** The current exchange rate in the market. */
exchange_rate: Decimal256
}
export interface LendMarketBorrower {
id: string,
/** Borrow balance at the last interaction of the borrower. */
principal_balance: Uint256,
/** Current borrow balance. */
actual_balance: Uint256,
liquidity: LendAccountLiquidity,
markets: LendOverseerMarket[]
}
export interface LendSimulatedLiquidation {
/** The amount that would be seized by that liquidation minus protocol fees. */
seize_amount: Uint256,
/** If the liquidation would be unsuccessful this will contain amount by which the seize amount falls flat. Otherwise, it's 0. */
shortfall: Uint256
}
export interface LendOverseerMarket {
contract: ContractLink,
/** The symbol of the underlying asset. Note that this is the same as the symbol
* that the oracle expects, not what the actual token has in its storage. */
symbol: string,
/** The decimals that the market has. Corresponds to the decimals of the underlying token. */
decimals: number,
/** The percentage rate at which tokens can be borrowed given the size of the collateral. */
ltv_ratio: Decimal256
}
/** One of the fields will always be 0, depending on the state of the account.*/
export interface LendAccountLiquidity {
/** The USD value borrowable by the user, before it reaches liquidation. */
liquidity: Uint256,
/** If > 0 the account is currently below the collateral requirement and is subject to liquidation. */
shortfall: Uint256
}
export interface LendOverseerConfig {
/** The discount on collateral that a liquidator receives. */
premium: Decimal256,
/** The percentage of a liquidatable account's borrow that can be repaid in a single liquidate transaction.
* If a user has multiple borrowed assets, the close factor applies to any single borrowed asset,
* not the aggregated value of a user’s outstanding borrowing. */
close_factor: Decimal256
}
export type LendMarketPermissions = 'account_info' | 'balance' | 'id'
export type LendOverseerPermissions = 'account_info'
export class LendAuth {
private constructor (private readonly strategy: LendAuthStrategy) { }
static vk (address: Address, key: ViewingKey): LendAuth {
return new this({ type: 'vk', viewing_key: { address, key } })
}
static permit (signer: Signer): LendAuth {
return new this({ type: 'permit', signer })
}
async createMethod <T> (address: Address, permission: T): Promise<LendAuthMethod<T>> {
if (this.strategy.type === 'permit') {
const permit = await this.strategy.signer.sign({
permit_name: `SiennaJS permit for ${address}`,
allowed_tokens: [ address ],
permissions: [ permission ]
})
return { permit }
} else {
return { viewing_key: this.strategy.viewing_key }
}
}
}
export class LendMarket extends Client {
fees = {
accrue_interest: new Fee( '40000', 'uscrt'),
borrow: new Fee( '80000', 'uscrt'),
deposit: new Fee( '60000', 'uscrt'),
liquidate: new Fee('130000', 'uscrt'),
redeem_token: new Fee( '60000', 'uscrt'),
redeem_underlying: new Fee( '60000', 'uscrt'),
repay: new Fee( '90000', 'usrct'),
transfer: new Fee( '80000', 'uscrt'),
}
get vk () {
return new ViewingKeyClient(this.agent, this.address, this.codeHash)
}
/** Convert and burn the specified amount of slToken to the underlying asset
* based on the current exchange rate and transfer them to the user. */
async redeemFromSL (burn_amount: Uint256) {
return await this.execute({ redeem_token: { burn_amount } })
}
/** Burn slToken amount of tokens equivalent to the specified amount
* based on the current exchange rate and transfer the specified amount
* of the underyling asset to the user. */
async redeemFromUnderlying (receive_amount: Uint256) {
return this.execute({ redeem_underlying: { receive_amount } })
}
async borrow (amount: Uint256) {
return await this.execute({ borrow: { amount } })
}
async transfer (amount: Uint256, recipient: Address) {
return await this.execute({ transfer: { amount, recipient } })
}
/** This function is automatically called before every transaction to update to
* the latest state of the market but can also be called manually through here. */
async accrueInterest () {
return this.execute({ accrue_interest: {} })
}
async deposit (amount: Uint256, underlying_asset?: Address) {
const address = underlying_asset || (await this.getUnderlyingAsset()).address
return this.agent!.getClient(Snip20, address)
.withFee(this.getFee('deposit')!)
.send(amount, this.address!, 'deposit')
}
async repay (
amount: Uint256,
/** Optionally specify a borrower ID to repay someone else's debt. */
borrower?: string,
underlying_asset?: Address
) {
const address = underlying_asset || (await this.getUnderlyingAsset()).address
return this.agent!.getClient(Snip20, address)
.withFee(this.getFee('repay')!)
.send(amount, this.address!, { repay: { borrower } })
}
/** Try to liquidate an existing borrower in this market. */
async liquidate (
/** @param amount - the amount to liquidate by. */
amount: Uint256,
/** @param borrower - the ID corresponding to the borrower to liquidate. */
borrower: string,
/** @param collateral - the collateral market address to receive a premium on. */
collateral: Address,
/** @param underlying_asset - The address of the underlying token for this market. Omitting it will result in an extra query. */
underlying_asset?: Address
) {
const address = underlying_asset || (await this.getUnderlyingAsset()).address
return this.agent!.getClient(Snip20, address)
.withFee(this.getFee('liquidate')!)
.send(amount, this.address!, { liquidate: { borrower, collateral } })
}
/** Dry run a liquidation returning a result indicating the amount of `collateral`
* which would be seized, and whether the liquidation would be successful depending
* on whether the borrower posseses the seized collateral amount.
* If it wouldn't, throw any other error that might occur during the actual liquidation.
*
* If you haven't taken the close factor into account already, you might want to look for
* an error that starts with "Repay amount is too high." as that indicates that you are
* trying to liquidate a bigger portion of the borrower's collateral than permitted by
* the close factor. */
async simulateLiquidation (
/** @param borrower - the ID corresponding to the borrower to liquidate. */
borrower: string,
/** @param collateral - the collateral market address to receive a premium on. */
collateral: Address,
/** @param amount - the amount to liquidate by. */
amount: Uint256,
block?: number
): Promise<LendSimulatedLiquidation> {
block = block || await this.agent!.height
return this.query({ simulate_liquidation: { block, borrower, collateral, amount } })
}
async getTokenInfo (): Promise<TokenInfo> {
return this.agent!.getClient(Snip20, this.address!).getTokenInfo()
}
async getBalance (address: Address, key: ViewingKey): Promise<Uint128> {
return this.agent!.getClient(Snip20, this.address!).getBalance(address, key)
}
async getUnderlyingBalance (auth: LendAuth, block?: number): Promise<Uint128> {
block = block || await this.agent!.height
const method = await auth.createMethod<LendMarketPermissions>(this.address!, 'balance')
return this.query({ balance_underlying: { block, method } })
}
async getState (block?: number): Promise<LendMarketState> {
block = block || await this.agent!.height
return this.query({ state: { block } })
}
async getUnderlyingAsset (): Promise<ContractLink> {
return this.query({ underlying_asset: {} })
}
async getBorrowRate (block?: number): Promise<Decimal256> {
block = block || await this.agent!.height
return this.query({ borrow_rate: { block } })
}
async getSupplyRate (block?: number): Promise<Decimal256> {
block = block || await this.agent!.height
return this.query({ supply_rate: { block } })
}
async getExchangeRate (block?: number): Promise<Decimal256> {
block = block || await this.agent!.height
return this.query({ exchange_rate: { block } })
}
async getAccount (auth: LendAuth, block?: number): Promise<LendMarketAccount> {
block = block || await this.agent!.height
const method = await auth.createMethod<LendMarketPermissions>(this.address!, 'account_info')
return this.query({ account: { block, method } })
}
/** Will throw if the account hasn't borrowed at least once before. */
async getAccountId (auth: LendAuth): Promise<string> {
const method = await auth.createMethod<LendMarketPermissions>(this.address!, 'id')
return this.query({ id: { method } })
}
/** Max limit is 10. */
async getBorrowers (
pagination: Pagination,
block?: number
): Promise<PaginatedResponse<LendMarketBorrower>> {
block = block || await this.agent!.height
return this.query({ borrowers: { block, pagination } })
}
}
export class LendOverseer extends Client {
fees = {
enter: new Fee('40000', 'uscrt'),
exit: new Fee('50000', 'uscrt')
}
async enter (markets: Address[]) {
return await this.execute({ enter: { markets } })
}
async exit (market_address: Address) {
return await this.execute({ exit: { market_address } })
}
/** Max limit per page is `30`. */
async getMarkets (pagination: Pagination): Promise<PaginatedResponse<LendOverseerMarket>> {
return this.query({ markets: { pagination } })
}
async getMarket (address: Address): Promise<LendOverseerMarket> {
return this.query({ market: { address } })
}
async getEnteredMarkets (auth: LendAuth): Promise<LendOverseerMarket[]> {
const method = await auth.createMethod<LendOverseerPermissions>(this.address!, 'account_info')
return this.query({ entered_markets: { method } })
}
async getCurrentLiquidity (
auth: LendAuth,
block?: number
): Promise<LendAccountLiquidity> {
return this.query({
account_liquidity: {
block: block ?? await this.agent!.height,
method: await auth.createMethod<LendOverseerPermissions>(this.address!, 'account_info'),
market: null,
redeem_amount: '0',
borrow_amount: '0'
}
})
}
/** The hypothetical liquidity after a redeem operation from a market. */
async getLiquidityAfterRedeem (
auth: LendAuth,
/** The market to redeem from. Must have been entered that market. */
market: Address,
/** The amount to redeem. */
redeem_amount: Uint256,
block?: number
): Promise<LendAccountLiquidity> {
return this.query({
account_liquidity: {
block: block ?? await this.agent!.height,
method: await auth.createMethod<LendOverseerPermissions>(this.address!, 'account_info'),
market,
redeem_amount,
borrow_amount: '0'
}
})
}
async getOracleContract(): Promise<ContractLink> {
return this.query({oracle_contract: {}})
}
/** The hypothetical liquidity after a borrow operation from a market. */
async getLiquidityAfterBorrow (
auth: LendAuth,
/** The market to borrow from. Must have been entered that market. */
market: Address,
/** The amount to borrow. */
borrow_amount: Uint256,
block?: number
): Promise<LendAccountLiquidity> {
return this.query({
account_liquidity: {
block: block ?? await this.agent!.height,
method: await auth.createMethod<LendOverseerPermissions>(this.address!, 'account_info'),
market,
redeem_amount: '0',
borrow_amount
}
})
}
/** The hypothetical amount that will be seized from a liquidation. */
async getSeizeAmount (
/** The market that is being liquidated. */
borrowed: Address,
/** The slToken collateral to be seized. */
collateral: Address,
/** The liquidation amount. */
repay_amount: Uint256
): Promise<Uint256> {
return this.query({ seize_amount: { borrowed, collateral, repay_amount } })
}
async config (): Promise<LendOverseerConfig> {
return this.query({ config: {} })
}
}
export class LendOracle extends Client {}
export class MockOracle extends Client {}
export class LendInterestModel extends Client { }