Set nested map value
Set nested map value

Database · Acts onchain

Writes a value into a two-level keyed map in the socket's persistent storage. The outer and inner keys are both bytes32, so any values can serve, such as user then token. Scope chooses socket storage, shared across every plug the socket runs, or plug storage, private to this plug bundle. Writing zero deletes the entry. Reach for it to record two-dimensional state across executions, like how much of each token has been sent to each recipient, so later runs read it back and enforce limits.

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