-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/revert-swap-from-paymaster
- Loading branch information
Showing
8 changed files
with
743 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.17; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "../../token/oracles/IOracleAggregator.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import "hardhat/console.sol"; | ||
|
||
/** | ||
* @title Mock Oracle Aggregator contract | ||
* @notice DO NOT use this in any environment for use | ||
*/ | ||
contract MockChainlinkOracleAggregator is Ownable, IOracleAggregator { | ||
struct TokenInfo { | ||
/* Number of decimals represents the precision of the price returned by the feed. For example, | ||
a price of $100.50 might be represented as 100500000000 in the contract, with 9 decimal places | ||
of precision */ | ||
uint8 decimals; | ||
// uint8 tokenDecimals; | ||
bool dataSigned; | ||
address callAddress; | ||
bytes callData; | ||
} | ||
|
||
mapping(address => TokenInfo) internal tokensInfo; | ||
|
||
constructor(address _owner) { | ||
_transferOwnership(_owner); | ||
} | ||
|
||
/** | ||
* @dev set price feed information for specific feed | ||
* @param callAddress price feed / derived price feed address to call | ||
* @param decimals decimals (precision) defined in this price feed | ||
* @param callData function selector which will be used to query price data | ||
* @param signed if the feed may return result as signed integrer | ||
*/ | ||
function setTokenOracle( | ||
address token, | ||
address callAddress, | ||
uint8 decimals, | ||
bytes calldata callData, | ||
bool signed | ||
) external onlyOwner { | ||
require( | ||
callAddress != address(0), | ||
"ChainlinkOracleAggregator:: call address can not be zero" | ||
); | ||
require( | ||
token != address(0), | ||
"ChainlinkOracleAggregator:: token address can not be zero" | ||
); | ||
tokensInfo[token].callAddress = callAddress; | ||
tokensInfo[token].decimals = decimals; | ||
tokensInfo[token].callData = callData; | ||
tokensInfo[token].dataSigned = signed; | ||
} | ||
|
||
/** | ||
* @dev query deciamls used by set feed for specific token | ||
* @param token ERC20 token address | ||
*/ | ||
function getTokenOracleDecimals( | ||
address token | ||
) external view returns (uint8 _tokenOracleDecimals) { | ||
_tokenOracleDecimals = tokensInfo[token].decimals; | ||
} | ||
|
||
/** | ||
* @dev query price feed | ||
* @param token ERC20 token address | ||
*/ | ||
function getTokenPrice( | ||
address token | ||
) external view returns (uint256 tokenPrice) { | ||
// usually token / native (depends on price feed) | ||
tokenPrice = _getTokenPrice(token); | ||
} | ||
|
||
/** | ||
* @dev exchangeRate : each aggregator implements this method based on how it sources the quote/price | ||
* @notice here it is token / native sourced from chainlink so in order to get defined exchangeRate we inverse the feed | ||
* @param token ERC20 token address | ||
*/ | ||
function getTokenValueOfOneNativeToken( | ||
address token | ||
) external view virtual returns (uint256 exchangeRate) { | ||
// we'd actually want eth / token | ||
uint256 tokenPriceUnadjusted = _getTokenPrice(token); | ||
uint8 _tokenOracleDecimals = tokensInfo[token].decimals; | ||
exchangeRate = | ||
((10 ** _tokenOracleDecimals) * | ||
(10 ** IERC20Metadata(token).decimals())) / | ||
tokenPriceUnadjusted; | ||
} | ||
|
||
// Making explicit revert or make use of stale price feed which reverts | ||
// like done in below function and the test case | ||
/*function _getTokenPrice( | ||
address token | ||
) internal view returns (uint256 tokenPriceUnadjusted) { | ||
bool success = false; | ||
require(success, "ChainlinkOracleAggregator:: query failed"); | ||
}*/ | ||
|
||
function _getTokenPrice( | ||
address token | ||
) internal view returns (uint256 tokenPriceUnadjusted) { | ||
(bool success, bytes memory ret) = tokensInfo[token] | ||
.callAddress | ||
.staticcall(tokensInfo[token].callData); | ||
console.log("price feed reverted here? success = %s ", success); | ||
require(success, "ChainlinkOracleAggregator:: query failed"); | ||
if (tokensInfo[token].dataSigned) { | ||
tokenPriceUnadjusted = uint256(abi.decode(ret, (int256))); | ||
} else { | ||
tokenPriceUnadjusted = abi.decode(ret, (uint256)); | ||
} | ||
} | ||
} |
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,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.17; | ||
|
||
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
error WrongPrice(); | ||
|
||
//@review againsnt chainlink reference PriceConverter https://docs.chain.link/docs/get-the-latest-price/ | ||
//@review decimals for individual feeds | ||
contract MockStalePriceFeed { | ||
AggregatorV3Interface internal priceFeed1; | ||
AggregatorV3Interface internal priceFeed2; | ||
|
||
constructor() { | ||
// todo // do not hard code // polygon values | ||
priceFeed1 = AggregatorV3Interface( | ||
0xAB594600376Ec9fD91F8e885dADF0CE036862dE0 | ||
); // matic usd | ||
priceFeed2 = AggregatorV3Interface( | ||
0xfE4A8cc5b5B2366C1B58Bea3858e81843581b2F7 | ||
); // usdc usd | ||
} | ||
|
||
function decimals() public view returns (uint8) { | ||
return 18; | ||
} | ||
|
||
function getThePrice() public view returns (int) { | ||
// Always using decimals 18 for derived price feeds | ||
revert WrongPrice(); | ||
} | ||
} |
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
Oops, something went wrong.