|
| 1 | +import { PythGovernanceActionImpl } from "./PythGovernanceAction"; |
| 2 | +import * as BufferLayout from "@solana/buffer-layout"; |
| 3 | +import { ChainName } from "../chains"; |
| 4 | +import { pyth_lazer_transaction } from "@pythnetwork/pyth-lazer-state-sdk/governance"; |
| 5 | + |
| 6 | +/** Executes a Lazer governance instruction with the specified directives */ |
| 7 | +export class LazerExecute extends PythGovernanceActionImpl { |
| 8 | + static layout: BufferLayout.Structure< |
| 9 | + Readonly<{ |
| 10 | + governanceInstruction: Uint8Array; |
| 11 | + }> |
| 12 | + > = BufferLayout.struct([ |
| 13 | + BufferLayout.blob(new BufferLayout.GreedyCount(), "governanceInstruction"), |
| 14 | + ]); |
| 15 | + |
| 16 | + constructor( |
| 17 | + targetChainId: ChainName, |
| 18 | + readonly directives: pyth_lazer_transaction.IGovernanceDirective[], |
| 19 | + readonly minExecutionTimestamp?: Date, |
| 20 | + readonly maxExecutionTimestamp?: Date, |
| 21 | + readonly governanceSequenceNo?: number, |
| 22 | + ) { |
| 23 | + super(targetChainId, "LazerExecute"); |
| 24 | + } |
| 25 | + |
| 26 | + static decode(data: Buffer): LazerExecute | undefined { |
| 27 | + const decoded = PythGovernanceActionImpl.decodeWithPayload( |
| 28 | + data, |
| 29 | + "LazerExecute", |
| 30 | + this.layout, |
| 31 | + ); |
| 32 | + if (!decoded) return undefined; |
| 33 | + |
| 34 | + try { |
| 35 | + // Decode the protobuf GovernanceInstruction |
| 36 | + const governanceInstruction = |
| 37 | + pyth_lazer_transaction.GovernanceInstruction.decode( |
| 38 | + decoded[1].governanceInstruction, |
| 39 | + ); |
| 40 | + |
| 41 | + return new LazerExecute( |
| 42 | + decoded[0].targetChainId, |
| 43 | + governanceInstruction.directives || [], |
| 44 | + governanceInstruction.minExecutionTimestamp |
| 45 | + ? new Date( |
| 46 | + governanceInstruction.minExecutionTimestamp.seconds * 1000 + |
| 47 | + (governanceInstruction.minExecutionTimestamp.nanos || 0) / |
| 48 | + 1000000, |
| 49 | + ) |
| 50 | + : undefined, |
| 51 | + governanceInstruction.maxExecutionTimestamp |
| 52 | + ? new Date( |
| 53 | + governanceInstruction.maxExecutionTimestamp.seconds * 1000 + |
| 54 | + (governanceInstruction.maxExecutionTimestamp.nanos || 0) / |
| 55 | + 1000000, |
| 56 | + ) |
| 57 | + : undefined, |
| 58 | + governanceInstruction.governanceSequenceNo || undefined, |
| 59 | + ); |
| 60 | + } catch (error) { |
| 61 | + console.error("Failed to decode Lazer governance instruction:", error); |
| 62 | + return undefined; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + encode(): Buffer { |
| 67 | + try { |
| 68 | + // Create the GovernanceInstruction protobuf message |
| 69 | + const governanceInstruction = |
| 70 | + pyth_lazer_transaction.GovernanceInstruction.create({ |
| 71 | + directives: this.directives, |
| 72 | + minExecutionTimestamp: this.minExecutionTimestamp |
| 73 | + ? { |
| 74 | + seconds: Math.floor( |
| 75 | + this.minExecutionTimestamp.getTime() / 1000, |
| 76 | + ), |
| 77 | + nanos: (this.minExecutionTimestamp.getTime() % 1000) * 1000000, |
| 78 | + } |
| 79 | + : undefined, |
| 80 | + maxExecutionTimestamp: this.maxExecutionTimestamp |
| 81 | + ? { |
| 82 | + seconds: Math.floor( |
| 83 | + this.maxExecutionTimestamp.getTime() / 1000, |
| 84 | + ), |
| 85 | + nanos: (this.maxExecutionTimestamp.getTime() % 1000) * 1000000, |
| 86 | + } |
| 87 | + : undefined, |
| 88 | + governanceSequenceNo: this.governanceSequenceNo, |
| 89 | + }); |
| 90 | + |
| 91 | + // Validate the message before encoding |
| 92 | + const error = pyth_lazer_transaction.GovernanceInstruction.verify( |
| 93 | + governanceInstruction, |
| 94 | + ); |
| 95 | + if (error) { |
| 96 | + throw new Error(`GovernanceInstruction validation failed: ${error}`); |
| 97 | + } |
| 98 | + |
| 99 | + // Encode the protobuf message to bytes |
| 100 | + const encodedInstruction = |
| 101 | + pyth_lazer_transaction.GovernanceInstruction.encode( |
| 102 | + governanceInstruction, |
| 103 | + ).finish(); |
| 104 | + |
| 105 | + // Create a layout with the known instruction length for encoding |
| 106 | + const layout_with_known_span: BufferLayout.Structure< |
| 107 | + Readonly<{ |
| 108 | + governanceInstruction: Uint8Array; |
| 109 | + }> |
| 110 | + > = BufferLayout.struct([ |
| 111 | + BufferLayout.blob(encodedInstruction.length, "governanceInstruction"), |
| 112 | + ]); |
| 113 | + |
| 114 | + return super.encodeWithPayload(layout_with_known_span, { |
| 115 | + governanceInstruction: encodedInstruction, |
| 116 | + }); |
| 117 | + } catch (error) { |
| 118 | + console.error("LazerExecute encoding error:", error); |
| 119 | + console.error("Directives:", JSON.stringify(this.directives, null, 2)); |
| 120 | + console.error("minExecutionTimestamp:", this.minExecutionTimestamp); |
| 121 | + console.error("maxExecutionTimestamp:", this.maxExecutionTimestamp); |
| 122 | + console.error("governanceSequenceNo:", this.governanceSequenceNo); |
| 123 | + throw error; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments