Skip to content

Commit

Permalink
Working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0neerpat committed Dec 13, 2024
1 parent 96f3272 commit bb89321
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
14 changes: 10 additions & 4 deletions script/DeployOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {CommonMainnet} from '@script/Common.s.sol';
import 'forge-std/console2.sol';
import {CamelotV2RelayerFactory} from '@contracts/factories/CamelotV2RelayerFactory.sol';

import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';

Expand Down Expand Up @@ -179,14 +180,19 @@ contract DeployCamelotEPendleUsdOracle is Script, CommonMainnet {
IBaseOracle public _PendleEthOracleRelayer;
IBaseOracle public _PendleUsdOracleRelayer;
IBaseOracle public _ePendleUsdOracle;
IBaseOracle public _ePendleDelayedOracle;

address public MAINNET_E_PENDLE = 0x3EaBE18eAE267D1B57f917aBa085bb5906114600;
address public MAINNET_PENDLE = 0x0c880f6761F1af8d9Aa9C466984b80DAb9a8c9e8;
CamelotV2RelayerFactory internal _camelotV2RelayerFactory;

function run() public {
vm.startBroadcast();

_ePendlePendleCamelotV2Relayer = camelotV2RelayerFactory.deployCamelotV2Relayer(
// TODO: remove factory deployment
_camelotV2RelayerFactory = new CamelotV2RelayerFactory();

_ePendlePendleCamelotV2Relayer = _camelotV2RelayerFactory.deployCamelotV2Relayer(
MAINNET_CAMELOT_V2_FACTORY, MAINNET_E_PENDLE, MAINNET_PENDLE, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
);

Expand All @@ -201,14 +207,14 @@ contract DeployCamelotEPendleUsdOracle is Script, CommonMainnet {
_ePendleUsdOracle =
denominatedOracleFactory.deployDenominatedOracle(_ePendlePendleCamelotV2Relayer, _PendleUsdOracleRelayer, false);

IBaseOracle ePendleDelayedOracle =
_ePendleDelayedOracle =
delayedOracleFactory.deployDelayedOracle(IBaseOracle(_ePendleUsdOracle), MAINNET_ORACLE_DELAY);

_PendleUsdOracleRelayer.symbol();
_PendleUsdOracleRelayer.getResultWithValidity();

ePendleDelayedOracle.symbol();
ePendleDelayedOracle.getResultWithValidity();
_ePendleDelayedOracle.symbol();
_ePendleDelayedOracle.getResultWithValidity();

vm.stopBroadcast();
}
Expand Down
3 changes: 0 additions & 3 deletions script/predeployment/DeployFactories.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ contract DeployCamelotV2FactoryMain is Script {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
_camelotV2RelayerFactory = new CamelotV2RelayerFactory();

_camelotV2RelayerFactory.addAuthorization(MAINNET_TIMELOCK_CONTROLLER);
_camelotV2RelayerFactory.removeAuthorization(MAINNET_DEPLOYER);

vm.stopBroadcast();
}
}
63 changes: 38 additions & 25 deletions src/contracts/oracles/CamelotV2Relayer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ contract CamelotV2Relayer {
constructor(address _camelotV2Factory, address _baseToken, address _quoteToken, uint32 _quotePeriod) {
camelotV2Pair = ICamelotFactory(_camelotV2Factory).getPair(_baseToken, _quoteToken);
require(camelotV2Pair != address(0));
require(camelotV2Pair.stableSwap() == false);
require(ICamelotPair(camelotV2Pair).stableSwap() == false);

Check warning on line 26 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L23-L26

Added lines #L23 - L26 were not covered by tests

uint112 reserve0;
uint112 reserve1;
(reserve0, reserve1,,) = ICamelotPair(camelotV2Pair).getReserves();
require(reserve0 != 0 && reserve1 != 0, 'CamelotV2Relayer: INSUFFICIENT_RESERVES');

Check warning on line 31 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L28-L31

Added lines #L28 - L31 were not covered by tests

address _token0 = ICamelotPair(camelotV2Pair).token0();
address _token1 = ICamelotPair(camelotV2Pair).token1();

Check warning on line 34 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L33-L34

Added lines #L33 - L34 were not covered by tests
Expand All @@ -44,35 +49,43 @@ contract CamelotV2Relayer {
symbol = string(abi.encodePacked(IERC20Metadata(_baseToken).symbol(), ' / ', IERC20Metadata(_quoteToken).symbol()));

Check warning on line 49 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L49

Added line #L49 was not covered by tests
}

// TODO: Update calculation for V2

function getResultWithValidity() external view returns (uint256 _result, bool _validity) {
// TODO: add catch if the pool doesn't have enough history - return false

// Consult the query with a TWAP period of QUOTE_PERIOD
int24 _arithmeticMeanTick = DataStorageLibrary.consult(camelotV2Pool, QUOTE_PERIOD);
// Calculate the quote amount
uint256 _quoteAmount = DataStorageLibrary.getQuoteAtTick({
tick: _arithmeticMeanTick,
baseAmount: BASE_AMOUNT,
baseToken: baseToken,
quoteToken: quoteToken
});
// Process the quote result to 18 decimal quote
_result = _parseResult(_quoteAmount);
uint112 _reserve0;
uint112 _reserve1;
(_reserve0, _reserve1,,) = ICamelotPair(camelotV2Pair).getReserves();

Check warning on line 55 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L52-L55

Added lines #L52 - L55 were not covered by tests

require(_reserve0 > 0 && _reserve1 > 0, 'CamelotPair: INSUFFICIENT_RESERVES');

Check warning on line 57 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L57

Added line #L57 was not covered by tests

uint256 price;
if (baseToken == ICamelotPair(camelotV2Pair).token0()) {

Check warning on line 60 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L59-L60

Added lines #L59 - L60 were not covered by tests
// baseToken is token0, quoteToken is token1
price = uint256(_reserve1) * BASE_AMOUNT / uint256(_reserve0);

Check warning on line 62 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L62

Added line #L62 was not covered by tests
} else {
// baseToken is token1, quoteToken is token0
price = uint256(_reserve0) * BASE_AMOUNT / uint256(_reserve1);

Check warning on line 65 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L65

Added line #L65 was not covered by tests
}

_result = _parseResult(price);
_validity = true;

Check warning on line 69 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L68-L69

Added lines #L68 - L69 were not covered by tests
}

function read() external view returns (uint256 _result) {
// This call may revert with 'OLD!' if the pool doesn't have enough cardinality or initialized history
int24 _arithmeticMeanTick = DataStorageLibrary.consult(camelotV2Pool, QUOTE_PERIOD);
uint256 _quoteAmount = DataStorageLibrary.getQuoteAtTick({
tick: _arithmeticMeanTick,
baseAmount: BASE_AMOUNT,
baseToken: baseToken,
quoteToken: quoteToken
});
_result = _parseResult(_quoteAmount);
uint112 _reserve0;
uint112 _reserve1;
(_reserve0, _reserve1,,) = ICamelotPair(camelotV2Pair).getReserves();

Check warning on line 75 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L72-L75

Added lines #L72 - L75 were not covered by tests

require(_reserve0 > 0 && _reserve1 > 0, 'CamelotPair: INSUFFICIENT_RESERVES');

Check warning on line 77 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L77

Added line #L77 was not covered by tests

uint256 price;
if (baseToken == ICamelotPair(camelotV2Pair).token0()) {

Check warning on line 80 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L79-L80

Added lines #L79 - L80 were not covered by tests
// baseToken is token0, quoteToken is token1
price = uint256(_reserve1) * BASE_AMOUNT / uint256(_reserve0);

Check warning on line 82 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L82

Added line #L82 was not covered by tests
} else {
// baseToken is token1, quoteToken is token0
price = uint256(_reserve0) * BASE_AMOUNT / uint256(_reserve1);

Check warning on line 85 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L85

Added line #L85 was not covered by tests
}

_result = _parseResult(price);

Check warning on line 88 in src/contracts/oracles/CamelotV2Relayer.sol

View check run for this annotation

Codecov / codecov/patch

src/contracts/oracles/CamelotV2Relayer.sol#L88

Added line #L88 was not covered by tests
}

function _parseResult(uint256 _quoteResult) internal view returns (uint256 _result) {
Expand Down

0 comments on commit bb89321

Please sign in to comment.