|
| 1 | +import { |
| 2 | + Contract, |
| 3 | + decodeRlp, |
| 4 | + encodeRlp, |
| 5 | + ethers, formatEther, |
| 6 | + hexlify, |
| 7 | + JsonRpcProvider, |
| 8 | + keccak256, |
| 9 | + solidityPackedKeccak256, |
| 10 | + toBeHex, toBigInt, |
| 11 | + toQuantity, |
| 12 | + zeroPadValue, |
| 13 | +} from 'ethers'; |
| 14 | +import { RLP } from '@ethereumjs/rlp'; |
| 15 | +import { GetProofResponse, TokenInfo } from './types'; |
| 16 | + |
| 17 | +/** |
| 18 | + * We need to support the MiniMeToken, because of LIDO. |
| 19 | + * See the token contract here: |
| 20 | + * https://github.com/aragon/minime/blob/master/contracts/MiniMeToken.sol#L293-L312 |
| 21 | + */ |
| 22 | +const MiniMeTokenAbi = [ |
| 23 | + 'function name() public view returns (string)', |
| 24 | + 'function symbol() public view returns (string)', |
| 25 | + 'function decimals() public view returns (uint8)', |
| 26 | + 'function totalSupply() public view returns (uint256)', |
| 27 | + `function balanceOfAt(address _owner, uint _blockNumber) public constant returns (uint)` |
| 28 | +]; |
| 29 | + |
| 30 | +const testMiniMeOwner = '0x3e40D73EB977Dc6a537aF587D48316feE66E9C8c' |
| 31 | +const testMiniMeBlock = 21672028 |
| 32 | + |
| 33 | +export async function miniMeTokenDetailsFromProvider( |
| 34 | + addr: string, |
| 35 | + provider: JsonRpcProvider, |
| 36 | +): Promise<TokenInfo> { |
| 37 | + const c = new Contract(addr, MiniMeTokenAbi, provider); |
| 38 | + const network = await provider.getNetwork(); |
| 39 | + // Test the presence of this specific function |
| 40 | + await c.balanceOfAt(testMiniMeOwner, testMiniMeBlock) |
| 41 | + return { |
| 42 | + addr: addr, |
| 43 | + chainId: network.chainId, |
| 44 | + name: await c.name(), |
| 45 | + symbol: await c.symbol(), |
| 46 | + decimals: await c.decimals(), |
| 47 | + totalSupply: await c.totalSupply(), |
| 48 | + type: 'MiniMe', |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +export async function getMiniMeStorageSlot( |
| 53 | + provider: JsonRpcProvider, |
| 54 | + account: string, |
| 55 | + holder: string, |
| 56 | + _isStillFresh: () => boolean = () => true, |
| 57 | + progressCallback?: (progress: string) => void | undefined, |
| 58 | +): Promise<{ |
| 59 | + index: number; |
| 60 | + balance: bigint; |
| 61 | + balanceDecimal?: string; |
| 62 | +} | null> { |
| 63 | + if (progressCallback) progressCallback("Getting latest block..."); |
| 64 | + const block = await provider.getBlock('latest') |
| 65 | + if (!block) throw new Error("Can't get latest block") |
| 66 | + if (progressCallback) progressCallback("Checking balance..."); |
| 67 | + const contract = new Contract(account, MiniMeTokenAbi, provider); |
| 68 | + const balance = await contract.balanceOfAt(holder, block.number) |
| 69 | + return {index: 8, balance, balanceDecimal: formatEther(balance)} |
| 70 | +} |
| 71 | + |
| 72 | +/// Retrieve RLP encoded block header |
| 73 | +export async function getMiniMeBlockHeaderRLP(provider: JsonRpcProvider, wantedBlock: { number?: number; hash?: string}) { |
| 74 | + |
| 75 | + if (!wantedBlock.number && !wantedBlock.hash) throw new Error("Please supply either block number or block hash!") |
| 76 | + |
| 77 | + // Get block data |
| 78 | + const block = wantedBlock.hash |
| 79 | + ? await provider.send('eth_getBlockByHash', [wantedBlock.hash, false]) |
| 80 | + : await provider.send('eth_getBlockByNumber', [toBeHex(wantedBlock.number!), false]) |
| 81 | + |
| 82 | + if (!block) { |
| 83 | + throw new Error(`Block ${JSON.stringify(wantedBlock)} not found`) |
| 84 | + } |
| 85 | + |
| 86 | + const headerArray = [ |
| 87 | + block.parentHash, |
| 88 | + block.sha3Uncles, |
| 89 | + block.miner, |
| 90 | + block.stateRoot, |
| 91 | + block.transactionsRoot, |
| 92 | + block.receiptsRoot, |
| 93 | + block.logsBloom, |
| 94 | + ethers.toBeHex(block.difficulty), |
| 95 | + ethers.toBeHex(block.number), |
| 96 | + ethers.toBeHex(block.gasLimit), |
| 97 | + ethers.toBeHex(block.gasUsed), |
| 98 | + ethers.toBeHex(block.timestamp), |
| 99 | + block.extraData, |
| 100 | + block.mixHash, |
| 101 | + block.nonce, |
| 102 | + ethers.toBeHex(block.baseFeePerGas || 0), |
| 103 | + // Cancun-specific fields |
| 104 | + ethers.toBeHex(block.withdrawalsRoot || '0x'), |
| 105 | + ethers.toBeHex(block.blobGasUsed || 0), |
| 106 | + ethers.toBeHex(block.excessBlobGas || 0), |
| 107 | + ethers.toBeHex(block.parentBeaconBlockRoot || '0x') |
| 108 | + ]; |
| 109 | + |
| 110 | + return hexlify(RLP.encode(headerArray)); |
| 111 | +} |
| 112 | + |
| 113 | +export async function fetchMiniMeAccountProof( |
| 114 | + provider: JsonRpcProvider, |
| 115 | + wantedBlock: { number?: number; hash?: string }, |
| 116 | + address: string, |
| 117 | +) { |
| 118 | + if (!wantedBlock.number && !wantedBlock.hash) throw new Error("Please supply either block number or block hash!") |
| 119 | + const response = (await provider.send('eth_getProof', [ |
| 120 | + address, |
| 121 | + [], |
| 122 | + wantedBlock.hash ?? toQuantity(wantedBlock.number!) |
| 123 | + ])) as GetProofResponse; |
| 124 | + |
| 125 | + return encodeRlp(response.accountProof.map(decodeRlp)); |
| 126 | +} |
| 127 | + |
| 128 | +export async function getMiniMeAccountBalance( |
| 129 | + provider: JsonRpcProvider, |
| 130 | + contractAddress: string, |
| 131 | + accountAddress: string, |
| 132 | + slotNumber: number, |
| 133 | + wantedBlock: { number?: number; hash?: string }, |
| 134 | + withVoteData?: boolean |
| 135 | +): Promise<{ balance: bigint, balanceString?: string, voteData?: string }> { |
| 136 | + if (!wantedBlock.number && !wantedBlock.hash) throw new Error("Please supply either block number or block hash!") |
| 137 | + const baseSlot = solidityPackedKeccak256( |
| 138 | + ['bytes', 'uint256'], |
| 139 | + [zeroPadValue(accountAddress, 32), slotNumber] |
| 140 | + ); |
| 141 | + |
| 142 | + const blockNumber = wantedBlock.number ?? (await provider.getBlock(wantedBlock.hash!))?.number |
| 143 | + if (!blockNumber) { |
| 144 | + console.log("Block not found", wantedBlock.hash); |
| 145 | + return { balance: 0n } |
| 146 | + } |
| 147 | + |
| 148 | + const lengthValue = await provider.send('eth_getStorageAt', [ |
| 149 | + contractAddress, |
| 150 | + baseSlot, |
| 151 | + ethers.toQuantity(blockNumber) |
| 152 | + ]); |
| 153 | + |
| 154 | + if (toBigInt(lengthValue) === 0n) { |
| 155 | + // No checkpoints, no balance |
| 156 | + return { balance: 0n }; |
| 157 | + } |
| 158 | + |
| 159 | + const checkpointValue = await provider.send('eth_getStorageAt', [ |
| 160 | + contractAddress, |
| 161 | + keccak256(baseSlot), |
| 162 | + toQuantity(blockNumber) |
| 163 | + ]); |
| 164 | + const balance = toBigInt(checkpointValue) >> 128n; |
| 165 | + const balanceString = formatEther(balance) |
| 166 | + if (!withVoteData) return { balance, balanceString } |
| 167 | + |
| 168 | + // Generate storage proofs |
| 169 | + const checkpointSlot = keccak256(baseSlot); |
| 170 | + const storageKeys = [baseSlot, checkpointSlot]; |
| 171 | + |
| 172 | + const proof = await provider.send('eth_getProof', [ |
| 173 | + contractAddress, |
| 174 | + storageKeys, |
| 175 | + ethers.toQuantity(blockNumber) |
| 176 | + ]); |
| 177 | + |
| 178 | + // return encodeRlp(response.accountProof.map(decodeRlp)); |
| 179 | + const encodedProofs = proof.storageProof.map((p: any) => |
| 180 | + hexlify(encodeRlp(p.proof.map(decodeRlp))) |
| 181 | + ); |
| 182 | + |
| 183 | + // Encode vote data |
| 184 | + const voteData = ethers.AbiCoder.defaultAbiCoder().encode(['bytes[]'], [encodedProofs]); |
| 185 | + |
| 186 | + return { balance, balanceString, voteData } |
| 187 | +} |
0 commit comments