Skip to content

Commit

Permalink
Factory access (#22)
Browse files Browse the repository at this point in the history
* add systemCoinOracle

* update postSetup script

* math err

* access control factories

* fix compilation error

* update naming convention

* rm enumerable sets

* add initPrice test

* add price read checks

* add token order check

* add auth
  • Loading branch information
daopunk authored Jan 2, 2024
1 parent 3e51e58 commit 4410e68
Show file tree
Hide file tree
Showing 39 changed files with 6,765 additions and 184 deletions.
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.

457 changes: 457 additions & 0 deletions broadcast/DeployOracles.s.sol/421614/run-1703718630.json

Large diffs are not rendered by default.

457 changes: 457 additions & 0 deletions broadcast/DeployOracles.s.sol/421614/run-1703718669.json

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

717 changes: 717 additions & 0 deletions broadcast/DeployPool.s.sol/421614/run-1703712392.json

Large diffs are not rendered by default.

717 changes: 717 additions & 0 deletions broadcast/DeployPool.s.sol/421614/run-1703712427.json

Large diffs are not rendered by default.

717 changes: 717 additions & 0 deletions broadcast/DeployPool.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.

66 changes: 63 additions & 3 deletions script/Common.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,71 @@ pragma solidity 0.7.6;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {Sqrt} from '@algebra-core/libraries/Sqrt.sol';
import {IERC20Metadata} from '@algebra-periphery/interfaces/IERC20Metadata.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.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);
ChainlinkRelayerFactory public chainlinkRelayerFactory = ChainlinkRelayerFactory(CHAINLINK_RELAYER_FACTORY);
CamelotRelayerFactory public camelotRelayerFactory = CamelotRelayerFactory(CAMELOT_RELAYER_FACTORY);
DenominatedOracleFactory public denominatedOracleFactory = DenominatedOracleFactory(DENOMINATED_ORACLE_FACTORY);

IAuthorizable public chainlinkRelayerFactoryAuth = IAuthorizable(CHAINLINK_RELAYER_FACTORY);
IAuthorizable public camelotRelayerFactoryAuth = IAuthorizable(CAMELOT_RELAYER_FACTORY);
IAuthorizable public denominatedOracleFactoryAuth = IAuthorizable(DENOMINATED_ORACLE_FACTORY);

address public deployer = vm.envAddress('ARB_SEPOLIA_DEPLOYER_PC');
address public admin = vm.envAddress('ARB_SEPOLIA_PC');

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

function revokeFactories() internal {
_revoke(chainlinkRelayerFactoryAuth, TEST_GOVERNOR, deployer);
_revoke(camelotRelayerFactoryAuth, TEST_GOVERNOR, deployer);
_revoke(denominatedOracleFactoryAuth, TEST_GOVERNOR, deployer);
}

// basePrice = OD, quotePrice = WETH
function initialPrice(
uint256 _basePrice,
uint256 _quotePrice,
address _pool
) internal returns (uint160 _sqrtPriceX96) {
address _token0 = IAlgebraPool(_pool).token0();
bytes32 _symbol = keccak256(abi.encodePacked(IERC20Metadata(_token0).symbol()));
uint256 _price;

// price = token1 / token0
if (_token0 == SEPOLIA_SYSTEM_COIN) {
require(keccak256(abi.encodePacked('OD')) == _symbol, '!OD');
_price = ((_quotePrice * WAD) / _basePrice);
} else {
require(keccak256(abi.encodePacked('WETH')) == _symbol, '!WETH');
_price = ((_basePrice * WAD) / _quotePrice);
}

_sqrtPriceX96 = uint160(Sqrt.sqrtAbs(int256(_price)) * (2 ** 96));
}

/**
* note FOR TEST
*/
function authOnlyFactories() internal {
if (!chainlinkRelayerFactoryAuth.authorizedAccounts(admin)) {
chainlinkRelayerFactoryAuth.addAuthorization(admin);
}
if (!camelotRelayerFactoryAuth.authorizedAccounts(admin)) {
camelotRelayerFactoryAuth.addAuthorization(admin);
}
if (!denominatedOracleFactoryAuth.authorizedAccounts(admin)) {
denominatedOracleFactoryAuth.addAuthorization(admin);
}
}
}
21 changes: 15 additions & 6 deletions script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@
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;
uint256 constant ORACLE_INTERVAL_TEST = 1 minutes;
uint256 constant WAD = 1e18;
uint256 constant MINT_AMOUNT = 1_000_000 ether;
uint256 constant INIT_WETH_AMOUNT = 1 ether;
uint256 constant INIT_OD_AMOUNT = 2230 ether;

// Members for governance
address constant H = 0x37c5B029f9c3691B3d47cb024f84E5E257aEb0BB;

// Data for dexrelayer script (for test)
address constant RELAYER_DATA = 0x98A724ECA2804DE70Cae1f28E070D3973CAdcf05;
// Data for dexrelayer script (for test) and Router for AlgebraPool
address constant RELAYER_DATA = 0x91A35Ad2f71C25eB84A19eD27f12c362920b1DB2;
address constant ROUTER = 0x2a004eA6266eA1A340D1a7D78F1e0F4e9Ae2e685;

// 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
4 changes: 2 additions & 2 deletions script/dexrelayer/CallResult.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.7.6;
import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {IRelayer} from '@interfaces/oracles/IRelayer.sol';
import {ICamelotRelayer} from '@interfaces/oracles/ICamelotRelayer.sol';
import {Data} from '@contracts/for-test/Data.sol';

// BROADCAST
Expand All @@ -16,7 +16,7 @@ import {Data} from '@contracts/for-test/Data.sol';
contract CallResult is Script {
Data public data = Data(RELAYER_DATA);

IRelayer public relayer = data.relayer();
ICamelotRelayer public relayer = data.camelotRelayer();

function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_PK'));
Expand Down
63 changes: 0 additions & 63 deletions script/dexrelayer/DeployOracle.s.sol

This file was deleted.

68 changes: 68 additions & 0 deletions script/dexrelayer/DeployOracles.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {ChainlinkRelayerFactory} from '@contracts/factories/ChainlinkRelayerFactory.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {DenominatedOracleFactory} from '@contracts/factories/DenominatedOracleFactory.sol';
import {ICamelotRelayer} from '@interfaces/oracles/ICamelotRelayer.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {MintableERC20} from '@contracts/for-test/MintableERC20.sol';
import {Data} from '@contracts/for-test/Data.sol';

// TODO test denominated oracle after relayer works
// import {DenominatedOracleFactory} from '@contracts/factories/DenominatedOracleFactory.sol';
// import {ChainlinkRelayerFactory, IChainlinkRelayerFactory} from '@contracts/factories/ChainlinkRelayerFactory.sol';

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

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

contract DeployOracles is Script {
Data public data = Data(RELAYER_DATA);

IBaseOracle public chainlinkEthUSDPriceFeed;
IBaseOracle public camelotRelayer;
IBaseOracle public denominatedOracle;

ChainlinkRelayerFactory public chainlinkRelayerFactory;
CamelotRelayerFactory public camelotRelayerFactory;
DenominatedOracleFactory public denominatedOracleFactory;

function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_PK'));

// deploy oracle factories
deployFactories();

// deploy chainlink relayer
chainlinkEthUSDPriceFeed =
chainlinkRelayerFactory.deployChainlinkRelayer(SEPOLIA_CHAINLINK_ETH_USD_FEED, ORACLE_INTERVAL_TEST);
data.setChainlinkRelayer(address(chainlinkEthUSDPriceFeed));

// deploy camelot relayer
camelotRelayer = camelotRelayerFactory.deployAlgebraRelayer(
SEPOLIA_ALGEBRA_FACTORY, data.tokenA(), data.tokenB(), uint32(ORACLE_INTERVAL_TEST)
);
data.setCamelotRelayer(address(camelotRelayer));

// deploy denominated oracle
denominatedOracle =
denominatedOracleFactory.deployDenominatedOracle(chainlinkEthUSDPriceFeed, camelotRelayer, false);
data.setDenominatedOracle(address(denominatedOracle));

vm.stopBroadcast();
}

/**
* @dev setup functions
*/
function deployFactories() public {
chainlinkRelayerFactory = new ChainlinkRelayerFactory();
camelotRelayerFactory = new CamelotRelayerFactory();
denominatedOracleFactory = new DenominatedOracleFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ pragma solidity 0.7.6;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.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 {RelayerFactory} from '@contracts/factories/RelayerFactory.sol';
import {IRelayer} from '@interfaces/oracles/IRelayer.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {ICamelotRelayer} from '@interfaces/oracles/ICamelotRelayer.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {MintableERC20} from '@contracts/for-test/MintableERC20.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {Router} from '@contracts/for-test/Router.sol';
import {Data} from '@contracts/for-test/Data.sol';

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

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

contract DeployBase is Script {
// Pool & Relayer Factories
contract DeployPool is Script {
// Pool Factory
IAlgebraFactory public algebraFactory = IAlgebraFactory(SEPOLIA_ALGEBRA_FACTORY);

// Router
Expand Down Expand Up @@ -68,22 +69,12 @@ contract DeployBase is Script {
function deployPool() public {
algebraFactory.createPool(data.tokenA(), data.tokenB());
data.setPool(IAlgebraPool(algebraFactory.poolByPair(data.tokenA(), data.tokenB())));
data.pool().initialize(getSqrtPrice(1 ether, 1656.62 ether));
data.pool().initialize(getSqrtPrice(1 ether, 2355 ether));
}

function getSqrtPrice(uint256 _initWethAmount, uint256 _initODAmount) public pure returns (uint160) {
uint256 price = (_initWethAmount * WAD) / _initODAmount;
uint256 sqrtPriceX96 = sqrt(price * WAD) * (2 ** 96);
uint256 sqrtPriceX96 = Sqrt.sqrtAbs(int256(price)) * (2 ** 96);
return uint160(sqrtPriceX96);
}

// TODO test against isomate sqrt function
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;
}
}
}
Loading

0 comments on commit 4410e68

Please sign in to comment.