Monitor Your Health Factor
In this section, we're gonna make a simple monitor to check your HealthFactor
.
With our powerful
DataCompressor
, we can get theCreditAccount
list of our wallet. There are many data in the return, you could try to output it.jsxconst dataCompressorAddress = await addressProvider.getDataCompressor();const dataCompressor = DataCompressor__factory.connect(dataCompressorAddress, provider);const myCreditAccountList = await dataCompressor.getCreditAccountList(MY_WALLET);jsxconst dataCompressorAddress = await addressProvider.getDataCompressor();const dataCompressor = DataCompressor__factory.connect(dataCompressorAddress, provider);const myCreditAccountList = await dataCompressor.getCreditAccountList(MY_WALLET);Since we want to build a monitor, the easy way is writting a while loop. And check the
blockNumber
each time, when a new block is finalized, we start doing our work.jsxlet lastBlockNum: number = 0;while (true) {let currentBlockNum = await provider.getBlockNumber();if (currentBlockNum > lastBlockNum) {lastBlockNum = currentBlockNum;...}await sleep(5000);}jsxlet lastBlockNum: number = 0;while (true) {let currentBlockNum = await provider.getBlockNumber();if (currentBlockNum > lastBlockNum) {lastBlockNum = currentBlockNum;...}await sleep(5000);}For each
CreditAccount
we opened, get theCreditFilter
fromCreditManager
(Note here that CreditFilter and CreditManager are one-to-one correspondence.). Then we can usecalcCreditAccountHealthFactor
function fromCreditFilter
to get theHealthFactor
directly. (You can add some alert here.)jsxif (currentBlockNum > lastBlockNum) {lastBlockNum = currentBlockNum;for (let i = 0; i < myCreditAccountList.length; ++i) {const creditManager = CreditManager__factory.connect(myCreditAccountList[i].creditManager, provider);const creditFilterAddress = await creditManager.creditFilter();const creditFilter = CreditFilter__factory.connect(creditFilterAddress, provider);const healhFactor = await creditFilter.calcCreditAccountHealthFactor(myCreditAccountList[i].addr);console.log(`BlockNumber ${currentBlockNum}, CreditAccount ${myCreditAccountList[i].addr}'s healh factor is ${healhFactor}`);}}jsxif (currentBlockNum > lastBlockNum) {lastBlockNum = currentBlockNum;for (let i = 0; i < myCreditAccountList.length; ++i) {const creditManager = CreditManager__factory.connect(myCreditAccountList[i].creditManager, provider);const creditFilterAddress = await creditManager.creditFilter();const creditFilter = CreditFilter__factory.connect(creditFilterAddress, provider);const healhFactor = await creditFilter.calcCreditAccountHealthFactor(myCreditAccountList[i].addr);console.log(`BlockNumber ${currentBlockNum}, CreditAccount ${myCreditAccountList[i].addr}'s healh factor is ${healhFactor}`);}}NOTE: In step 1, if you output the return data from
DataCompressor
, you will see theHealthFactor
is also there. Why don't we querygetCreditAccountList
each time to get theHealthFactor
of all ourCreditAccount
? The answer is that this query is inefficient, it will query many other data at the same. Further, we could actually save theCreditFilter
list first, so that we don't need to query theCreditfilter
every time.