PIP: Optimising Gas Cost for Settlement of Markets

Title: Optimising Gas cost for Settlement of markets

Simple Summary
Optimising the smart contract to settle markets more efficiently

This proposal describes gas optimization in settlement of markets. Instead of computing the settlement price on chain, it is proposed to calculate the settlement price off chain and verify the correctness of price on chain.

Currently, PlotX Smart contracts are using Chainlink to settle the markets at the settlement time. Chainlink node operators post asset price data on Ethereum by aggregating data from 7+ sources either at a deviation of 0.5% of price or at a regular time heartbeat. Every feed posted via Chainlink operator, results in a new roundId being posted on-chain. Sample feed address for the ETH/USD pair can be seen here. In order to avoid incorrect settlement data, the smart contract backtracks the price feed of Chainlink to go back to the correct feed posted at the settlement time. Eg, If the market settlement time is 6:00 pm UTC, and the settlement transaction is mined at 7:00 pm UTC, the PlotX smart contracts loops through all roundids(starting from the latest one) to fetch price from the roundId posted just before 6:00 pm UTC.

This approach however has the following problems:

  1. Increased gas cost if currency is volatile and Chainlink operators post several rounds in between
  2. Increased gas costs if settlement transaction is mined at a later time due to increased gas prices
  3. Out of gas situation for a market which has not been settled for a long time

Considering recent gas price surge + ETH volatility and missed settlements of markets eg PlotX it is important to optimise the strategy.

An equivalent to the above result can be reached, if the roundId for settlement is calculated off-chain and the resulting roundId is cross verified on smart contract. Eg, if roundId 100070 is the roundId computed off-chain, we verify on-chain that the time stamp for 100070 is just below the settlement time and the one for roundId 100071 is after. This way, we save the gas of computation and pay for verification only.

This approach helps us keep the gas cost of settlement constant and eliminate the scope of unsettled markets.

Copyright and related rights waived via CC0.