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

Add datasource for Safe Decoder Service #2313

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Jan 29, 2025

Summary

The new Safe Decoder Service improves upon the decoder of the Safe Transaction Service. As such, we intend to migrate to it.

This lays the foundation of usage, by creating a datasource (with manager) and base repository for validating the responses.

Changes

  • Add safeDataDecoder.baseUri (test) configuration
  • Add DataDecoderApiManager interface, implementation and module
  • Add DataDecoderApi interface, implementation and module
  • Add validation schemas for DataDecoded and `Contract, with inferred types and builders
  • Add base DataDecoderRepository for accessing/validating datasource responses
  • Add appropriate test coverage of service and schemas

@iamacook iamacook self-assigned this Jan 29, 2025
@iamacook iamacook linked an issue Jan 29, 2025 that may be closed by this pull request
Base automatically changed from version-data-decoded to main January 29, 2025 16:08
@iamacook iamacook force-pushed the safe-decoder-datasource branch from ef803f7 to 4b4cb62 Compare January 29, 2025 16:09
Comment on lines +6 to +26
export const ProjectSchema = z.object({
description: z.string(),
logo_file: z.string().url(),
});

export const AbiSchema = z.object({
// We could use abitype here, but we don't consume the ABI/it would increase entity complexity
abi_json: z.array(z.record(z.unknown())),
abi_hash: HexSchema,
modified: z.coerce.date(),
});

export const ContractSchema = z.object({
address: AddressSchema,
name: z.string(),
display_name: z.string().nullable(),
chain_id: z.number().transform(String),
project: ProjectSchema.nullable(),
abi: AbiSchema,
modified: z.coerce.date(),
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Uxio0 has confirmed that this (and decoded data) will be changed to camel case, hence leaving this as a draft.

Comment on lines +11 to +53
export const MultisendSchema = z.object({
operation: z.nativeEnum(Operation),
value: NumericStringSchema,
data_decoded: z.lazy(() => BaseDataDecodedSchema.nullable()),
to: AddressSchema,
data: HexSchema.nullable(),
});

export const ValueDecodedSchema = z.union([
z.array(z.lazy(() => MultisendSchema)),
z.lazy(() => BaseDataDecodedSchema),
]);

export const ParameterSchema = z.object({
name: z.string(),
type: z.string(),
value: HexSchema,
value_decoded: ValueDecodedSchema.nullable(),
});

export const BaseDataDecodedSchemaShape = {
method: z.string(),
parameters: z.array(z.lazy(() => ParameterSchema)),
};

// We need explicitly define ZodType due to recursion
export const BaseDataDecodedSchema: z.ZodType<{
method: string;
parameters: Array<z.infer<typeof ParameterSchema>>;
}> = z.lazy(() => z.object(BaseDataDecodedSchemaShape));

export const Accuracy = [
'FULL_MATCH', // Matched contract and chain ID
'PARTIAL_MATCH', // Matched contract
'ONLY_FUNCTION_MATCH', // Matched function from another contract
'NO_MATCH', // Selector cannot be decoded
] as const;

export const DataDecodedSchema = z.lazy(() =>
z.object(BaseDataDecodedSchemaShape).extend({
accuracy: z.enum([...Accuracy, 'UNKNOWN']).catch('UNKNOWN'),
}),
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before re. snake- to camelcase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add/migrate to Safe Decoder Service
1 participant