Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainlink Functions | Gateway #5

Closed
wants to merge 6 commits into from

Conversation

uklok-owner
Copy link
Collaborator

Description

A unified Gateway for all the Chainlink Function interactions was created.
It will be used to abstract all the required logic from the different places we need to use this feature and simplified as follows:

  • The gateway will have registered all the different codes that could be run. -- Imagine this as an on-chain API --
  • Each "code" will have an independent subscription tied to it. -- Imagine each code/function is an endpoint and the path is the subscription ID--
  • The code and configuration could be updated whenever it's needed, so NO new contracts/subscriptions/deployments are required for this.
  • The gateway has an ACL so only allowed Managers could update endpoints, and only registered clients can use the API.
  • Each client could use 1 or more "endpoints" as each response contains the "subscription Id" (endpoint) requested. This means a simple contract implementation where several endpoints are required could be easily managed by a switch(subscriptionId) where each case is an internal call to the corresponding handler.

example:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {ICCGatewayClient} from "./interfaces/ICCGatewayClient.sol";
import {ICCGateway} from "./interfaces/ICCGateway.sol";

contract CCExampleClient is ICCGatewayClient {
    ICCGateway private immutable gateway;
    uint64 private chat_gpt_endpoint = 234;
    uint64 private star_wars_endpoint = 235;

    constructor(address gatewayAddress) {
        gateway = ICCGateway(gatewayAddress);
    }

    function requestGPT(string[] calldata args, bytes[] calldata bytesArgs) external {
        gateway.sendRequest(chat_gpt_endpoint, args, bytesArgs, "");
    }

    function handleGPTResponse(ICCGatewayClient.CCGResponse calldata response)  {
        if (response.state == ICCGatewayClient.CCGResponseState.Success) {
            // Do something with the `response.data`
        } else {
            // Handle the `response.error`
        }
    }

    function requestStarWars(string[] calldata args, bytes[] calldata bytesArgs) external {
        gateway.sendRequest(star_wars_endpoint, args, bytesArgs, "");
    }

    function handleStarWarsResponse(ICCGatewayClient.CCGResponse calldata response)  {
        if (response.state == ICCGatewayClient.CCGResponseState.Success) {
            // Do something with the `response.data`
        } else {
            // Handle the `response.error`
        }
    }

    function callback(bytes32 requestId) external override {
        ICCGatewayClient.CCGResponse memory response = gateway.getResponse(requestId, true);

        if (response.subscriptionId == chat_gpt_endpoint) {
            handleGPTResponse(response);
        } else if (response.subscriptionId == star_wars_endpoint) {
            handleStarWarsResponse(response);
        }
    }
}

Additional Information

  • A set of helper interfaces & types were defined.
  • The hardhat config was modified to use the ChainlinkUserConfig Type.
  • Chainlink contracts were installed.
  • Compiler version updated to fill chainlink contracts requirements.
  • A helper function to get the current network config as a ChainLinkNetworkUserConfig type was created.
  • A final deploy script to grant all clients access to the gateway was created.
  • standard-version lib was created for an easy CHANGELOG management. Note: Read this to properly do commits and auto generate the changelog of your progress.

Your ENS/address: uklok.eth

- A set of helper interfaces & types were defined.
- The hardhat config was modified to use the `ChainlinkUserConfig` Type.
- `tsconfig-paths` lib was installed and configured.
- Chainlink contracts were installed.
- Compiler version updated to fill chainlink contracts requirements.
- Gateway and GatewayClient interfaces were defined.
- Example contracts and deploy scripts were removed.
- The CCGateway was implemented.
- Some errors were mapped.
- A helper function to get the current network config as a `ChainLinkNetworkUserConfig` type was created.
- The deploy Gateway script was configured.
- An Simple GatewayClient contract was created so anyone could easily use the gateway.
- The deploy script for the ExampleClient was created.
- A final deploy script to grant all clients access to the gateway was created.
@uklok-owner uklok-owner added the enhancement New feature or request label May 22, 2024
Copy link

vercel bot commented May 22, 2024

Someone is attempting to deploy a commit to the BlockMagic Ceptor Club Team on Vercel.

A member of the Team first needs to authorize it.

@uklok-owner uklok-owner changed the base branch from main to dev May 22, 2024 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant