Assertions

Assertions check the correctness of the simulation flow.

Example Assertion Code:

try {
    let currentBlock = await provider.getBlockNumber();
    let previousBlock = (currentBlock - 1).toString(16);

    const WBTC_FEED = "0xF4030086522a5bEEa4988F8cA5B36dbC97BeE88c";
    let wbtcFeed = new ethers.Contract(WBTC_FEED, oracleAbi, provider);
    let [latestRound, pastRound] = await Promise.all([
        wbtcFeed.latestRoundData(),
        wbtcFeed.latestRoundData({ blockTag: ("0x" + previousBlock) })
    ]);
    let currentPrice = latestRound[1] / 1e8;
    let pastPrice = pastRound[1] / 1e8;
    if (currentPrice <= pastPrice) {
        return true;
    } else if (currentPrice > pastPrice) {
        return false;
    } else {
        return "NC";
    }
} catch (err) {
    console.log("error in priceDrop Assertion", err);
    return err;
}

In this example:-

We compare the price of WBTC from the last block to the current block.

  • If the price has stayed the same or decreased, we return true.

  • If the price has increased, we return false.

  • If there is a failure, we return "NC".

Last updated