@@ -10,7 +10,7 @@ import {
10
10
nativeTokenTransferLayout
11
11
} from './nttLayout'
12
12
import { derivePostedVaaKey , getWormholeDerivedAccounts } from '@certusone/wormhole-sdk/lib/cjs/solana/wormhole'
13
- import { BN , translateError , type IdlAccounts , Program } from '@coral-xyz/anchor'
13
+ import { BN , translateError , type IdlAccounts , Program , AnchorProvider , Wallet , } from '@coral-xyz/anchor'
14
14
import { associatedAddress } from '@coral-xyz/anchor/dist/cjs/utils/token'
15
15
import { getAssociatedTokenAddressSync } from '@solana/spl-token'
16
16
import {
@@ -19,7 +19,9 @@ import {
19
19
Transaction ,
20
20
sendAndConfirmTransaction ,
21
21
type TransactionSignature ,
22
- type Connection
22
+ type Connection ,
23
+ TransactionMessage ,
24
+ VersionedTransaction
23
25
} from '@solana/web3.js'
24
26
import { Keccak } from 'sha3'
25
27
import { type ExampleNativeTokenTransfers as RawExampleNativeTokenTransfers } from '../../target/types/example_native_token_transfers'
@@ -170,6 +172,44 @@ export class NTT {
170
172
return this . derivePda ( [ 'registered_transceiver' , transceiver . toBytes ( ) ] )
171
173
}
172
174
175
+ // View functions
176
+
177
+ async version ( pubkey : PublicKey ) : Promise < string > {
178
+ // the anchor library has a built-in method to read view functions. However,
179
+ // it requires a signer, which would trigger a wallet prompt on the frontend.
180
+ // Instead, we manually construct a versioned transaction and call the
181
+ // simulate function with sigVerify: false below.
182
+ //
183
+ // This way, the simulation won't require a signer, but it still requires
184
+ // the pubkey of an account that has some lamports in it (since the
185
+ // simulation checks if the account has enough money to pay for the transaction).
186
+ //
187
+ // It's a little unfortunate but it's the best we can do.
188
+ const ix = await this . program . methods . version ( )
189
+ . accountsStrict ( { } ) . instruction ( )
190
+ const latestBlockHash = await this . program . provider . connection . getLatestBlockhash ( )
191
+
192
+ const msg = new TransactionMessage ( {
193
+ payerKey : pubkey ,
194
+ recentBlockhash : latestBlockHash . blockhash ,
195
+ instructions : [ ix ] ,
196
+ } ) . compileToV0Message ( ) ;
197
+
198
+ const tx = new VersionedTransaction ( msg ) ;
199
+
200
+ const txSimulation =
201
+ await this . program . provider . connection
202
+ . simulateTransaction ( tx , {
203
+ sigVerify : false ,
204
+ } )
205
+
206
+ // the return buffer is in base64 and it encodes the string with a 32 bit
207
+ // little endian length prefix.
208
+ const buffer = Buffer . from ( txSimulation . value . returnData ?. data [ 0 ] , 'base64' )
209
+ const len = buffer . readUInt32LE ( 0 )
210
+ return buffer . slice ( 4 , len + 4 ) . toString ( )
211
+ }
212
+
173
213
// Instructions
174
214
175
215
async initialize ( args : {
0 commit comments