Skip to content

Instantly share code, notes, and snippets.

@ikachko
Created August 29, 2021 16:38
Show Gist options
  • Save ikachko/15848dde66a9893dd506c3190060f560 to your computer and use it in GitHub Desktop.
Save ikachko/15848dde66a9893dd506c3190060f560 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
import "contracts/IUniswapV2Factory.sol";
import "contracts/IUniswapV2Pair.sol";
import "contracts/IERC20.sol";
contract DexScanner {
struct Reserves {
string symbol;
uint112 reserve0;
uint112 reserve1;
}
constructor() public {}
function scanAllReserves(address factoryAddress) public view returns (Reserves[] memory){
IUniswapV2Factory factory = IUniswapV2Factory(factoryAddress);
uint256 pairsLength = factory.allPairsLength();
Reserves[] memory reserves = new Reserves[](pairsLength);
for (uint256 i = 0; i < pairsLength; i++) {
IUniswapV2Pair pair = IUniswapV2Pair(factory.allPairs(i));
(uint112 reserve0, uint112 reserve1,) = pair.getReserves();
(address token0address, address token1address) = (pair.token0(), pair.token1());
string memory token0symbol;
string memory token1symbol;
try IERC20(token0address).symbol() returns (string memory symbol0) {
token0symbol = symbol0;
} catch {
token0symbol = "X";
}
try IERC20(token1address).symbol() returns (string memory symbol1) {
token1symbol = symbol1;
} catch {
token1symbol = "X";
}
string memory pairSymbol = append(token0symbol, token1symbol, "-");
reserves[i] = Reserves(pairSymbol, reserve0, reserve1);
}
return reserves;
}
function append(string memory a, string memory b, string memory delimiter) internal pure returns (string memory) {
return string(abi.encodePacked(a, delimiter, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment