Skip to content

Commit

Permalink
♻️ Refactor sdk and add policies
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Jan 14, 2025
1 parent c66eb09 commit f8289d8
Show file tree
Hide file tree
Showing 18 changed files with 833 additions and 116 deletions.
67 changes: 33 additions & 34 deletions packages/sdk/src/bindings/contracts.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,6 @@ import { DojoProvider } from "@dojoengine/core";
import { Account, AccountInterface, BigNumberish, ByteArray } from "starknet";

export function setupWorld(provider: DojoProvider) {
const Registry_pin = async (snAccount: Account | AccountInterface, achievementId: BigNumberish) => {
try {
return await provider.execute(
snAccount,
{
contractName: "Registry",
entrypoint: "pin",
calldata: [achievementId],
},
"ARCADE",
);
} catch (error) {
console.error(error);
}
};

const Registry_unpin = async (snAccount: Account | AccountInterface, achievementId: BigNumberish) => {
try {
return await provider.execute(
snAccount,
{
contractName: "Registry",
entrypoint: "unpin",
calldata: [achievementId],
},
"ARCADE",
);
} catch (error) {
console.error(error);
}
};

const Registry_registerGame = async (
snAccount: Account | AccountInterface,
worldAddress: BigNumberish,
Expand Down Expand Up @@ -370,6 +338,37 @@ export function setupWorld(provider: DojoProvider) {
console.error(error);
}
};
const Social_pin = async (snAccount: Account | AccountInterface, achievementId: BigNumberish) => {
try {
return await provider.execute(
snAccount,
{
contractName: "Social",
entrypoint: "pin",
calldata: [achievementId],
},
"ARCADE",
);
} catch (error) {
console.error(error);
}
};

const Social_unpin = async (snAccount: Account | AccountInterface, achievementId: BigNumberish) => {
try {
return await provider.execute(
snAccount,
{
contractName: "Social",
entrypoint: "unpin",
calldata: [achievementId],
},
"ARCADE",
);
} catch (error) {
console.error(error);
}
};

const Social_follow = async (snAccount: Account | AccountInterface, target: BigNumberish) => {
try {
Expand Down Expand Up @@ -823,8 +822,6 @@ export function setupWorld(provider: DojoProvider) {

return {
Registry: {
pin: Registry_pin,
unpin: Registry_unpin,
registerGame: Registry_registerGame,
updateGame: Registry_updateGame,
publishGame: Registry_publishGame,
Expand All @@ -841,6 +838,8 @@ export function setupWorld(provider: DojoProvider) {
removeAchievement: Registry_removeAchievement,
},
Social: {
pin: Social_pin,
unpin: Social_unpin,
follow: Social_follow,
unfollow: Social_unfollow,
createAlliance: Social_createAlliance,
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./metadata";
export * from "./socials";
25 changes: 25 additions & 0 deletions packages/sdk/src/classes/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class Metadata {
constructor(
public color: string,
public name: string,
public description: string,
public image: string,
public banner: string,
) {
this.color = color;
this.name = name;
this.description = description;
this.image = image;
this.banner = banner;
}

static from(value: string) {
try {
const json = JSON.parse(value);
return new Metadata(json.color, json.name, json.description, json.image, json.banner);
} catch (error: unknown) {
console.error("Error parsing metadata:", error);
return new Metadata("", "", "", "", "");
}
}
}
25 changes: 25 additions & 0 deletions packages/sdk/src/classes/socials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export class Socials {
constructor(
public discord: string,
public telegram: string,
public twitter: string,
public youtube: string,
public website: string,
) {
this.discord = discord;
this.telegram = telegram;
this.twitter = twitter;
this.youtube = youtube;
this.website = website;
}

static from(value: string) {
try {
const json = JSON.parse(value);
return new Socials(json.discord, json.telegram, json.twitter, json.youtube, json.website);
} catch (error: unknown) {
console.error("Error parsing socials:", error);
return new Socials("", "", "", "", "");
}
}
}
1 change: 1 addition & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./provider";
export * from "./bindings";
export * from "./manifests";
export * from "./modules";
export * from "./classes";
1 change: 1 addition & 0 deletions packages/sdk/src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./registry";
export * from "./social";
import { init } from "@dojoengine/sdk";
import { configs } from "../configs";
import { SchemaType, schema } from "../bindings/models.gen";
Expand Down
30 changes: 29 additions & 1 deletion packages/sdk/src/modules/registry/achievement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export class AchievementModel {
}

export const Achievement = {
address: undefined as string | undefined,
sdk: undefined as SDK<SchemaType> | undefined,
unsubscribe: undefined as (() => void) | undefined,

init: (sdk: SDK<SchemaType>) => {
init: (sdk: SDK<SchemaType>, address: string) => {
Achievement.sdk = sdk;
Achievement.address = address;
},

fetch: async (callback: (models: AchievementModel[]) => void) => {
Expand Down Expand Up @@ -101,4 +103,30 @@ export const Achievement = {
Achievement.unsubscribe();
Achievement.unsubscribe = undefined;
},

policies: () => {
if (!Achievement.address) {
throw new Error("Achievement module is not initialized");
}
return {
contracts: {
[Achievement.address]: {
name: "Registry",
description: "Registry contract for games and achievements",
methods: Achievement.methods(),
},
},
};
},

methods: () => [
{ name: "register_achievement", entrypoint: "register_achievement", description: "Register an achievement." },
{ name: "update_achievement", entrypoint: "update_achievement", description: "Update an achievement." },
{ name: "publish_achievement", entrypoint: "publish_achievement", description: "Publish an achievement." },
{ name: "hide_achievement", entrypoint: "hide_achievement", description: "Hide an achievement." },
{ name: "whitelist_achievement", entrypoint: "whitelist_achievement", description: "Whitelist an achievement." },
{ name: "blacklist_achievement", entrypoint: "blacklist_achievement", description: "Blacklist an achievement." },
{ name: "remove_achievement", entrypoint: "remove_achievement", description: "Remove an achievement." },
]
};

82 changes: 29 additions & 53 deletions packages/sdk/src/modules/registry/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NAMESPACE } from "../../constants";
import { shortString, addAddressPadding } from "starknet";
import { SchemaType } from "../../bindings";
import { ParsedEntity, QueryBuilder, SDK, StandardizedQueryResult } from "@dojoengine/sdk";
import { Metadata, Socials } from "../../classes";

const MODEL_NAME = "Game";

Expand Down Expand Up @@ -60,64 +61,14 @@ export class GameModel {
}
}

export class Metadata {
constructor(
public color: string,
public name: string,
public description: string,
public image: string,
public banner: string,
) {
this.color = color;
this.name = name;
this.description = description;
this.image = image;
this.banner = banner;
}

static from(value: string) {
try {
const json = JSON.parse(value);
return new Metadata(json.color, json.name, json.description, json.image, json.banner);
} catch (error: unknown) {
console.error("Error parsing metadata:", error);
return new Metadata("", "", "", "", "");
}
}
}

export class Socials {
constructor(
public discord: string,
public telegram: string,
public twitter: string,
public youtube: string,
public website: string,
) {
this.discord = discord;
this.telegram = telegram;
this.twitter = twitter;
this.youtube = youtube;
this.website = website;
}

static from(value: string) {
try {
const json = JSON.parse(value);
return new Socials(json.discord, json.telegram, json.twitter, json.youtube, json.website);
} catch (error: unknown) {
console.error("Error parsing socials:", error);
return new Socials("", "", "", "", "");
}
}
}

export const Game = {
address: undefined as string | undefined,
sdk: undefined as SDK<SchemaType> | undefined,
unsubscribe: undefined as (() => void) | undefined,

init: (sdk: SDK<SchemaType>) => {
init: (sdk: SDK<SchemaType>, address: string) => {
Game.sdk = sdk;
Game.address = address;
},

fetch: async (callback: (models: GameModel[]) => void) => {
Expand Down Expand Up @@ -180,4 +131,29 @@ export const Game = {
Game.unsubscribe();
Game.unsubscribe = undefined;
},

policies: () => {
if (!Game.address) {
throw new Error("Game module is not initialized");
}
return {
contracts: {
[Game.address]: {
name: "Registry",
description: "Registry contract for games and achievements",
methods: Game.methods(),
},
},
};
},

methods: () => [
{ name: "register_game", entrypoint: "register_game", description: "Register a game." },
{ name: "update_game", entrypoint: "update_game", description: "Update a game." },
{ name: "publish_game", entrypoint: "publish_game", description: "Publish a game." },
{ name: "hide_game", entrypoint: "hide_game", description: "Hide a game." },
{ name: "whitelist_game", entrypoint: "whitelist_game", description: "Whitelist a game." },
{ name: "blacklist_game", entrypoint: "blacklist_game", description: "Blacklist a game." },
{ name: "remove_game", entrypoint: "remove_game", description: "Remove a game." },
]
};
34 changes: 28 additions & 6 deletions packages/sdk/src/modules/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import { Pinning, PinningEvent } from "./pinning";
import { Game, GameModel, Metadata, Socials } from "./game";
import { Game, GameModel } from "./game";
import { initSDK } from "..";
import { constants } from "starknet";
export type { PinningEvent, GameModel, Metadata, Socials };
import { Achievement } from "./achievement";
import { configs } from "../../configs";
import { getContractByName } from "../../provider/helpers";

export type { GameModel };

export const Registry = {
Pinning: Pinning,
address: undefined as string | undefined,
Game: Game,
Achievement: Achievement,

init: async (chainId: constants.StarknetChainId) => {
const config = configs[chainId];
const address = getContractByName(config.manifest, "Registry");
const sdk = await initSDK(chainId);
Pinning.init(sdk);
Game.init(sdk);
Registry.address = address;
Game.init(sdk, address);
Achievement.init(sdk, address);
},

policies: () => {
if (!Registry.address) {
throw new Error("Registry module is not initialized");
}
return {
contracts: {
[Registry.address]: {
name: "Registry",
description: "Registry contract for games and achievements",
methods: [...Game.methods(), ...Achievement.methods()],
},
},
};
},
};
Loading

0 comments on commit f8289d8

Please sign in to comment.