Skip to content

Commit

Permalink
fix: use both node and permanode for client data fetch (#984)
Browse files Browse the repository at this point in the history
* fix: use both node and permanode for client data fetch

* feat: create mqttClient inside the StardustFeed class

* chore: rename `tryFetchNodeThenPermanode` function

* fix: wrap functions in try catch in `stardustTangleHelper`

* chore: rename `stardustTangleHelper` to `stardustApiService`

* fix: rename tangleHelper
  • Loading branch information
VmMad authored Jan 22, 2024
1 parent 0a15fce commit cc23cfb
Show file tree
Hide file tree
Showing 21 changed files with 619 additions and 646 deletions.
29 changes: 15 additions & 14 deletions api/src/initServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MqttClient as ChrysalisMqttClient } from "@iota/mqtt.js";
import { Client as StardustClient } from "@iota/sdk";
import { IClientOptions, Client as StardustClient } from "@iota/sdk";
import { ServiceFactory } from "./factories/serviceFactory";
import logger from "./logger";
import { IConfiguration } from "./models/configuration/IConfiguration";
Expand All @@ -24,6 +24,7 @@ import { ChronicleService } from "./services/stardust/chronicleService";
import { StardustFeed } from "./services/stardust/feed/stardustFeed";
import { InfluxDBService } from "./services/stardust/influx/influxDbService";
import { NodeInfoService } from "./services/stardust/nodeInfoService";
import { StardustApiService } from "./services/stardust/stardustApiService";
import { StardustStatsService } from "./services/stardust/stats/stardustStatsService";

const CURRENCY_UPDATE_INTERVAL_MS = 5 * 60000;
Expand Down Expand Up @@ -141,30 +142,30 @@ function initChrysalisServices(networkConfig: INetwork): void {
*/
function initStardustServices(networkConfig: INetwork): void {
logger.verbose(`Initializing Stardust services for ${networkConfig.network}`);
const stardustClient = new StardustClient({
nodes: [networkConfig.provider],
brokerOptions: { useWs: true },
});
ServiceFactory.register(`client-${networkConfig.network}`, () => stardustClient);

const stardustClientParams: IClientOptions = {
primaryNode: networkConfig.provider,
};

if (networkConfig.permaNodeEndpoint) {
// Client with permanode needs the ignoreNodeHealth as chronicle is considered "not healthy" by the sdk
// Related: https://github.com/iotaledger/inx-chronicle/issues/1302
const stardustPermanodeClient = new StardustClient({
nodes: [networkConfig.permaNodeEndpoint],
ignoreNodeHealth: true,
});
ServiceFactory.register(`permanode-client-${networkConfig.network}`, () => stardustPermanodeClient);
stardustClientParams.nodes = [networkConfig.permaNodeEndpoint];
stardustClientParams.ignoreNodeHealth = true;

const chronicleService = new ChronicleService(networkConfig);
ServiceFactory.register(`chronicle-${networkConfig.network}`, () => chronicleService);
}

const stardustClient = new StardustClient(stardustClientParams);
ServiceFactory.register(`client-${networkConfig.network}`, () => stardustClient);

const stardustApiService = new StardustApiService(networkConfig);
ServiceFactory.register(`api-service-${networkConfig.network}`, () => stardustApiService);

// eslint-disable-next-line no-void
void NodeInfoService.build(networkConfig).then((nodeInfoService) => {
ServiceFactory.register(`node-info-${networkConfig.network}`, () => nodeInfoService);

const stardustFeed = new StardustFeed(networkConfig.network);
const stardustFeed = new StardustFeed(networkConfig);
ServiceFactory.register(`feed-${networkConfig.network}`, () => stardustFeed);
});

Expand Down
5 changes: 3 additions & 2 deletions api/src/routes/stardust/address/balance/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import IAddressDetailsWithBalance from "../../../../models/api/stardust/IAddress
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -24,5 +24,6 @@ export async function get(config: IConfiguration, request: IAddressBalanceReques
return undefined;
}

return StardustTangleHelper.addressDetails(networkConfig, request.address);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.addressDetails(request.address);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/address/outputs/alias/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IAddressDetailsResponse } from "../../../../../models/api/stardust/IAdd
import { IConfiguration } from "../../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../../services/networkService";
import { StardustTangleHelper } from "../../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
Expand All @@ -24,5 +24,6 @@ export async function get(config: IConfiguration, request: IAddressDetailsReques
return {};
}

return StardustTangleHelper.aliasOutputDetailsByAddress(networkConfig, request.address);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.aliasOutputDetailsByAddress(request.address);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/address/outputs/basic/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IAddressDetailsResponse } from "../../../../../models/api/stardust/IAdd
import { IConfiguration } from "../../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../../services/networkService";
import { StardustTangleHelper } from "../../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
Expand All @@ -24,5 +24,6 @@ export async function get(config: IConfiguration, request: IAddressDetailsReques
return {};
}

return StardustTangleHelper.basicOutputDetailsByAddress(networkConfig, request.address);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.basicOutputDetailsByAddress(request.address);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/address/outputs/nft/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IAddressDetailsResponse } from "../../../../../models/api/stardust/IAdd
import { IConfiguration } from "../../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../../services/networkService";
import { StardustTangleHelper } from "../../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../../utils/validationHelper";

/**
Expand All @@ -24,5 +24,6 @@ export async function get(config: IConfiguration, request: IAddressDetailsReques
return {};
}

return StardustTangleHelper.nftOutputDetailsByAddress(networkConfig, request.address);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.nftOutputDetailsByAddress(request.address);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/alias/foundries/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IFoundriesResponse } from "../../../../models/api/stardust/foundry/IFou
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: IFoundriesRequest): P
return {};
}

return StardustTangleHelper.aliasFoundries(networkConfig, request.aliasAddress);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.aliasFoundries(request.aliasAddress);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/alias/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IAliasResponse } from "../../../models/api/stardust/IAliasResponse";
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: IAliasRequest): Promi
return {};
}

return StardustTangleHelper.aliasDetails(networkConfig, request.aliasId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.aliasDetails(request.aliasId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/block/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IBlockResponse } from "../../../models/api/stardust/IBlockResponse";
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(_: IConfiguration, request: IBlockRequest): Promise<IB
return {};
}

return StardustTangleHelper.block(networkConfig, request.blockId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.block(request.blockId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/block/metadata/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IBlockRequest } from "../../../../models/api/stardust/IBlockRequest";
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(_: IConfiguration, request: IBlockRequest): Promise<IB
return {};
}

return StardustTangleHelper.blockDetails(networkConfig, request.blockId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.blockDetails(request.blockId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/foundry/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IFoundryResponse } from "../../../models/api/stardust/foundry/IFoundryR
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: IFoundryRequest): Pro
return {};
}

return StardustTangleHelper.foundryDetails(networkConfig, request.foundryId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.foundryDetails(request.foundryId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/milestone/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IMilestoneDetailsResponse } from "../../../models/api/stardust/mileston
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,7 +25,8 @@ export async function get(config: IConfiguration, request: IMilestoneDetailsRequ
return {};
}

const milestoneDetails = await StardustTangleHelper.milestoneDetailsByIndex(networkConfig, Number(request.milestoneIndex));
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
const milestoneDetails = await stardustApiService.milestoneDetailsByIndex(Number(request.milestoneIndex));

return milestoneDetails;
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/nft/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { INftDetailsResponse } from "../../../models/api/stardust/nft/INftDetail
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: INftDetailsRequest):
return {};
}

return StardustTangleHelper.nftDetails(networkConfig, request.nftId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.nftDetails(request.nftId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/output/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IOutputDetailsResponse } from "../../../models/api/stardust/IOutputDeta
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: IOutputDetailsRequest
return {};
}

return StardustTangleHelper.outputDetails(networkConfig, request.outputId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.outputDetails(request.outputId);
}
7 changes: 4 additions & 3 deletions api/src/routes/stardust/output/tagged/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { INftOutputsResponse } from "../../../../models/api/stardust/nft/INftOut
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { StardustApiService } from "../../../../services/stardust/stardustApiService";
import { Converter } from "../../../../utils/convertUtils";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -29,11 +29,12 @@ export async function get(_: IConfiguration, request: ITaggedOutputsRequest): Pr
}

const tagHex = Converter.utf8ToHex(request.tag, true);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);

if (request.outputType === "basic") {
return StardustTangleHelper.taggedBasicOutputs(networkConfig, tagHex, 10, request.cursor);
return stardustApiService.taggedBasicOutputs(tagHex, 10, request.cursor);
} else if (request.outputType === "nft") {
return StardustTangleHelper.taggedNftOutputs(networkConfig, tagHex, 10, request.cursor);
return stardustApiService.taggedNftOutputs(tagHex, 10, request.cursor);
}

return { error: "Unsupported output type" };
Expand Down
5 changes: 3 additions & 2 deletions api/src/routes/stardust/participation/events/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IParticipationEventResponse } from "../../../../models/api/stardust/par
import { IConfiguration } from "../../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../../models/db/protocolVersion";
import { NetworkService } from "../../../../services/networkService";
import { StardustTangleHelper } from "../../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../../utils/validationHelper";

/**
Expand All @@ -24,5 +24,6 @@ export async function get(config: IConfiguration, request: IParticipationEventRe
return {};
}

return StardustTangleHelper.participationEventDetails(networkConfig, request.eventId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.participationEventDetails(request.eventId);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ISearchResponse } from "../../models/api/stardust/ISearchResponse";
import { IConfiguration } from "../../models/configuration/IConfiguration";
import { STARDUST } from "../../models/db/protocolVersion";
import { NetworkService } from "../../services/networkService";
import { StardustTangleHelper } from "../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function search(_: IConfiguration, request: ISearchRequest): Promis
return {};
}

return StardustTangleHelper.search(networkConfig, request.query);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.search(request.query);
}
5 changes: 3 additions & 2 deletions api/src/routes/stardust/transaction/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ITransactionDetailsResponse } from "../../../models/api/stardust/ITrans
import { IConfiguration } from "../../../models/configuration/IConfiguration";
import { STARDUST } from "../../../models/db/protocolVersion";
import { NetworkService } from "../../../services/networkService";
import { StardustTangleHelper } from "../../../utils/stardust/stardustTangleHelper";
import { StardustApiService } from "../../../services/stardust/stardustApiService";
import { ValidationHelper } from "../../../utils/validationHelper";

/**
Expand All @@ -25,5 +25,6 @@ export async function get(config: IConfiguration, request: ITransactionDetailsRe
return {};
}

return StardustTangleHelper.transactionIncludedBlock(networkConfig, request.transactionId);
const stardustApiService = ServiceFactory.get<StardustApiService>(`api-service-${networkConfig.network}`);
return stardustApiService.transactionIncludedBlock(request.transactionId);
}
Loading

0 comments on commit cc23cfb

Please sign in to comment.