Skip to content

Commit c732fcf

Browse files
authored
Abolish xc governance sdk (#957)
* Replace xc-governance-sdk with xc_admin_common package xc_admin_package was not using the CHAIN overrides declared in the governance-sdk so it was moved to that package as well * Replace xc-governance-sdk with xc_admin_common in other packages * Remove the package and all of its references * Fix tests * Fix bug in GovernanceDataSourceTransfer encoding * Rename all references to the old package * Redeploy neutron_testnet contract with new chain id * Move SetWormholeAddress to separate file
1 parent b64090a commit c732fcf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+5397
-7794
lines changed

.github/workflows/ethereum-contract.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ on:
22
pull_request:
33
paths:
44
- target_chains/ethereum/contracts/**
5-
- governance/xc_governance_sdk_js/**
5+
- governance/xc_admin/packages/xc_admin_common/**
66
push:
77
branches:
88
- main
99
paths:
1010
- target_chains/ethereum/contracts/**
11-
- governance/xc_governance_sdk_js/**
11+
- governance/xc_admin/packages/xc_admin_common/**
1212

1313
name: Ethereum Contract
1414

@@ -22,10 +22,6 @@ jobs:
2222
steps:
2323
- uses: actions/checkout@v3
2424

25-
- name: Install XC-governance sdk dependencies
26-
run: npm ci
27-
working-directory: governance/xc_governance_sdk_js
28-
2925
- name: Install contract npm dependencies
3026
run: npm ci
3127

contract_manager/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

contract_manager/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"@certusone/wormhole-sdk": "^0.9.8",
1818
"@pythnetwork/cosmwasm-deploy-tools": "*",
1919
"@pythnetwork/price-service-client": "*",
20-
"@pythnetwork/xc-governance-sdk": "*",
2120
"bs58": "^5.0.0",
2221
"ts-node": "^10.9.1",
2322
"typescript": "^4.9.3"

contract_manager/src/aptos.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Contract } from "./base";
22
import { AptosChain, Chain } from "./chains";
3-
import { DataSource, HexString32Bytes } from "@pythnetwork/xc-governance-sdk";
4-
import { AptosClient } from "aptos";
3+
import { DataSource } from "xc_admin_common";
54

65
export class AptosContract extends Contract {
76
static type: string = "AptosContract";
@@ -64,19 +63,25 @@ export class AptosContract extends Contract {
6463
async getDataSources(): Promise<DataSource[]> {
6564
const data = (await this.findResource("DataSources")) as any;
6665
return data.sources.keys.map((source: any) => {
67-
return new DataSource(
68-
Number(source.emitter_chain),
69-
new HexString32Bytes(source.emitter_address.external_address)
70-
);
66+
return {
67+
emitterChain: Number(source.emitter_chain),
68+
emitterAddress: source.emitter_address.external_address.replace(
69+
"0x",
70+
""
71+
),
72+
};
7173
});
7274
}
7375

7476
async getGovernanceDataSource(): Promise<DataSource> {
7577
const data = (await this.findResource("GovernanceDataSource")) as any;
76-
return new DataSource(
77-
Number(data.source.emitter_chain),
78-
new HexString32Bytes(data.source.emitter_address.external_address)
79-
);
78+
return {
79+
emitterChain: Number(data.source.emitter_chain),
80+
emitterAddress: data.source.emitter_address.external_address.replace(
81+
"0x",
82+
""
83+
),
84+
};
8085
}
8186

8287
getId(): string {

contract_manager/src/base.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
CHAINS,
3-
DataSource,
4-
HexString32Bytes,
5-
SetFeeInstruction,
6-
} from "@pythnetwork/xc-governance-sdk";
1+
import { DataSource } from "xc_admin_common";
72
import { Chain } from "./chains";
83

94
export abstract class Storable {

contract_manager/src/chains.ts

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { readdirSync, readFileSync, writeFileSync } from "fs";
21
import { Storable } from "./base";
32
import {
3+
ChainName,
44
CHAINS,
5-
CosmwasmUpgradeContractInstruction,
6-
EthereumUpgradeContractInstruction,
7-
HexString20Bytes,
8-
HexString32Bytes,
9-
SetFeeInstruction,
10-
SuiAuthorizeUpgradeContractInstruction,
11-
} from "@pythnetwork/xc-governance-sdk";
12-
import { BufferBuilder } from "@pythnetwork/xc-governance-sdk/lib/serialize";
5+
SetFee,
6+
CosmosUpgradeContract,
7+
EvmUpgradeContract,
8+
SuiAuthorizeUpgradeContract,
9+
AptosAuthorizeUpgradeContract,
10+
} from "xc_admin_common";
1311
import { AptosClient } from "aptos";
1412

1513
export abstract class Chain extends Storable {
@@ -27,11 +25,11 @@ export abstract class Chain extends Storable {
2725
* @param exponent the new fee exponent to set
2826
*/
2927
generateGovernanceSetFeePayload(fee: number, exponent: number): Buffer {
30-
return new SetFeeInstruction(
31-
CHAINS[this.getId() as keyof typeof CHAINS],
28+
return new SetFee(
29+
this.getId() as ChainName,
3230
BigInt(fee),
3331
BigInt(exponent)
34-
).serialize();
32+
).encode();
3533
}
3634

3735
/**
@@ -84,10 +82,10 @@ export class CosmWasmChain extends Chain {
8482
}
8583

8684
generateGovernanceUpgradePayload(codeId: bigint): Buffer {
87-
return new CosmwasmUpgradeContractInstruction(
88-
CHAINS[this.getId() as keyof typeof CHAINS],
85+
return new CosmosUpgradeContract(
86+
this.getId() as ChainName,
8987
codeId
90-
).serialize();
88+
).encode();
9189
}
9290
}
9391

@@ -115,37 +113,42 @@ export class SuiChain extends Chain {
115113
return SuiChain.type;
116114
}
117115

116+
//TODO: Move this logic to xc_admin_common
118117
private wrapWithWormholeGovernancePayload(
119118
actionVariant: number,
120119
payload: Buffer
121120
): Buffer {
122-
const builder = new BufferBuilder();
123-
builder.addBuffer(
121+
const actionVariantBuffer = Buffer.alloc(1);
122+
actionVariantBuffer.writeUint8(actionVariant, 0);
123+
const chainBuffer = Buffer.alloc(2);
124+
chainBuffer.writeUint16BE(CHAINS["sui"], 0);
125+
const result = Buffer.concat([
124126
Buffer.from(
125127
"0000000000000000000000000000000000000000000000000000000000000001",
126128
"hex"
127-
)
128-
);
129-
builder.addUint8(actionVariant);
130-
builder.addUint16(CHAINS["sui"]); // should always be sui (21) no matter devnet or testnet
131-
builder.addBuffer(payload);
132-
return builder.build();
129+
),
130+
actionVariantBuffer,
131+
chainBuffer,
132+
payload,
133+
]);
134+
return result;
133135
}
134136

137+
/**
138+
* Returns the payload for a governance contract upgrade instruction for contracts deployed on this chain
139+
* @param digest hex string of the 32 byte digest for the new package without the 0x prefix
140+
*/
135141
generateGovernanceUpgradePayload(digest: string): Buffer {
136-
let setFee = new SuiAuthorizeUpgradeContractInstruction(
137-
CHAINS["sui"],
138-
new HexString32Bytes(digest)
139-
).serialize();
142+
let setFee = new SuiAuthorizeUpgradeContract("sui", digest).encode();
140143
return this.wrapWithWormholeGovernancePayload(0, setFee);
141144
}
142145

143146
generateGovernanceSetFeePayload(fee: number, exponent: number): Buffer {
144-
let setFee = new SetFeeInstruction(
145-
CHAINS["sui"],
147+
let setFee = new SetFee(
148+
"sui", // should always be sui no matter devnet or testnet or mainnet
146149
BigInt(fee),
147150
BigInt(exponent)
148-
).serialize();
151+
).encode();
149152
return this.wrapWithWormholeGovernancePayload(3, setFee);
150153
}
151154
}
@@ -162,11 +165,12 @@ export class EVMChain extends Chain {
162165
return new EVMChain(parsed.id, parsed.rpcUrl);
163166
}
164167

165-
generateGovernanceUpgradePayload(address: HexString20Bytes): Buffer {
166-
return new EthereumUpgradeContractInstruction(
167-
CHAINS[this.getId() as keyof typeof CHAINS],
168-
address
169-
).serialize();
168+
/**
169+
* Returns the payload for a governance contract upgrade instruction for contracts deployed on this chain
170+
* @param address hex string of the 20 byte address of the contract to upgrade to without the 0x prefix
171+
*/
172+
generateGovernanceUpgradePayload(address: string): Buffer {
173+
return new EvmUpgradeContract(this.getId() as ChainName, address).encode();
170174
}
171175

172176
toJson(): any {
@@ -193,19 +197,20 @@ export class AptosChain extends Chain {
193197
return new AptosClient(this.rpcUrl);
194198
}
195199

200+
/**
201+
* Returns the payload for a governance contract upgrade instruction for contracts deployed on this chain
202+
* @param digest hex string of the 32 byte digest for the new package without the 0x prefix
203+
*/
196204
generateGovernanceUpgradePayload(digest: string): Buffer {
197-
return new SuiAuthorizeUpgradeContractInstruction(
198-
CHAINS["aptos"],
199-
new HexString32Bytes(digest)
200-
).serialize();
205+
return new AptosAuthorizeUpgradeContract("aptos", digest).encode();
201206
}
202207

203208
generateGovernanceSetFeePayload(fee: number, exponent: number): Buffer {
204-
return new SetFeeInstruction(
205-
CHAINS["aptos"], // should always be aptos (22) no matter devnet or testnet or mainnet
209+
return new SetFee(
210+
"aptos", // should always be aptos no matter devnet or testnet or mainnet
206211
BigInt(fee),
207212
BigInt(exponent)
208-
).serialize();
213+
).encode();
209214
}
210215

211216
getType(): string {

contract_manager/src/cosmwasm.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Chain, CosmWasmChain } from "./chains";
22
import { readFileSync } from "fs";
33
import { getPythConfig } from "@pythnetwork/cosmwasm-deploy-tools/lib/configs";
4-
import {
5-
CHAINS,
6-
DataSource,
7-
HexString32Bytes,
8-
} from "@pythnetwork/xc-governance-sdk";
4+
import { CHAINS, DataSource } from "xc_admin_common";
95
import { DeploymentType } from "@pythnetwork/cosmwasm-deploy-tools/lib/helper";
106
import {
117
CosmwasmExecutor,
@@ -45,23 +41,21 @@ export class CosmWasmContract extends Contract {
4541
async getDataSources(): Promise<DataSource[]> {
4642
const config = await this.getConfig();
4743
return config.config_v1.data_sources.map(({ emitter, chain_id }: any) => {
48-
return new DataSource(
49-
Number(chain_id),
50-
new HexString32Bytes(Buffer.from(emitter, "base64").toString("hex"))
51-
);
44+
return {
45+
emitterChain: Number(chain_id),
46+
emitterAddress: Buffer.from(emitter, "base64").toString("hex"),
47+
};
5248
});
5349
}
5450

5551
async getGovernanceDataSource(): Promise<DataSource> {
5652
const config = await this.getConfig();
5753
const { emitter: emitterAddress, chain_id: chainId } =
5854
config.config_v1.governance_source;
59-
return new DataSource(
60-
Number(chainId),
61-
new HexString32Bytes(
62-
Buffer.from(emitterAddress, "base64").toString("hex")
63-
)
64-
);
55+
return {
56+
emitterChain: Number(chainId),
57+
emitterAddress: Buffer.from(emitterAddress, "base64").toString("hex"),
58+
};
6559
}
6660

6761
static type = "CosmWasmContract";

contract_manager/src/evm.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Web3 from "web3"; //TODO: decide on using web3 or ethers.js
22
import PythInterfaceAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json";
33
import { Contract } from "./base";
44
import { Chain, EVMChain } from "./chains";
5-
import { DataSource, HexString32Bytes } from "@pythnetwork/xc-governance-sdk";
5+
import { DataSource } from "xc_admin_common";
66

77
const EXTENDED_PYTH_ABI = [
88
{
@@ -136,23 +136,31 @@ export class EVMContract extends Contract {
136136
async getDataSources(): Promise<DataSource[]> {
137137
const pythContract = this.getContract();
138138
const result = await pythContract.methods.validDataSources().call();
139-
return result.map(({ chainId, emitterAddress }: any) => {
140-
return new DataSource(
141-
Number(chainId),
142-
new HexString32Bytes(emitterAddress)
143-
);
144-
});
139+
return result.map(
140+
({
141+
chainId,
142+
emitterAddress,
143+
}: {
144+
chainId: string;
145+
emitterAddress: string;
146+
}) => {
147+
return {
148+
emitterChain: Number(chainId),
149+
emitterAddress: emitterAddress.replace("0x", ""),
150+
};
151+
}
152+
);
145153
}
146154

147155
async getGovernanceDataSource(): Promise<DataSource> {
148156
const pythContract = this.getContract();
149157
const [chainId, emitterAddress] = await pythContract.methods
150158
.governanceDataSource()
151159
.call();
152-
return new DataSource(
153-
Number(chainId),
154-
new HexString32Bytes(emitterAddress)
155-
);
160+
return {
161+
emitterChain: Number(chainId),
162+
emitterAddress: emitterAddress.replace("0x", ""),
163+
};
156164
}
157165

158166
async executeGovernanceInstruction(privateKey: string, vaa: Buffer) {

contract_manager/src/sui.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TransactionBlock,
99
} from "@mysten/sui.js";
1010
import { Chain, SuiChain } from "./chains";
11-
import { DataSource, HexString32Bytes } from "@pythnetwork/xc-governance-sdk";
11+
import { DataSource } from "xc_admin_common";
1212
import { Contract } from "./base";
1313

1414
export class SuiContract extends Contract {
@@ -294,14 +294,12 @@ export class SuiContract extends Contract {
294294
}
295295
return result.data.content.fields.value.fields.keys.map(
296296
({ fields }: any) => {
297-
return new DataSource(
298-
Number(fields.emitter_chain),
299-
new HexString32Bytes(
300-
Buffer.from(
301-
fields.emitter_address.fields.value.fields.data
302-
).toString("hex")
303-
)
304-
);
297+
return {
298+
emitterChain: Number(fields.emitter_chain),
299+
emitterAddress: Buffer.from(
300+
fields.emitter_address.fields.value.fields.data
301+
).toString("hex"),
302+
};
305303
}
306304
);
307305
}
@@ -312,10 +310,10 @@ export class SuiContract extends Contract {
312310
const chainId = governanceFields.emitter_chain;
313311
const emitterAddress =
314312
governanceFields.emitter_address.fields.value.fields.data;
315-
return new DataSource(
316-
Number(chainId),
317-
new HexString32Bytes(Buffer.from(emitterAddress).toString("hex"))
318-
);
313+
return {
314+
emitterChain: Number(chainId),
315+
emitterAddress: Buffer.from(emitterAddress).toString("hex"),
316+
};
319317
}
320318

321319
async getBaseUpdateFee() {

0 commit comments

Comments
 (0)