Chainlink Functions Architecture
Request and Receive Data
Requests to Chainlink Functions follow the Request & Receive Data cycle.
- A EOA (Externally Owned Account) initiates the transaction by calling the consumer contract.
- The consumer contract must inherit FunctionsClient to send the request to the FunctionsOracle contract.
- The FunctionsOracle contract:
- 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). - Emits an
OracleRequest
event containing information about the request.
- Calls the FunctionsBillingRegistry 's
- On reception of the event, each DON's oracle initiates the API call on a serverless environment.
- Each serverless environment calls the API provider to fetch the API response.
- The DON runs the Off-chain Reporting protocol(OCR) to aggregate all the API responses.
- The aggregate API response is transmitted by a DON's oracle node to the FunctionsOracle contract.
- The FunctionsOracle contract calls the FunctionsBillingRegistry's
fulfillAndBill
function to calculate the fulfillment costs and finalize the billing (To learn more, read Cost calculation). - 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:
- Connect your EOA to the Subscription App.
- Fund your subscription account. The Subscriptions App abstracts the following:
- Call
transferAndCall
on the LINK token contract, transferring LINK tokens along with the Subscription ID in the payload. - 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.
- Call
Add Consumer
You must allowlist your consumers' contracts on your subscription account before they can make Chainlink Functions requests. To do so:
- Connect your EOA to the Subscription App.
- Add the address of your consumer contract to your subscription account.
- 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:
- Connect your EOA to the Subscription App.
- Remove the address of your consumer contract from the allowlist.
- 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:
- Connect your EOA to the Subscription App.
- Cancel your subscription, passing the Subscription Balance receiver account address. The Subscriptions App abstracts the following:
- Call the
cancelSubscription
function on the Functions Billing Registry contract, deleting the Subscription ID and removing any existing consumers. - The outstanding Subscription Balance is sent to the receiver account.
- Call the
Note: You cannot cancel a subscription if there are in-flight requests.
Transferring ownership of a Subscription
Transferring ownership works as follows:
- Connect your EOA to the Subscription App.
- Initiate the ownership transfer by specifying the new owner's address.
- The new owner must connect their EOA to the Subscription App and accept the ownership.