-
Notifications
You must be signed in to change notification settings - Fork 422
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
refactor: remove mailbox from warp config #5312
base: main
Are you sure you want to change the base?
Changes from all commits
64e6074
e559490
da36297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,58 +101,86 @@ export const HypTokenRouterConfigSchema = HypTokenConfigSchema.and( | |
); | ||
export type HypTokenRouterConfig = z.infer<typeof HypTokenRouterConfigSchema>; | ||
|
||
export const WarpRouteDeployConfigSchema = z | ||
.record(HypTokenRouterConfigSchema) | ||
.refine((configMap) => { | ||
const entries = Object.entries(configMap); | ||
return ( | ||
entries.some( | ||
([_, config]) => | ||
isCollateralTokenConfig(config) || | ||
isCollateralRebaseTokenConfig(config) || | ||
isNativeTokenConfig(config), | ||
) || entries.every(([_, config]) => isTokenMetadata(config)) | ||
); | ||
}, WarpRouteDeployConfigSchemaErrors.NO_SYNTHETIC_ONLY) | ||
.transform((warpRouteDeployConfig, ctx) => { | ||
const collateralRebaseEntry = Object.entries(warpRouteDeployConfig).find( | ||
([_, config]) => isCollateralRebaseTokenConfig(config), | ||
); | ||
|
||
const syntheticRebaseEntry = Object.entries(warpRouteDeployConfig).find( | ||
([_, config]) => isSyntheticRebaseTokenConfig(config), | ||
); | ||
|
||
// Require both collateral rebase and synthetic rebase to be present in the config | ||
if (!collateralRebaseEntry && !syntheticRebaseEntry) { | ||
// Pass through for other token types | ||
return warpRouteDeployConfig; | ||
} | ||
|
||
if ( | ||
collateralRebaseEntry && | ||
isCollateralRebasePairedCorrectly(warpRouteDeployConfig) | ||
) { | ||
const collateralChainName = collateralRebaseEntry[0]; | ||
return objMap(warpRouteDeployConfig, (_, config) => { | ||
if (config.type === TokenType.syntheticRebase) | ||
config.collateralChainName = collateralChainName; | ||
return config; | ||
}) as Record<string, HypTokenRouterConfig>; | ||
} | ||
|
||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: WarpRouteDeployConfigSchemaErrors.ONLY_SYNTHETIC_REBASE, | ||
export const HypTokenRouterConfigWithoutMailboxSchema = | ||
HypTokenConfigSchema.and(GasRouterConfigSchema.omit({ mailbox: true })); | ||
|
||
export type HypTokenRouterConfigWithoutMailbox = z.infer< | ||
typeof HypTokenRouterConfigWithoutMailboxSchema | ||
>; | ||
|
||
export const WarpRouteDeployConfigSchemaWithoutMailbox = z | ||
.record(HypTokenRouterConfigWithoutMailboxSchema) | ||
.refine(refineTokens, WarpRouteDeployConfigSchemaErrors.NO_SYNTHETIC_ONLY) | ||
.transform((config, ctx) => transformRebaseConfig(config, ctx)); | ||
|
||
export type WarpRouteDeployConfigWithoutMailbox = z.infer< | ||
typeof WarpRouteDeployConfigSchemaWithoutMailbox | ||
>; | ||
|
||
function refineTokens< | ||
T extends HypTokenRouterConfig | HypTokenRouterConfigWithoutMailbox, | ||
>(configMap: Record<string, T>): boolean { | ||
const entries = Object.entries(configMap); | ||
return ( | ||
entries.some( | ||
([_, config]) => | ||
isCollateralTokenConfig(config) || | ||
isCollateralRebaseTokenConfig(config) || | ||
isNativeTokenConfig(config), | ||
) || entries.every(([_, config]) => isTokenMetadata(config)) | ||
); | ||
} | ||
|
||
function transformRebaseConfig< | ||
T extends HypTokenRouterConfig | HypTokenRouterConfigWithoutMailbox, | ||
>( | ||
warpRouteDeployConfig: Record<string, T>, | ||
ctx: z.RefinementCtx, | ||
): Record<string, T> | typeof z.NEVER { | ||
const collateralRebaseEntry = Object.entries(warpRouteDeployConfig).find( | ||
([_, config]) => isCollateralRebaseTokenConfig(config), | ||
); | ||
|
||
const syntheticRebaseEntry = Object.entries(warpRouteDeployConfig).find( | ||
([_, config]) => isSyntheticRebaseTokenConfig(config), | ||
); | ||
|
||
// Require both collateral rebase and synthetic rebase to be present in the config | ||
if (!collateralRebaseEntry && !syntheticRebaseEntry) { | ||
// Pass through for other token types | ||
return warpRouteDeployConfig; | ||
} | ||
|
||
if ( | ||
collateralRebaseEntry && | ||
isCollateralRebasePairedCorrectly(warpRouteDeployConfig) | ||
) { | ||
const collateralChainName = collateralRebaseEntry[0]; | ||
return objMap(warpRouteDeployConfig, (_, config) => { | ||
if (config.type === TokenType.syntheticRebase) | ||
config.collateralChainName = collateralChainName; | ||
return config; | ||
}); | ||
} | ||
|
||
return z.NEVER; // Causes schema validation to throw with above issue | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: WarpRouteDeployConfigSchemaErrors.ONLY_SYNTHETIC_REBASE, | ||
}); | ||
|
||
return z.NEVER; // Causes schema validation to throw with above issue | ||
} | ||
|
||
export const WarpRouteDeployConfigSchema = z | ||
.record(HypTokenRouterConfigSchema) | ||
.refine(refineTokens, WarpRouteDeployConfigSchemaErrors.NO_SYNTHETIC_ONLY) | ||
.transform(transformRebaseConfig); | ||
Comment on lines
+174
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generally prefer not to include drive-bys like this except because it makes what would otherwise be a very easy to review PR much harder to review There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really sure what exactly you mean with drive-bys, |
||
|
||
export type WarpRouteDeployConfig = z.infer<typeof WarpRouteDeployConfigSchema>; | ||
|
||
function isCollateralRebasePairedCorrectly( | ||
warpRouteDeployConfig: Record<string, HypTokenRouterConfig>, | ||
): boolean { | ||
function isCollateralRebasePairedCorrectly< | ||
T extends HypTokenRouterConfig | HypTokenRouterConfigWithoutMailbox, | ||
>(warpRouteDeployConfig: Record<string, T>): boolean { | ||
// Filter out all the non-collateral rebase configs to check if they are only synthetic rebase tokens | ||
const otherConfigs = Object.entries(warpRouteDeployConfig).filter( | ||
([_, config]) => !isCollateralRebaseTokenConfig(config), | ||
|
@@ -161,8 +189,8 @@ function isCollateralRebasePairedCorrectly( | |
if (otherConfigs.length === 0) return false; | ||
|
||
// The other configs MUST be synthetic rebase | ||
const allOthersSynthetic: boolean = otherConfigs.every( | ||
([_, config], _index) => isSyntheticRebaseTokenConfig(config), | ||
const allOthersSynthetic: boolean = otherConfigs.every(([_, config]) => | ||
isSyntheticRebaseTokenConfig(config), | ||
); | ||
return allOthersSynthetic; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think zod will not error on extraneous fields so wondering why not just remove the mailbox field from the existing schema?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understood correctly but it is not possible do remove mailbox from
MailBoxClienSchema
as it is required for other parts of code, also omit is not supported forHypTokenRouterConfigMailboxSchema