Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q math #19

Merged
merged 18 commits into from
Jan 3, 2024
160 changes: 160 additions & 0 deletions broadcast/DeployFactories.s.sol/421614/run-1703187670.json

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions broadcast/DeployFactories.s.sol/421614/run-1703187704.json

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions broadcast/DeployFactories.s.sol/421614/run-1703193890.json

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions broadcast/DeployFactories.s.sol/421614/run-1703193893.json

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions broadcast/DeployFactories.s.sol/421614/run-latest.json

Large diffs are not rendered by default.

622 changes: 622 additions & 0 deletions broadcast/SetupPostEnvironment.s.sol/421614/run-1703188636.json

Large diffs are not rendered by default.

622 changes: 622 additions & 0 deletions broadcast/SetupPostEnvironment.s.sol/421614/run-1703188670.json

Large diffs are not rendered by default.

622 changes: 622 additions & 0 deletions broadcast/SetupPostEnvironment.s.sol/421614/run-latest.json

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions script/Common.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@ pragma solidity 0.7.6;
import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {IAuthorizable} from '@interfaces/utils/IAuthorizable.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {ChainlinkRelayerFactory} from '@contracts/factories/ChainlinkRelayerFactory.sol';
import {DenominatedOracleFactory} from '@contracts/factories/DenominatedOracleFactory.sol';

abstract contract Common is Script {
function _revoke(IAuthorizable _contract, address _target) internal {
_contract.addAuthorization(_target);
_contract.removeAuthorization(msg.sender);
CamelotRelayerFactory public camelotRelayerFactory = CamelotRelayerFactory(CAMELOT_RELAYER_FACTORY);
ChainlinkRelayerFactory public chainlinkRelayerFactory = ChainlinkRelayerFactory(CHAINLINK_RELAYER_FACTORY);
DenominatedOracleFactory public denominatedOracleFactory = DenominatedOracleFactory(DENOMINATED_ORACLE_FACTORY);

function _revoke(IAuthorizable _contract, address _authorize, address _deauthorize) internal {
_contract.addAuthorization(_authorize);
_contract.removeAuthorization(_deauthorize);
}

function revokeFactories() internal {
_revoke(IAuthorizable(address(camelotRelayerFactory)), TEST_GOVERNOR, vm.envAddress('ARB_SEPOLIA_DEPLOYER_PC'));
_revoke(IAuthorizable(address(chainlinkRelayerFactory)), TEST_GOVERNOR, vm.envAddress('ARB_SEPOLIA_DEPLOYER_PC'));
_revoke(IAuthorizable(address(denominatedOracleFactory)), TEST_GOVERNOR, vm.envAddress('ARB_SEPOLIA_DEPLOYER_PC'));
}
}
14 changes: 10 additions & 4 deletions script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pragma solidity 0.7.6;

// DAO
address constant TEST_GOVERNOR = address(0);
address constant TEST_GOVERNOR = 0x37c5B029f9c3691B3d47cb024f84E5E257aEb0BB;

// Registry of protocol deployment
address constant SEPOLIA_SYSTEM_COIN = address(0);
address constant SEPOLIA_WETH = address(0);
address constant SEPOLIA_SYSTEM_COIN = 0x94beB5fC16824338Eaa538c3c857D7f7fFf4B2Ce;
address constant SEPOLIA_WETH = 0x980B62Da83eFf3D4576C647993b0c1D7faf17c73;

// Testnet Params
uint256 constant ORACLE_PERIOD = 1 seconds;
Expand All @@ -21,9 +21,15 @@ address constant H = 0x37c5B029f9c3691B3d47cb024f84E5E257aEb0BB;
address constant RELAYER_DATA = 0x98A724ECA2804DE70Cae1f28E070D3973CAdcf05;

// Camelot Relayer
address constant CAMELOT_RELAYER_FACTORY = address(0); // from pre-deployment
address constant CAMELOT_RELAYER_FACTORY = 0x9a8E0E6611EC9ebd5A2c8168A2f808e0206d85E4; // from pre-deployment
address constant RELAYER_ONE = 0xa430DD704aC39756fbA7C26FEAF9A220741c05b0;

// Chainlink Relayer
address constant CHAINLINK_RELAYER_FACTORY = 0x12383AFE7d8590fC820bEDfdf41174ADff66210a; // from pre-deployment

// Denominated Oracle
address constant DENOMINATED_ORACLE_FACTORY = 0xe96060D7f58A51B82595cF21dB1BE5097FB65A32; // from pre-deployment

// Chainlink feeds
address constant SEPOLIA_CHAINLINK_ETH_USD_FEED = 0xd30e2101a97dcbAeBCBC04F14C3f624E67A35165;
address constant SEPOLIA_CHAINLINK_ARB_USD_FEED = 0xD1092a65338d049DB68D7Be6bD89d17a0929945e;
Expand Down
2 changes: 1 addition & 1 deletion script/dexrelayer/DeployBase.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {IAlgebraFactory} from '@algebra-core/interfaces/IAlgebraFactory.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {RelayerFactory} from '@contracts/factories/RelayerFactory.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {IRelayer} from '@interfaces/oracles/IRelayer.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {MintableERC20} from '@contracts/for-test/MintableERC20.sol';
Expand Down
6 changes: 3 additions & 3 deletions script/dexrelayer/DeployOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.7.6;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {RelayerFactory} from '@contracts/factories/RelayerFactory.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {IRelayer} from '@interfaces/oracles/IRelayer.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {MintableERC20} from '@contracts/for-test/MintableERC20.sol';
Expand All @@ -27,7 +27,7 @@ contract DeployOracle is Script {
// IBaseOracle public denominatedOracle;

// ChainlinkRelayerFactory public chainlinkRelayerFactory;
RelayerFactory public relayerFactory;
CamelotRelayerFactory public relayerFactory;
// DenominatedOracleFactory public denominatedOracleFactory;

function run() public {
Expand Down Expand Up @@ -57,7 +57,7 @@ contract DeployOracle is Script {
*/
function deployFactories() public {
// chainlinkRelayerFactory = new ChainlinkRelayerFactory();
relayerFactory = new RelayerFactory();
relayerFactory = new CamelotRelayerFactory();
// denominatedOracleFactory = new DenominatedOracleFactory();
}
}
48 changes: 25 additions & 23 deletions script/postdeployment/SetupPostEnvironment.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pragma solidity 0.7.6;
import 'forge-std/console2.sol';
import '@script/Registry.s.sol';
import {Common} from '@script/Common.s.sol';
import {Sqrt} from '@algebra-core/libraries/Sqrt.sol';
import {IAlgebraFactory} from '@algebra-core/interfaces/IAlgebraFactory.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {IERC20Metadata} from '@algebra-periphery/interfaces/IERC20Metadata.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {RelayerFactory} from '@contracts/factories/RelayerFactory.sol';
import {IAuthorizable} from '@interfaces/utils/IAuthorizable.sol';
import {MintableERC20} from '@contracts/for-test/MintableERC20.sol';

// BROADCAST
// source .env && forge script SetupPostEnvironment --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC --broadcast --verify --etherscan-api-key $ARB_ETHERSCAN_API_KEY
Expand All @@ -19,12 +19,16 @@ import {IAuthorizable} from '@interfaces/utils/IAuthorizable.sol';

contract SetupPostEnvironment is Common {
IAlgebraFactory public algebraFactory = IAlgebraFactory(SEPOLIA_ALGEBRA_FACTORY);
RelayerFactory public camelotRelayerFactory = RelayerFactory(CAMELOT_RELAYER_FACTORY);

function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_DEPLOYER_PK'));
algebraFactory.createPool(SEPOLIA_SYSTEM_COIN, SEPOLIA_WETH);
address _pool = algebraFactory.poolByPair(SEPOLIA_SYSTEM_COIN, SEPOLIA_WETH);
MintableERC20 mockWeth = new MintableERC20('Wrapped ETH', 'WETH', 18);

// algebraFactory.createPool(SEPOLIA_SYSTEM_COIN, SEPOLIA_WETH);
// address _pool = algebraFactory.poolByPair(SEPOLIA_SYSTEM_COIN, SEPOLIA_WETH);

algebraFactory.createPool(SEPOLIA_SYSTEM_COIN, address(mockWeth));
address _pool = algebraFactory.poolByPair(SEPOLIA_SYSTEM_COIN, address(mockWeth));

IERC20Metadata _token0 = IERC20Metadata(IAlgebraPool(_pool).token0());
IERC20Metadata _token1 = IERC20Metadata(IAlgebraPool(_pool).token1());
Expand All @@ -33,35 +37,33 @@ contract SetupPostEnvironment is Common {
require(keccak256(abi.encodePacked('WETH')) == keccak256(abi.encodePacked(_token1.symbol())), '!WETH');

uint256 initWethAmount = 1 ether;
uint256 initODAmount = 1656.62 ether;
uint256 initODAmount = 2221.3997 ether;

uint256 _price = ((initWethAmount * WAD) / initODAmount);
console2.logUint(uint160(Sqrt.sqrtAbs(int256(_price)) * (2 ** 96)));

uint256 _price = (initWethAmount * WAD) / initODAmount;
uint256 _sqrtPriceX96 = sqrt(_price * WAD) * (2 ** 96);
console2.logUint((_sqrtPriceX96 / (2 ** 96)) ** 2);
// uint256 _sqrtPriceX96 = sqrt(_price * WAD) * (2 ** 96);
uint256 _sqrtPriceX96 = Sqrt.sqrtAbs(int256(_price)) * (2 ** 96);

IAlgebraPool(_pool).initialize(uint160(_sqrtPriceX96));

IBaseOracle _odWethOracle = camelotRelayerFactory.deployAlgebraRelayer(
SEPOLIA_ALGEBRA_FACTORY, SEPOLIA_SYSTEM_COIN, SEPOLIA_WETH, uint32(ORACLE_INTERVAL_TEST)
);

IBaseOracle chainlinkEthUSDPriceFeed =
chainlinkRelayerFactory.deployChainlinkRelayer(SEPOLIA_CHAINLINK_ETH_USD_FEED, ORACLE_INTERVAL_TEST);

// deploy systemOracle
denominatedOracleFactory.deployDenominatedOracle(_odWethOracle, chainlinkEthUSDPriceFeed, false);

revokeFactories();

/**
* TODO: set denominated oracle to (_odWethOracle + chainlink ETH/USD)
* systemCoinOracle = denominatedOracleFactory.deployDenominatedOracle(_odWethOracle, chainlinkEthUSDPriceFeed, false);
* oracleRelayer.modifyParameters('systemCoinOracle', abi.encode(systemCoinOracle));
* note oracleRelayer will be set to systemOracle in odContracts post deploy script
* code: `oracleRelayer.modifyParameters('systemCoinOracle', abi.encode(systemCoinOracle));`
*/

_revoke(IAuthorizable(address(camelotRelayerFactory)), TEST_GOVERNOR);

vm.stopBroadcast();
}

function sqrt(uint256 x) public pure returns (uint256 y) {
uint256 z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
}
31 changes: 31 additions & 0 deletions script/predeployment/DeployFactories.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {ChainlinkRelayerFactory} from '@contracts/factories/ChainlinkRelayerFactory.sol';
import {DenominatedOracleFactory} from '@contracts/factories/DenominatedOracleFactory.sol';

// BROADCAST
// source .env && forge script DeployFactories --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC --broadcast --verify --etherscan-api-key $ARB_ETHERSCAN_API_KEY

// SIMULATE
// source .env && forge script DeployFactories --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC

contract DeployFactories is Script {
CamelotRelayerFactory public camelotRelayerFactory;
ChainlinkRelayerFactory public chainlinkRelayerFactory;
DenominatedOracleFactory public denominatedOracleFactory;

/**
* @dev CamelotRelayerFactory must be deployed by deployer of protocol
*/
function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_DEPLOYER_PK'));
camelotRelayerFactory = new CamelotRelayerFactory();
chainlinkRelayerFactory = new ChainlinkRelayerFactory();
denominatedOracleFactory = new DenominatedOracleFactory();
vm.stopBroadcast();
}
}
25 changes: 0 additions & 25 deletions script/predeployment/DeployRelayerFactory.s.sol

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

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

contract RelayerChild is Relayer, FactoryChild {
contract CamelotRelayerChild is CamelotRelayer, FactoryChild {
// --- Init ---
constructor(
address _algebraV3Factory,
address _baseToken,
address _quoteToken,
uint32 _quotePeriod
) Relayer(_algebraV3Factory, _baseToken, _quoteToken, _quotePeriod) {}
) CamelotRelayer(_algebraV3Factory, _baseToken, _quoteToken, _quotePeriod) {}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import {EnumerableSet} from '@openzeppelin/contracts/utils/EnumerableSet.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {RelayerChild} from '@contracts/factories/RelayerChild.sol';
import {CamelotRelayerChild} from '@contracts/factories/CamelotRelayerChild.sol';
import {Authorizable} from '@contracts/utils/Authorizable.sol';

contract RelayerFactory is Authorizable {
using EnumerableSet for EnumerableSet.AddressSet;

contract CamelotRelayerFactory is Authorizable {
uint256 public relayerId;

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

// --- Data ---
// TODO: remove enumerable set?
EnumerableSet.AddressSet internal _relayers;
mapping(uint256 => address) public relayerById;

// --- Init ---
Expand All @@ -30,8 +25,7 @@ contract RelayerFactory is Authorizable {
address _quoteToken,
uint32 _quotePeriod
) external isAuthorized returns (IBaseOracle _relayer) {
_relayer = IBaseOracle(address(new RelayerChild(_algebraV3Factory, _baseToken, _quoteToken, _quotePeriod)));
_relayers.add(address(_relayer));
_relayer = IBaseOracle(address(new CamelotRelayerChild(_algebraV3Factory, _baseToken, _quoteToken, _quotePeriod)));
relayerId++;
relayerById[relayerId] = address(_relayer);
emit NewAlgebraRelayer(address(_relayer), _baseToken, _quoteToken, _quotePeriod);
Expand Down
15 changes: 15 additions & 0 deletions src/contracts/factories/ChainlinkRelayerChild.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 {ChainlinkRelayer} from '@contracts/oracles/ChainlinkRelayer.sol';
import {FactoryChild} from '@contracts/factories/FactoryChild.sol';

contract ChainlinkRelayerChild is ChainlinkRelayer, FactoryChild {
// --- Init ---

/**
* @param _aggregator The address of the aggregator to relay
* @param _staleThreshold The threshold in seconds to consider the aggregator stale
*/
constructor(address _aggregator, uint256 _staleThreshold) ChainlinkRelayer(_aggregator, _staleThreshold) {}
}
31 changes: 31 additions & 0 deletions src/contracts/factories/ChainlinkRelayerFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

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

contract ChainlinkRelayerFactory is Authorizable {
uint256 public relayerId;

// --- Events ---
event NewChainlinkRelayer(address indexed _chainlinkRelayer, address _aggregator, uint256 _staleThreshold);

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

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

// --- Methods ---

function deployChainlinkRelayer(
address _aggregator,
uint256 _staleThreshold
) external isAuthorized returns (IBaseOracle _chainlinkRelayer) {
_chainlinkRelayer = IBaseOracle(address(new ChainlinkRelayerChild(_aggregator, _staleThreshold)));
relayerId++;
relayerById[relayerId] = address(_chainlinkRelayer);
emit NewChainlinkRelayer(address(_chainlinkRelayer), _aggregator, _staleThreshold);
}
}
21 changes: 21 additions & 0 deletions src/contracts/factories/DenominatedOracleChild.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {DenominatedOracle} from '@contracts/oracles/DenominatedOracle.sol';
import {FactoryChild} from '@contracts/factories/FactoryChild.sol';

contract DenominatedOracleChild is DenominatedOracle, FactoryChild {
// --- Init ---

/**
* @param _priceSource Address of the price source
* @param _denominationPriceSource Address of the denomination price source
* @param _inverted Boolean indicating if the denomination quote should be inverted
*/
constructor(
IBaseOracle _priceSource,
IBaseOracle _denominationPriceSource,
bool _inverted
) DenominatedOracle(_priceSource, _denominationPriceSource, _inverted) {}
}
Loading
Loading