|
| 1 | +import { GrpcConfigs, ResubOpts } from './types'; |
| 2 | +import { Program } from '@coral-xyz/anchor'; |
| 3 | +import { Context, MemcmpFilter, PublicKey } from '@solana/web3.js'; |
| 4 | +import * as Buffer from 'buffer'; |
| 5 | +import { WebSocketProgramAccountSubscriber } from './webSocketProgramAccountSubscriber'; |
| 6 | + |
| 7 | +import { |
| 8 | + LaserCommitmentLevel, |
| 9 | + LaserSubscribe, |
| 10 | + LaserstreamConfig, |
| 11 | + LaserSubscribeRequest, |
| 12 | + LaserSubscribeUpdate, |
| 13 | + CompressionAlgorithms, |
| 14 | + CommitmentLevel, |
| 15 | +} from '../isomorphic/grpc'; |
| 16 | + |
| 17 | +type LaserCommitment = |
| 18 | + (typeof LaserCommitmentLevel)[keyof typeof LaserCommitmentLevel]; |
| 19 | + |
| 20 | +export class LaserstreamProgramAccountSubscriber< |
| 21 | + T, |
| 22 | +> extends WebSocketProgramAccountSubscriber<T> { |
| 23 | + private stream: |
| 24 | + | { |
| 25 | + id: string; |
| 26 | + cancel: () => void; |
| 27 | + write?: (req: LaserSubscribeRequest) => Promise<void>; |
| 28 | + } |
| 29 | + | undefined; |
| 30 | + |
| 31 | + private commitmentLevel: CommitmentLevel; |
| 32 | + public listenerId?: number; |
| 33 | + |
| 34 | + private readonly laserConfig: LaserstreamConfig; |
| 35 | + |
| 36 | + private constructor( |
| 37 | + laserConfig: LaserstreamConfig, |
| 38 | + commitmentLevel: CommitmentLevel, |
| 39 | + subscriptionName: string, |
| 40 | + accountDiscriminator: string, |
| 41 | + program: Program, |
| 42 | + decodeBufferFn: (accountName: string, ix: Buffer) => T, |
| 43 | + options: { filters: MemcmpFilter[] } = { filters: [] }, |
| 44 | + resubOpts?: ResubOpts |
| 45 | + ) { |
| 46 | + super( |
| 47 | + subscriptionName, |
| 48 | + accountDiscriminator, |
| 49 | + program, |
| 50 | + decodeBufferFn, |
| 51 | + options, |
| 52 | + resubOpts |
| 53 | + ); |
| 54 | + this.laserConfig = laserConfig; |
| 55 | + this.commitmentLevel = this.toLaserCommitment(commitmentLevel); |
| 56 | + } |
| 57 | + |
| 58 | + public static async create<U>( |
| 59 | + grpcConfigs: GrpcConfigs, |
| 60 | + subscriptionName: string, |
| 61 | + accountDiscriminator: string, |
| 62 | + program: Program, |
| 63 | + decodeBufferFn: (accountName: string, ix: Buffer) => U, |
| 64 | + options: { filters: MemcmpFilter[] } = { |
| 65 | + filters: [], |
| 66 | + }, |
| 67 | + resubOpts?: ResubOpts |
| 68 | + ): Promise<LaserstreamProgramAccountSubscriber<U>> { |
| 69 | + const laserConfig: LaserstreamConfig = { |
| 70 | + apiKey: grpcConfigs.token, |
| 71 | + endpoint: grpcConfigs.endpoint, |
| 72 | + maxReconnectAttempts: grpcConfigs.enableReconnect ? 10 : 0, |
| 73 | + channelOptions: { |
| 74 | + 'grpc.default_compression_algorithm': CompressionAlgorithms.zstd, |
| 75 | + 'grpc.max_receive_message_length': 1_000_000_000, |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const commitmentLevel = |
| 80 | + grpcConfigs.commitmentLevel ?? CommitmentLevel.CONFIRMED; |
| 81 | + |
| 82 | + return new LaserstreamProgramAccountSubscriber<U>( |
| 83 | + laserConfig, |
| 84 | + commitmentLevel, |
| 85 | + subscriptionName, |
| 86 | + accountDiscriminator, |
| 87 | + program, |
| 88 | + decodeBufferFn, |
| 89 | + options, |
| 90 | + resubOpts |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + async subscribe( |
| 95 | + onChange: ( |
| 96 | + accountId: PublicKey, |
| 97 | + data: T, |
| 98 | + context: Context, |
| 99 | + buffer: Buffer |
| 100 | + ) => void |
| 101 | + ): Promise<void> { |
| 102 | + if (this.listenerId != null || this.isUnsubscribing) return; |
| 103 | + |
| 104 | + this.onChange = onChange; |
| 105 | + |
| 106 | + const filters = this.options.filters.map((filter) => { |
| 107 | + return { |
| 108 | + memcmp: { |
| 109 | + offset: filter.memcmp.offset, |
| 110 | + base58: filter.memcmp.bytes, |
| 111 | + }, |
| 112 | + }; |
| 113 | + }); |
| 114 | + |
| 115 | + const request: LaserSubscribeRequest = { |
| 116 | + slots: {}, |
| 117 | + accounts: { |
| 118 | + drift: { |
| 119 | + account: [], |
| 120 | + owner: [this.program.programId.toBase58()], |
| 121 | + filters, |
| 122 | + }, |
| 123 | + }, |
| 124 | + transactions: {}, |
| 125 | + blocks: {}, |
| 126 | + blocksMeta: {}, |
| 127 | + accountsDataSlice: [], |
| 128 | + commitment: this.commitmentLevel, |
| 129 | + entry: {}, |
| 130 | + transactionsStatus: {}, |
| 131 | + }; |
| 132 | + |
| 133 | + try { |
| 134 | + const stream = await LaserSubscribe( |
| 135 | + this.laserConfig, |
| 136 | + request, |
| 137 | + async (update: LaserSubscribeUpdate) => { |
| 138 | + if (update.account) { |
| 139 | + const slot = Number(update.account.slot); |
| 140 | + const acc = update.account.account; |
| 141 | + |
| 142 | + const accountInfo = { |
| 143 | + owner: new PublicKey(acc.owner), |
| 144 | + lamports: Number(acc.lamports), |
| 145 | + data: Buffer.Buffer.from(acc.data), |
| 146 | + executable: acc.executable, |
| 147 | + rentEpoch: Number(acc.rentEpoch), |
| 148 | + }; |
| 149 | + |
| 150 | + const payload = { |
| 151 | + accountId: new PublicKey(acc.pubkey), |
| 152 | + accountInfo, |
| 153 | + }; |
| 154 | + |
| 155 | + if (this.resubOpts?.resubTimeoutMs) { |
| 156 | + this.receivingData = true; |
| 157 | + clearTimeout(this.timeoutId); |
| 158 | + this.handleRpcResponse({ slot }, payload); |
| 159 | + this.setTimeout(); |
| 160 | + } else { |
| 161 | + this.handleRpcResponse({ slot }, payload); |
| 162 | + } |
| 163 | + } |
| 164 | + }, |
| 165 | + async (error) => { |
| 166 | + console.error('LaserStream client error:', error); |
| 167 | + throw error; |
| 168 | + } |
| 169 | + ); |
| 170 | + |
| 171 | + this.stream = stream; |
| 172 | + this.listenerId = 1; |
| 173 | + |
| 174 | + if (this.resubOpts?.resubTimeoutMs) { |
| 175 | + this.receivingData = true; |
| 176 | + this.setTimeout(); |
| 177 | + } |
| 178 | + } catch (err) { |
| 179 | + console.error('Failed to start LaserStream client:', err); |
| 180 | + throw err; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public async unsubscribe(onResub = false): Promise<void> { |
| 185 | + if (!onResub && this.resubOpts) { |
| 186 | + this.resubOpts.resubTimeoutMs = undefined; |
| 187 | + } |
| 188 | + this.isUnsubscribing = true; |
| 189 | + clearTimeout(this.timeoutId); |
| 190 | + this.timeoutId = undefined; |
| 191 | + |
| 192 | + if (this.listenerId != null && this.stream) { |
| 193 | + try { |
| 194 | + this.stream.cancel(); |
| 195 | + } finally { |
| 196 | + this.listenerId = undefined; |
| 197 | + this.isUnsubscribing = false; |
| 198 | + } |
| 199 | + } else { |
| 200 | + this.isUnsubscribing = false; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + public toLaserCommitment( |
| 205 | + level: string | number | undefined |
| 206 | + ): LaserCommitment { |
| 207 | + if (typeof level === 'string') { |
| 208 | + return ( |
| 209 | + (LaserCommitmentLevel as any)[level.toUpperCase()] ?? |
| 210 | + LaserCommitmentLevel.CONFIRMED |
| 211 | + ); |
| 212 | + } |
| 213 | + return (level as LaserCommitment) ?? LaserCommitmentLevel.CONFIRMED; |
| 214 | + } |
| 215 | +} |
0 commit comments