generateViewFunctionPayloadWithABI function
Generates a payload for a view function call using the provided ABI and arguments. This function is synchronous and works offline with pre-fetched ABIs (no network calls). Does NOT support plain-object struct/enum arguments.
Throws if the type argument count does not match the ABI or if the function arguments do not match the expected parameters defined in the ABI.
Implementation
EntryFunction generateViewFunctionPayloadWithABI(InputViewFunctionData data) {
final functionAbi = data.abi;
if (functionAbi == null) {
throw ArgumentError(
'generateViewFunctionPayloadWithABI requires an ABI on the input data',
);
}
final parts = getFunctionParts(data.function);
// Ensure that all type arguments are typed properly.
final typeArguments = standardizeTypeTags(data.typeArguments);
// Check the type argument count against the ABI.
if (typeArguments.length != functionAbi.typeParameters.length) {
throw ArgumentError(
'Type argument count mismatch, expected '
'${functionAbi.typeParameters.length}, received '
'${typeArguments.length}',
);
}
// Check all BCS types, and convert any non-BCS types.
final functionArguments = data.functionArguments
?.asMap()
.entries
.map((entry) => convertArgument(
data.function,
functionAbi,
entry.value,
entry.key,
typeArguments,
))
.toList() ??
[];
// Check that all arguments are accounted for.
if (functionArguments.length != functionAbi.parameters.length) {
throw ArgumentError(
"Too few arguments for '${parts.moduleAddress}::${parts.moduleName}::"
"${parts.functionName}', expected ${functionAbi.parameters.length} "
'but got ${functionArguments.length}',
);
}
// Generate the entry function payload.
return EntryFunction.build(
'${parts.moduleAddress}::${parts.moduleName}',
parts.functionName,
typeArguments,
functionArguments,
);
}