Skip to content

Commit

Permalink
Derive routes DF - Done
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzal3x committed Dec 24, 2024
1 parent cf43083 commit da40c77
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
1 change: 0 additions & 1 deletion portal/common/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ export const SITE_NAMES: { [key: string]: string } = {
export const FALLBACK_PORTAL = "blob.store";
// The string representing the ResourcePath struct in the walrus_site package.
export const RESOURCE_PATH_MOVE_TYPE = SITE_PACKAGE + "::site::ResourcePath";
export const ROUTES_MOVE_TYPE = SITE_PACKAGE + "::site::Routes";
56 changes: 14 additions & 42 deletions portal/common/lib/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import { bcs, fromBase64 } from "@mysten/bcs";
import logger from "./logger";
import { RPCSelector } from "./rpc_selector";
import { deriveDynamicFieldID } from "@mysten/sui/utils";
import { ROUTES_MOVE_TYPE } from "./constants";

/**
* 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 @@ -25,74 +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(siteObjectId);
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" },
});
}

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

Expand Down

0 comments on commit da40c77

Please sign in to comment.