Oracle: Move query methods to linked library #749
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This reduces the bytecode size of MetaStablePools enough that they can be deployed. With 200 runs, they end up at 23.919kB.
The way bytecode is reduced is by removing all oracle query functionality outside of the pool contract itself, and into a linked library. This adds a slight overhead of ~3k gas (2600 from the
delegatecall
), but gets rid of the binary search code and exponentiation (fromfromLowResLog
).Ideally, we'd be able to deploy the library at construction time in either the Pool or the Factory. Unfortunately, Solidity does not allow for this (though I'll create a language proposal soon with this idea). Some available options are:
delegatecall
is not consideredview
) - essentially implementing a kind of proxy pattern. This could work, but it requires a way to keep the storage slots of the base and library contracts in syncview
calls back into the main contract for storage reads. Even if these repeated calls only cost 100 gas, the overhead of call encoding, decoding, function dispatch, return value encoding and decoding quickly adds up.In the end, I went with 1), as this is the most 'standard' approach. I modified the
deploy
function to allow for libraries, which are manually linked. Unfortunately, Hardhat does not support linking an artifact directly (see NomicFoundation/hardhat#1716), so this will have to make do for now.This PR is split into two commits. The first moves functionality from the pool and oracle into the
QueryProcessor
library, while the second moves the query tests from the pools into the pool oracle, which is where they are implemented. No funtionality should change as a result of this.