Skip to content

Commit

Permalink
reduce price read by factor 1e18
Browse files Browse the repository at this point in the history
  • Loading branch information
daopunk committed Jan 4, 2024
1 parent b1b539d commit 9d217ff
Show file tree
Hide file tree
Showing 11 changed files with 410 additions and 405 deletions.
527 changes: 207 additions & 320 deletions broadcast/SetupPostEnvironment.s.sol/421614/run-latest.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions script/Registry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ address constant SEPOLIA_SYSTEM_COIN = 0x94beB5fC16824338Eaa538c3c857D7f7fFf4B2C
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;
Expand All @@ -20,18 +19,18 @@ uint256 constant INIT_OD_AMOUNT = 2230 ether;
address constant H = 0x37c5B029f9c3691B3d47cb024f84E5E257aEb0BB;

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

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

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

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

// Chainlink feeds
address constant SEPOLIA_CHAINLINK_ETH_USD_FEED = 0xd30e2101a97dcbAeBCBC04F14C3f624E67A35165;
Expand Down
22 changes: 22 additions & 0 deletions script/dexrelayer/DeployDataLog.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 {Data} from '@contracts/for-test/Data.sol';

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

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

contract DeployDataLog is Script, Test {
function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_PK'));
Data data = new Data();
emit log_named_address('Data:', address(data));
}
}
43 changes: 43 additions & 0 deletions script/dexrelayer/DeployOracleFactories.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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';
import {IAuthorizable} from '@interfaces/utils/IAuthorizable.sol';
import {Data} from '@contracts/for-test/Data.sol';

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

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

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

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

/**
* @dev CamelotRelayerFactory must be deployed by deployer of protocol
*/
function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_DEPLOYER_PK'));
chainlinkRelayerFactory = new ChainlinkRelayerFactory();
camelotRelayerFactory = new CamelotRelayerFactory();
denominatedOracleFactory = new DenominatedOracleFactory();

IAuthorizable(address(chainlinkRelayerFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));
IAuthorizable(address(camelotRelayerFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));
IAuthorizable(address(denominatedOracleFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));

data.modifyFactory(bytes32('chainlinkRelayerFactory'), address(chainlinkRelayerFactory));
data.modifyFactory(bytes32('camelotRelayerFactory'), address(camelotRelayerFactory));
data.modifyFactory(bytes32('denominatedOracleFactory'), address(denominatedOracleFactory));
vm.stopBroadcast();
}
}
39 changes: 28 additions & 11 deletions script/dexrelayer/DeployPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 {IAlgebraFactory} from '@algebra-core/interfaces/IAlgebraFactory.sol';
import {IAlgebraPool} from '@algebra-core/interfaces/IAlgebraPool.sol';
import {CamelotRelayerFactory} from '@contracts/factories/CamelotRelayerFactory.sol';
Expand All @@ -13,6 +14,7 @@ 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';
import {Common} from '@script/Common.s.sol';

// BROADCAST
// source .env && forge script DeployPool --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC --broadcast --verify --etherscan-api-key $ARB_ETHERSCAN_API_KEY
Expand All @@ -21,19 +23,17 @@ import {Data} from '@contracts/for-test/Data.sol';
// source .env && forge script DeployPool --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC

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

// Pool Factory
IAlgebraFactory public algebraFactory = IAlgebraFactory(SEPOLIA_ALGEBRA_FACTORY);

// Router
Router public router;

Data public data;

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

data = new Data();

deployTestTokens();
deployPool();

Expand All @@ -59,8 +59,8 @@ contract DeployPool is Script {
* @dev setup functions
*/
function deployTestTokens() public {
MintableERC20 token0 = new MintableERC20('LST Test1', 'LST1', 18);
MintableERC20 token1 = new MintableERC20('LST Test2', 'LST2', 18);
MintableERC20 token0 = new MintableERC20('Open Dollar', 'OD', 18);
MintableERC20 token1 = new MintableERC20('Wrapped ETH', 'WETH', 18);
token0.mint(H, MINT_AMOUNT);
token1.mint(H, MINT_AMOUNT);
data.setTokens(address(token0), address(token1));
Expand All @@ -69,12 +69,29 @@ contract DeployPool 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, 2355 ether));
uint160 _sqrtPriceX96 = initialPrice(INIT_OD_AMOUNT, INIT_WETH_AMOUNT, address(data.pool()));
data.pool().initialize(_sqrtPriceX96);
}

function getSqrtPrice(uint256 _initWethAmount, uint256 _initODAmount) public pure returns (uint160) {
uint256 price = (_initWethAmount * WAD) / _initODAmount;
uint256 sqrtPriceX96 = Sqrt.sqrtAbs(int256(price)) * (2 ** 96);
return uint160(sqrtPriceX96);
// 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 == data.tokenA()) {
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));
}
}
44 changes: 23 additions & 21 deletions script/dexrelayer/GetPrice.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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';
import {DataStorageLibrary} from '@algebra-periphery/libraries/DataStorageLibrary.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
Expand All @@ -22,17 +23,13 @@ import {Data} from '@contracts/for-test/Data.sol';
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);
uint256 public initPrice = ((INIT_WETH_AMOUNT * WAD) / INIT_OD_AMOUNT);

// Relayers
IBaseOracle public chainlinkRelayer = IBaseOracle(address(data.chainlinkRelayer()));
Expand All @@ -43,40 +40,45 @@ contract GetPrice is Script, Test {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_PK'));

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

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

emit log_named_uint('sqrtPriceX96', _sqrtPriceX96);
emit log_named_uint('Sq Root Price X96', _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
emit log_named_uint('Price from L-Pool', _price);
emit log_named_uint('Price Calculated', (INIT_WETH_AMOUNT * WAD) / INIT_OD_AMOUNT);
}

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

assertApproxEqAbs(initPrice, _result / 1e18, 10_000_000_000); // 0.000000001000000000 variability
function chainlinkRelayerPrice() public {
uint256 _result = chainlinkRelayer.read();
emit log_named_uint('Chainlink ETH/USD', _result);
}

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

/**
* == Logs ==
* Sq Root Price X96: 1677749592786826637668640749594345472
* Price from L-Pool: 448430472335329
* Price Calculated: 448430493273542
* Camelot OD/WETH: 448402863189474 ($0.0004484)
* Chainlink ETH/USD: 2217140000000000000000 ($2217.1400000)
* SystOracle OD/USD: 994171924091910384 ($0.9941719)
*/
72 changes: 72 additions & 0 deletions script/dexrelayer/Read.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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';
import {DataStorageLibrary} from '@algebra-periphery/libraries/DataStorageLibrary.sol';

// BROADCAST
// source .env && forge script Read --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 Read --with-gas-price 2000000000 -vvvvv --rpc-url $ARB_SEPOLIA_RPC

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

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

// Pool
IAlgebraPool public pool = data.pool();
uint256 public initPrice = ((INIT_WETH_AMOUNT * WAD) / INIT_OD_AMOUNT);

// 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'));
readPrice();
readPriceInverse();
}

function readPrice() public {
int24 _arithmeticMeanTick = DataStorageLibrary.consult(address(data.pool()), uint32(ORACLE_INTERVAL_TEST));
uint256 _quoteAmount = DataStorageLibrary.getQuoteAtTick({
tick: _arithmeticMeanTick,
baseAmount: 1e18,
baseToken: data.tokenA(),
quoteToken: data.tokenB()
});
emit log_named_uint('Quote Base A:', _quoteAmount);
}

function readPriceInverse() public {
int24 _arithmeticMeanTick = DataStorageLibrary.consult(address(data.pool()), uint32(ORACLE_INTERVAL_TEST));
uint256 _quoteAmount = DataStorageLibrary.getQuoteAtTick({
tick: _arithmeticMeanTick,
baseAmount: 1e18,
baseToken: data.tokenB(),
quoteToken: data.tokenA()
});
emit log_named_uint('Quote Base B:', _quoteAmount);
}
}

/**
* == Logs ==
* Quote Base A:: 2230
* Quote Base B:: 448402863189474895173495757900703
*/
2 changes: 1 addition & 1 deletion script/postdeployment/SetupPostEnvironment.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract MockSetupPostEnvironment is Common {
address _pool = algebraFactory.poolByPair(SEPOLIA_SYSTEM_COIN, address(mockWeth));

uint160 _sqrtPriceX96 = initialPrice(INIT_OD_AMOUNT, INIT_WETH_AMOUNT, _pool);
IAlgebraPool(_pool).initialize(uint160(_sqrtPriceX96));
IAlgebraPool(_pool).initialize(_sqrtPriceX96);

IBaseOracle _odWethOracle = camelotRelayerFactory.deployAlgebraRelayer(
SEPOLIA_ALGEBRA_FACTORY, SEPOLIA_SYSTEM_COIN, address(mockWeth), uint32(ORACLE_INTERVAL_TEST)
Expand Down
35 changes: 0 additions & 35 deletions script/predeployment/DeployFactories.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ 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';
import {IAuthorizable} from '@interfaces/utils/IAuthorizable.sol';
import {Data} from '@contracts/for-test/Data.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
Expand All @@ -31,36 +29,3 @@ contract DeployFactories is Script {
vm.stopBroadcast();
}
}

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

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

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

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

/**
* @dev CamelotRelayerFactory must be deployed by deployer of protocol
*/
function run() public {
vm.startBroadcast(vm.envUint('ARB_SEPOLIA_DEPLOYER_PK'));
chainlinkRelayerFactory = new ChainlinkRelayerFactory();
camelotRelayerFactory = new CamelotRelayerFactory();
denominatedOracleFactory = new DenominatedOracleFactory();

IAuthorizable(address(chainlinkRelayerFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));
IAuthorizable(address(camelotRelayerFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));
IAuthorizable(address(denominatedOracleFactory)).addAuthorization(vm.envAddress('ARB_SEPOLIA_PC'));

data.modifyFactory(bytes32('chainlinkRelayerFactory'), address(chainlinkRelayerFactory));
data.modifyFactory(bytes32('camelotRelayerFactory'), address(camelotRelayerFactory));
data.modifyFactory(bytes32('denominatedOracleFactory'), address(denominatedOracleFactory));
vm.stopBroadcast();
}
}
Loading

0 comments on commit 9d217ff

Please sign in to comment.