Skip to content

Commit

Permalink
Add ePendle and Pendle address, scaffold UniswapV2 oracle factory
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0neerpat committed Dec 13, 2024
1 parent d6d772a commit cc6ab66
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 13 deletions.
2 changes: 2 additions & 0 deletions script/Common.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.
import {ChainlinkRelayerFactory} from '@contracts/factories/ChainlinkRelayerFactory.sol';
import {DenominatedOracleFactory} from '@contracts/factories/DenominatedOracleFactory.sol';
import {IDelayedOracleFactory} from '@interfaces/factories/IDelayedOracleFactory.sol';
// import {UniswapV2RelayerFactory} from '@contracts/factories/UniswapV2RelayerFactory.sol';

abstract contract CommonMainnet is Script {
ChainlinkRelayerFactory public chainlinkRelayerFactory = ChainlinkRelayerFactory(MAINNET_CHAINLINK_RELAYER_FACTORY);
CamelotRelayerFactory public camelotRelayerFactory = CamelotRelayerFactory(MAINNET_CAMELOT_RELAYER_FACTORY);
// UniswapV2RelayerFactory public camelotV2RelayerFactory = UniswapV2RelayerFactory(MAINNET_CAMELOT_V2_RELAYER_FACTORY);
DenominatedOracleFactory public denominatedOracleFactory =
DenominatedOracleFactory(MAINNET_DENOMINATED_ORACLE_FACTORY);
IDelayedOracleFactory public delayedOracleFactory = IDelayedOracleFactory(MAINNET_DELAYED_ORACLE_FACTORY);
Expand Down
50 changes: 48 additions & 2 deletions script/DeployOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ contract DeployCamelotOdUsdOracle is Script, CommonMainnet {
vm.startBroadcast();

_odEthCamelotRelayer = camelotRelayerFactory.deployAlgebraRelayer(
MAINNET_ALGEBRA_FACTORY, MAINNET_SYSTEM_COIN, MAINNET_WETH, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
MAINNET_ALGEBRA_V3_FACTORY, MAINNET_SYSTEM_COIN, MAINNET_WETH, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
);

_odUsdOracle = denominatedOracleFactory.deployDenominatedOracle(
Expand All @@ -155,7 +155,7 @@ contract DeployCamelotOdgUsdOracle is Script, CommonMainnet {
vm.startBroadcast();

_odgEthCamelotRelayer = camelotRelayerFactory.deployAlgebraRelayer(
MAINNET_ALGEBRA_FACTORY, MAINNET_PROTOCOL_TOKEN, MAINNET_WETH, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
MAINNET_ALGEBRA_V3_FACTORY, MAINNET_PROTOCOL_TOKEN, MAINNET_WETH, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
);

_odgUsdOracle = denominatedOracleFactory.deployDenominatedOracle(
Expand All @@ -167,3 +167,49 @@ contract DeployCamelotOdgUsdOracle is Script, CommonMainnet {
vm.stopBroadcast();
}
}

// BROADCAST
// source .env && forge script DeployCamelotEPendleUsdOracle --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC --broadcast --verify --etherscan-api-key $ARB_ETHERSCAN_API_KEY --sender $DEFAULT_KEY_PUBLIC_ADDRESS --account defaultKey

// SIMULATE
// source .env && forge script DeployCamelotEPendleUsdOracle --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC --sender $DEFAULT_KEY_PUBLIC_ADDRESS

contract DeployCamelotEPendleUsdOracle is Script, CommonMainnet {
IBaseOracle public _ePendlePendleUniswapV2Relayer;
IBaseOracle public _PendleEthOracleRelayer;
IBaseOracle public _PendleUsdOracleRelayer;
IBaseOracle public _ePendleUsdOracle;

address public MAINNET_E_PENDLE = 0x3EaBE18eAE267D1B57f917aBa085bb5906114600;
address public MAINNET_PENDLE = 0x0c880f6761F1af8d9Aa9C466984b80DAb9a8c9e8;

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

// _ePendleEthUniswapV2Relayer = uniswapV2RelayerFactory.deployAlgebraRelayer(
// MAINNET_ALGEBRA_V2_FACTORY, MAINNET_E_PENDLE, MAINNET_PENDLE, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
// );

_PendleEthOracleRelayer = camelotRelayerFactory.deployAlgebraRelayer(
MAINNET_ALGEBRA_V3_FACTORY, MAINNET_PENDLE, MAINNET_WETH, uint32(MAINNET_CAMELOT_QUOTE_PERIOD)
);

_PendleUsdOracleRelayer = denominatedOracleFactory.deployDenominatedOracle(
_PendleEthOracleRelayer, IBaseOracle(MAINNET_CHAINLINK_L2VALIDITY_ETH_USD_RELAYER), false
);

// _ePendleUsdOracle = denominatedOracleFactory.deployDenominatedOracle(
// _ePendleEthUniswapV2Relayer, _PendleUsdOracleRelayer, false
// );

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

_PendleUsdOracleRelayer.symbol();
_PendleUsdOracleRelayer.getResultWithValidity();
// _ePendleUsdOracle.getResultWithValidity();
// ePendleDelayedOracle.getResultWithValidity();

vm.stopBroadcast();
}
}
3 changes: 2 additions & 1 deletion script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ address constant ETH_ARB_POOL = 0xe51635ae8136aBAc44906A8f230C2D235E9c195F;
address constant MAINNET_CAMELOT_RELAYER_FACTORY = 0x36645830479170265A154Acb726780fdaE41A28F;
address constant MAINNET_CHAINLINK_RELAYER_FACTORY = 0x06C32500489C28Bd57c551afd8311Fef20bFaBB5;
address constant MAINNET_DENOMINATED_ORACLE_FACTORY = 0xBF760b23d2ef3615cec549F22b95a34DB0F8f5CD;
address constant MAINNET_ALGEBRA_FACTORY = 0x1a3c9B1d2F0529D97f2afC5136Cc23e58f1FD35B;
address constant MAINNET_ALGEBRA_V3_FACTORY = 0x1a3c9B1d2F0529D97f2afC5136Cc23e58f1FD35B;
address constant MAINNET_ALGEBRA_V2_FACTORY = 0x6EcCab422D763aC031210895C81787E87B43A652;
address constant MAINNET_DELAYED_ORACLE_FACTORY = 0x9Dd63fA54dEfd8820BCAb3e3cC39aeEc1aE88098;

////////// RELAYERS //////////
Expand Down
14 changes: 7 additions & 7 deletions script/predeployment/DeployRelayers.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
// source .env && forge script DeployODGCamelotRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployODGCamelotRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
camelotRelayerFactory.deployAlgebraRelayer(
MAINNET_ALGEBRA_FACTORY, MAINNET_PROTOCOL_TOKEN, MAINNET_WETH, uint32(MAINNET_ORACLE_DELAY)
MAINNET_ALGEBRA_V3_FACTORY, MAINNET_PROTOCOL_TOKEN, MAINNET_WETH, uint32(MAINNET_ORACLE_DELAY)
);
vm.stopBroadcast();
}
Expand All @@ -33,7 +33,7 @@ contract DeployODGCamelotRelayerMainnet is CommonMainnet {
// source .env && forge script DeployOdgUsdRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployOdgUsdRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
Expand All @@ -53,7 +53,7 @@ contract DeployOdgUsdRelayerMainnet is CommonMainnet {
// source .env && forge script DeployEthUsdChainlinkRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployEthUsdChainlinkRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
Expand All @@ -69,7 +69,7 @@ contract DeployEthUsdChainlinkRelayerMainnet is CommonMainnet {
// source .env && forge script DeployRethEthChainlinkRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployRethEthChainlinkRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
Expand All @@ -92,7 +92,7 @@ contract DeployRethEthChainlinkRelayerMainnet is CommonMainnet {
// source .env && forge script DeployWstethEthChainlinkRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployWstethEthChainlinkRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
Expand Down Expand Up @@ -140,7 +140,7 @@ contract DeployEzEthUSDPriceFeed is CommonMainnet {
// source .env && forge script DeployArbUsdChainlinkRelayerMainnet --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_MAINNET_RPC

contract DeployArbUsdChainlinkRelayerMainnet is CommonMainnet {
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_FACTORY);
IAlgebraFactory public algebraFactory = IAlgebraFactory(MAINNET_ALGEBRA_V3_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_MAINNET_DEPLOYER_PK'));
Expand Down
15 changes: 15 additions & 0 deletions src/contracts/factories/UniswapV2RelayerChild.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import {UniswapV2Relayer} from '@contracts/oracles/UniswapV2Relayer.sol';
import {FactoryChild} from '@contracts/factories/FactoryChild.sol';

contract UniswapV2RelayerChild is UniswapV2Relayer, FactoryChild {
// --- Init ---
constructor(
address _algebraV2Factory,
address _baseToken,
address _quoteToken,
uint32 _quotePeriod
) UniswapV2Relayer(_algebraV2Factory, _baseToken, _quoteToken, _quotePeriod) {}
}
33 changes: 33 additions & 0 deletions src/contracts/factories/UniswapV2RelayerFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {UniswapV2RelayerChild} from '@contracts/factories/UniswapV2RelayerChild.sol';
import {Authorizable} from '@contracts/utils/Authorizable.sol';

contract UniswapV2RelayerFactory is Authorizable {
uint256 public relayerId;

// --- Events ---
event NewUniswapV2Relayer(address indexed _relayer, address _baseToken, address _quoteToken, uint32 _quotePeriod);

// --- Data ---
mapping(uint256 => address) public relayerById;

// --- Init ---
constructor() Authorizable(msg.sender) {}

// --- Methods ---

function deployUniswapV2Relayer(
address _algebraV2Factory,
address _baseToken,
address _quoteToken,
uint32 _quotePeriod
) external isAuthorized returns (IBaseOracle _relayer) {
_relayer = IBaseOracle(address(new UniswapV2RelayerChild(_algebraV2Factory, _baseToken, _quoteToken, _quotePeriod)));
relayerId++;
relayerById[relayerId] = address(_relayer);
emit NewUniswapV2Relayer(address(_relayer), _baseToken, _quoteToken, _quotePeriod);
}
}
90 changes: 90 additions & 0 deletions src/contracts/oracles/UniswapV2Relayer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import {IERC20Metadata} from '@algebra-periphery/interfaces/IERC20Metadata.sol';
import {IAlgebraFactory} from '@algebra-core/interfaces/IAlgebraFactory.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {IDataStorageOperator} from '@algebra-core/interfaces/IDataStorageOperator.sol';
import {DataStorageLibrary} from '@algebra-periphery/libraries/DataStorageLibrary.sol';

contract UniswapV2Relayer {
int256 public immutable MULTIPLIER;
uint32 public immutable QUOTE_PERIOD;
uint128 public immutable BASE_AMOUNT;

// --- Registry ---
address public algebraPool;
address public baseToken;
address public quoteToken;

// --- Data ---
string public symbol;

constructor(address _algebraV3Factory, address _baseToken, address _quoteToken, uint32 _quotePeriod) {
algebraPool = IAlgebraFactory(_algebraV3Factory).poolByPair(_baseToken, _quoteToken);
require(algebraPool != address(0));

address _token0 = IAlgebraPool(algebraPool).token0();
address _token1 = IAlgebraPool(algebraPool).token1();

// The factory validates that both token0 and token1 are desired baseToken and quoteTokens
if (_token0 == _baseToken) {
baseToken = _token0;
quoteToken = _token1;
} else {
baseToken = _token1;
quoteToken = _token0;
}

BASE_AMOUNT = uint128(10 ** IERC20Metadata(_baseToken).decimals());
MULTIPLIER = int256(18) - int256(uint256(IERC20Metadata(_quoteToken).decimals()));
QUOTE_PERIOD = _quotePeriod;

symbol = string(abi.encodePacked(IERC20Metadata(_baseToken).symbol(), ' / ', IERC20Metadata(_quoteToken).symbol()));
}

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(algebraPool, 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);
_validity = true;
}

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(algebraPool, QUOTE_PERIOD);
uint256 _quoteAmount = DataStorageLibrary.getQuoteAtTick({
tick: _arithmeticMeanTick,
baseAmount: BASE_AMOUNT,
baseToken: baseToken,
quoteToken: quoteToken
});
_result = _parseResult(_quoteAmount);
}

function _parseResult(uint256 _quoteResult) internal view returns (uint256 _result) {
if (MULTIPLIER == 0) {
return _quoteResult;
} else if (MULTIPLIER > 0) {
return _quoteResult * (10 ** uint256(MULTIPLIER));
} else {
return _quoteResult / (10 ** _abs(MULTIPLIER));
}
}

// @notice Return the absolute value of a signed integer as an unsigned integer
function _abs(int256 x) internal pure returns (uint256) {
x >= 0 ? x : -x;
return uint256(x);
}
}
6 changes: 3 additions & 3 deletions test/e2e/SystemOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.7.6;
pragma abicoder v2;

import {
MAINNET_ALGEBRA_FACTORY,
MAINNET_ALGEBRA_V3_FACTORY,
MAINNET_CHAINLINK_ETH_USD_FEED,
MAINNET_CHAINLINK_ARB_USD_FEED,
MAINNET_CHAINLINK_SEQUENCER_FEED,
Expand Down Expand Up @@ -103,8 +103,8 @@ contract OracleSetup is DSTestPlus {
);

// --- Camelot ---
arbEthPriceSource = IBaseOracle(address(new CamelotRelayer(MAINNET_ALGEBRA_FACTORY, ARB, ETH, STALE_PRICE))); // correct
ethArbPriceSource = IBaseOracle(address(new CamelotRelayer(MAINNET_ALGEBRA_FACTORY, ETH, ARB, STALE_PRICE))); // inverted
arbEthPriceSource = IBaseOracle(address(new CamelotRelayer(MAINNET_ALGEBRA_V3_FACTORY, ARB, ETH, STALE_PRICE))); // correct
ethArbPriceSource = IBaseOracle(address(new CamelotRelayer(MAINNET_ALGEBRA_V3_FACTORY, ETH, ARB, STALE_PRICE))); // inverted

// --- Denominated ---
arbUsdPriceSource = IDenominatedOracle(address(new DenominatedOracle(arbEthPriceSource, ethUsdPriceSource, false)));
Expand Down

0 comments on commit cc6ab66

Please sign in to comment.