


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.
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



1·Take
The caller supplies a token at slot 0.0, naming the asset whose moving average this plug computes.
2·Take
A window parameter is declared next, the count of intervals the average will span.
3·Get current timestamp
Block time is read as Unix seconds to anchor the day being recorded.
4·Get interval start
Taking the window value, this returns the start timestamp of the 1 day interval containing it.
5·Get price
Chainlink returns the token's latest price in USD, normalized to 18 decimals.
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·Set
A day_index variable is initialized to 0 to drive the loop counter.
8·Set
A running total variable is set to 0 before accumulation begins.
9·Predicate
Looping while day_index is less than the window value, the block repeats once per day in the span.
10·Use
Inside the loop, the Get Price Running Total plug is composed to accumulate stored prices.
11·Set
Total is assigned the value that composed plug returned.
12·Calculate
Adding 1 to day_index advances the loop counter.
13·Set
The incremented counter is written back to day_index.
14·End block
This closes the loop block once day_index reaches the window value.
15·Calculate
Dividing the accumulated total by the window yields the simple moving average.
16·Bubble
The computed average is surfaced as an output.
17·Bubble
The latest Chainlink price recorded this run is also surfaced as an output.
Posts · 3
More on Database · 5