o1-labs / o1js

TypeScript framework for zk-SNARKs and zkApps
https://docs.minaprotocol.com/en/zkapps/how-to-write-a-zkapp
Apache License 2.0
512 stars 116 forks source link

Conditional zkApp method calls #1889

Open mitschabaude opened 1 hour ago

mitschabaude commented 1 hour ago

Currently, it's possible to add account updates to the tx conditionally with AccountUpdate.createIf(), but it's not possible to call another method conditionally.

Since a method call just means adding the account update that another method produces to the AU tree, it should be fairly easy to support conditional method calls. There's simply an additional boolean condition that needs to be considered before pushing the AU to the tree.

The API probably can't be as clean as the current zkApp call API, but I think that's ok, something like this should work well enough:

this.callIf(bool, otherContract, "methodName", [...arguments]);

note, the method name + arguments can be made type-safe if this is polymorphic over the contract, something like:

callIf<Contract extends SmartContract & { [k in MethodName]: Function }, MethodName extends string>(condition: Bool, contract: Contract, methodName: MethodName, arguments: Parameters<Contract[MethodName]>): void;
mitschabaude commented 1 hour ago

@Trivo25 @nholland94 something to consider from the get-go in the new API

mitschabaude commented 1 hour ago

a clear-cut use case for conditional method calls is spawning method calls from a reducer, where some of the input actions (necessarily) are dummy actions

see also discord discussion https://discord.com/channels/484437221055922177/1298526066394140672/1300855340522537084