-
-
Save ajb413/17ed80160e911768c1336a65f58ef3e5 to your computer and use it in GitHub Desktop.
API Examples
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
function calculateAccountHealths(instance) { | |
const borrowers = Object.keys(db[instance].borrowers); | |
borrowers.forEach((account, i) => { | |
const borrower = db[instance].borrowers[account]; | |
const collaterals = Object.keys(borrower.collaterals); | |
const borrowBalance = borrower.borrowBalance; | |
const basePrice = db[instance].assets[Object.keys(db[instance].assets)[0]].price; | |
const borrowValue = borrowBalance * basePrice; | |
let borrowLimit = 0; | |
let liquidationLimit = 0; | |
collaterals.forEach((asset, i) => { | |
const { price, cf, lcf } = db[instance].assets[asset]; | |
const amount = borrower.collaterals[asset]; | |
borrowLimit += amount * cf * price; | |
liquidationLimit += amount * lcf * price; | |
}); | |
db[instance].borrowers[account].borrowLimit = borrowLimit / basePrice; | |
db[instance].borrowers[account].liquidationLimit = liquidationLimit / basePrice; | |
db[instance].borrowers[account].percentToLiquidation = +((borrowValue / liquidationLimit) * 100).toFixed(); | |
if (collaterals.length === 1) { | |
const [ asset, collateralAmount ] = Object.entries(borrower.collaterals)[0]; | |
const collateralPrice = db[instance].assets[asset].price; | |
const lcf = db[instance].assets[asset].lcf; | |
db[instance].borrowers[account].liquidationPrice = borrowValue / collateralAmount / lcf; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment