Updated 60 days ago

Pattern Matching and Time Analysis

Implement solidity libraries to enable statistical and time series analysis of on-chain data, simplifying the creation of Drosera Trap and unveiling new use cases.

  • Crypto / Web3
  • Defi
  • Infra / API

Introduction

For long, smart contracts had computation complexity as a huge limiter of what could be built. Drosera opens up this door by collecting on-chain data and running computations on shadow forks - enabling a whole new layer of on-chain security.

In this build, I provide a couple of tools which can be used by Traps to extract meaningful analysis, further solidifying the validation of the contract's state before taking emergency actions.

Math Library

Basic functions needed to implement more complex algorithms, involving computations such as mean, mode, median, standard deviation, and other important metrics.

Code

Pattern Matching Library

The Pattern Matching library focuses on detecting specific patterns within time series data, essential for on-chain analysis and emergency response mechanisms.

  • Exact Pattern Detection: Identifies an exact sequence within the data, useful for recognizing predefined patterns.
  • Threshold Pattern Detection: Allows for a margin of error, detecting patterns that are similar but not identical to the target sequence.
  • Moving Average Crossover Detection: Analyzes short-term and long-term moving averages to identify potential buy/sell signals.
  • Volume Spike Detection: Identifies significant increases in trading volume, indicating potential market interest or large trades.
  • Trend Reversal Detection: Detects simplified double top patterns, signaling potential trend reversals.
  • Anomaly Detection: Finds data points that deviate significantly from the mean, useful for identifying outliers.
  • Hidden Markov Model (HMM) Detection: Uses state transitions to model and predict future states in the data.
  • K-Nearest Neighbors (KNN) Detection: Classifies data points based on their proximity to known patterns in the training set.
  • Dynamic Time Warping (DTW): Measures similarity between two time series, useful for pattern recognition.
  • Support Vector Machines (SVM): Classifies data points by finding the optimal hyperplane separating different classes.
  • Exponential Moving Average (EMA): Smooths data by applying more weight to recent observations, commonly used in financial analysis.
  • Bollinger Bands Calculation: Identifies overbought or oversold conditions using moving averages and standard deviations.
  • Head and Shoulders Pattern Detection: Identifies potential trend reversals from bullish to bearish.
  • Cup and Handle Pattern Detection: Signals potential continuation of an upward trend.

Code

Time Series Analysis Library

The Time Series Analysis library provides advanced analytical tools to perform in-depth analyses of on-chain data, uncovering insights that drive innovation in decentralized applications.

  • Autoregressive Model (AR): Predicts future values based on past values in the time series, useful for modeling and forecasting.
  • Lagged Values and Differences: Creates lag features and transforms non-stationary time series into stationary ones.
  • Autocorrelation and Partial Autocorrelation: Measures the linear relationship between lagged values, identifying repeating patterns or cycles.
  • Augmented Dickey-Fuller (ADF) Test: Tests whether a time series is stationary, a key assumption in many models.
  • Seasonal Decomposition of Time Series (STL): Decomposes a time series into trend, seasonal, and residual components, useful for understanding underlying patterns.
  • Discrete Fast Fourier Transform (FFT): Transforms a time series into its frequency components, identifying periodic patterns.

Code

Conclusion

By integrating these libraries, developers can better analyze and interpret on-chain data, simplifying the creation of Drosera Traps and exploring new use cases.