Skip to content

Commit

Permalink
Merge branch 'dev' into edit-relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
daopunk committed Jan 2, 2024
2 parents fcf1922 + 55a6640 commit a9467bf
Show file tree
Hide file tree
Showing 21 changed files with 3,915 additions and 132 deletions.
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.

55 changes: 51 additions & 4 deletions script/Common.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +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 {
CamelotRelayerFactory public camelotRelayerFactory = CamelotRelayerFactory(CAMELOT_RELAYER_FACTORY);
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(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'));
_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);
}
}
}
7 changes: 5 additions & 2 deletions script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ 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 = 0x9a8E0E6611EC9ebd5A2c8168A2f808e0206d85E4; // from pre-deployment
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 {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
import {IRelayer} from '@interfaces/oracles/IRelayer.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;
}
}
}
82 changes: 82 additions & 0 deletions script/dexrelayer/GetPrice.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;
pragma abicoder v2;

import '@script/Registry.s.sol';
import {Script} from 'forge-std/Script.sol';
import {Test} from 'forge-std/Test.sol';
import {SafeMath} from '@openzeppelin/contracts/math/SafeMath.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {IChainlinkRelayer} from '@interfaces/oracles/IChainlinkRelayer.sol';
import {ICamelotRelayer} from '@interfaces/oracles/ICamelotRelayer.sol';
import {IDenominatedOracle} from '@interfaces/oracles/IDenominatedOracle.sol';
import {IBaseOracle} from '@interfaces/oracles/IBaseOracle.sol';
import {Data} from '@contracts/for-test/Data.sol';

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

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

contract GetPrice is Script, Test {
Data public data = Data(RELAYER_DATA);

// -- Constants --
uint256 constant INIT_WETH = 1 ether;
uint256 constant INIT_OD = 2355 ether;

// Tokens
address public tokenA = data.tokenA();
address public tokenB = data.tokenB();

// Pool
IAlgebraPool public pool = data.pool();
uint256 public initPrice = ((INIT_WETH * WAD) / INIT_OD);

// Relayers
IBaseOracle public chainlinkRelayer = IBaseOracle(address(data.chainlinkRelayer()));
IBaseOracle public camelotRelayer = IBaseOracle(address(data.camelotRelayer()));
IBaseOracle public denominatedOracle = IBaseOracle(address(data.denominatedOracle()));

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

poolPrice();
chainlinkRelayerPrice();
camelotRelayerPrice();
denominatedOraclePrice();
}

function poolPrice() public {
IAlgebraPool _pool = IAlgebraPool(pool);
(uint160 _sqrtPriceX96,,,,,,) = _pool.globalState();

emit log_named_uint('sqrtPriceX96', _sqrtPriceX96);

uint256 _price = (SafeMath.div(uint256(_sqrtPriceX96), (2 ** 96))) ** 2;
assertApproxEqAbs(initPrice, _price, 100_000_000); // 0.000000000100000000 variability

emit log_named_uint('Price from LPool', _price); // 0.000424628419232196 ether
emit log_named_uint('Price Calculated', (INIT_WETH * WAD) / INIT_OD); // 0.000424628450106157 ether
}

function chainlinkRelayerPrice() public {
uint256 _result = chainlinkRelayer.read();
emit log_named_uint('Chainlink ETH/USD', _result); // 2382270000000000000000 (w/ 18 decimal = 2382.270...)

assertApproxEqAbs(INIT_OD / 1e18, _result / 1e18, 500); // $500 flex for
}

function camelotRelayerPrice() public {
uint256 _result = camelotRelayer.read();
emit log_named_uint('Camelot OD/WETH', _result); // 424620063704448204165193502948931 (w/ 36 decimal = 0.000424...)

assertApproxEqAbs(initPrice, _result / 1e18, 10_000_000_000); // 0.000000001000000000 variability
}

function denominatedOraclePrice() public {
uint256 _result = denominatedOracle.read(); // 1008432145061984058301035060743127269 (w/ 36 decimal = 1.008...)
emit log_named_uint('SystemOracle OD/USD', _result);
}
}
Loading

0 comments on commit a9467bf

Please sign in to comment.