NEW

CCIP is now available on testnet for all developers. Get started today.

Chainlink Functions Architecture

Request and Receive Data

Requests to Chainlink Functions follow the Request & Receive Data cycle.

  1. A EOA (Externally Owned Account) initiates the transaction by calling the consumer contract.
  2. The consumer contract must inherit FunctionsClient to send the request to the FunctionsOracle contract.
  3. The FunctionsOracle contract:
    1. Calls the FunctionsBillingRegistry 's startBilling function to estimate the fulfillment costs and block the amount in the Reservation balance (To learn more, read Cost simulation).
    2. Emits an OracleRequest event containing information about the request.
  4. On reception of the event, each DON's oracle initiates the API call on a serverless environment.
  5. Each serverless environment calls the API provider to fetch the API response.
  6. The DON runs the Off-chain Reporting protocol(OCR) to aggregate all the API responses.
  7. The aggregate API response is transmitted by a DON's oracle node to the FunctionsOracle contract.
  8. The FunctionsOracle contract calls the FunctionsBillingRegistry's fulfillAndBill function to calculate the fulfillment costs and finalize the billing (To learn more, read Cost calculation).
  9. The FunctionsBillingRegistry contract calls back the consumer contract.

Note: Chainlink Functions requests are not limited to API requests. The diagram depicts an example of API requests, but you can request the DON to run any computation.

Subscription Management

Chainlink Functions requests receive funding from subscription accounts. As explained in Concepts, the Subscription App is a User Interface that abstracts the communications with the Subscriptions contract (aka Functions Billing Registry contract). The Functions Billing Registry lets you manage your subscription accounts.

Create Subscription

EOAs (Externally Owned Accounts) create subscriptions using the Subscriptions App. The App communicates with the Functions Billing Registry, which assigns a unique identifier (aka Subscription ID).

Fund Subscription

You must fund your subscription accounts with enough LINK tokens:

  1. Connect your EOA to the Subscription App.
  2. Fund your subscription account. The Subscriptions App abstracts the following:
    1. Call transferAndCall on the LINK token contract, transferring LINK tokens along with the Subscription ID in the payload.
    2. The Functions Billing Registry contract implements onTokenTransfer: It parses the Subscription ID from the payload and funds the subscription account with the transferred LINK amount.

Add Consumer

You must allowlist your consumers' contracts on your subscription account before they can make Chainlink Functions requests. To do so:

  1. Connect your EOA to the Subscription App.
  2. Add the address of your consumer contract to your subscription account.
  3. The Subscription App calls the Functions Billing Registry contract to add the consumer contract address to your subscription account.

Remove Consumer

To remove a consumer contract:

  1. Connect your EOA to the Subscription App.
  2. Remove the address of your consumer contract from the allowlist.
  3. The Subscription App calls the Functions Billing Registry contract to remove the consumer contract address from your subscription account.

Note: You can still remove consumers from your subscription if there are in-flight requests. Your consumer contract will still be called back, and your Subscription Account will be charged.

Cancel Subscription

To cancel a subscription:

  1. Connect your EOA to the Subscription App.
  2. Cancel your subscription, passing the Subscription Balance receiver account address. The Subscriptions App abstracts the following:
    1. Call the cancelSubscription function on the Functions Billing Registry contract, deleting the Subscription ID and removing any existing consumers.
    2. The outstanding Subscription Balance is sent to the receiver account.

Note: You cannot cancel a subscription if there are in-flight requests.

Transferring ownership of a Subscription

Transferring ownership works as follows:

  1. Connect your EOA to the Subscription App.
  2. Initiate the ownership transfer by specifying the new owner's address.
  3. The new owner must connect their EOA to the Subscription App and accept the ownership.

What's next

Stay updated on the latest Chainlink news