Get interval start

Get interval start on Time

Ethereum
Ethereum
Base
Base

Reads onchain state

Ready to run on Base

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.

Plug lets you combine onchain actions like this one into a single transaction, without writing a smart contract. Use in App opens a new plug with this action already in it.

Get Simple Moving Average

plug
time
chainlink
17actions
  1. 1·Take

    The caller supplies a token at slot 0.0, naming the asset whose moving average this plug computes.

  2. 2·Take

    A window parameter is declared next, the count of intervals the average will span.

  3. 3·Get current timestamp

    Block time is read as Unix seconds to anchor the day being recorded.

  4. 4·Get interval start

    Taking the window value, this returns the start timestamp of the 1 day interval containing it.

  5. 5·Get price

    Chainlink returns the token's latest price in USD, normalized to 18 decimals.

  6. 6·Set nested map value

    That price is written into the sma nested map in socket storage, keyed by the token and the day start.

  7. 7·Set

    A day_index variable is initialized to 0 to drive the loop counter.

  8. 8·Set

    A running total variable is set to 0 before accumulation begins.

  9. 9·Predicate

    Looping while day_index is less than the window value, the block repeats once per day in the span.

  10. 10·Use

    Inside the loop, the Get Price Running Total plug is composed to accumulate stored prices.

  11. 11·Set

    Total is assigned the value that composed plug returned.

  12. 12·Calculate

    Adding 1 to day_index advances the loop counter.

  13. 13·Set

    The incremented counter is written back to day_index.

  14. 14·End block

    This closes the loop block once day_index reaches the window value.

  15. 15·Calculate

    Dividing the accumulated total by the window yields the simple moving average.

  16. 16·Bubble

    The computed average is surfaced as an output.

  17. 17·Bubble

    The latest Chainlink price recorded this run is also surfaced as an 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

More plugs on Base

Dollar-cost average on Base · 4 plugs

Posts · 3

More on Time · 7