-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from diwu1989/registryHelper
Introduce registry helper contracts to speed up pair discovery & bulk pair refresh
- Loading branch information
Showing
7 changed files
with
232 additions
and
15 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
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 | ||
); | ||
} | ||
} |
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,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; | ||
} | ||
|
||
function findPairs( | ||
IUniswapV2Factory factory, | ||
uint offset, | ||
uint limit | ||
) 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++) { | ||
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); | ||
} | ||
} | ||
} |
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 @@ | ||
{"address":"0xe9579Fc54Be093972b44aA7E12b130a2D9B1b6af"} |
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