API3
API3β
API3 is a collaborative project to deliver traditional API services to smart contract platforms in a decentralized and trust-minimized way. It is governed by a decentralized autonomous organization (DAO), namely the API3 DAO.
Read more about how The API3 DAO works. Click here
Airnodeβ
Developers can use Airnode to request off-chain data inside their Smart Contracts on Linea. An Airnode is a first-party oracle that pushes off-chain API data to your on-chain contract. Airnode lets API providers easily run their own first-party oracle nodes. That way, they can provide data to any on-chain dApp that's interested in their services, all without an intermediary.
An on-chain smart contract makes a request in the RRP (Request Response Protocol) contract (AirnodeRrpV0.sol
) that adds the request to the event logs. The Airnode then accesses the event logs, fetches the API data and performs a callback to the requester with the requested data.
Requesting off-chain data by calling an Airnodeβ
Requesting off-chain data essentially involves triggering an Airnode and getting its response through your smart contract. The smart contract in this case would be the requester contract which will make a request to the desired off-chain Airnode and then capture its response.
The requester calling an Airnode primarily focuses on two tasks:
- Make the request
- Accept and decode the response
Here is an example of a basic requester contract to request data from an Airnode:
pragma solidity 0.8.9;
import "@api3/airnode-protocol/contracts/rrp/requesters/RrpRequesterV0.sol";
// A Requester that will return the requested data by calling the specified airnode.
// Make sure you specify the right _rrpAddress for your chain.
contract Requester is RrpRequesterV0 {
mapping(bytes32 => bool) public incomingFulfillments;
mapping(bytes32 => int256) public fulfilledData;
constructor(address _rrpAddress) RrpRequesterV0(_rrpAddress) {}
/**
* The main makeRequest function that will trigger the Airnode request
* airnode: Airnode address
* endpointId: The endpoint ID for the specific endpoint
* sponsor: The requester contract itself (in this case)
* sponsorWallet: The wallet that will make the actual request (needs to be funded)
* parameters: encoded API parameters
*/
function makeRequest(
address airnode,
bytes32 endpointId,
address sponsor,
address sponsorWallet,
bytes calldata parameters
) external {
bytes32 requestId = airnodeRrp.makeFullRequest(
airnode,
endpointId,
sponsor,
sponsorWallet,
address(this),
this.fulfill.selector,
parameters
);
incomingFulfillments[requestId] = true;
}
// The callback function with the requested data
function fulfill(bytes32 requestId, bytes calldata data)
external
onlyAirnodeRrp
{
require(incomingFulfillments[requestId], "No such request made");
delete incomingFulfillments[requestId];
int256 decodedData = abi.decode(data, (int256));
fulfilledData[requestId] = decodedData;
}
}
The _rrpAddress
is the main airnodeRrpAddress
. The RRP Contracts have already been deployed on-chain. You can also try deploying it on Remix
Contract | Addresses |
---|---|
AirnodeRrpV0 | 0xa0AD79D995DdeeB18a14eAef56A549A04e3Aa1Bd |
Request parametersβ
The makeRequest()
function expects the following parameters to make a valid request.
airnode
: Specifies the Airnode Address.endpointId
: Specifies which endpoint to be used.sponsor
andsponsorWallet
: Specifies which wallet will be used to fulfill the request.parameters
: Specifies the API and Reserved Parameters (see Airnode ABI specifications for how these are encoded). Parameters can be encoded off-chain using@airnode-abi
library.
Response parametersβ
The callback to the Requester contains two parameters:
requestId
: First acquired when making the request and passed here as a reference to identify the request for which the response is intended.data
: In case of a successful response, this is the requested data which has been encoded and contains a timestamp in addition to other response data. Decode it using thedecode()
function from theabi
object.
Sponsors should not fund a sponsorWallet
with more then they can trust the Airnode with, as the Airnode controls the private key to the sponsorWallet
. The deployer of such Airnode undertakes no custody obligations, and the risk of loss or misuse of any excess funds sent to the sponsorWallet
remains with the sponsor.
Using dAPIs - API3 Datafeedsβ
dAPIs are continuously updated streams of off-chain data, such as the latest cryptocurrency, stock and commodity prices. They can power various decentralized applications such as DeFi lending, synthetic assets, stablecoins, derivatives, NFTs and more.
The data feeds are continuously updated by first-party oracles using signed data. dApp owners can read the on-chain value of any dAPI in realtime.
Due to being composed of first-party data feeds, dAPIs offer security, transparency, cost-efficiency and scalability in a turn-key package.
The API3 Market enables users to connect to a dAPI and access the associated data feed services.
Types of dAPIsβ
Self-funded dAPIsβ
Self-funded dAPIs are single-source data feeds that can be funded by the users with their own funds. The amount of gas supplied determines how long the dAPI will be available to use. If it runs out of gas, the dAPI will no longer be updated unless it is funded again.
Click here to read more about Self-funded dAPIs.
Managed dAPIsβ
Managed dAPIs are sourced directly from multiple first-party data providers running an Airnode and aggregated using Airnode's signed data using a median function. The gas costs and availability of Managed dAPIs is managed by the API3 DAO.
Click here to read more about Managed dAPIs.
Subscribing to Self-funded dAPIsβ
While Managed dAPIs are just available on mainnets, Self-funded dAPIs are available on both mainnets and testnets. The process to read from a dAPI proxy remains same for both Self-funded and Managed dAPIs.
The API3 Market lets users access both Self-funded and Managed dAPIs.
With Self-funded dAPIs, you can fund the dAPI with your own funds. The amount of gas you supply will determine how long your dAPI will be available for use. If you run out of gas, you can fund the dAPI again to keep it available for use.
Exploring and selecting your dAPIβ
The API3 Market provides a list of all the dAPIs available across multiple chains including testnets. You can filter the list by chains and data providers. You can also search for a specific dAPI by name. Once selected you will land on the details page where you can find more information about the dAPI.
You can then decide if you want to use Self-funded or Managed dAPIs.
Funding a sponsor walletβ
If you are trying to access Self-funded dAPIs, you need to make sure that the sponsor wallet for the dAPI is funded. You can activate it by using the API3 Market and send Matic to the sponsorWallet
. Make sure your:
- Wallet is connected to the Market and is the same network as the dAPI you are funding.
- Balance of the wallet should be greater than the amount you are sending to the
sponsorWallet
.
To fund the dAPI, you need to click on the Fund Gas button. Depending upon if a proxy contract is already deployed, you will see a different UI.
Use the gas estimator to select how much gas is needed by the dAPI. Click on Send ETH to send the entered amount to the sponsor wallet of the respective dAPI.
Once the transaction is broadcasted & confirmed on the blockchain a transaction confirmation screen will appear.
Deploying a proxy contract to access the dAPIβ
Smart contracts can interact and read values from contracts that are already deployed on the blockchain. By deploying a proxy contract via the API3 Market, a dApp can interact and read values from a dAPI like ETH/USD.
If a proxy is already deployed for a self-funded dAPI, the dApp can read the dAPI without having to deploy a proxy contract. They do this by using the address of the already deployed proxy contract which will be visible on the API3 Market.
If you are deploying a proxy contract during the funding process, clicking on the Get proxy button will initiate a transaction to your MetaMask that will deploy a proxy contract.
Once the transaction is broadcasted & confirmed on the blockchain, the proxy contract address will be shown on the UI.