|
| 1 | +import { num, RpcProvider, shortString, transaction, uint256 } from 'starknet'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Parses the raw result from a Starknet function call based on its ABI. |
| 5 | + * It handles different types like felt252, u8, u256, etc., and decodes them accordingly. |
| 6 | + * @param rawResult - The raw result from the Starknet function call. |
| 7 | + * @param functionAbi - The ABI of the function that was called. |
| 8 | + * @returns The parsed result in a more usable format. |
| 9 | + */ |
| 10 | +function parseStarknetResult(rawResult: string[], functionAbi: any): any { |
| 11 | + if ( |
| 12 | + !functionAbi || |
| 13 | + !functionAbi.outputs || |
| 14 | + !Array.isArray(rawResult) || |
| 15 | + rawResult.length === 0 |
| 16 | + ) { |
| 17 | + return rawResult; |
| 18 | + } |
| 19 | + |
| 20 | + const output = functionAbi.outputs[0]; |
| 21 | + const rawValue = rawResult[0]; |
| 22 | + |
| 23 | + try { |
| 24 | + switch (output.type) { |
| 25 | + case 'core::felt252': |
| 26 | + // Try to decode as shortString (for name, symbol) |
| 27 | + try { |
| 28 | + return shortString.decodeShortString(rawValue); |
| 29 | + } catch { |
| 30 | + // If shortString decode fails, return as hex |
| 31 | + return rawValue; |
| 32 | + } |
| 33 | + |
| 34 | + // Unsigned integers |
| 35 | + case 'core::integer::u8': |
| 36 | + case 'core::integer::u16': |
| 37 | + case 'core::integer::u32': |
| 38 | + case 'core::integer::u64': |
| 39 | + return parseInt(rawValue, 16); |
| 40 | + |
| 41 | + case 'core::integer::u128': |
| 42 | + case 'core::integer::usize': |
| 43 | + return BigInt(rawValue).toString(); |
| 44 | + |
| 45 | + case 'core::integer::u256': |
| 46 | + return uint256.uint256ToBN({ |
| 47 | + low: rawValue, |
| 48 | + high: rawResult[1] || '0x0' |
| 49 | + }); |
| 50 | + |
| 51 | + // Signed integers |
| 52 | + case 'core::integer::i8': |
| 53 | + case 'core::integer::i16': |
| 54 | + case 'core::integer::i32': |
| 55 | + case 'core::integer::i64': |
| 56 | + return parseInt(rawValue, 16); |
| 57 | + |
| 58 | + case 'core::integer::i128': |
| 59 | + return BigInt(rawValue).toString(); |
| 60 | + |
| 61 | + // Boolean type |
| 62 | + case 'core::bool': |
| 63 | + return rawValue === '0x1' || rawValue === '0x01'; |
| 64 | + |
| 65 | + // Address types |
| 66 | + case 'core::starknet::contract_address::ContractAddress': |
| 67 | + case 'core::starknet::class_hash::ClassHash': |
| 68 | + case 'core::starknet::storage_access::StorageAddress': |
| 69 | + return rawValue; |
| 70 | + |
| 71 | + // Byte array |
| 72 | + case 'core::bytes_31::bytes31': |
| 73 | + return rawValue; |
| 74 | + |
| 75 | + default: |
| 76 | + // Return raw value for unknown types |
| 77 | + return rawValue; |
| 78 | + } |
| 79 | + } catch { |
| 80 | + // Fallback to raw result if parsing fails |
| 81 | + return rawResult; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Partitions the responses from a Starknet multicall into individual call results. |
| 87 | + * Each response starts with its length, followed by the actual response data. |
| 88 | + * @param responses - The array of responses from the Starknet multicall. |
| 89 | + * @returns An array of arrays, where each inner array contains the response data for a single call. |
| 90 | + */ |
| 91 | +const partitionResponses = (responses: string[]): string[][] => { |
| 92 | + if (responses.length === 0) { |
| 93 | + return []; |
| 94 | + } |
| 95 | + |
| 96 | + const [responseLength, ...restResponses] = responses; |
| 97 | + const responseLengthInt = Number(num.toBigInt(responseLength)); |
| 98 | + const response = restResponses.slice(0, responseLengthInt); |
| 99 | + const remainingResponses = restResponses.slice(responseLengthInt); |
| 100 | + |
| 101 | + return [response, ...partitionResponses(remainingResponses)]; |
| 102 | +}; |
| 103 | + |
| 104 | +export default async function multicall( |
| 105 | + address: string, |
| 106 | + provider: RpcProvider, |
| 107 | + abi: any[], |
| 108 | + calls: any[], |
| 109 | + limit: number, |
| 110 | + options: Record<string, any> = {} |
| 111 | +) { |
| 112 | + const callData = calls.map((call) => { |
| 113 | + return { |
| 114 | + contractAddress: call[0], |
| 115 | + entrypoint: call[1], |
| 116 | + calldata: call[2] || [] |
| 117 | + }; |
| 118 | + }); |
| 119 | + |
| 120 | + // Chunk calls into batches based on limit |
| 121 | + const chunks: any[][] = []; |
| 122 | + for (let i = 0; i < callData.length; i += limit) { |
| 123 | + chunks.push(callData.slice(i, i + limit)); |
| 124 | + } |
| 125 | + |
| 126 | + // Process each chunk |
| 127 | + const paginatedResults = await Promise.all( |
| 128 | + chunks.map((chunk) => |
| 129 | + provider.callContract( |
| 130 | + { |
| 131 | + contractAddress: address, |
| 132 | + entrypoint: 'aggregate', |
| 133 | + calldata: transaction.fromCallsToExecuteCalldata(chunk) |
| 134 | + }, |
| 135 | + options.blockTag ?? 'latest' |
| 136 | + ) |
| 137 | + ) |
| 138 | + ); |
| 139 | + |
| 140 | + const callResults = paginatedResults |
| 141 | + .map((callContractResult) => { |
| 142 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 143 | + const [_blockNumber, _totalLength, ...results] = callContractResult; |
| 144 | + return partitionResponses(results); |
| 145 | + }) |
| 146 | + .flat(); |
| 147 | + |
| 148 | + return callResults.map((result, index) => { |
| 149 | + const [, functionName] = calls[index]; |
| 150 | + const functionAbi = abi.find((item) => item.name === functionName); |
| 151 | + |
| 152 | + return [parseStarknetResult(result, functionAbi)]; |
| 153 | + }); |
| 154 | +} |
0 commit comments