[xc-admin] Add ser/de for Cosmos Upgrades (#758)

* Add ser/de for Cosmos Upgrades

* Add comment

* Fix print
This commit is contained in:
guibescos 2023-04-13 11:51:47 -05:00 committed by GitHub
parent c2484b1514
commit 99ed193764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 1 deletions

View File

@ -286,7 +286,13 @@ program
keys: ix.keys as AccountMeta[],
})
);
console.log(JSON.stringify(parsed, null, 2));
console.log(
JSON.stringify(
parsed,
(key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
2
)
);
});
multisigCommand("approve", "Approve a transaction sitting in the multisig")

View File

@ -0,0 +1,44 @@
import {
decodeGovernancePayload,
PythGovernanceHeader,
} from "../governance_payload";
import { CosmosUpgradeContract } from "../governance_payload/UpgradeContract";
test("Upgrade contract ser/de", (done) => {
jest.setTimeout(60000);
const expectedUpgradeContract = new CosmosUpgradeContract(
"injective",
BigInt("18446744073709551614")
);
const buffer = expectedUpgradeContract.encode();
console.log(buffer.toJSON());
expect(
buffer.equals(
Buffer.from([
80, 84, 71, 77, 1, 0, 0, 19, 255, 255, 255, 255, 255, 255, 255, 254,
])
)
).toBeTruthy();
const actualHeader = PythGovernanceHeader.decode(buffer);
if (actualHeader) {
expect(actualHeader.targetChainId).toBe("injective");
expect(actualHeader.action).toBe("UpgradeContract");
} else {
done("Not an instance of CosmosUpgradeContract");
}
const actualUpgradeContract = decodeGovernancePayload(buffer);
if (actualUpgradeContract instanceof CosmosUpgradeContract) {
expect(actualUpgradeContract.targetChainId).toBe("injective");
expect(actualUpgradeContract.codeId).toBe(BigInt("18446744073709551614"));
} else {
done("Not an instance of CosmosUpgradeContract");
}
done();
});

View File

@ -0,0 +1,38 @@
import { ChainName } from "@certusone/wormhole-sdk";
import { PythGovernanceAction, PythGovernanceHeader } from ".";
export class CosmosUpgradeContract implements PythGovernanceAction {
readonly targetChainId: ChainName;
readonly codeId: bigint;
constructor(targetChainId: ChainName, codeId: bigint) {
this.targetChainId = targetChainId;
this.codeId = codeId;
}
static span: number = 8;
static decode(data: Buffer): CosmosUpgradeContract | undefined {
const header = PythGovernanceHeader.decode(data);
if (!header) return undefined;
const codeId = data.subarray(PythGovernanceHeader.span).readBigUInt64BE();
if (!codeId) return undefined;
return new CosmosUpgradeContract(header.targetChainId, codeId);
}
/** Encode CosmosUpgradeContract */
encode(): Buffer {
const headerBuffer = new PythGovernanceHeader(
this.targetChainId,
"UpgradeContract"
).encode();
const buffer = Buffer.alloc(
PythGovernanceHeader.span + CosmosUpgradeContract.span
);
const span = buffer.writeBigUInt64BE(this.codeId);
return Buffer.concat([headerBuffer, buffer.subarray(0, span)]);
}
}

View File

@ -7,6 +7,7 @@ import {
import * as BufferLayout from "@solana/buffer-layout";
import { PACKET_DATA_SIZE } from "@solana/web3.js";
import { ExecutePostedVaa } from "./ExecutePostedVaa";
import { CosmosUpgradeContract } from "./UpgradeContract";
export interface PythGovernanceAction {
readonly targetChainId: ChainName;
@ -148,6 +149,9 @@ export function decodeGovernancePayload(
switch (header.action) {
case "ExecutePostedVaa":
return ExecutePostedVaa.decode(data);
case "UpgradeContract":
//TO DO : Support non-cosmos upgrades
return CosmosUpgradeContract.decode(data);
default:
return undefined;
}