11import { Contract , PriceFeed , PrivateKey , TxResult } from "../base" ;
2- import { ApiError , AptosAccount , BCS , TxnBuilderTypes } from "aptos" ;
2+ import { ApiError , BCS , CoinClient , TxnBuilderTypes } from "aptos" ;
33import { AptosChain , Chain } from "../chains" ;
44import { DataSource } from "xc_admin_common" ;
5- import { CoinClient } from "aptos" ;
5+ import { WormholeContract } from "./wormhole" ;
6+
7+ type WormholeState = {
8+ chain_id : { number : string } ;
9+ guardian_set_index : { number : string } ;
10+ guardian_sets : { handle : string } ;
11+ } ;
12+
13+ type GuardianSet = {
14+ guardians : { address : { bytes : string } } [ ] ;
15+ expiration_time : { number : string } ;
16+ index : { number : string } ;
17+ } ;
18+
19+ export class WormholeAptosContract extends WormholeContract {
20+ constructor ( public chain : AptosChain , public address : string ) {
21+ super ( ) ;
22+ }
23+
24+ async getState ( ) : Promise < WormholeState > {
25+ const client = this . chain . getClient ( ) ;
26+ const resources = await client . getAccountResources ( this . address ) ;
27+ const type = "WormholeState" ;
28+ for ( const resource of resources ) {
29+ if ( resource . type === `${ this . address } ::state::${ type } ` ) {
30+ return resource . data as WormholeState ;
31+ }
32+ }
33+ throw new Error ( `${ type } resource not found in account ${ this . address } ` ) ;
34+ }
35+
36+ async getCurrentGuardianSetIndex ( ) : Promise < number > {
37+ const data = await this . getState ( ) ;
38+ return Number ( data . guardian_set_index . number ) ;
39+ }
40+
41+ async getChainId ( ) : Promise < number > {
42+ const data = await this . getState ( ) ;
43+ return Number ( data . chain_id . number ) ;
44+ }
45+
46+ async getGuardianSet ( ) : Promise < string [ ] > {
47+ const data = await this . getState ( ) ;
48+ const client = this . chain . getClient ( ) ;
49+ const result = ( await client . getTableItem ( data . guardian_sets . handle , {
50+ key_type : `u64` ,
51+ value_type : `${ this . address } ::structs::GuardianSet` ,
52+ key : data . guardian_set_index . number . toString ( ) ,
53+ } ) ) as GuardianSet ;
54+ return result . guardians . map ( ( guardian ) => guardian . address . bytes ) ;
55+ }
56+
57+ async upgradeGuardianSets (
58+ senderPrivateKey : PrivateKey ,
59+ vaa : Buffer
60+ ) : Promise < TxResult > {
61+ const txPayload = new TxnBuilderTypes . TransactionPayloadEntryFunction (
62+ TxnBuilderTypes . EntryFunction . natural (
63+ `${ this . address } ::guardian_set_upgrade` ,
64+ "submit_vaa_entry" ,
65+ [ ] ,
66+ [ BCS . bcsSerializeBytes ( vaa ) ]
67+ )
68+ ) ;
69+ return this . chain . sendTransaction ( senderPrivateKey , txPayload ) ;
70+ }
71+ }
672
773export class AptosContract extends Contract {
874 static type = "AptosContract" ;
@@ -45,25 +111,11 @@ export class AptosContract extends Contract {
45111 [ BCS . bcsSerializeBytes ( vaa ) ]
46112 )
47113 ) ;
48- return this . sendTransaction ( senderPrivateKey , txPayload ) ;
114+ return this . chain . sendTransaction ( senderPrivateKey , txPayload ) ;
49115 }
50116
51- private async sendTransaction (
52- senderPrivateKey : PrivateKey ,
53- txPayload : TxnBuilderTypes . TransactionPayloadEntryFunction
54- ) : Promise < TxResult > {
55- const client = this . chain . getClient ( ) ;
56- const sender = new AptosAccount (
57- new Uint8Array ( Buffer . from ( senderPrivateKey , "hex" ) )
58- ) ;
59- const result = await client . generateSignSubmitWaitForTransaction (
60- sender ,
61- txPayload ,
62- {
63- maxGasAmount : BigInt ( 30000 ) ,
64- }
65- ) ;
66- return { id : result . hash , info : result } ;
117+ public getWormholeContract ( ) : WormholeAptosContract {
118+ return new WormholeAptosContract ( this . chain , this . wormholeStateId ) ;
67119 }
68120
69121 async executeUpdatePriceFeed (
@@ -78,7 +130,7 @@ export class AptosContract extends Contract {
78130 [ BCS . serializeVectorWithFunc ( vaas , "serializeBytes" ) ]
79131 )
80132 ) ;
81- return this . sendTransaction ( senderPrivateKey , txPayload ) ;
133+ return this . chain . sendTransaction ( senderPrivateKey , txPayload ) ;
82134 }
83135
84136 getStateResources ( ) {
0 commit comments