Skip to content

Commit

Permalink
oracle mock: chainlink init
Browse files Browse the repository at this point in the history
  • Loading branch information
arbaz-immunefi committed Jan 5, 2024
1 parent 1096c55 commit 3739b73
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/oracle/OracleProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ enum OracleProviders {
CHAINLINK
}

// Fiat currencies follow https://en.wikipedia.org/wiki/ISO_4217
// struct PairQuote {
// USD: address(840),
// GBP: address(826),
// EUR: address(978)
// }

library OracleProvider {
/**
* @dev Returns the name of the specified oracle provider.
Expand Down
19 changes: 19 additions & 0 deletions src/oracle/examples/OracleMockExample.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity ^0.8.0;

import "../OracleMock.sol";
import "../../tokens/Tokens.sol";

import "forge-std/console.sol";

contract OracleMockExample is OracleMock,Tokens {
OracleProviders internal _op;

function initiateAttack(OracleProviders op) external {
_op = op;
if (op == OracleProviders.CHAINLINK) {
console.log("CHAINLNK");
}
mockOracleData(op,bytes("xx"));
}

}
53 changes: 49 additions & 4 deletions src/oracle/lib/ChainlinkOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ pragma solidity ^0.8.0;

import "../../tokens/Tokens.sol";


/**
* ChainlinkOracle is a library used to facilitate interactions with Chainlink Oracles.
* This includes functions to mock oracle data for testing and to retrieve context information
* specific to the blockchain environment the contract is operating in.
*/
library ChainlinkOracle {
struct Context {
EACAggregatorProxy aggregator;
FeedRegistryInterface registry;
}

/**
Expand All @@ -20,6 +21,24 @@ library ChainlinkOracle {
function mockOracleData(bytes memory data) internal {
Context memory context = context();

console.log("HEREEE");
console.log(address(context.registry));

EACAggregatorProxy feed = context.registry.getFeed(0x514910771AF9Ca656af840dff83E8264EcF986CA,0x0000000000000000000000000000000000000348);
console.log(address(feed));

feed.decimals();

//vm.roll(100);

//vm.prank(context.registry);
feed.latestAnswer();
//feed.description();


//console.log(feed.description());
// feed.latestRoundData();

// It's likely we can generalize this function to take a token pair to mock the returned price of, but we should provide a generic
// function which can be used to mock any oracle data, not just price data. This way the Oracle specific library can have their own
// wrapper function which serializes/deserializes the generic mock oracle data into the appropriate parameters.
Expand All @@ -34,16 +53,16 @@ library ChainlinkOracle {
* @return Context The context information for the current blockchain.
*/
function context() internal view returns (Context memory) {
EACAggregatorProxy aggregator;
FeedRegistryInterface registry;

if (block.chainid == 1) {
// Ethereum mainnet
aggregator = EACAggregatorProxy(0x72AFAECF99C9d9C8215fF44C77B94B99C28741e8);
registry = FeedRegistryInterface(0x47Fb2585D2C56Fe188D0E6ec628a38b74fCeeeDf);
} else {
revert("ChainlinkOracle: Chain not supported");
}

return Context(aggregator);
return Context(registry);
}

// /**
Expand Down Expand Up @@ -74,6 +93,32 @@ library ChainlinkOracle {
// }
}

interface FeedRegistryInterface {
function latestRoundData(
address base,
address quote
)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);

function getFeed(
address base,
address quote
)
external
view
returns (
EACAggregatorProxy aggregator
);
}

interface EACAggregatorProxy {
event AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 updatedAt);
event NewRound(uint256 indexed roundId, address indexed startedBy, uint256 startedAt);
Expand Down
22 changes: 22 additions & 0 deletions test/examples/OracleMockExample.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "../../src/oracle/examples/OracleMockExample.sol";

contract OracleMockExampleTest is Test {
uint256 mainnetFork;

OracleMockExample public oracleMockExample;

function setUp() public {
mainnetFork = vm.createFork("eth");
vm.selectFork(mainnetFork);

oracleMockExample = new OracleMockExample();
}

function testMe() public {
console.log("XXX");
oracleMockExample.initiateAttack(OracleProviders.CHAINLINK);
}
}

0 comments on commit 3739b73

Please sign in to comment.