@@ -12,14 +12,20 @@ import {
12
12
DataSource ,
13
13
EvmSetWormholeAddress ,
14
14
} from "xc_admin_common" ;
15
- import { AptosClient } from "aptos" ;
15
+ import { AptosClient , AptosAccount , CoinClient } from "aptos" ;
16
16
import Web3 from "web3" ;
17
17
import {
18
18
CosmwasmExecutor ,
19
19
CosmwasmQuerier ,
20
20
InjectiveExecutor ,
21
21
} from "@pythnetwork/cosmwasm-deploy-tools" ;
22
22
import { Network } from "@injectivelabs/networks" ;
23
+ import {
24
+ Connection ,
25
+ Ed25519Keypair ,
26
+ JsonRpcProvider ,
27
+ RawSigner ,
28
+ } from "@mysten/sui.js" ;
23
29
24
30
export type ChainConfig = Record < string , string > & {
25
31
mainnet : boolean ;
@@ -96,19 +102,44 @@ export abstract class Chain extends Storable {
96
102
* @param upgradeInfo based on the contract type, this can be a contract address, codeId, package digest, etc.
97
103
*/
98
104
abstract generateGovernanceUpgradePayload ( upgradeInfo : unknown ) : Buffer ;
105
+
106
+ /**
107
+ * Returns the account address associated with the given private key.
108
+ * @param privateKey the account private key
109
+ */
110
+ abstract getAccountAddress ( privateKey : PrivateKey ) : Promise < string > ;
111
+
112
+ /**
113
+ * Returns the balance of the account associated with the given private key.
114
+ * @param privateKey the account private key
115
+ */
116
+ abstract getAccountBalance ( privateKey : PrivateKey ) : Promise < number > ;
99
117
}
100
118
119
+ /**
120
+ * A Chain object that represents all chains. This is used for governance instructions that apply to all chains.
121
+ * For example, governance instructions to upgrade Pyth data sources.
122
+ */
101
123
export class GlobalChain extends Chain {
102
124
static type = "GlobalChain" ;
103
125
constructor ( ) {
104
126
super ( "global" , true , "unset" ) ;
105
127
}
128
+
106
129
generateGovernanceUpgradePayload ( ) : Buffer {
107
130
throw new Error (
108
131
"Can not create a governance message for upgrading contracts on all chains!"
109
132
) ;
110
133
}
111
134
135
+ async getAccountAddress ( _privateKey : PrivateKey ) : Promise < string > {
136
+ throw new Error ( "Can not get account for GlobalChain." ) ;
137
+ }
138
+
139
+ async getAccountBalance ( _privateKey : PrivateKey ) : Promise < number > {
140
+ throw new Error ( "Can not get account balance for GlobalChain." ) ;
141
+ }
142
+
112
143
getType ( ) : string {
113
144
return GlobalChain . type ;
114
145
}
@@ -177,7 +208,9 @@ export class CosmWasmChain extends Chain {
177
208
return new CosmosUpgradeContract ( this . wormholeChainName , codeId ) . encode ( ) ;
178
209
}
179
210
180
- async getExecutor ( privateKey : PrivateKey ) {
211
+ async getExecutor (
212
+ privateKey : PrivateKey
213
+ ) : Promise < CosmwasmExecutor | InjectiveExecutor > {
181
214
if ( this . getId ( ) . indexOf ( "injective" ) > - 1 ) {
182
215
return InjectiveExecutor . fromPrivateKey (
183
216
this . isMainnet ( ) ? Network . Mainnet : Network . Testnet ,
@@ -190,6 +223,20 @@ export class CosmWasmChain extends Chain {
190
223
this . gasPrice + this . feeDenom
191
224
) ;
192
225
}
226
+
227
+ async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
228
+ const executor = await this . getExecutor ( privateKey ) ;
229
+ if ( executor instanceof InjectiveExecutor ) {
230
+ return executor . getAddress ( ) ;
231
+ } else {
232
+ return await executor . getAddress ( ) ;
233
+ }
234
+ }
235
+
236
+ async getAccountBalance ( privateKey : PrivateKey ) : Promise < number > {
237
+ const executor = await this . getExecutor ( privateKey ) ;
238
+ return await executor . getBalance ( ) ;
239
+ }
193
240
}
194
241
195
242
export class SuiChain extends Chain {
@@ -238,6 +285,27 @@ export class SuiChain extends Chain {
238
285
digest
239
286
) . encode ( ) ;
240
287
}
288
+
289
+ getProvider ( ) : JsonRpcProvider {
290
+ return new JsonRpcProvider ( new Connection ( { fullnode : this . rpcUrl } ) ) ;
291
+ }
292
+
293
+ async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
294
+ const provider = this . getProvider ( ) ;
295
+ const keypair = Ed25519Keypair . fromSecretKey (
296
+ Buffer . from ( privateKey , "hex" )
297
+ ) ;
298
+ const wallet = new RawSigner ( keypair , provider ) ;
299
+ return await wallet . getAddress ( ) ;
300
+ }
301
+
302
+ async getAccountBalance ( privateKey : PrivateKey ) : Promise < number > {
303
+ const provider = this . getProvider ( ) ;
304
+ const balance = await provider . getBalance ( {
305
+ owner : await this . getAccountAddress ( privateKey ) ,
306
+ } ) ;
307
+ return Number ( balance . totalBalance ) / 10 ** 9 ;
308
+ }
241
309
}
242
310
243
311
export class EvmChain extends Chain {
@@ -361,6 +429,20 @@ export class EvmChain extends Chain {
361
429
} ) ;
362
430
return deployedContract . options . address ;
363
431
}
432
+
433
+ async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
434
+ const web3 = new Web3 ( this . getRpcUrl ( ) ) ;
435
+ const signer = web3 . eth . accounts . privateKeyToAccount ( privateKey ) ;
436
+ return signer . address ;
437
+ }
438
+
439
+ async getAccountBalance ( privateKey : PrivateKey ) : Promise < number > {
440
+ const web3 = new Web3 ( this . getRpcUrl ( ) ) ;
441
+ const balance = await web3 . eth . getBalance (
442
+ await this . getAccountAddress ( privateKey )
443
+ ) ;
444
+ return Number ( balance ) / 10 ** 18 ;
445
+ }
364
446
}
365
447
366
448
export class AptosChain extends Chain {
@@ -413,4 +495,20 @@ export class AptosChain extends Chain {
413
495
parsed . rpcUrl
414
496
) ;
415
497
}
498
+
499
+ async getAccountAddress ( privateKey : PrivateKey ) : Promise < string > {
500
+ const account = new AptosAccount (
501
+ new Uint8Array ( Buffer . from ( privateKey , "hex" ) )
502
+ ) ;
503
+ return account . address ( ) . toString ( ) ;
504
+ }
505
+
506
+ async getAccountBalance ( privateKey : PrivateKey ) : Promise < number > {
507
+ const client = this . getClient ( ) ;
508
+ const account = new AptosAccount (
509
+ new Uint8Array ( Buffer . from ( privateKey , "hex" ) )
510
+ ) ;
511
+ const coinClient = new CoinClient ( client ) ;
512
+ return Number ( await coinClient . checkBalance ( account ) ) / 10 ** 8 ;
513
+ }
416
514
}
0 commit comments