Skip to content

Commit

Permalink
mocks: WIP : chainlink oracle lib
Browse files Browse the repository at this point in the history
  • Loading branch information
arbaz-immunefi committed Feb 6, 2024
1 parent 3739b73 commit 2740b85
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at a28aaf
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts-upgradeable
Submodule openzeppelin-contracts-upgradeable added at 25aabd
1 change: 1 addition & 0 deletions lib/v2-core
Submodule v2-core added at ee547b
1 change: 1 addition & 0 deletions lib/v2-periphery
Submodule v2-periphery added at 0335e8
4 changes: 2 additions & 2 deletions src/oracle/OracleMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ abstract contract OracleMock {
* @param op The OracleProvider instance to use for mocking data.
* @param data The data to be used in the mock call to the oracle.
*/
function mockOracleData(OracleProviders op, bytes memory data) internal virtual {
function mockOracleData(OracleProviders op, address tokenAddress,address baseQuote,uint256 mockPrice) internal virtual {
_ops.push(op);
op.mockOracleData(data);
op.mockOracleData(tokenAddress,baseQuote,mockPrice);
_ops.pop();
}

Expand Down
6 changes: 4 additions & 2 deletions src/oracle/examples/OracleMockExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ contract OracleMockExample is OracleMock,Tokens {
function initiateAttack(OracleProviders op) external {
_op = op;
if (op == OracleProviders.CHAINLINK) {
console.log("CHAINLNK");
address tokenAddress = address(EthereumTokens.LINK);
address baseQuote = 0x0000000000000000000000000000000000000348;
uint256 mockPrice = 100;
mockOracleData(op,tokenAddress,baseQuote,mockPrice);
}
mockOracleData(op,bytes("xx"));
}

}
32 changes: 23 additions & 9 deletions src/oracle/lib/ChainlinkOracle.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity ^0.8.0;

import "../../tokens/Tokens.sol";
import "forge-std/Vm.sol";


/**
Expand All @@ -9,6 +10,9 @@ import "../../tokens/Tokens.sol";
* specific to the blockchain environment the contract is operating in.
*/
library ChainlinkOracle {
address constant HEVM_ADDRESS = address(bytes20(uint160(uint256(keccak256("hevm cheat code")))));
Vm constant vm = Vm(HEVM_ADDRESS);

struct Context {
FeedRegistryInterface registry;
}
Expand All @@ -18,7 +22,7 @@ library ChainlinkOracle {
* within a test environment, enabling the testing of contract interactions with oracles.
* @param data Byte array containing the mocked data to be used in the oracle simulation.
*/
function mockOracleData(bytes memory data) internal {
function mockOracleData(address tokenAddress,address baseQuote,uint256 mockPrice) internal {
Context memory context = context();

console.log("HEREEE");
Expand All @@ -29,21 +33,31 @@ library ChainlinkOracle {

feed.decimals();

console.log("\n================\n");
context.registry.latestRoundData(0x514910771AF9Ca656af840dff83E8264EcF986CA,0x0000000000000000000000000000000000000348);

// 1. Registry. (LINK, BASE)
// 2. PriceFee


vm.mockCall(
address(context.registry),
abi.encodeCall(FeedRegistryInterface.latestRoundData,(0x514910771AF9Ca656af840dff83E8264EcF986CA,0x0000000000000000000000000000000000000348)),
abi.encode(1,0,0,block.timestamp,0)
);

context.registry.latestRoundData(0x514910771AF9Ca656af840dff83E8264EcF986CA,0x0000000000000000000000000000000000000348);

//console.logBytes(data);
//vm.roll(100);

//vm.prank(context.registry);
feed.latestAnswer();
//vm.prank(address(context.registry));
//feed.latestRoundData();
//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.

// Code to mock the oracle call...
}

/**
Expand Down
1 change: 0 additions & 1 deletion test/examples/OracleMockExample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ contract OracleMockExampleTest is Test {
}

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

0 comments on commit 2740b85

Please sign in to comment.