Set Conditions

Set Contract Event and Webhook Conditions.

The setConditions method let's you specify and assign either webhook or contract event conditions to the circuit. When the specified conditions are met, the Lit Action code will be executed.

Each condition has a maximum retry limit of 3 for encountered errors before logging an unmatched condition and continuing the Circuit. See Error Strict Mode and Logs & Error Handling for a more in depth view of how errors are handled in the SDK.

When invoking this method, you provide an array of conditions that you want to set. These conditions are called either at the specified interval set in ConditionLogic or monitored in real-time by the specified API endpoint and emitted contract events.

SetConditions is optional. You can run the Circuit without specifying conditions.

import { CHAIN_NAME } from "lit-listener-sdk";
import { BigNumber } from "ethers";

newCircuit.setConditions(
    [
        new ContractCondition(
              "0x6968105460f67c3bf751be7c15f92f5286fd0ce5", // contract address
              [
                {
                  "anonymous": false,
                  "inputs": [
                    {
                      "indexed": true,
                      "internalType": "address",
                      "name": "to",
                      "type": "address"
                    },
                    {
                      "indexed": false,
                      "internalType": "uint256",
                      "name": "value",
                      "type": "uint256"
                    }
                  ],
                  "name": "Transfer",
                  "type": "event"
                },
              ], // abi
              CHAIN_NAME.polygon, // chainId
              "https://your_provider_url_for_this_network", // provider URL
              "Transfer", // event name
               ["to", "value"], // event name args
               ["0x6968105460f67c3bf751be7c15f92f5286fd0ce5", 
               BigNumber.from("500000")], // expected value
              "===", // match operator
              async (emittedValue) => { console.log("Value Emmited by the contract event",         emittedValue); }, // onMatched function
              async (emittedValue) => { console.log("Value Emmited by the contract event",         emittedValue); }, // onUnMatched function
              (error: Error) => { console.log("Error:", error); } // onError function,
        ), 
        new WebhookCondition(
          "https://api.example.com", // baseUrl
          "/endpoint", // endpoint
          "path.to.value", // responsePath
          20, // expected value
          "===", // match operator
          "my-api-key", // apiKey
              async (emittedValue) => { console.log("Value Emmited by the webhook event",         emittedValue); }, // onMatched function
              async (emittedValue) => { console.log("Value Emmited by the webhook event",         emittedValue); }, // onUnMatched function
          (error: Error) => { console.log("Error:", error); } // onError function,
        )  
    ]
)

Webhook Condition Parameters:

/* The base URL of the webhook endpoint.*/
baseUrl: string;

/* The specific endpoint for the webhook.*/
endpoint: string;

/* The path to access the expected value in the response body.*/
responsePath: string;

/* The value to match against the emitted value.*/
expectedValue:  number | string | number[] | string[] | bigint | bigint[] | object | object[] | (string | number | bigint | object)[];

/* The operator used for the comparison. It must be one of the following: "<", ">", "==", "===", "!==", "!=", ">=", "<=".*/
matchOperator: "<" | ">" | "==" | "===" | "!==" | "!=" | ">=" | "<=";

/* Optional API key for authorization.*/
apiKey?: string;

/* A callback function to execute when the emitted value matches 
the expected value.*/
onMatched: (emittedValue:  number | string | number[] | string[] | bigint | bigint[] | object | object[] | (string | number | bigint | object)[]) => Promise<void>;

/* A callback function to execute when the emitted value does not 
match the expected value.*/
onUnMatched: (emittedValue:  number | string | number[] | string[] | bigint | bigint[] | object | object[] | (string | number | bigint | object)[]) => Promise<void>;

/* A callback function to execute when an error occurs during monitoring.*/
onError: (error: Error) => void;

Contract Event Condition Parameters:

import { CHAIN_NAME } from "lit-listener-sdk";
import { InterfaceAbi } from "ethers";

/* The address of the contract to monitor.*/
contractAddress: `0x${string}`;

/* The ABI (Application Binary Interface) of the contract.*/
abi: InterfaceAbi;

/* The Lit supported blockchain network chainId. Import 
    CHAIN_NAME from lit-listener-sdk.*/
chainId: string;

/* The provider URL that is used to create the ethers contract object and monitor the on-chain event. Make sure that your provider URL is compatible with the same network indicated in chain_id for this contract condition.*/
chainId: string;

/* The address of the contract to monitor.*/
eventName: string;

/* The name of the event arg/s that the expectedValue will be matched against.*/
eventArgName: string[]

/* The value that will be matched against the emitted value. This will be compared against the arguments specified in the eventArgName */
expectedValue: number[] | string[] | bigint[] | object[] | (string | number | bigint | object)[];

/* The operator used for the comparison. It must be one of the following: "<", ">", "==", "===", "!==", "!=", ">=", "<=".*/
matchOperator: "<" | ">" | "==" | "===" | "!==" | "!=" | ">=" | "<=";

/* A callback function to execute when the emitted value matches 
the expected value.*/
onMatched: (emittedValue: number[] | string[] | bigint[] | object[] | (string | number | bigint | object)[]) => Promise<void>;

/* A callback function to execute when the emitted value does not 
match the expected value.*/
onUnMatched: (emittedValue: number[] | string[] | bigint[] | object[] | (string | number | bigint | object)[]) => Promise<void>;

/* A callback function to execute when an error occurs during monitoring.*/
onError: (error: Error) => void;

Last updated