You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+179Lines changed: 179 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,8 @@
7
7
Api3 delivers reliable, first-party oracle data via dAPIs (continuously updated data feeds). However, dApps often require this data in formats or derivations not directly provided by a base dAPI, such as inverted prices (e.g., USD/ETH from ETH/USD), scaled values, or new feeds derived from combining multiple dAPIs (e.g., BTC/ETH from BTC/USD and ETH/USD). Data Feed Proxy Combinators address this as (soon-to-be) audited, standardized, and reusable smart contracts. These combinators operate on top of existing `IApi3ReaderProxy` contracts, enabling developers to combine them or transform their output to read data in different formats or derive new data feeds on-chain. This approach avoids repetitive, error-prone, and gas-intensive custom logic within individual dApps and the impracticality of deploying new oracles for minor data variations, thereby enhancing code reuse and reliability.
8
8
Being documented, easily usable, and officially recommended by Api3, they offer a trusted and straightforward way for developers to implement these common data transformations.
9
9
10
+
It's important to note that these combinator proxies are designed as lightweight wrappers. They do not perform additional validation on the data (e.g., value sanity checks or timestamp verification) returned by the underlying dAPIs or external feeds. The responsibility for data quality and timeliness rests with the source oracle or feed provider. Furthermore, these contracts are provided "as is" without any warranty, and there is no financial coverage or insurance associated with their use.
11
+
10
12
### Core Capabilities
11
13
12
14
Data Feed Proxy Combinators provide modular and composable smart contracts for on-chain data feed adaptation. They typically take the address(es) of underlying feed(s) and operational parameters (like scaling factors) in their constructors. Key contract implementations include:
@@ -18,6 +20,183 @@ Data Feed Proxy Combinators provide modular and composable smart contracts for o
18
20
19
21
These combinators either consume and expose the Api3 `IApi3ReaderProxy` interface or act as adapters to/from other interfaces like Chainlink's `AggregatorV3Interface`. This facilitates integration within the Api3 ecosystem or when bridging with other oracle systems. The output of one combinator can often serve as input for another, enabling complex data transformation pipelines.
20
22
23
+
## Deployment Guide
24
+
25
+
This guide provides step-by-step instructions for dApp developers who need to deploy and use these Data Feed Proxy Combinator contracts.
_Note: This repository uses Hardhat. The `hardhat.config.ts` file relies on these environment variables for proper configuration. Specifically, the `networks` and `etherscan` properties are automatically configured using helper functions from the [`@api3/contracts`](https://github.com/api3dao/contracts/blob/main/src/hardhat-config.ts) package._
85
+
86
+
### 3. Deploying Contracts
87
+
88
+
This repository uses `hardhat-deploy` for managing deployments. Each combinator contract has a corresponding deployment script.
89
+
90
+
To deploy a specific contract, you need to set the `NETWORK` environment variable and any contract-specific environment variables required by its deployment script. Then, use the corresponding `pnpm deploy:<ContractName>` script.
91
+
92
+
The `NETWORK` variable should be set to a chain name as defined by `@api3/contracts` (e.g., `ethereum-sepolia-testnet`, `polygon-mainnet`, `base-mainnet`). You can find a list of available chain names [here](https://github.com/api3dao/contracts/blob/main/src/generated/chains.ts).
93
+
94
+
**Required Environment Variables per Contract:**
95
+
96
+
-**`InverseApi3ReaderProxyV1`**:
97
+
98
+
-`NETWORK`: Target network name.
99
+
-`PROXY`: Address of the underlying `IApi3ReaderProxy` contract (e.g., an ETH/USD dAPI proxy).
_Note: The specific `pnpm deploy:<ContractName>` scripts foreach combinator are definedin the `package.json` file._
134
+
135
+
**Deployment Artifacts**: After deployment, contract artifacts (including ABI and deployed address) are saved in the `deployments/<network_name>/` directory.
136
+
For example, deploying `InverseApi3ReaderProxyV1` to the `ethereum-sepolia-testnet` network would create an artifact file like `deployments/ethereum-sepolia-testnet/InverseApi3ReaderProxyV1_SomeHash.json`. The `_SomeHash` part is derived from the constructor arguments, allowing multiple instances of the same contract with different configurations to be deployed and tracked.
137
+
These artifact files contain the deployed contract address, ABI, and the constructor arguments used fordeployment (look for the `"args"` arrayin the JSON file). This information is crucial for integration and manual verification if needed.
138
+
139
+
**Contract Verification**:
140
+
The deployment scripts automatically attempt to verify the contract on the appropriate block explorer (e.g., Etherscan forEthereum networks, Polygonscan for Polygon) after a successful deployment. This process uses the API keys (e.g., `ETHERSCAN_API_KEY_ETHEREUM_SEPOLIA_TESTNET`, `ETHERSCAN_API_KEY_POLYGON_MAINNET`) configuredin your `.env` file, as described in"### 2. Environment Setup".
141
+
If verification fails during the deployment (e.g., due to network latency or an explorer API issue), you can simply re-run the exact same deployment command. `hardhat-deploy` is idempotent; it will detect that the contract is already deployed with the same arguments and bytecode, skip the deployment step, and only re-attempt the verification.
142
+
143
+
### 4. Combining Contracts (Advanced Usage)
144
+
145
+
The true power of these combinators is realized when they are chained together. The deployed address of one combinator contract can serve as an input (a constructor argument, typically an `IApi3ReaderProxy` or `AggregatorV3Interface` address) for another. This allows you to build sophisticated data transformation pipelines tailored to your dApp's specific needs.
146
+
147
+
Below are some common scenarios illustrating how you can combine these proxies:
148
+
149
+
**Scenario 1: Inverting and Scaling a dAPI**
150
+
151
+
Imagine your dApp requires a USD/ETH price feed with 8 decimal places, but the available API3 dAPI provides ETH/USD with 18 decimals.
152
+
153
+
1. **Deploy `InverseApi3ReaderProxyV1`**:
154
+
155
+
- Input `PROXY`: Address of the ETH/USD `IApi3ReaderProxy` dAPI.
156
+
- Output: An `IApi3ReaderProxy` contract (`InverseProxy_ETHUSD`) that reads USD/ETH.
157
+
- Example command: `NETWORK=your_network PROXY=0xAddressOfEthUsdDapi pnpm deploy:InverseApi3ReaderProxyV1`
158
+
159
+
2. **Deploy `ScaledApi3FeedProxyV1`**:
160
+
- Input `PROXY`: Address of `InverseProxy_ETHUSD` (from step 1).
161
+
- Input `DECIMALS`: `8`.
162
+
- Output: An `AggregatorV3Interface` contract that reads USD/ETH scaled to 8 decimals.
163
+
- Example command: `NETWORK=your_network PROXY=0xAddressOfInverseProxyEthUsd DECIMALS=8 pnpm deploy:ScaledApi3FeedProxyV1`
164
+
165
+
**Scenario 2: Creating a Cross-Currency Pair (e.g., BTC/ETH) and Adapting its Interface**
166
+
167
+
Suppose your dApp needs a BTC/ETH price feed, and you have access to BTC/USD and ETH/USD dAPIs. You also want the final feed to be Chainlink-compatible.
168
+
169
+
1. **Deploy `InverseApi3ReaderProxyV1` for ETH/USD**:
170
+
171
+
- Input `PROXY`: Address of the ETH/USD `IApi3ReaderProxy` dAPI.
172
+
- Output: An `IApi3ReaderProxy` contract (`InverseProxy_ETHUSD`) that reads USD/ETH.
173
+
174
+
2. **Deploy `ProductApi3ReaderProxyV1`**:
175
+
176
+
- Input `PROXY1`: Address of the BTC/USD `IApi3ReaderProxy` dAPI.
177
+
- Input `PROXY2`: Address of `InverseProxy_ETHUSD` (from step 1).
178
+
- Output: An `IApi3ReaderProxy` contract (`ProductProxy_BTCETH`) that reads BTC/ETH (calculated as BTC/USD \* USD/ETH).
179
+
180
+
3. **Deploy `ScaledApi3FeedProxyV1` (Optional, for interface adaptation and scaling)**:
181
+
- Input `PROXY`: Address of `ProductProxy_BTCETH` (from step 2).
182
+
- Input `DECIMALS`: Desired decimals (e.g., `8` or `18`).
183
+
- Output: An `AggregatorV3Interface` contract providing the BTC/ETH price, compatible with systems expecting a Chainlink feed.
184
+
185
+
**Scenario 3: Normalizing an External Feed and Then Inverting It**
186
+
187
+
Your dApp wants to use an external (e.g., Chainlink) EUR/USD feed but requires it as USD/EUR and prefers to interact with it via the API3 `IApi3ReaderProxy` interface.
188
+
189
+
1. **Deploy `NormalizedApi3ReaderProxyV1`**:
190
+
191
+
- Input `FEED`: Address of the external EUR/USD `AggregatorV3Interface` feed.
192
+
- Output: An `IApi3ReaderProxy` contract (`NormalizedProxy_EURUSD`) that reads EUR/USD.
193
+
194
+
2. **Deploy `InverseApi3ReaderProxyV1`**:
195
+
- Input `PROXY`: Address of `NormalizedProxy_EURUSD` (from step 1).
196
+
- Output: An `IApi3ReaderProxy` contract that reads USD/EUR.
197
+
198
+
When deploying a combinator that depends on another already deployed combinator, you will need the address of the prerequisite contract. You can find this address in the deployment artifact file (e.g., `deployments/<network_name>/<ContractName>_SomeHash.json`) generated in the previous step.
199
+
21
200
## Further Information
22
201
23
202
For detailed specifications of each combinator, including constructor arguments, public methods, and Natspec documentation, please consult the contract source code within this repository. Familiarity with the `IApi3ReaderProxy` interface from the `@api3/contracts` repository is also highly recommended for effective integration.
0 commit comments