Class: AbiParser2
Defined in: src/utils/calldata/parser/parser-2.ts:51
The abi parser for an abi that groups its functions under an interface entry.
It is chosen when getAbiVersion answers 2, which is decided by the mere presence of that
entry — the abi of a Cairo V2 contract that declares its functions through a
#[starknet::interface], the usual way since compiler 2, mid-2023. A Cairo V2 contract with no
external function, or that declares its functions one by one with #[external(v0)], has no
such entry, and goes to AbiParser1.
Everything it does differently from AbiParser1 follows from that one nesting : a method
is looked up among the interface's items rather than at the top level, and getLegacyFormat
unwraps those items back into a flat list for the rest of the library. The types are the same
Cairo 1 types, serialized the same way — an abi of this shape stripped of its interface entry
would be parsed by AbiParser1 with the same result.
An abi can hold several interfaces; only the first one is searched for a method.
Example
const abi = [
{
type: 'interface',
name: 'test::IBalance',
items: [
{
type: 'function',
name: 'get_balance',
inputs: [],
outputs: [{ type: 'core::integer::u256' }],
state_mutability: 'view',
},
],
},
];
const parser = new AbiParser2(abi);
const result = parser.getMethod('get_balance')?.name;
// result = "get_balance"
const result2 = parser.getLegacyFormat().length;
// result2 = 1 the interface replaced by the single item it held
Extends
Constructors
Constructor
new AbiParser2(
abi,parsingStrategy?):AbiParser2
Defined in: src/utils/calldata/parser/parser-1.ts:75
Parameters
abi
parsingStrategy?
CairoTypeStrategy | ParsingStrategy
Returns
AbiParser2
Inherited from
Properties
abi
abi:
Abi
Defined in: src/utils/calldata/parser/parser-1.ts:58
Inherited from
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.
Inherited from
Methods
getMethod()
getMethod(
name):FunctionAbi|undefined
Defined in: src/utils/calldata/parser/parser-2.ts:65
Find a method by name, among the items of the abi's interface.
Only the first interface entry is searched, so a method declared in a second one is not
found here.
Parameters
name
string
the method to look for
Returns
FunctionAbi | undefined
the method, or nothing when the interface has no such name
Example
const result = new AbiParser2(abi).getMethod('get_balance')?.name;
// result = "get_balance"
Overrides
getLegacyFormat()
getLegacyFormat():
Abi
Defined in: src/utils/calldata/parser/parser-2.ts:84
The abi as a flat list of entries, each interface replaced by the items it held.
That flat shape is what the rest of the library reads — CallData looks a method up in it —
and it is exactly the shape AbiParser1 is handed to begin with.
Returns
the abi with its interfaces unwrapped
Example
const result = new AbiParser2(abi).getLegacyFormat().length;
// result = 1 the interface replaced by the single item it held
Overrides
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
Inherited from
AbiParser1.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"]
Inherited from
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
Inherited from
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