Skip to main content
Version: 11.x

Class: AbiParser0

Defined in: src/utils/calldata/parser/parser-0.ts:50

The abi parser for Cairo 0 contracts.

It is an exact twin of AbiParser1 at the moment it was split off, and the split is the whole point : a Cairo 0 abi speaks of felt and felt*, types the Cairo type classes do not register, so it stays on the parsers that predate them while Cairo 1 moves over. Everything Cairo 0 needs then sits in one place — this class, requestParser.ts, responseParser.ts and validate.ts — and the day support for it is dropped, that is what gets deleted.

A Cairo 0 abi is recognized by getAbiVersion returning 0, which happens when the first function that has any input or output declares it with a type carrying no ::.

Example

const abi = [
{
type: 'function',
name: 'get_balance',
inputs: [],
outputs: [{ name: 'balance', type: 'felt' }],
stateMutability: 'view',
},
];
const parser = new AbiParser0(abi);
const result = parser.getLegacyFormat() === abi;
// result = true

Implements

Constructors

Constructor

new AbiParser0(abi, parsingStrategy?): AbiParser0

Defined in: src/utils/calldata/parser/parser-0.ts:109

Build a parser for a Cairo 0 abi.

The two strategy shapes share one argument across the three parsers, so this one refuses the other's rather than ignore it : a CairoTypeStrategy registers core:: types, which a Cairo 0 abi never names, and accepting it would do nothing the caller asked for.

Parameters

abi

Abi

the contract's abi

parsingStrategy?

CairoTypeStrategy | ParsingStrategy

how to serialize each type, hdParsingStrategy when not given

Returns

AbiParser0

Throws

when handed a strategy of Cairo type classes

Example

const parser = new AbiParser0(abi);
const result = parser.getMethod('get_balance')?.name;
// result = "get_balance"

Properties

abi

abi: Abi

Defined in: src/utils/calldata/parser/parser-0.ts:59

The abi this parser was built from, untouched.

Example

const result = new AbiParser0(abi).abi.length;
// result = 1

parsingStrategy

parsingStrategy: ParsingStrategy

Defined in: src/utils/calldata/parser/parser-0.ts:69

How each abi type is turned into felts and read back.

Example

const result = new AbiParser0(abi).parsingStrategy === hdParsingStrategy;
// result = true (the default, when none is given)

parsingStrategies

parsingStrategies: CairoTypeStrategy[] = []

Defined in: src/utils/calldata/parser/parser-0.ts:84

The strategies driving the Cairo type classes — none, here.

This parser serializes through requestParser and responseParser, which predate those classes, so there is nothing to carry. Empty rather than absent: the field is on the interface so that reading myCallData.parser.parsingStrategies never has to be guarded, and a Cairo 0 abi has no core:: type a strategy could hold anyway.

Example

const result = new AbiParser0(abi).parsingStrategies;
// result = []

Implementation of

AbiParserInterface.parsingStrategies


structs

protected readonly structs: AbiStructs

Defined in: src/utils/calldata/parser/parser-0.ts:87

The structs the abi declares, read once here rather than at every field.


enums

protected readonly enums: AbiEnums

Defined in: src/utils/calldata/parser/parser-0.ts:90

The enums the abi declares, core::bool excepted — a Cairo 0 abi declares none.

Methods

validateRequestFields()

validateRequestFields(abiMethod, args): void

Defined in: src/utils/calldata/parser/parser-0.ts:141

Check the arguments of a method before any of them is serialized.

This parser does the checking and the serializing in two separate passes, which is why it has something to do here where AbiParser1 has not : its Cairo type classes check as they build, so a pass beforehand would build everything twice.

Parameters

abiMethod

FunctionAbi

the method the arguments are meant for

args

any[]

the arguments, in the abi's order

Returns

void

Throws

when an argument does not fit the type its input declares

Example

const method = {
type: 'function',
name: 'f',
inputs: [{ name: 'x', type: 'felt' }],
outputs: [],
stateMutability: 'view',
};
new AbiParser0(abi).validateRequestFields(method, [1000]); // passes

Implementation of

AbiParserInterface.validateRequestFields


parseRequestField()

parseRequestField(value, input): string | string[]

Defined in: src/utils/calldata/parser/parser-0.ts:161

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

AbiEntry

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 AbiParser0(abi);
const result = parser.parseRequestField(1000, { name: 'x', type: 'felt' });
// result = ["1000"]

Implementation of

AbiParserInterface.parseRequestField


parseResponse()

parseResponse(responseIterator, output, parsedResult?): any

Defined in: src/utils/calldata/parser/parser-0.ts:192

Read one value off a contract response, advancing the iterator past it.

parsedResult is what makes a Cairo 0 array readable : its length is a separate output, ${name}_len, read just before it, and there is nothing in the felts themselves to say where the array stops.

Parameters

responseIterator

Iterator<string>

the response felts, positioned on this value

output

AbiEntry | EventEntry

the abi entry describing what to read

parsedResult?

ParsedStruct | Args

what has been read so far, which is where an array finds its ${name}_len

Returns

any

the value, as the plain JS type the caller reads

Example

const parser = new AbiParser0(abi);
const result = parser.parseResponse(['0x3e8'].values(), { name: 'x', type: 'felt' });
// result = 1000n
const response = ['0x1', '0x2', '0x3'].values();
const result2 = parser.parseResponse(response, { name: 'r', type: 'felt*' }, { r_len: 3n });
// result2 = [1n, 2n, 3n]

Implementation of

AbiParserInterface.parseResponse


getRequestParser()

getRequestParser(abiType): (val) => any

Defined in: src/utils/calldata/parser/parser-0.ts:221

How to serialize a value of this abi type.

Cairo 0's own type names never reach here : parseBaseTypes asks for core::felt252 for everything it does not recognize, which is what a felt ends up being.

Parameters

abiType

string

the abi type to serialize

Returns

the function turning a value into its felts

(val) => any

Throws

when the strategy has no entry for this type

Example

const result = new AbiParser0(abi).getRequestParser('core::felt252')(1000);
// result = ["1000"]

getResponseParser()

getResponseParser(abiType): (responseIterator) => any

Defined in: src/utils/calldata/parser/parser-0.ts:240

How to read a value of this abi type off a response.

Parameters

abiType

string

the abi type to read

Returns

the function reading the value from the response felts

(responseIterator) => any

Throws

when the strategy has no entry for this type

Example

const response = ['0x3e8'].values();
const result = new AbiParser0(abi).getResponseParser('core::felt252')(response);
// result = 1000n

methodInputsLength()

methodInputsLength(abiMethod): number

Defined in: src/utils/calldata/parser/parser-0.ts:270

How many arguments a caller has to provide for this method.

A Cairo 0 array is declared as two inputs, a_len and a, but a caller passes one value : the length is derived from it. So the _len inputs are not counted.

Parameters

abiMethod

FunctionAbi

the method to measure

Returns

number

the number of arguments expected

Example

const method = {
type: 'function',
name: 'sum',
inputs: [
{ name: 'a_len', type: 'felt' },
{ name: 'a', type: 'felt*' },
],
outputs: [],
stateMutability: 'view',
};
const result = new AbiParser0(abi).methodInputsLength(method);
// result = 1 (two inputs declared, one value to pass)

Implementation of

AbiParserInterface.methodInputsLength


getMethod()

getMethod(name): FunctionAbi | undefined

Defined in: src/utils/calldata/parser/parser-0.ts:284

Find a method by name in the abi.

Parameters

name

string

the method to look for

Returns

FunctionAbi | undefined

the method, or nothing when the abi has no such name

Example

const result = new AbiParser0(abi).getMethod('get_balance')?.outputs[0].type;
// result = "felt"

Implementation of

AbiParserInterface.getMethod


getLegacyFormat()

getLegacyFormat(): Abi

Defined in: src/utils/calldata/parser/parser-0.ts:300

The abi as a flat list of entries.

A Cairo 0 abi is already flat — there is no interface entry to unwrap, as there is from Cairo 2 on — so this hands back what it was given.

Returns

Abi

the abi, unchanged

Example

const result = new AbiParser0(abi).getLegacyFormat().length;
// result = 1

Implementation of

AbiParserInterface.getLegacyFormat