113 lines
3.3 KiB
Solidity
113 lines
3.3 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.20;
|
|
|
|
import "forge-std/Test.sol";
|
|
import "../../contracts/vault/XAUOracle.sol";
|
|
import "../../contracts/oracle/Aggregator.sol";
|
|
|
|
contract XAUOracleTest is Test {
|
|
XAUOracle public oracle;
|
|
Aggregator public feed1;
|
|
Aggregator public feed2;
|
|
|
|
address public admin = address(0x1);
|
|
// Aggregator has decimals=8; use 8-decimal prices (0.05 -> 5e6)
|
|
uint256 public constant PRICE1 = 5e6; // 0.05 oz XAU in 8 decimals
|
|
uint256 public constant PRICE2 = 51e5; // 0.051 oz XAU in 8 decimals
|
|
// Expected 18-decimal price: 0.0505 -> 5.05e16
|
|
uint256 public constant EXPECTED_AVG_18 = 505e14; // (PRICE1+PRICE2)/2 converted to 18 decimals
|
|
|
|
function setUp() public {
|
|
vm.startPrank(admin);
|
|
|
|
oracle = new XAUOracle(admin);
|
|
|
|
// Deploy price feeds
|
|
feed1 = new Aggregator("ETH/XAU Feed 1", admin, 3600, 50);
|
|
feed2 = new Aggregator("ETH/XAU Feed 2", admin, 3600, 50);
|
|
|
|
feed1.addTransmitter(admin);
|
|
feed2.addTransmitter(admin);
|
|
|
|
// Update feeds (8-decimal values)
|
|
feed1.updateAnswer(PRICE1);
|
|
feed2.updateAnswer(PRICE2);
|
|
|
|
// Add feeds to oracle (equal weights)
|
|
oracle.addPriceFeed(address(feed1), 5000); // 50%
|
|
oracle.addPriceFeed(address(feed2), 5000); // 50%
|
|
|
|
vm.stopPrank();
|
|
}
|
|
|
|
function test_GetETHPriceInXAU() public {
|
|
vm.prank(admin);
|
|
oracle.updatePrice();
|
|
|
|
(uint256 price, uint256 timestamp) = oracle.getETHPriceInXAU();
|
|
|
|
assertGt(price, 0);
|
|
assertGt(timestamp, 0);
|
|
// Price should be weighted average: (PRICE1 + PRICE2) / 2, converted to 18 decimals
|
|
assertApproxEqAbs(price, EXPECTED_AVG_18, 1e15); // Allow small rounding error
|
|
}
|
|
|
|
function test_UpdatePrice() public {
|
|
vm.prank(admin);
|
|
oracle.updatePrice();
|
|
|
|
(uint256 price, ) = oracle.getETHPriceInXAU();
|
|
assertGt(price, 0);
|
|
}
|
|
|
|
function test_AddPriceFeed() public {
|
|
vm.startPrank(admin);
|
|
Aggregator feed3 = new Aggregator("ETH/XAU Feed 3", admin, 3600, 50);
|
|
feed3.addTransmitter(admin);
|
|
feed3.updateAnswer(PRICE1);
|
|
|
|
oracle.addPriceFeed(address(feed3), 3333); // 33.33%
|
|
|
|
oracle.updatePrice();
|
|
vm.stopPrank();
|
|
|
|
(uint256 price, ) = oracle.getETHPriceInXAU();
|
|
assertGt(price, 0);
|
|
}
|
|
|
|
function test_RemovePriceFeed() public {
|
|
vm.prank(admin);
|
|
oracle.removePriceFeed(address(feed1));
|
|
|
|
// Should still work with feed2
|
|
vm.prank(admin);
|
|
oracle.updatePrice();
|
|
|
|
(uint256 price, ) = oracle.getETHPriceInXAU();
|
|
assertGt(price, 0);
|
|
}
|
|
|
|
function test_Freeze() public {
|
|
vm.prank(admin);
|
|
oracle.freeze();
|
|
|
|
assertTrue(oracle.isFrozen());
|
|
|
|
vm.prank(admin);
|
|
oracle.unfreeze();
|
|
|
|
assertFalse(oracle.isFrozen());
|
|
}
|
|
|
|
function test_GetLiquidationPrice() public {
|
|
vm.prank(admin);
|
|
oracle.updatePrice();
|
|
|
|
(uint256 normalPrice, ) = oracle.getETHPriceInXAU();
|
|
uint256 liquidationPrice = oracle.getLiquidationPrice(address(0x999));
|
|
|
|
// Liquidation price should be normal price * (1 - 0.05) = normalPrice * 0.95
|
|
assertApproxEqAbs(liquidationPrice, (normalPrice * 9500) / 10000, 1e15);
|
|
}
|
|
}
|