o1js icon indicating copy to clipboard operation
o1js copied to clipboard

automatically initialize reducer

Open harrysolovay opened this issue 2 months ago • 7 comments

Multiple reducers are seemingly not supported. Yet, the way in which one initializes their reducer suggests multiple reducers are supported. Ie.

export class MyContract extends SmartContract {
  reducerA = Reducer({ actionType: ActionA })
  reducerB = Reducer({ actionType: ActionB })
}

Thoughts on automatically initializing a reducer based on the presence of an action field?

export class MyContract extends SmartContract<Action> {
  action = Action
}

Note: the type param on SmartContract would be necessary to get action-specific typing.

Then, devs could utilize the reducer as they currently do (albeit potentially from a different name "reducer").

@method
rollup() {
  const state = this.reducer.getActions({ fromActionState })
  //            ~~~~~~~~~~~~
  //            ^
  //            Would be consistent across all contracts (less cognitive load)
}

On one hand, this means devs can't use the name "reducer" for other members. On the other hand, the name is consistent and therefore easy for developers to recognize.

This API change would also make actions look more similar to events. Their APIs could even converge (perhaps part of the path to polymorphic actions / #1558). Does one even need to differentiate between actions and events? When emitting, the developer could specify whether they want it to be an action / whether that state should be accessible within the proof context. But I digress.

harrysolovay avatar Apr 12 '24 12:04 harrysolovay