Skip to content

Commit b76c64b

Browse files
committed
Adds dappId state var and casts proxies to IApi3ProxyReaderV1
1 parent 5b70632 commit b76c64b

10 files changed

+69
-31
lines changed

contracts/InverseApi3ReaderProxyV1.sol

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.27;
33

4-
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";
4+
import "@api3/contracts/api3-server-v1/proxies/interfaces/IApi3ReaderProxyV1.sol";
55
import "./interfaces/IInverseApi3ReaderProxyV1.sol";
66

77
/// @title An immutable proxy contract that inverts the value returned by an
8-
/// IApi3ReaderProxy data feed
8+
/// IApi3ReaderProxyV1 data feed
99
/// @dev This contract implements the AggregatorV2V3Interface to be compatible
1010
/// with Chainlink aggregators. This allows the contract to be used as a drop-in
1111
/// replacement for Chainlink aggregators in existing dApps.
1212
/// Refer to https://github.com/api3dao/migrate-from-chainlink-to-api3 for more
1313
/// information about the Chainlink interface implementation.
1414
contract InverseApi3ReaderProxyV1 is IInverseApi3ReaderProxyV1 {
15-
/// @notice IApi3ReaderProxy contract address
15+
/// @notice IApi3ReaderProxyV1 contract address
1616
address public immutable override proxy;
1717

18-
/// @param proxy_ IApi3ReaderProxy contract address
18+
/// @notice dApp ID of the proxy
19+
uint256 public immutable override dappId;
20+
21+
/// @param proxy_ IApi3ReaderProxyV1 contract address
1922
constructor(address proxy_) {
2023
if (proxy_ == address(0)) {
2124
revert ZeroProxyAddress();
2225
}
2326
proxy = proxy_;
27+
dappId = IApi3ReaderProxyV1(proxy_).dappId();
2428
}
2529

26-
/// @notice Returns the inverted value of the underlying IApi3ReaderProxy
30+
/// @notice Returns the inverted value of the underlying IApi3ReaderProxyV1
2731
/// @dev Calculates `int224(1e36) / baseValue`. The operation will revert if
2832
/// `baseValue` is zero. If `baseValue` is non-zero but its absolute value is
2933
/// greater than `1e36`, the result of the integer division will be `0`.
@@ -35,7 +39,7 @@ contract InverseApi3ReaderProxyV1 is IInverseApi3ReaderProxyV1 {
3539
override
3640
returns (int224 value, uint32 timestamp)
3741
{
38-
(int224 baseValue, uint32 baseTimestamp) = IApi3ReaderProxy(proxy)
42+
(int224 baseValue, uint32 baseTimestamp) = IApi3ReaderProxyV1(proxy)
3943
.read();
4044

4145
if (baseValue == 0) {

contracts/NormalizedApi3ReaderProxyV1.sol

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ contract NormalizedApi3ReaderProxyV1 is INormalizedApi3ReaderProxyV1 {
1515
/// @notice Chainlink AggregatorV2V3Interface contract address
1616
address public immutable override feed;
1717

18+
/// @notice dApp ID of the proxy
19+
uint256 public immutable override dappId;
20+
1821
/// @notice Pre-calculated factor for scaling the feed's value to 18
1922
/// decimals.
2023
int256 public immutable scalingFactor;
@@ -24,7 +27,8 @@ contract NormalizedApi3ReaderProxyV1 is INormalizedApi3ReaderProxyV1 {
2427
bool public immutable isUpscaling;
2528

2629
/// @param feed_ The address of the Chainlink AggregatorV2V3Interface feed
27-
constructor(address feed_) {
30+
/// @param dappId_ dApp ID of the proxy
31+
constructor(address feed_, uint256 dappId_) {
2832
if (feed_ == address(0)) {
2933
revert ZeroProxyAddress();
3034
}
@@ -36,6 +40,7 @@ contract NormalizedApi3ReaderProxyV1 is INormalizedApi3ReaderProxyV1 {
3640
revert NoNormalizationNeeded();
3741
}
3842
feed = feed_;
43+
dappId = dappId_;
3944
uint8 delta = feedDecimals_ > 18
4045
? feedDecimals_ - 18
4146
: 18 - feedDecimals_;

contracts/PriceCappedApi3ReaderProxyV1.sol

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.27;
33

4-
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";
4+
import "@api3/contracts/api3-server-v1/proxies/interfaces/IApi3ReaderProxyV1.sol";
55
import "./interfaces/IPriceCappedApi3ReaderProxyV1.sol";
66

77
/**
@@ -21,16 +21,19 @@ import "./interfaces/IPriceCappedApi3ReaderProxyV1.sol";
2121
* floored at 0 if `lowerBound_` is 0.
2222
*/
2323
contract PriceCappedApi3ReaderProxyV1 is IPriceCappedApi3ReaderProxyV1 {
24-
/// @notice IApi3ReaderProxy contract address
24+
/// @notice IApi3ReaderProxyV1 contract address
2525
address public immutable override proxy;
2626

27+
/// @notice dApp ID of the proxy
28+
uint256 public immutable override dappId;
29+
2730
/// @notice The minimum price (inclusive) that this proxy will report.
2831
int224 public immutable override lowerBound;
2932

3033
/// @notice The maximum price (inclusive) that this proxy will report.
3134
int224 public immutable override upperBound;
3235

33-
/// @param proxy_ IApi3ReaderProxy contract address
36+
/// @param proxy_ IApi3ReaderProxyV1 contract address
3437
/// @param lowerBound_ The minimum price (inclusive) this proxy will report
3538
/// @param upperBound_ The maximum price (inclusive) this proxy will report
3639
constructor(address proxy_, int224 lowerBound_, int224 upperBound_) {
@@ -44,12 +47,13 @@ contract PriceCappedApi3ReaderProxyV1 is IPriceCappedApi3ReaderProxyV1 {
4447
revert UpperBoundMustBeGreaterOrEqualToLowerBound();
4548
}
4649
proxy = proxy_;
50+
dappId = IApi3ReaderProxyV1(proxy_).dappId();
4751
lowerBound = lowerBound_;
4852
upperBound = upperBound_;
4953
}
5054

5155
/// @notice Reads the current value and timestamp from the underlying
52-
/// `IApi3ReaderProxy` and applies the price bounds.
56+
/// `IApi3ReaderProxyV1` and applies the price bounds.
5357
/// @dev If the `baseValue` from the underlying proxy is less than
5458
/// `lowerBound`, then `lowerBound` is returned as the `value`. If
5559
/// `baseValue` is greater than `upperBound`, then `upperBound` is returned.
@@ -63,7 +67,7 @@ contract PriceCappedApi3ReaderProxyV1 is IPriceCappedApi3ReaderProxyV1 {
6367
override
6468
returns (int224 value, uint32 timestamp)
6569
{
66-
(int224 baseValue, uint32 baseTimestamp) = IApi3ReaderProxy(proxy)
70+
(int224 baseValue, uint32 baseTimestamp) = IApi3ReaderProxyV1(proxy)
6771
.read();
6872

6973
timestamp = baseTimestamp;
@@ -82,7 +86,7 @@ contract PriceCappedApi3ReaderProxyV1 is IPriceCappedApi3ReaderProxyV1 {
8286
/// @return True if the base value is less than `lowerBound` or greater
8387
/// than `upperBound`, false otherwise.
8488
function isCapped() external view returns (bool) {
85-
(int224 baseValue, ) = IApi3ReaderProxy(proxy).read();
89+
(int224 baseValue, ) = IApi3ReaderProxyV1(proxy).read();
8690
return baseValue < lowerBound || baseValue > upperBound;
8791
}
8892

contracts/ProductApi3ReaderProxyV1.sol

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.27;
33

4-
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";
4+
import "@api3/contracts/api3-server-v1/proxies/interfaces/IApi3ReaderProxyV1.sol";
55
import "./interfaces/IProductApi3ReaderProxyV1.sol";
66

77
/// @title An immutable proxy contract that is used to read a composition of two
8-
/// IApi3ReaderProxy data feeds by multiplying their values
8+
/// IApi3ReaderProxyV1 data feeds by multiplying their values
99
/// @dev This contract implements the AggregatorV2V3Interface to be compatible
1010
/// with Chainlink aggregators. This allows the contract to be used as a drop-in
1111
/// replacement for Chainlink aggregators in existing dApps.
1212
/// Refer to https://github.com/api3dao/migrate-from-chainlink-to-api3 for more
1313
/// information about the Chainlink interface implementation.
1414
contract ProductApi3ReaderProxyV1 is IProductApi3ReaderProxyV1 {
15-
/// @notice First IApi3ReaderProxy contract address
15+
/// @notice First IApi3ReaderProxyV1 contract address
1616
address public immutable override proxy1;
1717

18-
/// @notice Second IApi3ReaderProxy contract address
18+
/// @notice Second IApi3ReaderProxyV1 contract address
1919
address public immutable override proxy2;
2020

21-
/// @param proxy1_ First IApi3ReaderProxy contract address
22-
/// @param proxy2_ Second IApi3ReaderProxy contract address
21+
/// @notice The dApp ID of the two proxies
22+
uint256 public immutable override dappId;
23+
24+
/// @param proxy1_ First IApi3ReaderProxyV1 contract address
25+
/// @param proxy2_ Second IApi3ReaderProxyV1 contract address
2326
constructor(address proxy1_, address proxy2_) {
2427
if (proxy1_ == address(0) || proxy2_ == address(0)) {
2528
revert ZeroProxyAddress();
2629
}
2730
if (proxy1_ == proxy2_) {
2831
revert SameProxyAddress();
2932
}
33+
uint256 dappId1 = IApi3ReaderProxyV1(proxy1_).dappId();
34+
uint256 dappId2 = IApi3ReaderProxyV1(proxy2_).dappId();
35+
if (dappId1 != dappId2) {
36+
revert DappIdMismatch();
37+
}
3038
proxy1 = proxy1_;
3139
proxy2 = proxy2_;
40+
dappId = dappId1;
3241
}
3342

3443
/// @notice Returns the current value and timestamp of the rate composition
35-
/// between two IApi3ReaderProxy proxies by multiplying their values
44+
/// between two IApi3ReaderProxyV1 proxies by multiplying their values
3645
/// @dev Calculates product as `(int256(value1) * int256(value2)) / 1e18`.
3746
/// The initial multiplication `int256(value1) * int256(value2)` may revert
3847
/// on `int256` overflow. The final `int256` result of the full expression
@@ -49,8 +58,8 @@ contract ProductApi3ReaderProxyV1 is IProductApi3ReaderProxyV1 {
4958
override
5059
returns (int224 value, uint32 timestamp)
5160
{
52-
(int224 value1, ) = IApi3ReaderProxy(proxy1).read();
53-
(int224 value2, ) = IApi3ReaderProxy(proxy2).read();
61+
(int224 value1, ) = IApi3ReaderProxyV1(proxy1).read();
62+
(int224 value2, ) = IApi3ReaderProxyV1(proxy2).read();
5463

5564
value = int224((int256(value1) * int256(value2)) / 1e18);
5665
timestamp = uint32(block.timestamp);

contracts/adapters/ScaledApi3FeedProxyV1.sol

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.27;
33

4-
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";
4+
import "@api3/contracts/api3-server-v1/proxies/interfaces/IApi3ReaderProxyV1.sol";
55
import "./interfaces/IScaledApi3FeedProxyV1.sol";
66

77
/// @title An immutable Chainlink AggregatorV2V3Interface feed contract that
8-
/// scales the value of an IApi3ReaderProxy data feed to a target number of
8+
/// scales the value of an IApi3ReaderProxyV1 data feed to a target number of
99
/// decimals
1010
/// @dev This contract reads an `int224` value (assumed to be 18 decimals)
11-
/// from the underlying `IApi3ReaderProxy` and scales it to `targetDecimals`.
11+
/// from the underlying `IApi3ReaderProxyV1` and scales it to `targetDecimals`.
1212
/// The scaling arithmetic uses `int256` for intermediate results, allowing the
1313
/// scaled value to exceed `int224` limits if upscaling significantly; it will
1414
/// revert on `int256` overflow.
1515
/// When downscaling, integer division (`proxyValue / scalingFactor`) is used,
1616
/// which truncates and may lead to precision loss. Integrators must carefully
1717
/// consider this potential precision loss for their specific use case.
1818
contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
19-
/// @notice IApi3ReaderProxy contract address
19+
/// @notice IApi3ReaderProxyV1 contract address
2020
address public immutable override proxy;
2121

22+
/// @notice dApp ID of the proxy
23+
uint256 public immutable override dappId;
24+
2225
/// @dev Target decimals for the scaled value.
2326
uint8 private immutable targetDecimals;
2427

@@ -30,8 +33,8 @@ contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
3033
/// downscaling (divide by `scalingFactor`), to scale to `targetDecimals`.
3134
bool public immutable isUpscaling;
3235

33-
/// @param proxy_ IApi3ReaderProxy contract address
34-
/// @param targetDecimals_ Decimals used to scale the IApi3ReaderProxy value
36+
/// @param proxy_ IApi3ReaderProxyV1 contract address
37+
/// @param targetDecimals_ Decimals to scale the IApi3ReaderProxyV1 value
3538
constructor(address proxy_, uint8 targetDecimals_) {
3639
if (proxy_ == address(0)) {
3740
revert ZeroProxyAddress();
@@ -43,6 +46,7 @@ contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
4346
revert NoScalingNeeded();
4447
}
4548
proxy = proxy_;
49+
dappId = IApi3ReaderProxyV1(proxy_).dappId();
4650
targetDecimals = targetDecimals_;
4751
uint8 delta = targetDecimals_ > 18
4852
? targetDecimals_ - 18
@@ -88,7 +92,7 @@ contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
8892
revert FunctionIsNotSupported();
8993
}
9094

91-
/// @dev Decimals used to scale the IApi3ReaderProxy value
95+
/// @dev Decimals used to scale the IApi3ReaderProxyV1 value
9296
function decimals() external view override returns (uint8) {
9397
return targetDecimals;
9498
}
@@ -137,7 +141,7 @@ contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
137141
updatedAt = startedAt;
138142
}
139143

140-
/// @notice Reads a value from the underlying `IApi3ReaderProxy` and
144+
/// @notice Reads a value from the underlying `IApi3ReaderProxyV1` and
141145
/// scales it to `targetDecimals`.
142146
/// @dev Reads from the underlying proxy and applies scaling to
143147
/// `targetDecimals`. Upscaling uses multiplication; downscaling uses integer
@@ -146,7 +150,7 @@ contract ScaledApi3FeedProxyV1 is IScaledApi3FeedProxyV1 {
146150
/// @return value The scaled signed fixed-point value with `targetDecimals`.
147151
/// @return timestamp The timestamp from the underlying proxy.
148152
function _read() internal view returns (int256 value, uint32 timestamp) {
149-
(int224 proxyValue, uint32 proxyTimestamp) = IApi3ReaderProxy(proxy)
153+
(int224 proxyValue, uint32 proxyTimestamp) = IApi3ReaderProxyV1(proxy)
150154
.read();
151155

152156
value = isUpscaling

contracts/adapters/interfaces/IScaledApi3FeedProxyV1.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ interface IScaledApi3FeedProxyV1 is AggregatorV2V3Interface {
1717
function scalingFactor() external view returns (int256);
1818

1919
function isUpscaling() external view returns (bool);
20+
21+
function dappId() external view returns (uint256);
2022
}

contracts/interfaces/IInverseApi3ReaderProxyV1.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ interface IInverseApi3ReaderProxyV1 is
1515
error FunctionIsNotSupported();
1616

1717
function proxy() external view returns (address proxy);
18+
19+
function dappId() external view returns (uint256);
1820
}

contracts/interfaces/INormalizedApi3ReaderProxyV1.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface INormalizedApi3ReaderProxyV1 is
1818

1919
function feed() external view returns (address feed);
2020

21+
function dappId() external view returns (uint256);
22+
2123
function scalingFactor() external view returns (int256);
2224

2325
function isUpscaling() external view returns (bool);

contracts/interfaces/IPriceCappedApi3ReaderProxyV1.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface IPriceCappedApi3ReaderProxyV1 is
1818

1919
function proxy() external view returns (address proxy);
2020

21+
function dappId() external view returns (uint256);
22+
2123
function lowerBound() external view returns (int224 lowerBound);
2224

2325
function upperBound() external view returns (int224 upperBound);

contracts/interfaces/IProductApi3ReaderProxyV1.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ interface IProductApi3ReaderProxyV1 is
1212

1313
error SameProxyAddress();
1414

15+
error DappIdMismatch();
16+
1517
error ZeroDenominator();
1618

1719
error FunctionIsNotSupported();
1820

1921
function proxy1() external view returns (address proxy1);
2022

2123
function proxy2() external view returns (address proxy2);
24+
25+
function dappId() external view returns (uint256);
2226
}

0 commit comments

Comments
 (0)