Skip to content

Commit

Permalink
Merge pull request #142 from VenusProtocol/feat/ven-2211
Browse files Browse the repository at this point in the history
[VEN-2211]: support direct feed registry in binance oracle
  • Loading branch information
GitGuru7 authored Dec 6, 2023
2 parents 1c78188 + 16288e9 commit b644f7a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
37 changes: 32 additions & 5 deletions contracts/oracles/BinanceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down
32 changes: 32 additions & 0 deletions test/BinanceOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("Binance Oracle unit tests", () => {

const fakeAccessControlManager = await smock.fake<AccessControlManager>("AccessControlManagerScenario");
fakeAccessControlManager.isAllowedToCall.returns(true);
this.fakeAccessControlManager = fakeAccessControlManager;

const binanceOracle = await ethers.getContractFactory("BinanceOracle", admin);
this.binanceOracle = <BinanceOracle>await upgrades.deployProxy(
Expand Down Expand Up @@ -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 = <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",
);
});
});

0 comments on commit b644f7a

Please sign in to comment.