From 5e9d40b0ae6badc1966fc60fa31fb24da23818bc Mon Sep 17 00:00:00 2001 From: GitGuru7 <128375421+GitGuru7@users.noreply.github.com> Date: Tue, 5 Dec 2023 11:58:19 +0530 Subject: [PATCH 1/2] feat: support direct feed registry in binanace oracle --- contracts/oracles/BinanceOracle.sol | 37 +++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/contracts/oracles/BinanceOracle.sol b/contracts/oracles/BinanceOracle.sol index 536c3da6..197033de 100755 --- a/contracts/oracles/BinanceOracle.sol +++ b/contracts/oracles/BinanceOracle.sol @@ -16,6 +16,7 @@ import "../interfaces/OracleInterface.sol"; * @notice This oracle fetches price of assets from Binance. */ contract BinanceOracle is AccessControlledV8, OracleInterface { + /// @notice Used to fetch feed registry address. address public sidRegistryAddress; /// @notice Set this as asset address for BNB. This is the underlying address for vBNB @@ -27,10 +28,18 @@ contract BinanceOracle is AccessControlledV8, OracleInterface { /// @notice Override symbols to be compatible with Binance feed registry mapping(string => string) public symbols; + /// @notice Used to fetch price of assets used directly when space ID is not supported by current chain. + address public feedRegistryAddress; + + /// @notice Emits when asset stale period is updated. event MaxStalePeriodAdded(string indexed asset, uint256 maxStalePeriod); + /// @notice Emits when symbol of the asset is updated. event SymbolOverridden(string indexed symbol, string overriddenSymbol); + /// @notice Emits when address of feed registry is updated. + event FeedRegistryUpdated(address indexed oldFeedRegistry, address indexed newFeedRegistry); + /** * @notice Checks whether an address is null or not */ @@ -50,10 +59,7 @@ contract BinanceOracle is AccessControlledV8, OracleInterface { * @param _sidRegistryAddress Address of SID registry * @param _accessControlManager Address of the access control manager contract */ - function initialize( - address _sidRegistryAddress, - address _accessControlManager - ) external initializer notNullAddress(_sidRegistryAddress) { + function initialize(address _sidRegistryAddress, address _accessControlManager) external initializer { sidRegistryAddress = _sidRegistryAddress; __AccessControlled_init(_accessControlManager); } @@ -85,6 +91,19 @@ contract BinanceOracle is AccessControlledV8, OracleInterface { emit SymbolOverridden(symbol, overrideSymbol); } + /** + * @notice Used to set feed registry address when current chain does not support space ID. + * @param newfeedRegistryAddress Address of new feed registry. + */ + function setFeedRegistryAddress( + address newfeedRegistryAddress + ) external notNullAddress(newfeedRegistryAddress) onlyOwner { + if (sidRegistryAddress != address(0)) revert("sidRegistryAddress must be zero"); + address oldFeedRegistry = feedRegistryAddress; + feedRegistryAddress = newfeedRegistryAddress; + emit FeedRegistryUpdated(oldFeedRegistry, newfeedRegistryAddress); + } + /** * @notice Uses Space ID to fetch the feed registry address * @return feedRegistryAddress Address of binance oracle feed registry. @@ -127,7 +146,15 @@ contract BinanceOracle is AccessControlledV8, OracleInterface { } function _getPrice(string memory symbol, uint256 decimals) internal view returns (uint256) { - FeedRegistryInterface feedRegistry = FeedRegistryInterface(getFeedRegistryAddress()); + FeedRegistryInterface feedRegistry; + + if (sidRegistryAddress != address(0)) { + // If sidRegistryAddress is available, fetch feedRegistryAddress from sidRegistry + feedRegistry = FeedRegistryInterface(getFeedRegistryAddress()); + } else { + // Use feedRegistry directly if sidRegistryAddress is not available + feedRegistry = FeedRegistryInterface(feedRegistryAddress); + } (, int256 answer, , uint256 updatedAt, ) = feedRegistry.latestRoundDataByName(symbol, "USD"); if (answer <= 0) revert("invalid binance oracle price"); From 16288e9d642f9fd6ce226cd9aec25b6e6c577315 Mon Sep 17 00:00:00 2001 From: GitGuru7 <128375421+GitGuru7@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:15:23 +0530 Subject: [PATCH 2/2] test: add unit tests for binance oracle --- test/BinanceOracle.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/BinanceOracle.ts b/test/BinanceOracle.ts index b3095449..a76c105c 100755 --- a/test/BinanceOracle.ts +++ b/test/BinanceOracle.ts @@ -34,6 +34,7 @@ describe("Binance Oracle unit tests", () => { const fakeAccessControlManager = await smock.fake("AccessControlManagerScenario"); fakeAccessControlManager.isAllowedToCall.returns(true); + this.fakeAccessControlManager = fakeAccessControlManager; const binanceOracle = await ethers.getContractFactory("BinanceOracle", admin); this.binanceOracle = await upgrades.deployProxy( @@ -95,4 +96,35 @@ describe("Binance Oracle unit tests", () => { "1333789241690000000000", ); }); + + it("revert when setting feed registry address and sid already available", async function () { + await expect(this.binanceOracle.setFeedRegistryAddress(this.mockBinanceFeedRegistry.address)).to.be.revertedWith( + "sidRegistryAddress must be zero", + ); + }); + + it("revert when feed registry address is zero", async function () { + const binanceOracle = await ethers.getContractFactory("BinanceOracle", this.admin); + this.binanceOracle = await upgrades.deployProxy( + binanceOracle, + [ethers.constants.AddressZero, this.fakeAccessControlManager.address], + { + constructorArgs: [], + }, + ); + + await expect(this.binanceOracle.setFeedRegistryAddress(ethers.constants.AddressZero)).to.be.revertedWith( + "can't be zero address", + ); + }); + + it("fetch price from direct feed registry ", async function () { + await this.binanceOracle.setMaxStalePeriod("ETH", 24 * 60 * 60); + await this.binanceOracle.setFeedRegistryAddress(this.mockBinanceFeedRegistry.address); + this.mockBinanceFeedRegistry.setAssetPrice("ETH", this.ethPrice); + expect((await this.mockBinanceFeedRegistry.assetPrices("ETH")).toString()).to.be.equal(this.ethPrice); + expect((await this.binanceOracle.getPrice(this.vEth.underlying())).toString()).to.be.equal( + "1333789241690000000000", + ); + }); });