-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.5.2 - Long tail assets support with Uniswap v3 TWAP + Chainlink (#153
) * Update coverage [skip ci] * Deploy LINK/USDC to base sepolia * v1.5 testnet redeploy to redo migration * Add SizeFactory and SizeRegistry (#5) * Add SizeFactory * Split factory/registry due to contract size limit * Create SizeRegistryFactoryTest * Add more tests for 2 markets * Test createMarket * Add more tests * Add fork test * Bump * Bump CI * Refactor SizeRegistryFactoryTest * Add isMarket function * Change description to percent instead of BP * Fix fork test * Use ISize and implementation in constructor * Add PriceFeed factory * Fix slither * Change _ by | on descriptions * Fix test * Remove unused code * v1.5 migration with `reinitialize` (#7) * Create migration function * Create NonTransferrableScaledTokenV1_5Test (WIP) * Attempt build * Fix circular dependency * Add Size deployment to SizeRegistry on tests * Pin foundry version * WIP * WIP + compiling * Alternate branch * Fix getUserView * Create withdrawV1_2 function * Do migration in a single reinitialize * Add testFork_ForkReinitializeV1_5_set_2_existing_markets * Create separate tests to check gas usage * Add v1.5 at the end of NonTransferrableScaledTokenV1_5 * Fix _testFork_ForkReinitializeV1_5_migrate * Rollback SizeDataView struct to not break integrations * Fix getMarketsDescriptions * Fix tests * Fix compilation warnings * Fix fork test * Attempt fix slither * fix slither * attempt fix slither * Fix slither * Fix slither * Add more consistency checks * Add more checks to reinitialize fn * Remove blank line * Add new methods * Add more tests * Disable slither calls-loop * Fix checks by using only scaled balances * Fix fork test * Fix CI * Add v1 token to /deprecated folder * Remove unused code * getBorrowATokensDescriptionsV1_5 * Fix README * Increase test coverage * Upgrade SizeView.version to v1.5 * Add withdraw checks * Fix tests * Add comment * Fix NonTransferrableScaledTokenV1_5.allowance * Create deposit/withdraw fork test * Deploy LINK/USDC to base sepolia * Deploy SizeRegistry, SizeFactory to base-sepolia * Update migration procedure * Add audit tests * Add per user checks * Fix recommendations from v1.5 review * Fix error * v1.5 libraries on prepare_crytic * Redeploy SizeFactory to base-sepolia * Update coverage [skip ci] * Deploy SizeFactory to production * Create PriceFeedUniswapV3TWAPChainlink v0 * Improve docs * Clarify docs * Improve docs * Create fork test for VIRTUAL/USDC price feed * Fix failed test * Fix failing test * Create DeployPriceFeedUniswapV3TWAPChainlinkScript * Deploy VIRTUAL/USDC price feed * Add data crunch script --------- Co-authored-by: Size <[email protected]>
- Loading branch information
Showing
21 changed files
with
932 additions
and
16 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734631714.json
Large diffs are not rendered by default.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734631896.json
Large diffs are not rendered by default.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734632195.json
Large diffs are not rendered by default.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734632379.json
Large diffs are not rendered by default.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734632817.json
Large diffs are not rendered by default.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-1734633175.json
Large diffs are not rendered by default.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
broadcast/DeployPriceFeedUniswapV3TWAPChainlink.s.sol/8453/run-latest.json
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
|
||
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; | ||
import {SizeFactory} from "@src/v1.5/SizeFactory.sol"; | ||
import {console2 as console} from "forge-std/Script.sol"; | ||
|
||
import {BaseScript, Deployment, Parameter} from "@script/BaseScript.sol"; | ||
import {Deploy} from "@script/Deploy.sol"; | ||
import {NetworkConfiguration, Networks} from "@script/Networks.sol"; | ||
|
||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
import {PriceFeedParams} from "@src/oracle/v1.5.1/PriceFeed.sol"; | ||
import {PriceFeedUniswapV3TWAPChainlink} from "@src/oracle/v1.5.2/PriceFeedUniswapV3TWAPChainlink.sol"; | ||
|
||
contract DeployPriceFeedUniswapV3TWAPChainlinkScript is BaseScript, Networks, Deploy { | ||
address deployer; | ||
|
||
function setUp() public {} | ||
|
||
modifier parseEnv() { | ||
deployer = vm.envOr("DEPLOYER_ADDRESS", vm.addr(vm.deriveKey(TEST_MNEMONIC, 0))); | ||
_; | ||
} | ||
|
||
function run() public parseEnv broadcast { | ||
console.log("[PriceFeedUniswapV3TWAPChainlink] deploying..."); | ||
|
||
(AggregatorV3Interface sequencerUptimeFeed, PriceFeedParams memory base, PriceFeedParams memory quote) = | ||
priceFeedVirtualUsdcBaseMainnet(); | ||
|
||
PriceFeedUniswapV3TWAPChainlink priceFeedUniswapV3TWAPChainlink = | ||
new PriceFeedUniswapV3TWAPChainlink(sequencerUptimeFeed, base, quote); | ||
|
||
console.log("[PriceFeedUniswapV3TWAPChainlink] priceFeed", address(priceFeedUniswapV3TWAPChainlink)); | ||
|
||
console.log("[PriceFeedUniswapV3TWAPChainlink] done"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
script/GetPriceFeedUniswapV3TWAPChainlinkHistoricalData.s.sol
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,47 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
|
||
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; | ||
import {SizeFactory} from "@src/v1.5/SizeFactory.sol"; | ||
import {console2 as console} from "forge-std/Script.sol"; | ||
|
||
import {BaseScript, Deployment, Parameter} from "@script/BaseScript.sol"; | ||
import {Deploy} from "@script/Deploy.sol"; | ||
import {NetworkConfiguration, Networks} from "@script/Networks.sol"; | ||
|
||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
import {PriceFeed, PriceFeedParams} from "@src/oracle/v1.5.1/PriceFeed.sol"; | ||
|
||
import {UniswapV3PriceFeed} from "@src/oracle/v1.5.1/adapters/UniswapV3PriceFeed.sol"; | ||
import {PriceFeedUniswapV3TWAPChainlink} from "@src/oracle/v1.5.2/PriceFeedUniswapV3TWAPChainlink.sol"; | ||
|
||
contract GetPriceFeedUniswapV3TWAPChainlinkHistoricalDataScript is BaseScript, Networks, Deploy { | ||
function setUp() public {} | ||
|
||
function run() public broadcast { | ||
console.log("GetPriceFeedUniswapV3TWAPChainlinkHistoricalData..."); | ||
|
||
uint256 toBlock = vm.getBlockNumber(); | ||
uint256 fromBlock = 23921785; | ||
uint256 steps = 30; | ||
|
||
vm.rollFork(fromBlock); | ||
|
||
PriceFeedUniswapV3TWAPChainlink priceFeedUniswapV3TWAPChainlink = | ||
PriceFeedUniswapV3TWAPChainlink(0x19960f5ffa579a0573BF9b9D0D3258C34F9f69a1); | ||
UniswapV3PriceFeed uniswapV3PriceFeed = priceFeedUniswapV3TWAPChainlink.basePriceFeed(); | ||
PriceFeed priceFeed = priceFeedUniswapV3TWAPChainlink.quotePriceFeed(); | ||
|
||
console.log("timestamp,base,quote"); | ||
for (uint256 b = fromBlock; b < toBlock; b += steps) { | ||
vm.rollFork(b); | ||
|
||
uint256 base = uniswapV3PriceFeed.getPrice(); | ||
uint256 quote = priceFeed.getPrice(); | ||
console.log("%s,%s,%s", block.timestamp, base, quote); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -31,7 +31,7 @@ import {SetUserConfigurationParams} from "@src/libraries/actions/SetUserConfigur | |
import {ISizeAdmin} from "@src/interfaces/ISizeAdmin.sol"; | ||
import {ISizeV1_5} from "@src/v1.5/interfaces/ISizeV1_5.sol"; | ||
|
||
string constant VERSION = "v1.5.1"; | ||
string constant VERSION = "v1.5.2"; | ||
|
||
/// @title ISize | ||
/// @custom:security-contact [email protected] | ||
|
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,12 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import {IPriceFeed} from "@src/oracle/IPriceFeed.sol"; | ||
|
||
/// @title IPriceFeedV1_5_2 | ||
/// @custom:security-contact [email protected] | ||
/// @author Size (https://size.credit/) | ||
interface IPriceFeedV1_5_2 is IPriceFeed { | ||
/// @notice Returns the description of the price feed | ||
function description() external view returns (string memory); | ||
} |
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,76 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
import {Math} from "@src/libraries/Math.sol"; | ||
|
||
import {IPriceFeed} from "@src/oracle/IPriceFeed.sol"; | ||
|
||
import {PriceFeedParams} from "@src/oracle/v1.5.1/PriceFeed.sol"; | ||
import {PriceFeed} from "@src/oracle/v1.5.1/PriceFeed.sol"; | ||
import {ChainlinkSequencerUptimeFeed} from "@src/oracle/v1.5.1/adapters/ChainlinkSequencerUptimeFeed.sol"; | ||
import {UniswapV3PriceFeed} from "@src/oracle/v1.5.1/adapters/UniswapV3PriceFeed.sol"; | ||
import {IPriceFeedV1_5_2} from "@src/oracle/v1.5.2/IPriceFeedV1_5_2.sol"; | ||
|
||
/// @title PriceFeedUniswapV3TWAPChainlink | ||
/// @custom:security-contact [email protected] | ||
/// @author Size (https://size.credit/) | ||
/// @notice A contract that provides the price of a `base` asset in terms of a `quote` asset, scaled to 18 decimals, | ||
/// using Uniswap V3 for `base` and a IPriceFeedV1_5_1 for `quote` | ||
/// The price is defined as `base * quote` | ||
/// For example, this can be used to calculate the price of an ABC token for the ABC/USDC pair through | ||
/// ABC/WETH via UniswapV3PriceFeed and WETH/USDC via IPriceFeedV1_5_1, ie, ABC/USDC = ABC/WETH * WETH/USDC | ||
/// @dev `decimals` must be 18 to comply with Size contracts | ||
/// `sequencerUptimeFeed` can be null for unsupported networks | ||
/// In case the sequencer is down, `getPrice` reverts (see `ChainlinkSequencerUptimeFeed`) | ||
/// This oracle should only be used for assets that are not supported by Chainlink | ||
contract PriceFeedUniswapV3TWAPChainlink is IPriceFeedV1_5_2 { | ||
/* solhint-disable */ | ||
uint256 public constant decimals = 18; | ||
ChainlinkSequencerUptimeFeed public immutable chainlinkSequencerUptimeFeed; | ||
UniswapV3PriceFeed public immutable basePriceFeed; | ||
PriceFeed public immutable quotePriceFeed; | ||
/* solhint-enable */ | ||
|
||
constructor( | ||
AggregatorV3Interface sequencerUptimeFeed, | ||
PriceFeedParams memory basePriceFeedParams, | ||
PriceFeedParams memory quotePriceFeedParams | ||
) { | ||
chainlinkSequencerUptimeFeed = new ChainlinkSequencerUptimeFeed(sequencerUptimeFeed); | ||
basePriceFeed = new UniswapV3PriceFeed( | ||
decimals, | ||
// other parameters of basePriceFeedParams are unused | ||
basePriceFeedParams.baseToken, | ||
basePriceFeedParams.quoteToken, | ||
basePriceFeedParams.uniswapV3Pool, | ||
basePriceFeedParams.twapWindow, | ||
basePriceFeedParams.averageBlockTime | ||
); | ||
|
||
quotePriceFeed = new PriceFeed(quotePriceFeedParams); // uses 18 decimals | ||
} | ||
|
||
function getPrice() external view override returns (uint256) { | ||
chainlinkSequencerUptimeFeed.validateSequencerIsUp(); | ||
|
||
uint256 basePrice = basePriceFeed.getPrice(); | ||
uint256 quotePrice = quotePriceFeed.getPrice(); | ||
|
||
return Math.mulDivDown(basePrice, quotePrice, 10 ** decimals); | ||
} | ||
|
||
function description() external view override returns (string memory) { | ||
return string.concat( | ||
"PriceFeedUniswapV3TWAPChainlink | (", | ||
basePriceFeed.baseToken().symbol(), | ||
"/", | ||
basePriceFeed.quoteToken().symbol(), | ||
") (Uniswap v3 TWAP) * ((", | ||
quotePriceFeed.base().description(), | ||
") / (", | ||
quotePriceFeed.quote().description(), | ||
")) (PriceFeed)" | ||
); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
test/fork/oracle/ForkPriceFeedUniswapV3TWAPChainlinkTest.t.sol
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,53 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.23; | ||
|
||
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import {ISize} from "@src/interfaces/ISize.sol"; | ||
import {Errors} from "@src/libraries/Errors.sol"; | ||
import {UpdateConfigParams} from "@src/libraries/actions/UpdateConfig.sol"; | ||
import {IPriceFeed} from "@src/oracle/IPriceFeed.sol"; | ||
import {PriceFeedV1_5} from "@src/oracle/deprecated/PriceFeedV1_5.sol"; | ||
import {PriceFeed, PriceFeedParams} from "@src/oracle/v1.5.1/PriceFeed.sol"; | ||
import {BaseTest} from "@test/BaseTest.sol"; | ||
import {ForkTest} from "@test/fork/ForkTest.sol"; | ||
|
||
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; | ||
import {Test} from "forge-std/Test.sol"; | ||
import {console} from "forge-std/console.sol"; | ||
|
||
import {PriceFeedUniswapV3TWAPChainlink} from "@src/oracle/v1.5.2/PriceFeedUniswapV3TWAPChainlink.sol"; | ||
|
||
import {PriceFeedUniswapV3TWAPChainlinkTest} from "@test/local/oracle/PriceFeedUniswapV3TWAPChainlink.t.sol"; | ||
|
||
import {Networks} from "@script/Networks.sol"; | ||
|
||
contract ForkPriceFeedUniswapV3TWAPChainlinkTest is ForkTest, Networks { | ||
PriceFeedUniswapV3TWAPChainlink public priceFeedVirtualToUsdc; | ||
|
||
function setUp() public override(ForkTest) { | ||
super.setUp(); | ||
vm.createSelectFork("base"); | ||
|
||
// 2024-12-19 16h20 | ||
vm.rollFork(23917935); | ||
|
||
(AggregatorV3Interface sequencerUptimeFeed, PriceFeedParams memory base, PriceFeedParams memory quote) = | ||
priceFeedVirtualUsdcBaseMainnet(); | ||
|
||
priceFeedVirtualToUsdc = new PriceFeedUniswapV3TWAPChainlink(sequencerUptimeFeed, base, quote); | ||
} | ||
|
||
function testFork_ForkPriceFeedUniswapV3TWAPChainlink_getPrice() public view { | ||
uint256 price = priceFeedVirtualToUsdc.getPrice(); | ||
assertEqApprox(price, 2.359e18, 0.001e18); | ||
} | ||
|
||
function testFork_PriceFeedUniswapV3TWAPChainlink_description() public view { | ||
assertEq( | ||
priceFeedVirtualToUsdc.description(), | ||
"PriceFeedUniswapV3TWAPChainlink | (VIRTUAL/WETH) (Uniswap v3 TWAP) * ((ETH / USD) / (USDC / USD)) (PriceFeed)" | ||
); | ||
} | ||
} |
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.