-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7a00ed
commit 5fbc198
Showing
25 changed files
with
693 additions
and
713 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import Web3 from 'web3'; | ||
|
||
import { Big } from '@goldenagellc/web3-blocks'; | ||
|
||
import { CTokenSymbol, cTokenSymbols } from './types/CTokens'; | ||
import { CToken } from './contracts/CToken'; | ||
import PriceLedger from './PriceLedger'; | ||
import StatefulComptroller from './StatefulComptroller'; | ||
|
||
export interface IBorrowerPosition { | ||
supply: Big; | ||
borrow: Big; | ||
borrowIndex: Big; | ||
} | ||
|
||
interface ILiquidity { | ||
liquidity: Big; | ||
shortfall: Big; | ||
symbols: CTokenSymbol[]; | ||
edges: ('min' | 'max')[]; | ||
} | ||
|
||
export default class Borrower { | ||
public readonly address: string; | ||
protected readonly positions: { readonly [_ in CTokenSymbol]: IBorrowerPosition }; | ||
|
||
constructor(address: string) { | ||
this.address = address; | ||
this.positions = Object.fromEntries( | ||
cTokenSymbols.map((symbol) => [symbol, { supply: new Big('0'), borrow: Big('0'), borrowIndex: Big('0') }]), | ||
) as { [_ in CTokenSymbol]: IBorrowerPosition }; | ||
} | ||
|
||
public async verify( | ||
provider: Web3, | ||
cTokens: { [_ in CTokenSymbol]: CToken }, | ||
borrowIndices: { [_ in CTokenSymbol]: Big }, | ||
threshold: number, | ||
): Promise<boolean> { | ||
for (let symbol of cTokenSymbols) { | ||
const snapshot = await cTokens[symbol].getAccountSnapshot(this.address)(provider); | ||
if (snapshot.error !== '0') { | ||
console.error(`Failed to get account snapshot for ${this.address}: ${snapshot.error}`); | ||
return false; | ||
} | ||
|
||
const position = this.positions[symbol]; | ||
if (position.borrowIndex.eq('0')) { | ||
console.error(`${this.address} invalud due to 0 borrow index`); | ||
return false; | ||
} | ||
const supply = position.supply; | ||
const borrow = position.borrow.times(borrowIndices[symbol]).div(position.borrowIndex); | ||
|
||
if (supply.eq('0')) { | ||
if (!snapshot.cTokenBalance.eq('0')) { | ||
console.error(`${this.address} invalid due to 0 supply mismatch`); | ||
return false; | ||
} | ||
} else { | ||
const supplyError = supply.minus(snapshot.cTokenBalance).div(snapshot.cTokenBalance).abs(); | ||
if (supplyError.toNumber() > threshold) { | ||
console.error(`${this.address} invalid due to high supply error (${supplyError.toFixed(5)})`); | ||
return false; | ||
} | ||
} | ||
|
||
if (borrow.eq('0')) { | ||
if (!snapshot.borrowBalance.eq('0')) { | ||
console.error(`${this.address} invalid due to 0 borrow mismatch`); | ||
return false; | ||
} | ||
} else { | ||
const borrowError = borrow.minus(snapshot.borrowBalance).div(snapshot.borrowBalance).abs(); | ||
if (borrowError.toNumber() > threshold) { | ||
console.error(`${this.address} invalid due to high borrow error (${borrowError.toFixed(5)})`); | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public liquidity( | ||
comptroller: StatefulComptroller, | ||
priceLedger: PriceLedger, | ||
exchangeRates: { [_ in CTokenSymbol]: Big }, | ||
borrowIndices: { [_ in CTokenSymbol]: Big }, | ||
): ILiquidity | null { | ||
let collat: Big = new Big('0'); | ||
let borrow: Big = new Big('0'); | ||
const symbols: CTokenSymbol[] = []; | ||
const edges: ('min' | 'max')[] = []; | ||
|
||
for (let symbol of cTokenSymbols) { | ||
const position = this.positions[symbol]; | ||
if (position.supply.eq('0') || position.borrow.eq('0') || position.borrowIndex.eq('0')) continue; | ||
|
||
const collateralFactor = comptroller.getCollateralFactor(symbol); | ||
const pricesUSD = priceLedger.getPrices(symbol); | ||
if (collateralFactor === null || pricesUSD.min === null || pricesUSD.max === null) return null; | ||
|
||
const edge: 'min' | 'max' = position.supply.gt('0') ? 'min' : 'max'; | ||
collat = collat.plus( | ||
position.supply | ||
.times(exchangeRates[symbol]) | ||
.div('1e+18') | ||
.times(collateralFactor) | ||
.div('1e+18') | ||
.times(pricesUSD[edge]!), | ||
); | ||
borrow = borrow.plus( | ||
position.borrow.times(borrowIndices[symbol]).div(position.borrowIndex).times(pricesUSD[edge]!), | ||
); | ||
symbols.push(symbol); | ||
edges.push(edge); | ||
} | ||
|
||
let liquidity: Big; | ||
let shortfall: Big; | ||
if (collat.gt(borrow)) { | ||
liquidity = collat.minus(borrow); | ||
shortfall = new Big('0'); | ||
} else { | ||
liquidity = new Big('0'); | ||
shortfall = borrow.minus(collat); | ||
} | ||
|
||
return { | ||
liquidity: liquidity, | ||
shortfall: shortfall, | ||
symbols: symbols, | ||
edges: edges, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import nfetch from 'node-fetch'; | ||
|
||
type CompoundAPIRequestValue = { value: string }; | ||
|
||
interface ICompoundAPIRequest { | ||
addresses?: string[]; | ||
block_number?: number; | ||
max_health?: CompoundAPIRequestValue; | ||
min_borrow_value_in_eth: CompoundAPIRequestValue; | ||
page_number?: number; | ||
page_size?: number; | ||
network?: string; | ||
} | ||
|
||
const fetch = async (r: ICompoundAPIRequest) => { | ||
const method = 'GET'; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}; | ||
|
||
const path = 'https://api.compound.finance/api/v2/account?'; | ||
const params = Object.keys(r) | ||
.map((key) => { | ||
const knownKey = key as keyof ICompoundAPIRequest; | ||
let uri; | ||
switch (knownKey) { | ||
case 'max_health': | ||
case 'min_borrow_value_in_eth': | ||
uri = `${knownKey}[value]=${r[knownKey]!.value}`; | ||
return encodeURIComponent(uri).replace('%3D', '='); | ||
case 'addresses': | ||
uri = `${knownKey}=${r[knownKey]!.join(',')}`; | ||
return encodeURIComponent(uri); | ||
default: | ||
return `${knownKey}=${r[knownKey]}`; | ||
} | ||
}) | ||
.join('&'); | ||
|
||
const res = await nfetch(path + params, { | ||
method: method, | ||
headers: headers, | ||
}); | ||
return res.json(); | ||
}; | ||
|
||
async function sleep(millis: number) { | ||
return new Promise((resolve) => setTimeout(resolve, millis)); | ||
} | ||
|
||
const getBorrowers = async (minBorrow_Eth: string) => { | ||
let borrowers = <string[]>[]; | ||
|
||
let i = 1; | ||
let pageCount = 0; | ||
|
||
let result; | ||
do { | ||
result = await fetch({ | ||
min_borrow_value_in_eth: { value: minBorrow_Eth }, | ||
page_size: 100, | ||
page_number: i, | ||
}); | ||
if (result.error) { | ||
console.warn(result.error.toString()); | ||
continue; | ||
} | ||
borrowers = borrowers.concat(result.accounts.map((account: any) => account.address)); | ||
pageCount = result.pagination_summary.total_pages; | ||
i++; | ||
|
||
await sleep(100); // Avoid rate limiting | ||
} while (i <= pageCount); | ||
|
||
return borrowers; | ||
}; | ||
|
||
export default getBorrowers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.