|
|
@@ -30,7 +30,7 @@ export const TargetAction = {
|
|
|
/** Helper to get the ActionName from a (moduleId, actionId) tuple*/
|
|
|
export function toActionName(
|
|
|
deserialized: Readonly<{ moduleId: number; actionId: number }>
|
|
|
-): ActionName {
|
|
|
+): ActionName | undefined {
|
|
|
if (deserialized.moduleId == MODULE_EXECUTOR && deserialized.actionId == 0) {
|
|
|
return "ExecutePostedVaa";
|
|
|
} else if (deserialized.moduleId == MODULE_TARGET) {
|
|
|
@@ -49,7 +49,7 @@ export function toActionName(
|
|
|
return "RequestGovernanceDataSourceTransfer";
|
|
|
}
|
|
|
}
|
|
|
- throw new Error("Invalid header, action doesn't match module");
|
|
|
+ return undefined;
|
|
|
}
|
|
|
|
|
|
export declare type ActionName =
|
|
|
@@ -84,23 +84,35 @@ export class PythGovernanceHeader {
|
|
|
this.action = action;
|
|
|
}
|
|
|
/** Decode Pyth Governance Header */
|
|
|
- static decode(data: Buffer): PythGovernanceHeader {
|
|
|
- let deserialized = this.layout.decode(data);
|
|
|
+ static decode(data: Buffer): PythGovernanceHeader | undefined {
|
|
|
+ let deserialized;
|
|
|
+ try {
|
|
|
+ deserialized = this.layout.decode(data);
|
|
|
+ } catch {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+
|
|
|
if (deserialized.magicNumber !== MAGIC_NUMBER) {
|
|
|
- throw new Error("Wrong magic number");
|
|
|
+ return undefined;
|
|
|
}
|
|
|
|
|
|
if (!toChainName(deserialized.chain)) {
|
|
|
- throw new Error("Chain Id not found");
|
|
|
+ return undefined;
|
|
|
}
|
|
|
|
|
|
- return new PythGovernanceHeader(
|
|
|
- toChainName(deserialized.chain),
|
|
|
- toActionName({
|
|
|
- actionId: deserialized.action,
|
|
|
- moduleId: deserialized.module,
|
|
|
- })
|
|
|
- );
|
|
|
+ const actionName = toActionName({
|
|
|
+ actionId: deserialized.action,
|
|
|
+ moduleId: deserialized.module,
|
|
|
+ });
|
|
|
+
|
|
|
+ if (actionName) {
|
|
|
+ return new PythGovernanceHeader(
|
|
|
+ toChainName(deserialized.chain),
|
|
|
+ actionName
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/** Encode Pyth Governance Header */
|
|
|
@@ -134,13 +146,19 @@ export const MODULE_EXECUTOR = 0;
|
|
|
export const MODULE_TARGET = 1;
|
|
|
|
|
|
/** Decode a governance payload */
|
|
|
-export function decodeGovernancePayload(data: Buffer): PythGovernanceAction {
|
|
|
+export function decodeGovernancePayload(
|
|
|
+ data: Buffer
|
|
|
+): PythGovernanceAction | undefined {
|
|
|
const header = PythGovernanceHeader.decode(data);
|
|
|
+ if (!header) {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+
|
|
|
switch (header.action) {
|
|
|
case "ExecutePostedVaa":
|
|
|
return ExecutePostedVaa.decode(data);
|
|
|
default:
|
|
|
- throw "Not supported";
|
|
|
+ return undefined;
|
|
|
}
|
|
|
}
|
|
|
|