DEX Exchange Rates

Monitor exchange rates - this monitor checks the price in Uniswap v3 pool and the exchange rate between 2 tokens. If it reaches a specific threshold, it alerts.

use Call, Aggregate, Max, Min, Abs, Len, Range, Approximately, FractionExponent from hexagate;


source poolContract: address = 0x87428a53e14d24ab19c6ca4939b4df93b8996ca9;
source changePercentage: integer = 1;
source numBlocks: integer = 2;

/* Call slot0 on the pool address in an aggregate over 10 blocks */
source lastSlots: list<tuple<integer, integer, integer, integer, integer, integer, boolean>> = Aggregate {
  source: Call {
    contract: poolContract,
    signature: "function slot0() returns (uint160 sqrtPriceX96,int24 tick,uint16 observationIndex,uint16 observationCardinality,uint16 observationCardinalityNext,uint8 feeProtocol,bool unlocked);"
  },
  blocks: numBlocks
};

/* Calculating a price in Uniswap is as follows price = (sqrtPriceX96 / 2**96) ** 2 */
source historicalRates: list<integer> = [
  FractionExponent {
    numerator: slot[0],
    denominator: 79228162514264337593543950336,
    exponent: 2,
    precision: 1000
  } for slot in lastSlots
];

source first: integer = historicalRates[0];
source last: integer = historicalRates[numBlocks - 1];

source approx: boolean = Approximately {
  source: first,
  target: last,
  percent: 1
};

source precision: integer = 100;

/* Check if the price decreased or increased by changePercentage amount  */
rule {
    description: "UniswapV3 pool exchange rate increase or decreases beyond threshold",
    condition: Len { sequence: historicalRates } == numBlocks ? approx : true
};

these can change to a parameter:

Last updated