Skip to content

Enhancements to Conditions API #258

@ghardin1314

Description

@ghardin1314

Currently working with conditions is very difficult. There is no way to get type inference on what the schema of the condition should be based on the condition type (evm, timelock, etc). Here are my suggestions to improve this.

  1. Add conditionType discriminating field to the condition schema itself

Right now it is very hard to tell from a JSON condition, what type it is. Adding a new top level field to the Condition schema itself would make this much easier. Not sure exactly who owns the condition schema definition right now, as if I pass extra fields like this to Porter I get an error. Example would be

{
  "conditionType": "evm" | "timelock" | ...,
  "method": "...",
  "parameters": [ ],
  ...
}

This also helps immensely on the ts side as you can use a discriminated union to parse from JSON

  1. Add types/interfaces to the constructor of each type of Condition

Right now the constructors of all types of conditions share the same interface as the base condition which is value: Record<string, unknown>. This provides very little feedback to the developer about what they are actually supposed to pass here and leads to a lot of trial/error and frustration.

To fix this, the actual type of the schema should be inferred in the constructor. For example the type for RpcCondition should look something like:

interface RpcConditionProps {
  method: 'eth_getBalance' | 'balanceOf'
  parameters: [':userAddress']
  chain: number
  returnValueTest: ReturnValueTestProps
}

class RpcCondition extends Condition {
  constructor(value: RpcConditionProps) {
    super(value)
  }
}

These types can be written manually to match the Joi schema, or generated using something like (Joi to Typescript)[https://github.com/mrjono1/joi-to-typescript]

My real recommendation is to switch from Joi to Zod so the types can be inferred directly from the schema itself. You can see an example here

I think these 2 changes will massively improve DX for working with conditions. More than happy to submit a PR for refactoring, but @piotr-roslaniec mentioned there may be big changes to the condition schema so would like to wait until that is finalized

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Completed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions