Xc admin/change error handling (#471)
* Switch from undefined to not undefined * Update tests
This commit is contained in:
parent
80fe230563
commit
92c17ad7cc
|
@ -60,22 +60,19 @@ test("GovernancePayload ser/de", (done) => {
|
|||
expect(governanceHeader?.action).toBe("SetFee");
|
||||
|
||||
// Wrong magic number
|
||||
governanceHeader = decodeHeader(
|
||||
Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0])
|
||||
);
|
||||
expect(governanceHeader).toBeUndefined();
|
||||
expect(() =>
|
||||
decodeHeader(Buffer.from([0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 0]))
|
||||
).toThrow("Wrong magic number");
|
||||
|
||||
// Wrong chain
|
||||
governanceHeader = decodeHeader(
|
||||
Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0])
|
||||
);
|
||||
expect(governanceHeader).toBeUndefined();
|
||||
expect(() =>
|
||||
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 0, 255, 255, 0, 0, 0, 0]))
|
||||
).toThrow("Chain Id not found");
|
||||
|
||||
// Wrong module/action combination
|
||||
governanceHeader = decodeHeader(
|
||||
Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0])
|
||||
);
|
||||
expect(governanceHeader).toBeUndefined();
|
||||
expect(() =>
|
||||
decodeHeader(Buffer.from([80, 84, 71, 77, 0, 1, 0, 26, 0, 0, 0, 0]))
|
||||
).toThrow("Invalid header, action doesn't match module");
|
||||
|
||||
// Decode executePostVaa with empty instructions
|
||||
let expectedExecuteVaaArgs = {
|
||||
|
|
|
@ -83,17 +83,11 @@ export type ExecutePostedVaaArgs = {
|
|||
};
|
||||
|
||||
/** Decode ExecutePostedVaaArgs and return undefined if it failed */
|
||||
export function decodeExecutePostedVaa(
|
||||
data: Buffer
|
||||
): ExecutePostedVaaArgs | undefined {
|
||||
export function decodeExecutePostedVaa(data: Buffer): ExecutePostedVaaArgs {
|
||||
let deserialized = executePostedVaaLayout.decode(data);
|
||||
|
||||
let header = verifyHeader(deserialized.header);
|
||||
|
||||
if (!header) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let instructions: TransactionInstruction[] = deserialized.instructions.map(
|
||||
(ix) => {
|
||||
let programId: PublicKey = new PublicKey(ix.programId);
|
||||
|
|
|
@ -75,7 +75,7 @@ export function governanceHeaderLayout(): BufferLayout.Structure<
|
|||
}
|
||||
|
||||
/** Decode Pyth Governance Header and return undefined if the header is invalid */
|
||||
export function decodeHeader(data: Buffer): PythGovernanceHeader | undefined {
|
||||
export function decodeHeader(data: Buffer): PythGovernanceHeader {
|
||||
let deserialized = governanceHeaderLayout().decode(data);
|
||||
return verifyHeader(deserialized);
|
||||
}
|
||||
|
@ -111,16 +111,15 @@ export function verifyHeader(
|
|||
action: number;
|
||||
chain: ChainId;
|
||||
}>
|
||||
) {
|
||||
): PythGovernanceHeader {
|
||||
if (deserialized.magicNumber !== MAGIC_NUMBER) {
|
||||
return undefined;
|
||||
throw new Error("Wrong magic number");
|
||||
}
|
||||
|
||||
if (!toChainName(deserialized.chain)) {
|
||||
return undefined;
|
||||
throw new Error("Chain Id not found");
|
||||
}
|
||||
|
||||
try {
|
||||
let governanceHeader: PythGovernanceHeader = {
|
||||
targetChainId: toChainName(deserialized.chain),
|
||||
action: toActionName({
|
||||
|
@ -129,9 +128,6 @@ export function verifyHeader(
|
|||
}),
|
||||
};
|
||||
return governanceHeader;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export { decodeExecutePostedVaa } from "./ExecutePostedVaa";
|
||||
|
|
Loading…
Reference in New Issue