-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce registry helper contracts to speed up pair discovery & bulk pair refresh #31
Changes from all commits
e6eea13
8506928
3bb25d2
4066e51
5b9ec7e
36aed31
d7cbf2c
113a5bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.6.8; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "../interfaces/balancer/IBPool.sol"; | ||
import "../interfaces/balancer/IBRegistry.sol"; | ||
|
||
contract RegistryHelperBalancer { | ||
|
||
struct TokenState { | ||
address token; | ||
uint balance; | ||
uint denormalizedWeight; | ||
} | ||
|
||
struct PoolInfo { | ||
IBPool pool; | ||
uint swapFee; | ||
TokenState[] tokenStates; | ||
} | ||
|
||
function findPools( | ||
IBRegistry registry, | ||
address[] calldata fromTokens, | ||
address[] calldata toTokens | ||
) external view returns (PoolInfo[] memory result) { | ||
require(fromTokens.length == toTokens.length, | ||
"fromTokens and toTokens must be of equal length"); | ||
|
||
IBPool[] memory foundPools = new IBPool[](fromTokens.length * 5); | ||
uint found = 0; | ||
|
||
for (uint i = 0; i < fromTokens.length; i++) { | ||
// only take up the best 5 pools for a particular pair | ||
address[] memory pools = | ||
registry.getBestPoolsWithLimit(fromTokens[i], toTokens[i], 5); | ||
for (uint j = 0; j < pools.length; j++) { | ||
IBPool pool = IBPool(pools[j]); | ||
if (!pool.isFinalized()) { | ||
continue; | ||
} | ||
|
||
bool addPool = true; | ||
for (uint k = 0; k < found; k++) { | ||
if (foundPools[k] == pool) { | ||
// already seen this pool, skip | ||
addPool = false; | ||
break; | ||
} | ||
} | ||
if (addPool) { | ||
// add this newly found pool | ||
foundPools[found++] = pool; | ||
} | ||
} | ||
} | ||
|
||
result = new PoolInfo[](found); | ||
for (uint i = 0; i < found; i++) { | ||
IBPool pool = foundPools[i]; | ||
result[i] = this.getPoolInfo(pool); | ||
} | ||
} | ||
|
||
function refreshPools( | ||
IBPool[] calldata pools | ||
) external view returns (PoolInfo[] memory result) { | ||
result = new PoolInfo[](pools.length); | ||
for (uint i = 0; i < pools.length; i++) { | ||
result[i] = this.getPoolInfo(pools[i]); | ||
} | ||
} | ||
|
||
function getPoolInfo(IBPool pool) external view returns (PoolInfo memory result) { | ||
address[] memory poolTokens = pool.getCurrentTokens(); | ||
TokenState[] memory tokenStates = new TokenState[](poolTokens.length); | ||
// collect information about all of the tokens in the pool | ||
for (uint j = 0; j < poolTokens.length; j++) { | ||
address token = poolTokens[j]; | ||
tokenStates[j] = TokenState( | ||
token, | ||
pool.getBalance(token), | ||
pool.getDenormalizedWeight(token) | ||
); | ||
} | ||
|
||
result = PoolInfo( | ||
pool, | ||
pool.getSwapFee(), | ||
tokenStates | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.6.8; | ||
pragma experimental ABIEncoderV2; | ||
|
||
import "../interfaces/uniswap/IUniswapV2Pair.sol"; | ||
import "../interfaces/uniswap/IUniswapV2Factory.sol"; | ||
|
||
contract RegistryHelperUniswapV2 { | ||
|
||
struct PairState { | ||
uint reserve0; | ||
uint reserve1; | ||
} | ||
|
||
struct PairInfo { | ||
IUniswapV2Pair pair; | ||
address token0; | ||
address token1; | ||
PairState state; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initial Pair information for initialization, address of pair, tokens, and the initial state |
||
|
||
function findPairs( | ||
IUniswapV2Factory factory, | ||
uint offset, | ||
uint limit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needed to introduce The safe limit I've found is to fetch in chunks of 100. |
||
) external view returns (PairInfo[] memory result) { | ||
uint allPairsUpTo = factory.allPairsLength(); | ||
|
||
if (allPairsUpTo > offset + limit) { | ||
// limit the number of pairs returned | ||
allPairsUpTo = offset + limit; | ||
} else if (allPairsUpTo < offset) { | ||
// there are no more pairs | ||
return result; | ||
} | ||
|
||
// allocate a buffer array with the upper bound of the number of pairs returned | ||
result = new PairInfo[](allPairsUpTo - offset); | ||
for (uint i = offset; i < allPairsUpTo; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. loop through starting from the given |
||
IUniswapV2Pair uniPair = IUniswapV2Pair(factory.allPairs(i)); | ||
address token0 = uniPair.token0(); | ||
address token1 = uniPair.token1(); | ||
(uint reserve0, uint reserve1, ) = uniPair.getReserves(); | ||
result[i - offset] = PairInfo(uniPair, token0, token1, PairState(reserve0, reserve1)); | ||
} | ||
} | ||
|
||
function refreshPairs( | ||
IUniswapV2Pair[] calldata pairs | ||
) external view returns (PairState[] memory result) { | ||
result = new PairState[](pairs.length); | ||
for (uint i = 0; i < pairs.length; i++) { | ||
(uint reserve0, uint reserve1, ) = pairs[i].getReserves(); | ||
result[i] = PairState(reserve0, reserve1); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"address":"0xe9579Fc54Be093972b44aA7E12b130a2D9B1b6af"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reserve information for the pair state information we need for
refresh()