-
-
Save ajb413/446f584e2eb53d512c8d7296a39cd385 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
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