Created
August 19, 2021 13:56
-
-
Save RealPeha/f6e6f1c0d3d06aad3ec37c8f876b442e to your computer and use it in GitHub Desktop.
Multicall
This file contains hidden or 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
| pragma solidity ^0.8.0; | |
| interface IERC20 { | |
| function name() external view returns (string memory); | |
| function symbol() external view returns (string memory); | |
| function decimals() external view returns (uint8); | |
| function totalSupply() external view returns (uint256); | |
| function balanceOf(address account) external view returns (uint256); | |
| } | |
| interface IPancakePair { | |
| function token0() external view returns (address); | |
| function token1() external view returns (address); | |
| function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast); | |
| } | |
| contract Multicall { | |
| struct TokenInfo { | |
| string name; | |
| string symbol; | |
| uint8 decimals; | |
| uint totalSupply; | |
| uint balance; | |
| } | |
| struct PoolInfo { | |
| address token0; | |
| address token1; | |
| uint112 reserve0; | |
| uint112 reserve1; | |
| } | |
| struct Call { | |
| address target; | |
| bytes callData; | |
| } | |
| function aggregate(Call[] memory calls) public returns (uint256 blockNumber, bytes[] memory returnData) { | |
| blockNumber = block.number; | |
| returnData = new bytes[](calls.length); | |
| for(uint256 i = 0; i < calls.length; i++) { | |
| (bool success, bytes memory ret) = calls[i].target.call(calls[i].callData); | |
| require(success); | |
| returnData[i] = ret; | |
| } | |
| } | |
| function balances(address target, address[] calldata tokens) external view returns (uint[] memory) { | |
| uint numberOfTokens = tokens.length; | |
| uint[] memory balances = new uint[](numberOfTokens); | |
| for (uint i = 0; i < numberOfTokens; i++) { | |
| balances[i] = IERC20(tokens[i]).balanceOf(target); | |
| } | |
| return balances; | |
| } | |
| function tokenInfo(address target, IERC20 token) public view returns (TokenInfo memory) { | |
| return TokenInfo({ | |
| name: token.name(), | |
| symbol: token.symbol(), | |
| decimals: token.decimals(), | |
| totalSupply: token.totalSupply(), | |
| balance: token.balanceOf(target) | |
| }); | |
| } | |
| function tokensInfo(address target, address[] calldata tokens) external view returns (TokenInfo[] memory) { | |
| uint numberOfTokens = tokens.length; | |
| TokenInfo[] memory tokensInfo = new TokenInfo[](numberOfTokens); | |
| for (uint i = 0; i < numberOfTokens; i++) { | |
| tokensInfo[i] = tokenInfo(target, IERC20(tokens[i])); | |
| } | |
| return tokensInfo; | |
| } | |
| function poolInfo(address target, address pairAddress) public view returns ( | |
| TokenInfo memory token0, | |
| TokenInfo memory token1, | |
| TokenInfo memory pair, | |
| PoolInfo memory pool | |
| ) { | |
| IPancakePair pancakePair = IPancakePair(pairAddress); | |
| (uint112 reserve0, uint112 reserve1,) = pancakePair.getReserves(); | |
| address token0Address = pancakePair.token0(); | |
| address token1Address = pancakePair.token1(); | |
| pair = tokenInfo(target, IERC20(pairAddress)); | |
| token0 = tokenInfo(target, IERC20(token0Address)); | |
| token1 = tokenInfo(target, IERC20(token1Address)); | |
| pool = PoolInfo({ | |
| token0: token0Address, | |
| token1: token1Address, | |
| reserve0: reserve0, | |
| reserve1: reserve1 | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment