Get interval start
Get interval start

Time · Reads onchain state

Returns the start timestamp of the hour, day, week, or larger interval containing a timestamp. Intervals are hour, day, week, fortnight, month, quarter, and year, aligned in UTC. Reach for it to build an interval bucket key for storage so a plug records one entry per period, or to compare the current period's start against a stored one and act only when a new period has begun.

Get Simple Moving Average

plug
time
chainlink
17actions
  1. 1·Take

    The caller names a token at slot 0.0, declaring which asset this SMA calculation reads prices for at execution time.

  2. 2·Take

    A second parameter, window, is declared at slot 1.0 so the caller supplies how many days the average spans.

  3. 3·Get current timestamp

    Block time is read as Unix seconds, giving the current timestamp the interval alignment below works from.

  4. 4·Get interval start

    Taking the current timestamp, this returns the start of the day containing it, snapping to a day boundary.

  5. 5·Get price

    Chainlink is queried for the latest price of the named token in USD, normalized to 18 decimals.

  6. 6·Set nested map value

    The queried price is written into the nested sma map in socket storage, keyed by the token and the day start, recording today's reading.

  7. 7·Set

    A variable named day_index is initialized to 0 to serve as the loop counter.

  8. 8·Set

    Setting a variable total to 0 opens an accumulator for summing prices across the window.

  9. 9·Predicate

    A loop begins that runs while day_index is less than the window count the caller supplied.

  10. 10·Use

    Inside the loop, the Get Price Running Total plug is composed to accumulate the per-day price sum.

  11. 11·Set

    The total variable is assigned the running total returned by that composed plug.

  12. 12·Calculate

    Advancing the counter, day_index is added to 1.

  13. 13·Set

    The day_index variable takes that incremented value, moving the loop forward.

  14. 14·End block

    Closing the loop block returns control to the condition check until day_index reaches the window.

  15. 15·Calculate

    After the loop, total is divided by the window count to produce the mean price across the window.

  16. 16·Bubble

    The computed average is surfaced as an output of this plug.

  17. 17·Bubble

    Today's stored price reading is also surfaced as a second output.

Take
Take
Get current timestamp
Get start of containing
Get price of in
Set nested map value of in in to
Set to
Set to
Use where
tokenis
todayis
days_backis
price_running_totalis
Set to
Calculate
Set to
End block
Calculate
Bubble
Bubble
  • Get Simple Moving Average

    plug
    time
    chainlink