1
- import { readdirSync , readFileSync , writeFileSync } from "fs" ;
2
1
import { Storable } from "./base" ;
3
2
import {
3
+ ChainName ,
4
4
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" ;
13
11
import { AptosClient } from "aptos" ;
14
12
15
13
export abstract class Chain extends Storable {
@@ -27,11 +25,11 @@ export abstract class Chain extends Storable {
27
25
* @param exponent the new fee exponent to set
28
26
*/
29
27
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 ,
32
30
BigInt ( fee ) ,
33
31
BigInt ( exponent )
34
- ) . serialize ( ) ;
32
+ ) . encode ( ) ;
35
33
}
36
34
37
35
/**
@@ -84,10 +82,10 @@ export class CosmWasmChain extends Chain {
84
82
}
85
83
86
84
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 ,
89
87
codeId
90
- ) . serialize ( ) ;
88
+ ) . encode ( ) ;
91
89
}
92
90
}
93
91
@@ -115,37 +113,42 @@ export class SuiChain extends Chain {
115
113
return SuiChain . type ;
116
114
}
117
115
116
+ //TODO: Move this logic to xc_admin_common
118
117
private wrapWithWormholeGovernancePayload (
119
118
actionVariant : number ,
120
119
payload : Buffer
121
120
) : 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 ( [
124
126
Buffer . from (
125
127
"0000000000000000000000000000000000000000000000000000000000000001" ,
126
128
"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 ;
133
135
}
134
136
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
+ */
135
141
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 ( ) ;
140
143
return this . wrapWithWormholeGovernancePayload ( 0 , setFee ) ;
141
144
}
142
145
143
146
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
146
149
BigInt ( fee ) ,
147
150
BigInt ( exponent )
148
- ) . serialize ( ) ;
151
+ ) . encode ( ) ;
149
152
return this . wrapWithWormholeGovernancePayload ( 3 , setFee ) ;
150
153
}
151
154
}
@@ -162,11 +165,12 @@ export class EVMChain extends Chain {
162
165
return new EVMChain ( parsed . id , parsed . rpcUrl ) ;
163
166
}
164
167
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 ( ) ;
170
174
}
171
175
172
176
toJson ( ) : any {
@@ -193,19 +197,20 @@ export class AptosChain extends Chain {
193
197
return new AptosClient ( this . rpcUrl ) ;
194
198
}
195
199
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
+ */
196
204
generateGovernanceUpgradePayload ( digest : string ) : Buffer {
197
- return new SuiAuthorizeUpgradeContractInstruction (
198
- CHAINS [ "aptos" ] ,
199
- new HexString32Bytes ( digest )
200
- ) . serialize ( ) ;
205
+ return new AptosAuthorizeUpgradeContract ( "aptos" , digest ) . encode ( ) ;
201
206
}
202
207
203
208
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
206
211
BigInt ( fee ) ,
207
212
BigInt ( exponent )
208
- ) . serialize ( ) ;
213
+ ) . encode ( ) ;
209
214
}
210
215
211
216
getType ( ) : string {
0 commit comments