|
| 1 | +import { type Factory, inject, injectable } from "inversify"; |
| 2 | +import { Either, Left, Right } from "purify-ts"; |
| 3 | + |
| 4 | +import type { JSONRPCRequest } from "../../../../api/model/eip/EIPTypes.js"; |
| 5 | +import { |
| 6 | + SolanaJSONRPCRequest, |
| 7 | + SolanaJsonRpcResponse, |
| 8 | +} from "../../../../api/model/solana/SolanaTypes.js"; |
| 9 | +import { backendModuleTypes } from "../../../backend/backendModuleTypes.js"; |
| 10 | +import { type BackendService } from "../../../backend/BackendService.js"; |
| 11 | +import { isJsonRpcResponse } from "../../../backend/types.js"; |
| 12 | +import { contextModuleTypes } from "../../../context/contextModuleTypes.js"; |
| 13 | +import { type ContextService } from "../../../context/ContextService.js"; |
| 14 | +import { loggerModuleTypes } from "../../../logger/loggerModuleTypes.js"; |
| 15 | +import { LoggerPublisher } from "../../../logger/service/LoggerPublisher.js"; |
| 16 | +import { |
| 17 | + DEFAULT_SOLANA_CLUSTER, |
| 18 | + getClusterFromCurrencyId, |
| 19 | +} from "../../utils/clusterUtils.js"; |
| 20 | + |
| 21 | +@injectable() |
| 22 | +export class SolanaRemoteDatasource { |
| 23 | + private readonly logger: LoggerPublisher; |
| 24 | + |
| 25 | + constructor( |
| 26 | + @inject(loggerModuleTypes.LoggerPublisher) |
| 27 | + private readonly loggerFactory: Factory<LoggerPublisher>, |
| 28 | + @inject(backendModuleTypes.BackendService) |
| 29 | + private readonly backendService: BackendService, |
| 30 | + @inject(contextModuleTypes.ContextService) |
| 31 | + private readonly contextService: ContextService, |
| 32 | + ) { |
| 33 | + this.logger = this.loggerFactory("SolanaRemoteDatasource"); |
| 34 | + } |
| 35 | + |
| 36 | + async JSONRPCRequest( |
| 37 | + args: SolanaJSONRPCRequest, |
| 38 | + ): Promise<Either<Error, SolanaJsonRpcResponse>> { |
| 39 | + try { |
| 40 | + const cluster = this.resolveCluster(); |
| 41 | + const response = await this.backendService.broadcast({ |
| 42 | + blockchain: { |
| 43 | + name: "solana", |
| 44 | + chainId: cluster, |
| 45 | + }, |
| 46 | + rpc: args as unknown as JSONRPCRequest, |
| 47 | + }); |
| 48 | + |
| 49 | + if (response.isLeft()) { |
| 50 | + return Left( |
| 51 | + new Error("Error in Solana JSONRPCRequest", { |
| 52 | + cause: response.extract(), |
| 53 | + }), |
| 54 | + ); |
| 55 | + } |
| 56 | + if (response.isRight() && isJsonRpcResponse(response.extract())) { |
| 57 | + return Right(response.extract() as SolanaJsonRpcResponse); |
| 58 | + } |
| 59 | + return Left( |
| 60 | + new Error("Error in Solana JSONRPCRequest", { |
| 61 | + cause: response.extract(), |
| 62 | + }), |
| 63 | + ); |
| 64 | + } catch (error) { |
| 65 | + this.logger.error("Error in Solana JSONRPCRequest", { error }); |
| 66 | + return Left(new Error("Error in Solana JSONRPCRequest", { cause: error })); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private resolveCluster(): string { |
| 71 | + const selectedAccount = this.contextService.getContext().selectedAccount; |
| 72 | + if (selectedAccount?.currencyId) { |
| 73 | + return getClusterFromCurrencyId(selectedAccount.currencyId); |
| 74 | + } |
| 75 | + return DEFAULT_SOLANA_CLUSTER; |
| 76 | + } |
| 77 | +} |
0 commit comments