LitOracleKit provides functionality to create and manage oracles using Lit Protocol. It allows fetching off-chain data and writing it to blockchain smart contracts using Programmable Key Pairs (PKPs).

Constructors

  • Creates a new instance of LitOracleKit

    Parameters

    • litNetwork: LIT_NETWORKS_KEYS = LIT_NETWORK.DatilDev

      The Lit Network to connect to (e.g., "datil-dev")

    • privateKey: string

      Private key used for signing transactions and minting PKPs

    Returns LitOracleKit

Methods

  • Checks the balance of a PKP and funds it if it's low

    Parameters

    • pkpEthAddress: string

      Ethereum address of the PKP

    • fundIfLow: boolean = false

      Whether to fund the PKP if it's low

    Returns Promise<string>

    Balance of the PKP in ETH

  • Connects to the Lit Network

    Returns Promise<void>

    Promise that resolves when connection is established

  • Disconnects from the Lit Network

    Returns Promise<void>

    Promise that resolves when disconnection is complete

  • Funds a PKP with 0.001 ETH if the balance is less than 0.00001 ETH

    Parameters

    • pkpEthAddress: string

      Ethereum address of the PKP

    Returns Promise<string>

    Transaction hash of the funding transaction

  • Generates Lit Action code and returns it along with its IPFS CID

    Parameters

    • params: FetchToChainParams

      Parameters for generating the Lit Action code

    Returns Promise<{
        ipfsCid: string;
        litActionCode: string;
    }>

    Object containing the generated code and its IPFS CID

  • Mints a new PKP and binds it to the given Lit Action IPFS CID

    Parameters

    • ipfsCid: string

      IPFS CID of the Lit Action to bind to the PKP

    Returns Promise<MintedPkpInfo>

    Information about the minted PKP

  • Reads data from a smart contract on the blockchain

    Type Parameters

    • T

    Parameters

    • params: ReadFromChainParams

      Parameters specifying the function to call and the contract to read from

    Returns Promise<T>

    Response from the read operation

    const weatherData = await sdk.readFromChain<WeatherData>({
    functionAbi: "function currentWeather() view returns (int256 temperature, uint8 precipitationProbability, uint256 lastUpdated)",
    contractAddress: "0xE2c2A8A1f52f8B19A46C97A6468628db80d31673",
    chain: "yellowstone"
    });
  • Checks if the client is connected to the Lit Network

    Returns boolean

    true if connected, false otherwise

  • Tests a data source function without writing to chain

    Parameters

    • dataSource: string

      JavaScript code that fetches and returns data

    Returns Promise<ExecuteJsResponse>

    Response containing the fetched data

    const result = await sdk.testDataSource(`
    const url = "https://api.weather.gov/gridpoints/LWX/97,71/forecast";
    const response = await fetch(url).then((res) => res.json());
    return [response.properties.periods[0].temperature];
    `);
  • Executes a Lit Action to fetch data and write it to a smart contract

    Parameters

    • params: FetchToChainParams

      Parameters specifying the data source, contract, and function to call

    Returns Promise<ExecuteJsResponse>

    Response from the Lit Action execution

    const result = await sdk.writeToChain({
    dataSource: `
    const url = "https://api.weather.gov/gridpoints/LWX/97,71/forecast";
    const response = await fetch(url).then((res) => res.json());
    const nearestForecast = response.properties.periods[0];
    return [nearestForecast.temperature, nearestForecast.probabilityOfPrecipitation.value || 0];
    `,
    functionAbi: "function updateWeather(int256 temperature, uint8 precipitationProbability) external",
    toAddress: "0xYourContractAddress",
    chain: "yellowstone"
    });