Skip to content

Instantly share code, notes, and snippets.

@ajb413

ajb413/server.js Secret

Created December 8, 2022 20:58
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/446f584e2eb53d512c8d7296a39cd385 to your computer and use it in GitHub Desktop.
Save ajb413/446f584e2eb53d512c8d7296a39cd385 to your computer and use it in GitHub Desktop.
API Examples
async function syncDbWithBlockchains() {
const instances = Object.keys(cometInstanceData);
for (let i = 0; i < instances.length; i++) {
const instance = instances[i];
if (!db[instance]) {
db[instance] = {
block: 0,
ts: 0,
borrowers: [],
assets: {},
numCollaterals: 0,
};
}
const timestamp = Date.now();
const { debounceMs } = cometInstanceData[instance];
// Polled too recently, debounce to limit frivolous JSON RPC calls
if (db[instance].ts + debounceMs > timestamp) {
return;
}
db[instance].ts = timestamp;
const provider = new ethers.providers.JsonRpcProvider(cometInstanceData[instance].rpc);
const comet = new ethers.Contract(cometInstanceData[instance].proxy, cometAbi, provider);
const numCollaterals = await comet.callStatic.numAssets();
// Update the in-memory DB first then save it to disk
if (numCollaterals !== db[instance].numCollaterals) {
db[instance].assets = await pullAssetDataFromChain(instance, db[instance].assets);
}
db[instance].numCollaterals = numCollaterals;
await pullPriceDataFromChain(instance, db[instance].assets);
await pullBorrowerDataFromChain(instance, db[instance].block);
calculateAccountHealths(instance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment