Skip to content

Commit 4cf82d4

Browse files
committed
test: add test for quote vault and fix little bug
1 parent 48e1bb0 commit 4cf82d4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/ChainlinkOracle.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ contract ChainlinkOracle is IChainlinkOracle {
145145
function price() external view returns (uint256) {
146146
return SCALE_FACTOR.mulDiv(
147147
BASE_VAULT.getAssets(BASE_VAULT_CONVERSION_SAMPLE) * BASE_FEED_1.getPrice() * BASE_FEED_2.getPrice(),
148-
BASE_VAULT.getAssets(QUOTE_VAULT_CONVERSION_SAMPLE) * QUOTE_FEED_1.getPrice() * QUOTE_FEED_2.getPrice()
148+
QUOTE_VAULT.getAssets(QUOTE_VAULT_CONVERSION_SAMPLE) * QUOTE_FEED_1.getPrice() * QUOTE_FEED_2.getPrice()
149149
);
150150
}
151151
}

test/ChainlinkOracleTest.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ IERC4626 constant vaultZero = IERC4626(address(0));
2828
IERC4626 constant sDaiVault = IERC4626(0x83F20F44975D03b1b09e64809B757c47f942BEeA);
2929

3030
contract ChainlinkOracleTest is Test {
31+
using Math for uint256;
32+
3133
function setUp() public {
3234
vm.createSelectFork(vm.envString("ETH_RPC_URL"));
3335
}
@@ -141,6 +143,35 @@ contract ChainlinkOracleTest is Test {
141143
assertApproxEqRel(oracle.price(), expectedPrice, deviation);
142144
}
143145

146+
function testEthSDaiOracle() public {
147+
ChainlinkOracle oracle =
148+
new ChainlinkOracle(vaultZero, 1, sDaiVault, 1e18, feedZero, feedZero, daiEthFeed, feedZero, 18, 18);
149+
(, int256 expectedPrice,,,) = daiEthFeed.latestRoundData();
150+
assertEq(
151+
oracle.price(),
152+
// 1e(36 + dQ1 + fpQ1 + fpQ2 - dB1 - fpB1 - fpB2) * qCS / bCS
153+
10 ** (36 + 18 + 18 + 0 - 18 - 0 - 0) * 1e18 / (sDaiVault.convertToAssets(1e18) * uint256(expectedPrice))
154+
);
155+
}
156+
157+
function testUsdcSDaiOracle() public {
158+
ChainlinkOracle oracle =
159+
new ChainlinkOracle(vaultZero, 1, sDaiVault, 1e18, usdcEthFeed, feedZero, daiEthFeed, feedZero, 6, 18);
160+
(, int256 baseAnswer,,,) = usdcEthFeed.latestRoundData();
161+
(, int256 quoteAnswer,,,) = daiEthFeed.latestRoundData();
162+
// 1e(36 + dQ1 + fpQ1 + fpQ2 - dB1 - fpB1 - fpB2) * qCS / bCS
163+
uint256 scaleFactor = 10 ** (36 + 18 + 18 + 0 - 6 - 18 - 0) * 1e18;
164+
assertEq(
165+
oracle.price(),
166+
scaleFactor.mulDiv(uint256(baseAnswer), (sDaiVault.convertToAssets(1e18) * uint256(quoteAnswer)))
167+
);
168+
// DAI has 12 more decimals than USDC.
169+
uint256 expectedPrice = 10 ** (36 + 12);
170+
// Admit a 50% interest gain before breaking this test.
171+
uint256 deviation = 0.66 ether;
172+
assertApproxEqRel(oracle.price(), expectedPrice, deviation);
173+
}
174+
144175
function testConstructorZeroVaultConversionSample() public {
145176
vm.expectRevert(bytes(ErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO));
146177
new ChainlinkOracle(sDaiVault, 0, vaultZero, 1, daiEthFeed, feedZero, usdcEthFeed, feedZero, 18, 6);

0 commit comments

Comments
 (0)