-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ePendle and Pendle address, scaffold UniswapV2 oracle factory
- Loading branch information
1 parent
d6d772a
commit cc6ab66
Showing
8 changed files
with
200 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters