Skip to content

Instantly share code, notes, and snippets.

@ajb413

ajb413/server.js Secret

Created December 8, 2022 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajb413/17ed80160e911768c1336a65f58ef3e5 to your computer and use it in GitHub Desktop.
Save ajb413/17ed80160e911768c1336a65f58ef3e5 to your computer and use it in GitHub Desktop.
API Examples
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