Skip to content

Instantly share code, notes, and snippets.

@Ashar2shahid
Last active December 14, 2023 07:13
Show Gist options
  • Save Ashar2shahid/10045435f5237355ee5b6a51638d354a to your computer and use it in GitHub Desktop.
Save Ashar2shahid/10045435f5237355ee5b6a51638d354a to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "@api3/contracts/v0.8/interfaces/IProxy.sol";
contract Api3AggregatorAdaptor {
// Updating the proxy address is a security-critical action which is why
// we have made it immutable.
address public immutable ethToUsdProxy;
address public immutable methToEthProxy;
constructor(address _ethToUsdProxy,address _methToEthProxy) {
ethToUsdProxy = _ethToUsdProxy;
methToEthProxy = _methToEthProxy;
}
function latestAnswer() external view returns (int256 value) {
(value, ) = readDataFeed();
}
function latestTimestamp() external view returns (uint256 timestamp) {
( , timestamp) = readDataFeed();
}
function decimals() external view returns (uint8) {
return 18;
}
function readDataFeed()
internal
view
returns (int224 value, uint256 timestamp)
{
(int224 ethValueInUsd, uint256 ethToUsdTimestamp) = IProxy(ethToUsdProxy).read();
(int224 methValueInEth, uint256 methToEthTimestamp) = IProxy(methToEthProxy).read();
value = methValueInEth * ethValueInUsd / (10 ** 18);
// use oldest timestamp
timestamp = ethToUsdTimestamp > methToEthTimestamp ? methToEthTimestamp : ethToUsdTimestamp;
}
function getTokenType() external pure returns (uint256) {
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment