-
Notifications
You must be signed in to change notification settings - Fork 5
readContracts using batchCall #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| import type { Abi } from '../strk-types/abi.js'; | ||
|
|
||
| import type { Uint512 } from '../strk-types/lib.js'; | ||
| import type { Uint256 } from '../strk-types/lib.js'; | ||
| import type { | ||
| AbiType, | ||
| Address, | ||
| } from './starkweb-abi.js'; | ||
| // import type { testAbi } from './testabi.js'; | ||
|
|
||
| export type CairoInt = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | ||
|
|
||
|
|
||
| export type CairoInt = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'u512' | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -24,17 +24,18 @@ export type AbiParameterKind = 'inputs' | 'outputs'; | |
| */ | ||
| interface PrimitiveTypeLookup { | ||
| 'core::felt252': { inputs: 'felt252'; outputs: 'felt252' }; | ||
| 'core::integer::u8': { inputs: 'u8'; outputs: 'u8' }; | ||
| 'core::integer::u16': { inputs: 'u16'; outputs: 'u16' }; | ||
| 'core::integer::u32': { inputs: 'u32'; outputs: 'u32' }; | ||
| 'core::integer::u64': { inputs: 'u64'; outputs: 'u64' }; | ||
| 'core::integer::u128': { inputs: 'u128'; outputs: 'u128' }; | ||
| 'core::integer::u256': { inputs: 'u256'; outputs: 'u256' }; | ||
| 'core::integer::u8': { inputs: number; outputs: number }; | ||
| 'core::integer::u16': { inputs: number; outputs: number }; | ||
| 'core::integer::u32': { inputs: number; outputs: number }; | ||
| 'core::integer::u64': { inputs: number; outputs: number }; | ||
| 'core::integer::u128': { inputs: number; outputs: number }; | ||
| 'core::integer::u256': { inputs: Uint256; outputs: Uint256 }; | ||
| 'core::integer::u512': { inputs: Uint512; outputs: Uint512 }; | ||
| 'core::array::Array<T>': { inputs: 'T[]'; outputs: 'T[]' }; | ||
| 'core::bool': { inputs: 'boolean'; outputs: 'boolean' }; | ||
| 'core::starknet::contract_address::ContractAddress': { inputs: 'contract_address'; outputs: 'contract_address' }; | ||
| 'core::string::String': { inputs: 'string'; outputs: 'string' }; | ||
| 'core::starknet::class_hash::ClassHash': { inputs: 'string'; outputs: 'string' }; | ||
| 'core::bool': { inputs: boolean; outputs: boolean }; | ||
| 'core::starknet::contract_address::ContractAddress': { inputs: Address; outputs: Address }; | ||
| 'core::string::String': { inputs: string; outputs: string }; | ||
| 'core::starknet::class_hash::ClassHash': { inputs: string; outputs: string }; | ||
| tuple: Record<string, unknown>; | ||
| } | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,8 @@ export async function readContract< | |
| parameters as ReadContractParameters<TAbi, TFunctionName> | ||
| const calldata: string[] = args ? compile(args as any) : [] | ||
|
|
||
| // Simplified block_id determination | ||
|
|
||
| const txCall: CallParameters = { | ||
| contract_address: address, | ||
| entry_point_selector: getSelectorFromName(functionName), | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,14 @@ import type { Transport } from '../../clients/transports/createTransport.js' | |
| import { calldataToHex, compile } from '../../strk-utils/calldata/compile.js' | ||
| import { getSelectorFromName } from '../../strk-utils/hash/selector.js' | ||
| import type { Chain } from '../../types/chain.js' | ||
| import { call } from './call.js' | ||
| import { call, type CallParameters } from './call.js' | ||
| import type { | ||
| PrimaryReadContractParameters, | ||
| SecondaryReadContractParameters, | ||
| } from './readContract.js' | ||
| import type { ContractFunctionName } from '../../types/contract.js' | ||
| import { createBatchScheduler } from '../../utils/promise/createBatchScheduler.js' | ||
| import { decodeFunctionCall } from '../../abi/output.js' | ||
|
|
||
| export type ReadContractsParameters< | ||
| abi extends Abi | readonly unknown[] = Abi, | ||
|
|
@@ -37,21 +39,46 @@ export async function readContracts< | |
| ): Promise<ReadContractsReturnTypes | ReadContractsErrorType> { | ||
| const { contracts, blockHash, blockNumber, blockTag } = parameters as ReadContractsParameters | ||
|
|
||
| const txCallsPromise = contracts.map((callParams) => { | ||
| if (contracts.length === 0) return [] | ||
|
|
||
| // Prepare all call requests | ||
| const callRequests = contracts.map((callParams) => { | ||
| const { address, functionName, args } = callParams | ||
| const calldata: string[] = args ? compile(args as any) : [] | ||
|
|
||
| const txCall = { | ||
| return { | ||
| contract_address: address, | ||
| entry_point_selector: getSelectorFromName(functionName), | ||
| calldata: calldataToHex(calldata), | ||
| block_hash: blockHash, | ||
| block_number: blockNumber, | ||
| block_tag: blockTag, | ||
| } | ||
|
|
||
| return call(client, txCall) | ||
| }) | ||
|
|
||
| return Promise.all(txCallsPromise) | ||
| // Create a batch scheduler for processing calls | ||
| const { schedule } = createBatchScheduler({ | ||
| id: 'readContracts', | ||
| fn: async (requests) => { | ||
| // Process all requests in a batch | ||
| const results = await Promise.all( | ||
| requests.map(request => call(client, request as CallParameters)) | ||
| ) | ||
| return results | ||
| } | ||
| }) | ||
| // Schedule all calls and collect results | ||
| const results = await Promise.all( | ||
| callRequests.map((request, index) => { | ||
| const contract = contracts[index]; | ||
| return schedule(request).then(([result]) => { | ||
| return decodeFunctionCall( | ||
| result as unknown as string[], | ||
| contract?.functionName, | ||
| contract?.abi as any | ||
| ); | ||
| }); | ||
| }) | ||
| ) | ||
| return results | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ export async function getStarknetIdName< | |
| client: Client<Transport, TChain, TAccount>, | ||
| { address }: GetStarknetIdNameParameters, | ||
| ): Promise<GetStarknetIdNameReturnType> { | ||
| const chainId = client.chain?.id as unknown as StarknetChainId | ||
| const chainId = client.chain?.chain_id as unknown as StarknetChainId | ||
| if (!chainId) { | ||
| throw new Error('Chain ID is required') | ||
| } | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -499,9 +499,10 @@ export { isBoolean, isNumber, isObject, isString, isUndefined } from '../strk-ut | |
| export { validateTypedData as snValidateTypedData, encodeData, encodeType, encodeValue } from '../strk-utils/typedData.js' | ||
|
|
||
| export { CairoFelt } from '../strk-utils/cairoDataTypes/felt.js' | ||
| export { CairoUint256 } from '../strk-utils/cairoDataTypes/uint256.js' | ||
| export { createUint256 } from '../strk-utils/cairoDataTypes/uint256.js' | ||
| export { toUint512 } from '../strk-utils/cairoDataTypes/uint512.js' | ||
| export { byteArrayFromString } from '../strk-utils/calldata/byteArray.js' | ||
| export { felt, uint256 } from '../strk-utils/calldata/cairo.js' | ||
| export { felt, uint256, uint512 } from '../strk-utils/calldata/cairo.js' | ||
| export { compile, calldataToHex } from '../strk-utils/calldata/compile.js' | ||
| export { CairoCustomEnum } from '../strk-utils/calldata/enum/CairoCustomEnum.js' | ||
| export { CairoOption, CairoOptionVariant } from '../strk-utils/calldata/enum/CairoOption.js' | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,37 +3,45 @@ import { isHex, isStringWholeNumber } from '../num.js' | |
| import { encodeShortString, isShortString, isText } from '../shortString.js' | ||
| import { isBigInt, isBoolean, isString } from '../typed.js' | ||
|
|
||
|
|
||
| /** | ||
| * Converts a BigNumberish value to a Cairo felt representation in string format | ||
| * @param it The value to convert to a felt | ||
| * @returns A string representation of the felt value | ||
| * @throws Error if the value cannot be converted to a felt | ||
| */ | ||
| export function CairoFelt(it: BigNumberish): string { | ||
| // BN or number | ||
| // Handle BigInt or integer numbers | ||
| if (isBigInt(it) || Number.isInteger(it)) { | ||
| return it.toString() | ||
| } | ||
|
|
||
| // Handling strings | ||
| // Handle string types | ||
| if (isString(it)) { | ||
| // Hex strings | ||
| // Convert hex strings | ||
| if (isHex(it)) { | ||
| return BigInt(it).toString() | ||
| } | ||
| // Text strings that must be short | ||
| // Handle text strings (must be short strings) | ||
| if (isText(it)) { | ||
| if (!isShortString(it)) { | ||
| throw new Error( | ||
| `${it} is a long string > 31 chars. Please split it into an array of short strings.`, | ||
| ) | ||
| } | ||
| // Assuming encodeShortString returns a hex representation of the string | ||
| // Convert short strings to felt | ||
| return BigInt(encodeShortString(it)).toString() | ||
| } | ||
| // Whole numeric strings | ||
| // Handle whole number strings | ||
| if (isStringWholeNumber(it)) { | ||
| return it | ||
| } | ||
| } | ||
| // bool to felt | ||
| // Handle boolean values | ||
| if (isBoolean(it)) { | ||
| return `${+it}` | ||
| } | ||
|
|
||
| // Error for values that cannot be converted | ||
| throw new Error(`${it} can't be computed by felt()`) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Copilot Autofix
AI 12 months ago
To fix the problem, we need to remove the unused import
getStarknetIdNamefrom the import statement on line 13. This will clean up the code and remove any confusion about unused imports.getStarknetIdNamefrom the import statement.