5
5
"fmt"
6
6
7
7
"github.com/gagliardetto/solana-go"
8
+ addresslookuptable "github.com/gagliardetto/solana-go/programs/address-lookup-table"
8
9
"github.com/gagliardetto/solana-go/rpc"
9
10
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client"
10
11
)
@@ -181,7 +182,7 @@ func generatePDAs(publicKeys []*solana.AccountMeta, seeds [][]byte, lookup PDALo
181
182
return addresses , nil
182
183
}
183
184
184
- func (s * SolanaChainWriterService ) getDerivedTableMap (ctx context.Context , lookupTables LookupTables , debugID string ) (map [string ]map [string ][]* solana.AccountMeta , map [solana.PublicKey ]solana.PublicKeySlice , error ) {
185
+ func (s * SolanaChainWriterService ) ResolveLookupTables (ctx context.Context , lookupTables LookupTables , debugID string ) (map [string ]map [string ][]* solana.AccountMeta , map [solana.PublicKey ]solana.PublicKeySlice , error ) {
185
186
derivedTableMap := make (map [string ]map [string ][]* solana.AccountMeta )
186
187
staticTableMap := make (map [solana.PublicKey ]solana.PublicKeySlice )
187
188
@@ -211,23 +212,8 @@ func (s *SolanaChainWriterService) getDerivedTableMap(ctx context.Context, looku
211
212
return nil , nil , errorWithDebugID (fmt .Errorf ("invalid static lookup table address: %s, error: %w" , staticTable , err ), debugID )
212
213
}
213
214
214
- // Fetch the account info for the static table
215
- accountInfo , err := s .reader .GetAccountInfoWithOpts (ctx , tableAddress , & rpc.GetAccountInfoOpts {
216
- Encoding : "base64" ,
217
- Commitment : rpc .CommitmentConfirmed ,
218
- })
219
- if err != nil || accountInfo == nil || accountInfo .Value == nil {
220
- return nil , nil , errorWithDebugID (fmt .Errorf ("error fetching account info for static table: %s, error: %w" , staticTable , err ), debugID )
221
- }
222
-
223
- // Decode the account data into an array of public keys
224
- addresses , err := decodeLookupTable (accountInfo .Value .Data .GetBinary ())
225
- if err != nil {
226
- return nil , nil , errorWithDebugID (fmt .Errorf ("error decoding static lookup table data for %s: %w" , staticTable , err ), debugID )
227
- }
228
-
229
- // Add the static lookup table to the map
230
- staticTableMap [tableAddress ] = addresses
215
+ addressses , err := getLookupTableAddress (ctx , s .reader , tableAddress , debugID )
216
+ staticTableMap [tableAddress ] = addressses
231
217
}
232
218
233
219
return derivedTableMap , staticTableMap , nil
@@ -246,18 +232,9 @@ func (s *SolanaChainWriterService) LoadTable(rlt DerivedLookupTable, ctx context
246
232
// Iterate over each address of the lookup table
247
233
for _ , addressMeta := range lookupTableAddresses {
248
234
// Fetch account info
249
- accountInfo , err := reader .GetAccountInfoWithOpts (ctx , addressMeta .PublicKey , & rpc.GetAccountInfoOpts {
250
- Encoding : "base64" ,
251
- Commitment : rpc .CommitmentConfirmed ,
252
- })
253
- if err != nil || accountInfo == nil || accountInfo .Value == nil {
254
- return nil , nil , errorWithDebugID (fmt .Errorf ("error fetching account info for address %s: %w" , addressMeta .PublicKey .String (), err ), debugID )
255
- }
256
-
257
- // Decode the account data into an array of public keys
258
- addresses , err := decodeLookupTable (accountInfo .Value .Data .GetBinary ())
235
+ addresses , err := getLookupTableAddress (ctx , reader , addressMeta .PublicKey , debugID )
259
236
if err != nil {
260
- return nil , nil , errorWithDebugID (fmt .Errorf ("error decoding lookup table data for address %s : %w" , addressMeta . PublicKey . String () , err ), debugID )
237
+ return nil , nil , errorWithDebugID (fmt .Errorf ("error fetching lookup table address: %w" , err ), debugID )
261
238
}
262
239
263
240
// Create the inner map for this lookup table
@@ -281,18 +258,19 @@ func (s *SolanaChainWriterService) LoadTable(rlt DerivedLookupTable, ctx context
281
258
return resultMap , lookupTableMetas , nil
282
259
}
283
260
284
- func decodeLookupTable (data []byte ) (solana.PublicKeySlice , error ) {
285
- // Example logic to decode lookup table data; you may need to adjust based on the actual format of the data.
286
- var addresses solana.PublicKeySlice
261
+ func getLookupTableAddress (ctx context.Context , reader client.Reader , tableAddress solana.PublicKey , debugID string ) (solana.PublicKeySlice , error ) {
262
+ // Fetch the account info for the static table
263
+ accountInfo , err := reader .GetAccountInfoWithOpts (ctx , tableAddress , & rpc.GetAccountInfoOpts {
264
+ Encoding : "base64" ,
265
+ Commitment : rpc .CommitmentConfirmed ,
266
+ })
287
267
288
- // Assuming the data is a list of 32-byte public keys in binary format:
289
- for i := 0 ; i < len (data ); i += solana .PublicKeyLength {
290
- if i + solana .PublicKeyLength > len (data ) {
291
- return nil , fmt .Errorf ("invalid lookup table data length" )
292
- }
293
- address := solana .PublicKeyFromBytes (data [i : i + solana .PublicKeyLength ])
294
- addresses = append (addresses , address )
268
+ if err != nil || accountInfo == nil || accountInfo .Value == nil {
269
+ return nil , errorWithDebugID (fmt .Errorf ("error fetching account info for table: %s, error: %w" , tableAddress .String (), err ), debugID )
295
270
}
296
-
297
- return addresses , nil
271
+ alt , err := addresslookuptable .DecodeAddressLookupTableState (accountInfo .GetBinary ())
272
+ if err != nil {
273
+ return nil , errorWithDebugID (fmt .Errorf ("error decoding address lookup table state: %w" , err ), debugID )
274
+ }
275
+ return alt .Addresses , nil
298
276
}
0 commit comments