@@ -55,6 +55,7 @@ import {
5555 type OracleType ,
5656 type IPrepareCreateStreamSolanaExt ,
5757 type IPrepareStreamSolanaExt ,
58+ type AlignedUnlocksContract ,
5859} from "./types.js" ;
5960import {
6061 decodeStream ,
@@ -116,7 +117,7 @@ import type { IPartnerLayout } from "./instructionTypes.js";
116117import { calculateTotalAmountToDeposit } from "../common/utils.js" ;
117118import { WITHDRAW_AVAILABLE_AMOUNT } from "../common/constants.js" ;
118119import type { StreamflowAlignedUnlocks as AlignedUnlocksProgramType } from "./descriptor/streamflow_aligned_unlocks.js" ;
119- import StreamflowAlignedUnlocksIDL from "./descriptor/idl/streamflow_aligned_unlocks.json" with { type : "json" } ;
120+ import StreamflowAlignedUnlocksIDL from "./descriptor/idl/streamflow_aligned_unlocks.json" ;
120121import { deriveContractPDA , deriveEscrowPDA , deriveTestOraclePDA } from "./lib/derive-accounts.js" ;
121122import { isCreateAlignedStreamData } from "../common/contractUtils.js" ;
122123
@@ -1632,20 +1633,51 @@ export class SolanaStreamClient extends BaseStreamClient {
16321633 filters,
16331634 } ) ;
16341635
1635- const mapped = await Promise . all (
1636- accounts . map ( async ( { pubkey, account } ) => {
1637- const stream = decodeStream ( account . data ) ;
1638- if ( this . isAlignedUnlock ( pubkey , stream . sender ) ) {
1639- const alignedProxy = await this . alignedProxyProgram . account . contract . fetch (
1640- deriveContractPDA ( this . alignedProxyProgram . programId , pubkey ) ,
1641- ) ;
1642- invariant ( alignedProxy , "Couldn't get aligned proxy account info" ) ;
1643- return { publicKey : pubkey , account : new AlignedContract ( stream , alignedProxy ) } ;
1644- }
1645- return { publicKey : pubkey , account : new Contract ( stream ) } ;
1646- } ) ,
1647- ) ;
1648- return mapped ;
1636+ const decoded = this . decodeStreamsFromAccounts ( accounts ) ;
1637+ const alignedEntries = this . collectAlignedEntries ( decoded ) ;
1638+ const alignedProxies = await this . fetchAlignedProxiesByEntries ( alignedEntries ) ;
1639+ return this . mapDecodedToProgramAccounts ( decoded , alignedEntries , alignedProxies ) ;
1640+ }
1641+
1642+ private decodeStreamsFromAccounts ( accounts : ReadonlyArray < { pubkey : PublicKey ; account : { data : Buffer } } > ) {
1643+ return accounts . map ( ( { pubkey, account } ) => ( {
1644+ pubkey,
1645+ stream : decodeStream ( account . data ) ,
1646+ } ) ) ;
1647+ }
1648+
1649+ private collectAlignedEntries ( decoded : { pubkey : PublicKey ; stream : DecodedStream } [ ] ) {
1650+ const entries : { index : number ; pda : PublicKey } [ ] = [ ] ;
1651+ decoded . forEach ( ( { pubkey, stream } , index ) => {
1652+ if ( this . isAlignedUnlock ( pubkey , stream . sender ) ) {
1653+ entries . push ( { index, pda : deriveContractPDA ( this . alignedProxyProgram . programId , pubkey ) } ) ;
1654+ }
1655+ } ) ;
1656+ return entries ;
1657+ }
1658+
1659+ private async fetchAlignedProxiesByEntries (
1660+ aligned : { index : number ; pda : PublicKey } [ ] ,
1661+ ) : Promise < ReadonlyArray < AlignedUnlocksContract | null > > {
1662+ if ( aligned . length === 0 ) return [ ] ;
1663+ const res = await this . alignedProxyProgram . account . contract . fetchMultiple ( aligned . map ( ( a ) => a . pda ) ) ;
1664+ return res as ReadonlyArray < AlignedUnlocksContract | null > ;
1665+ }
1666+
1667+ private mapDecodedToProgramAccounts (
1668+ decoded : { pubkey : PublicKey ; stream : DecodedStream } [ ] ,
1669+ alignedEntries : { index : number ; pda : PublicKey } [ ] ,
1670+ alignedProxies : ReadonlyArray < AlignedUnlocksContract | null > ,
1671+ ) : IProgramAccount < Stream > [ ] {
1672+ return decoded . map ( ( { pubkey, stream } , idx ) => {
1673+ const alignedIdx = alignedEntries . findIndex ( ( a ) => a . index === idx ) ;
1674+ if ( alignedIdx !== - 1 ) {
1675+ const alignedProxy = alignedProxies [ alignedIdx ] ;
1676+ invariant ( alignedProxy , "Couldn't get aligned proxy account info" ) ;
1677+ return { publicKey : pubkey , account : new AlignedContract ( stream , alignedProxy ) } ;
1678+ }
1679+ return { publicKey : pubkey , account : new Contract ( stream ) } ;
1680+ } ) ;
16491681 }
16501682
16511683 /**
0 commit comments