11import { type Address , assertIsAddress } from '@solana/addresses' ;
2+ import {
3+ SOLANA_ERROR__TRANSACTION_INVALID_NONCE_TRANSACTION_FIRST_INSTRUCTION_NOT_ADVANCE_NONCE ,
4+ SOLANA_ERROR__TRANSACTION_INVALID_NONCE_TRANSACTION_NO_INSTRUCTIONS ,
5+ SOLANA_ERROR__TRANSACTION_MISSING_ADDRESS ,
6+ SOLANA_ERROR__TRANSACTION_MISSING_FEE_PAYER ,
7+ SolanaError ,
8+ } from '@solana/errors' ;
29import { pipe } from '@solana/functional' ;
310import type { IAccountMeta , IInstruction } from '@solana/instructions' ;
411import { AccountRole } from '@solana/instructions' ;
@@ -33,8 +40,9 @@ function convertAccount(
3340) : IAccountMeta {
3441 const accountPublicKey = accountKeys . get ( accountIndex ) ;
3542 if ( ! accountPublicKey ) {
36- // TODO coded error
37- throw new Error ( `Could not find account address at index ${ accountIndex } ` ) ;
43+ throw new SolanaError ( SOLANA_ERROR__TRANSACTION_MISSING_ADDRESS , {
44+ index : accountIndex ,
45+ } ) ;
3846 }
3947 const isSigner = message . isAccountSigner ( accountIndex ) ;
4048 const isWritable = message . isAccountWritable ( accountIndex ) ;
@@ -60,8 +68,9 @@ function convertInstruction(
6068) : IInstruction {
6169 const programAddressPublicKey = accountKeys . get ( instruction . programIdIndex ) ;
6270 if ( ! programAddressPublicKey ) {
63- // TODO coded error
64- throw new Error ( `Could not find program address at index ${ instruction . programIdIndex } ` ) ;
71+ throw new SolanaError ( SOLANA_ERROR__TRANSACTION_MISSING_ADDRESS , {
72+ index : instruction . programIdIndex ,
73+ } ) ;
6574 }
6675
6776 const accounts = instruction . accountKeyIndexes . map ( accountIndex =>
@@ -99,15 +108,16 @@ export function fromVersionedTransactionWithBlockhash(
99108 // - will need to convert account instructions to `IAccountLookupMeta` when appropriate
100109 if ( transaction . message . addressTableLookups . length > 0 ) {
101110 // TODO coded error
111+ // This should probably not be a `SolanaError`, since we need to add
112+ // this functionality.
102113 throw new Error ( 'Cannot convert transaction with addressTableLookups' ) ;
103114 }
104115
105116 const accountKeys = transaction . message . getAccountKeys ( ) ;
106117
107118 // Fee payer is first account
108119 const feePayer = accountKeys . staticAccountKeys [ 0 ] ;
109- // TODO: coded error
110- if ( ! feePayer ) throw new Error ( 'No fee payer set in VersionedTransaction' ) ;
120+ if ( ! feePayer ) throw new SolanaError ( SOLANA_ERROR__TRANSACTION_MISSING_FEE_PAYER ) ;
111121
112122 const blockhashLifetime = {
113123 blockhash : transaction . message . recentBlockhash as Blockhash ,
@@ -140,29 +150,28 @@ export function fromVersionedTransactionWithDurableNonce(
140150 // - will need to convert account instructions to `IAccountLookupMeta` when appropriate
141151 if ( transaction . message . addressTableLookups . length > 0 ) {
142152 // TODO coded error
153+ // This should probably not be a `SolanaError`, since we need to add
154+ // this functionality.
143155 throw new Error ( 'Cannot convert transaction with addressTableLookups' ) ;
144156 }
145157
146158 const accountKeys = transaction . message . getAccountKeys ( ) ;
147159
148160 // Fee payer is first account
149161 const feePayer = accountKeys . staticAccountKeys [ 0 ] ;
150- // TODO: coded error
151- if ( ! feePayer ) throw new Error ( 'No fee payer set in VersionedTransaction' ) ;
162+ if ( ! feePayer ) throw new SolanaError ( SOLANA_ERROR__TRANSACTION_MISSING_FEE_PAYER ) ;
152163
153164 const instructions = transaction . message . compiledInstructions . map ( instruction =>
154165 convertInstruction ( transaction . message , accountKeys , instruction ) ,
155166 ) ;
156167
157168 // Check first instruction is durable nonce + extract params
158169 if ( instructions . length === 0 ) {
159- // TODO: coded error
160- throw new Error ( 'transaction with no instructions cannot be durable nonce transaction' ) ;
170+ throw new SolanaError ( SOLANA_ERROR__TRANSACTION_INVALID_NONCE_TRANSACTION_NO_INSTRUCTIONS ) ;
161171 }
162172
163173 if ( ! isAdvanceNonceAccountInstruction ( instructions [ 0 ] ) ) {
164- // TODO: coded error
165- throw new Error ( 'transaction first instruction is not advance nonce account instruction' ) ;
174+ throw new SolanaError ( SOLANA_ERROR__TRANSACTION_INVALID_NONCE_TRANSACTION_FIRST_INSTRUCTION_NOT_ADVANCE_NONCE ) ;
166175 }
167176
168177 // We know these accounts are defined because we checked `isAdvanceNonceAccountInstruction`
0 commit comments