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 theCreditAccountlist 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
blockNumbereach 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
CreditAccountwe opened, get theCreditFilterfromCreditManager(Note here that CreditFilter and CreditManager are one-to-one correspondence.). Then we can usecalcCreditAccountHealthFactorfunction fromCreditFilterto get theHealthFactordirectly. (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 theHealthFactoris also there. Why don't we querygetCreditAccountListeach time to get theHealthFactorof all ourCreditAccount? The answer is that this query is inefficient, it will query many other data at the same. Further, we could actually save theCreditFilterlist first, so that we don't need to query theCreditfilterevery time.