Switchboard Introduces TWAP Support

What is TWAP?

A time weighted average price (TWAP) is the average price of an asset over a specified time frame. Let’s say you’re a large trader and you want to enter a large SOL/USD position. If you execute a sudden market order, you could drain the order book and end up paying substantially more for the trade, plus bots will immediately start to arbitrage and your position will be underwater immediately. If you execute the same position using a TWAP, your orders will be spread throughout the given timeframe, allowing the market to absorb your liquidity, and giving you a more optimal price of the true market value.

  • Futures Expiry: Futures expiration are correlated with higher volatility. Using a TWAP reduces the risk of manipulation or banging the close.
  • Lending Liquidations: A TWAP reduces scam wicks causing erroneous lending position liquidations.
  • On-Chain Trading Algos: An on-chain program could use a Switchboard TWAP oracle to execute positions on Serum based on a given criteria.

How to Use TwapTask?

To start using the new TwapTask with Switchboard V2, you will need to initialize a history buffer account for your aggregator. You can initialize a history buffer using the javascript API method setHistoryBuffer. A history buffer account stores a set number of accepted results for an aggregator, and given Solana’s maximum account size of 10MB, the maximum number of samples a single history buffer can support is 350,000 samples (Keep in mind rent cost, this action is currently irreversible). The screenshot below shows the SOL/USD feed’s price history in action on our explorer page.

  • period: The time period, in seconds, to include in the TWAP calculation
  • weightByPropagationTime (Optional): Return the average, weighing each sample by the sample’s propagation time (currentTimestamp — previousTimestamp).
  • minSamples (Optional): The minimum number of samples that must be present in the history buffer, after filtering by time period, before accepting a result

Known Limitations

The TwapTask relies on accepted on-chain results being pushed to the history buffer. During periods of network degradations, aggregators may fail to post accepted results due to an insufficient number of oracle responses. This may cause TWAP calculations to have insufficient data to make a proper calculation. To mitigate this, you should specify a minSamples field in the job definition to only accept a TWAP result if enough samples are provided.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store