Skip to content

Commit

Permalink
feat: extract parser from CallData and Cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni authored and PhilippeR26 committed Jul 11, 2023
1 parent 2cb2848 commit 6e25aac
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/utils/calldata/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-plusplus */
import {
Abi,
AbiEntry,
AbiStructs,
Args,
ArgsOrCalldata,
Expand All @@ -19,6 +18,7 @@ import { getSelectorFromName } from '../selector';
import { isLongText, splitLongString } from '../shortString';
import { felt, isLen } from './cairo';
import formatter from './formatter';
import { AbiParser } from './parser';
import orderPropsByAbi from './propertyOrder';
import { parseCalldataField } from './requestParser';
import responseParser from './responseParser';
Expand All @@ -29,11 +29,14 @@ export * as cairo from './cairo';
export class CallData {
abi: Abi;

parser: AbiParser;

protected readonly structs: AbiStructs;

constructor(abi: Abi) {
this.abi = abi;
this.structs = CallData.getAbiStruct(abi);
this.parser = new AbiParser(abi);
}

/**
Expand Down Expand Up @@ -66,7 +69,7 @@ export class CallData {
) as FunctionAbi;

// validate arguments length
const inputsLength = CallData.abiInputsLength(abiMethod.inputs);
const inputsLength = this.parser.methodInputsLength(abiMethod);
if (args.length !== inputsLength) {
throw Error(
`Invalid number of arguments, expected ${inputsLength} arguments, but got ${args.length}`
Expand Down Expand Up @@ -194,15 +197,6 @@ export class CallData {
return formatter(parsed, format);
}

/**
* Helper to calculate inputs from abi
* @param inputs AbiEntry
* @returns number
*/
static abiInputsLength(inputs: AbiEntry[]) {
return inputs.reduce((acc, input) => (!isLen(input.name) ? acc + 1 : acc), 0);
}

/**
* Helper to extract structs from abi
* @param abi Abi
Expand Down

0 comments on commit 6e25aac

Please sign in to comment.