Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tuple return value typescript decoding #8319

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion yarn-project/foundation/src/abi/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ export interface BasicType<T extends string> {
/**
* A variable type.
*/
export type AbiType = BasicType<'field'> | BasicType<'boolean'> | IntegerType | ArrayType | StringType | StructType;
export type AbiType =
| BasicType<'field'>
| BasicType<'boolean'>
| IntegerType
| ArrayType
| StringType
| StructType
| TupleType;

type Sign = 'unsigned' | 'signed';

Expand Down Expand Up @@ -116,6 +123,16 @@ export interface ArrayType extends BasicType<'array'> {
type: AbiType;
}

/**
* A tuple type.
*/
export interface TupleType extends BasicType<'tuple'> {
/**
* The types of the tuple elements.
*/
fields: AbiType[];
}

/**
* A string type.
*/
Expand Down
7 changes: 7 additions & 0 deletions yarn-project/foundation/src/abi/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class ReturnValuesDecoder {
}
return array;
}
case 'tuple': {
const array = [];
for (const tupleAbiType of abiType.fields) {
array.push(this.decodeReturn(tupleAbiType));
}
return array;
}
default:
throw new Error(`Unsupported type: ${abiType}`);
}
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/foundation/src/abi/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ArgumentEncoder {
return abiType.length * ArgumentEncoder.typeSize(abiType.type);
case 'struct':
return abiType.fields.reduce((acc, field) => acc + ArgumentEncoder.typeSize(field.type), 0);
case 'tuple':
return abiType.fields.reduce((acc, field) => acc + ArgumentEncoder.typeSize(field), 0);
default: {
const exhaustiveCheck: never = abiType;
throw new Error(`Unhandled abi type: ${exhaustiveCheck}`);
Expand Down
Loading