Skip to content

Commit 99ed193

Browse files
authored
[xc-admin] Add ser/de for Cosmos Upgrades (#758)
* Add ser/de for Cosmos Upgrades * Add comment * Fix print
1 parent c2484b1 commit 99ed193

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

governance/xc_admin/packages/xc_admin_cli/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,13 @@ program
286286
keys: ix.keys as AccountMeta[],
287287
})
288288
);
289-
console.log(JSON.stringify(parsed, null, 2));
289+
console.log(
290+
JSON.stringify(
291+
parsed,
292+
(key, value) => (typeof value === "bigint" ? value.toString() : value), // return everything else unchanged
293+
2
294+
)
295+
);
290296
});
291297

292298
multisigCommand("approve", "Approve a transaction sitting in the multisig")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
decodeGovernancePayload,
3+
PythGovernanceHeader,
4+
} from "../governance_payload";
5+
import { CosmosUpgradeContract } from "../governance_payload/UpgradeContract";
6+
7+
test("Upgrade contract ser/de", (done) => {
8+
jest.setTimeout(60000);
9+
10+
const expectedUpgradeContract = new CosmosUpgradeContract(
11+
"injective",
12+
BigInt("18446744073709551614")
13+
);
14+
const buffer = expectedUpgradeContract.encode();
15+
16+
console.log(buffer.toJSON());
17+
expect(
18+
buffer.equals(
19+
Buffer.from([
20+
80, 84, 71, 77, 1, 0, 0, 19, 255, 255, 255, 255, 255, 255, 255, 254,
21+
])
22+
)
23+
).toBeTruthy();
24+
25+
const actualHeader = PythGovernanceHeader.decode(buffer);
26+
27+
if (actualHeader) {
28+
expect(actualHeader.targetChainId).toBe("injective");
29+
expect(actualHeader.action).toBe("UpgradeContract");
30+
} else {
31+
done("Not an instance of CosmosUpgradeContract");
32+
}
33+
34+
const actualUpgradeContract = decodeGovernancePayload(buffer);
35+
36+
if (actualUpgradeContract instanceof CosmosUpgradeContract) {
37+
expect(actualUpgradeContract.targetChainId).toBe("injective");
38+
expect(actualUpgradeContract.codeId).toBe(BigInt("18446744073709551614"));
39+
} else {
40+
done("Not an instance of CosmosUpgradeContract");
41+
}
42+
43+
done();
44+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { ChainName } from "@certusone/wormhole-sdk";
2+
import { PythGovernanceAction, PythGovernanceHeader } from ".";
3+
4+
export class CosmosUpgradeContract implements PythGovernanceAction {
5+
readonly targetChainId: ChainName;
6+
readonly codeId: bigint;
7+
8+
constructor(targetChainId: ChainName, codeId: bigint) {
9+
this.targetChainId = targetChainId;
10+
this.codeId = codeId;
11+
}
12+
13+
static span: number = 8;
14+
static decode(data: Buffer): CosmosUpgradeContract | undefined {
15+
const header = PythGovernanceHeader.decode(data);
16+
if (!header) return undefined;
17+
18+
const codeId = data.subarray(PythGovernanceHeader.span).readBigUInt64BE();
19+
if (!codeId) return undefined;
20+
21+
return new CosmosUpgradeContract(header.targetChainId, codeId);
22+
}
23+
24+
/** Encode CosmosUpgradeContract */
25+
encode(): Buffer {
26+
const headerBuffer = new PythGovernanceHeader(
27+
this.targetChainId,
28+
"UpgradeContract"
29+
).encode();
30+
31+
const buffer = Buffer.alloc(
32+
PythGovernanceHeader.span + CosmosUpgradeContract.span
33+
);
34+
35+
const span = buffer.writeBigUInt64BE(this.codeId);
36+
return Buffer.concat([headerBuffer, buffer.subarray(0, span)]);
37+
}
38+
}

governance/xc_admin/packages/xc_admin_common/src/governance_payload/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import * as BufferLayout from "@solana/buffer-layout";
88
import { PACKET_DATA_SIZE } from "@solana/web3.js";
99
import { ExecutePostedVaa } from "./ExecutePostedVaa";
10+
import { CosmosUpgradeContract } from "./UpgradeContract";
1011

1112
export interface PythGovernanceAction {
1213
readonly targetChainId: ChainName;
@@ -148,6 +149,9 @@ export function decodeGovernancePayload(
148149
switch (header.action) {
149150
case "ExecutePostedVaa":
150151
return ExecutePostedVaa.decode(data);
152+
case "UpgradeContract":
153+
//TO DO : Support non-cosmos upgrades
154+
return CosmosUpgradeContract.decode(data);
151155
default:
152156
return undefined;
153157
}

0 commit comments

Comments
 (0)