Class: AbiParser1
Defined in: src/utils/calldata/parser/parser-1.ts:57
The abi parser for a flat abi written in Cairo 1 vocabulary.
It is chosen when getAbiVersion answers 1 : the abi has no interface entry, and the first
function or constructor carrying any input or output declares it with a type containing ::.
Three different things land here.
The first is a genuine Cairo V1 contract (compiler 1.0 / 1.1), compiled before the abi
format grew its interface entry — roughly 2023. Those contracts are still on chain and still
callable.
The second is a Cairo V2 contract with no interface entry : one with no external function
(see __mocks__/cairo/onlyConstructor), or one that declares its functions one by one with
#[external(v0)]. Compiled by Cairo V2, its abi is flat all the same.
The third, and by far the most frequent, is an abi someone wrote or trimmed by hand : a
couple of functions kept for a decodeParameters call, a fixture in a test suite. Such an abi
is flat whatever the Cairo version of the contract it came from — so a Cairo V2 contract whose
abi has been stripped of its interface entry is parsed here, not by AbiParser2. The
class is picked on the shape of the abi, never on the contract behind it, which is why this
one stays busy rather than being a legacy path.
What separates it from AbiParser2 is only that shape : where to find a method, and how to flatten the abi. Both speak the same Cairo 1 types, and both are meant to serialize them the same way. AbiParser0 is the one that is genuinely apart.
Example
const abi = [
{
type: 'function',
name: 'get_balance',
inputs: [],
outputs: [{ type: 'core::integer::u256' }],
state_mutability: 'view',
},
];
const parser = new AbiParser1(abi);
const result = parser.getMethod('get_balance')?.name;
// result = "get_balance"
Extended by
Implements
Constructors
Constructor
new AbiParser1(
abi,parsingStrategy?):AbiParser1
Defined in: src/utils/calldata/parser/parser-1.ts:75
Parameters
abi
parsingStrategy?
CairoTypeStrategy | ParsingStrategy
Returns
AbiParser1
Properties
abi
abi:
Abi
Defined in: src/utils/calldata/parser/parser-1.ts:58
parsingStrategies
parsingStrategies:
CairoTypeStrategy[]
Defined in: src/utils/calldata/parser/parser-1.ts:73
The strategies driving the Cairo type classes, searched in order.
The language's own types come first, then the contract's : a struct and a custom enum have no shape to recognize, their abi type being the name the contract chose, so each is registered under that exact name in a strategy built from the abi that declares it.
Implementation of
AbiParserInterface.parsingStrategies
Methods
validateRequestFields()
validateRequestFields(
_abiMethod,_args):void
Defined in: src/utils/calldata/parser/parser-1.ts:126
Check the arguments of a method before any of them is serialized — which here is nothing.
The Cairo type classes refuse what does not fit as they build it, so the check and the serialization are the same pass. Running one beforehand would build every argument twice and raise on the first of the two, with the same message.
CallData.validate, whose whole purpose is to check without producing calldata, does so by
compiling and discarding the result.
Parameters
_abiMethod
_args
any[]
Returns
void
Example
const method = {
type: 'function',
name: 'f',
inputs: [{ name: 'x', type: 'core::integer::u8' }],
outputs: [],
state_mutability: 'view',
};
new AbiParser1(abi).validateRequestFields(method, [300]);
// returns, although 300 does not fit a u8: it is `compile` that refuses it
Implementation of
AbiParserInterface.validateRequestFields
parseRequestField()
parseRequestField(
value,input):string|string[]
Defined in: src/utils/calldata/parser/parser-1.ts:144
Serialize one argument to the felts a contract call carries.
The abi entry is passed whole rather than just its type, because the error messages name the
field : ABI expected parameter tokens to be array, got abc.
Parameters
value
unknown
the argument to serialize
input
the abi entry this argument stands for
Returns
string | string[]
the felts, one string for a single-felt type
Throws
when the value does not fit the declared type
Example
const parser = new AbiParser1(abi);
const result = parser.parseRequestField(44, { name: 'x', type: 'core::integer::u8' });
// result = ["44"]
Implementation of
AbiParserInterface.parseRequestField
parseResponse()
parseResponse(
responseIterator,output):any
Defined in: src/utils/calldata/parser/parser-1.ts:168
Read one value off a contract response, advancing the iterator past it.
The parsedResult the interface allows is not taken : it exists for a Cairo 0 array, whose
length is a separate output read just before it. A Cairo 1 array carries its own.
Parameters
responseIterator
Iterator<string>
the response felts, positioned on this value
output
the abi entry describing what to read
Returns
any
the value, as the plain JS type the caller reads
Example
const parser = new AbiParser1(abi);
const response = ['0x2c'].values();
const result = parser.parseResponse(response, { name: 'x', type: 'core::integer::u8' });
// result = 44n
Implementation of
AbiParserInterface.parseResponse
methodInputsLength()
methodInputsLength(
abiMethod):number
Defined in: src/utils/calldata/parser/parser-1.ts:211
How many arguments a caller has to provide for this method.
Every input is one argument. The _len inputs a Cairo 0 array is declared with used to be
discounted here, back when this class served a Cairo 0 abi too; AbiParser0 took that
over when it was split off, and a Cairo 1 array is one input carrying its own length.
Parameters
abiMethod
the method to measure
Returns
number
the number of arguments expected
Example
const method = {
type: 'function',
name: 'transfer',
inputs: [
{ name: 'to', type: 'core::starknet::contract_address::ContractAddress' },
{ name: 'amount', type: 'core::integer::u256' },
],
outputs: [],
state_mutability: 'external',
};
const result = new AbiParser1(abi).methodInputsLength(method);
// result = 2
Implementation of
AbiParserInterface.methodInputsLength
getMethod()
getMethod(
name):FunctionAbi|undefined
Defined in: src/utils/calldata/parser/parser-1.ts:220
get method definition from abi
Parameters
name
string
string
Returns
FunctionAbi | undefined
FunctionAbi | undefined
Implementation of
getLegacyFormat()
getLegacyFormat():
Abi
Defined in: src/utils/calldata/parser/parser-1.ts:228
Get Abi in legacy format
Returns
Abi