Skip to content

Commit

Permalink
feat(common-lib): derive routes df instead of fetch (#337)
Browse files Browse the repository at this point in the history
This change will save us from doing one additional request each time we
need to fetch the `Routes` dynamic field.

It derives the dynamic field object ID of `Routes` instead of fetching
it.
  • Loading branch information
Tzal3x authored Jan 7, 2025
1 parent b011ddb commit 1b59b85
Showing 1 changed file with 22 additions and 41 deletions.
63 changes: 22 additions & 41 deletions portal/common/lib/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { DynamicFieldStruct, RoutesStruct } from "./bcs_data_parsing";
import { bcs, fromBase64 } from "@mysten/bcs";
import logger from "./logger";
import { RPCSelector } from "./rpc_selector";
import { deriveDynamicFieldID } from "@mysten/sui/utils";

/**
* The WalrusSiteRouter class is responsible for handling the routing logic for published
* Walrus Sites, depending by the definition of the `routes` field inside the `ws-resources.json`.
*/
* The WalrusSiteRouter class is responsible for handling the routing logic for published
* Walrus Sites, depending by the definition of the `routes` field inside the `ws-resources.json`.
*/
export class WalrusSitesRouter {
constructor(private rpcSelector: RPCSelector) {}

Expand All @@ -23,67 +24,47 @@ export class WalrusSitesRouter {
* @param siteObjectId - The ID of the site object.
* @returns The routes list.
*/
public async getRoutes(
siteObjectId: string,
): Promise<Routes | undefined> {
logger.info({ message: "Fetching routes dynamic field.", siteObjectId })
const routesDF = await this.fetchRoutesDynamicField(siteObjectId);
if (!routesDF.data) {
logger.warn({
message: "No routes dynamic field found for site object. Exiting getRoutes.",
siteObjectId
});
return;
}
const routesObj = await this.fetchRoutesObject(routesDF.data.objectId);
public async getRoutes(siteObjectId: string): Promise<Routes | undefined> {
logger.info({ message: "Fetching routes dynamic field.", siteObjectId });
const routesObj = await this.fetchRoutesDynamicFieldObject(siteObjectId);
const objectData = routesObj.data;
if (objectData && objectData.bcs && objectData.bcs.dataType === "moveObject") {
return this.parseRoutesData(objectData.bcs.bcsBytes);
}
if (!objectData) {
logger.warn({
message: "Routes dynamic field does not contain a `data` field."
message: "Routes dynamic field does not contain a `data` field.",
});
} else if (!objectData.bcs) {
logger.warn({
message: "Routes dynamic field does not contain a `bcs` field."
message: "Routes dynamic field does not contain a `bcs` field.",
});
} else if (!objectData.bcs.dataType) {
logger.warn({
message: "Routes dynamic field does not contain a `dataType` field."
message: "Routes dynamic field does not contain a `dataType` field.",
});
}
throw new Error("Routes object data could not be fetched.");
}

/**
* Fetches the dynamic field object for routes.
*
* @param client - The SuiClient instance.
* @param siteObjectId - The ID of the site object.
* @returns The dynamic field object for routes.
*/
private async fetchRoutesDynamicField(
siteObjectId: string,
): Promise<SuiObjectResponse> {
return await this.rpcSelector.getDynamicFieldObject({
parentId: siteObjectId,
name: { type: "vector<u8>", value: "routes" },
});
}

/**
* Fetches the routes object using the dynamic field object ID.
* Derives and fetches the Routes dynamic field object.
*
* @param client - The SuiClient instance.
* @param objectId - The ID of the dynamic field object.
* @param siteObjectId - The site object ID.
* @returns The routes object.
*/
private async fetchRoutesObject(objectId: string): Promise<SuiObjectResponse> {
return await this.rpcSelector.getObject({
id: objectId,
private async fetchRoutesDynamicFieldObject(siteObjectId: string): Promise<SuiObjectResponse> {
const routesMoveType = "vector<u8>";
const dynamicFieldId = deriveDynamicFieldID(
siteObjectId,
routesMoveType,
bcs.vector(bcs.u8()).serialize(Buffer.from("routes")).toBytes(),
);
const dfObjectResponse = await this.rpcSelector.getObject({
id: dynamicFieldId,
options: { showBcs: true },
});
return dfObjectResponse;
}

/**
Expand Down

0 comments on commit 1b59b85

Please sign in to comment.