Skip to content

Commit fd2bdc0

Browse files
committed
fix: allow structs in calldata
1 parent 2e05902 commit fd2bdc0

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/contract.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { Abi, AbiEntry, FunctionAbi, StructAbi } from './types';
66
import { BigNumberish, toBN } from './utils/number';
77
import { getSelectorFromName } from './utils/stark';
88

9-
export type Args = { [inputName: string]: string | string[] };
9+
export type Args = {
10+
[inputName: string]: string | string[] | { type: 'struct'; [k: string]: string };
11+
};
1012
export type Calldata = string[];
1113

1214
function parseFelt(candidate: string): BN {
@@ -30,6 +32,10 @@ export function compileCalldata(args: Args): Calldata {
3032
return Object.values(args).flatMap((value) => {
3133
if (Array.isArray(value))
3234
return [toBN(value.length).toString(), ...value.map((x) => toBN(x).toString())];
35+
if (typeof value === 'object' && 'type' in value)
36+
return Object.entries(value)
37+
.filter(([k]) => k !== 'struct')
38+
.map(([, v]) => toBN(v).toString());
3339
return toBN(value).toString();
3440
});
3541
}
@@ -88,19 +94,16 @@ export class Contract {
8894
(abi) => abi.name === method && abi.type === 'function'
8995
) as FunctionAbi;
9096
methodAbi.inputs.forEach((input) => {
91-
if (args[input.name] !== undefined) {
97+
const arg = args[input.name];
98+
if (arg !== undefined) {
9299
if (input.type === 'felt') {
93-
assert(
94-
typeof args[input.name] === 'string',
95-
`arg ${input.name} should be a felt (string)`
96-
);
97-
assert(
98-
isFelt(args[input.name] as string),
99-
`arg ${input.name} should be decimal or hexadecimal`
100-
);
100+
assert(typeof arg === 'string', `arg ${input.name} should be a felt (string)`);
101+
assert(isFelt(arg as string), `arg ${input.name} should be decimal or hexadecimal`);
102+
} else if (typeof arg === 'object' && 'type' in arg) {
103+
assert(arg.type === 'struct', `arg ${input.name} should be a struct`);
101104
} else {
102-
assert(Array.isArray(args[input.name]), `arg ${input.name} should be a felt* (string[])`);
103-
(args[input.name] as string[]).forEach((felt, i) => {
105+
assert(Array.isArray(arg), `arg ${input.name} should be a felt* (string[])`);
106+
(arg as string[]).forEach((felt, i) => {
104107
assert(
105108
typeof felt === 'string',
106109
`arg ${input.name}[${i}] should be a felt (string) as part of a felt* (string[])`

0 commit comments

Comments
 (0)