Skip to content

Commit

Permalink
✨ Move pin ability from registry to social module
Browse files Browse the repository at this point in the history
✨ Add social policies to examples
✨ Upgrade to Next v15
  • Loading branch information
bal7hazar committed Jan 15, 2025
1 parent 204d860 commit 51893f6
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 234 deletions.
2 changes: 1 addition & 1 deletion examples/next/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
3 changes: 2 additions & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
"format:check": "prettier --check ./src"
},
"dependencies": {
"@bal7hazar/arcade-sdk": "^0.0.38",
"@cartridge/connector": "workspace:*",
"@cartridge/controller": "workspace:*",
"@cartridge/ui-next": "workspace:*",
"@starknet-react/chains": "^0.1.3",
"@starknet-react/core": "^3.0.2",
"next": "^14.2.5",
"next": "^15.1.4",
"next-themes": "^0.3.0",
"prettier": "^2.7.1",
"react": "^18.3.1",
Expand Down
16 changes: 15 additions & 1 deletion examples/next/src/components/providers/StarknetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { PropsWithChildren } from "react";
import ControllerConnector from "@cartridge/connector/controller";
import { SessionPolicies } from "@cartridge/controller";
import { constants } from "starknet";
import { getSocialPolicies } from "@bal7hazar/arcade-sdk";

export const ETH_CONTRACT_ADDRESS =
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Expand Down Expand Up @@ -45,6 +46,10 @@ const messageForChain = (chainId: constants.StarknetChainId) => {
};
};

const socialPolicies = (chainId: constants.StarknetChainId) => {
return getSocialPolicies(chainId, { pin: true }).contracts;
};

const policies: SessionPolicies = {
contracts: {
[ETH_CONTRACT_ADDRESS]: {
Expand Down Expand Up @@ -78,6 +83,7 @@ const policies: SessionPolicies = {
"0x0305f26ad19e0a10715d9f3137573d3a543de7b707967cd85d11234d6ec0fb7e": {
methods: [{ name: "new_game", entrypoint: "new_game" }],
},
...socialPolicies(constants.StarknetChainId.SN_SEPOLIA),
},
messages: [
messageForChain(constants.StarknetChainId.SN_MAIN),
Expand Down Expand Up @@ -126,8 +132,16 @@ const controller = new ControllerConnector({
process.env.NEXT_PUBLIC_PROFILE_DEPLOYMENT_URL ??
process.env.NEXT_PUBLIC_PROFILE_FRAME_URL,
slot: "profile-example",
preset: "eternum",
// slot: "ryomainnet",
// preset: "dope-wars",
namespace: "dopewars",
// namespace: "dopewars",
// slot: "eternum-prod",
preset: "eternum",
// namespace: "s0_eternum",
// slot: "darkshuffle-mainnet",
// preset: "dark-shuffle",
// namespace: "darkshuffle_s0",
tokens: {
erc20: [
// $LORDS
Expand Down
2 changes: 1 addition & 1 deletion packages/profile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"preview": "vite preview"
},
"dependencies": {
"@bal7hazar/arcade-sdk": "^0.0.28",
"@bal7hazar/arcade-sdk": "^0.0.38",
"@cartridge/controller": "workspace:*",
"@cartridge/penpal": "^6.2.3",
"@cartridge/ui-next": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions packages/profile/src/components/achievements/trophy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ function Track({
const pin = async () => {
setLoading(true);
try {
const calls = provider.registry.pin({ achievementId: id });
const calls = provider.social.pin({ achievementId: id });
const res = await parent.openExecute(
Array.isArray(calls) ? calls : [calls],
chainId,
Expand All @@ -348,7 +348,7 @@ function Track({
const unpin = async () => {
setLoading(true);
try {
const calls = provider.registry.unpin({ achievementId: id });
const calls = provider.social.unpin({ achievementId: id });
const res = await parent.openExecute(
Array.isArray(calls) ? calls : [calls],
chainId,
Expand Down
68 changes: 33 additions & 35 deletions packages/profile/src/components/context/arcade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
import {
ArcadeProvider as ExternalProvider,
Registry,
PinningEvent,
Social,
GameModel,
PinEvent,
} from "@bal7hazar/arcade-sdk";
import { constants } from "starknet";

Expand Down Expand Up @@ -55,35 +56,32 @@ export const ArcadeProvider = ({ children }: { children: ReactNode }) => {
[],
);

const handlePinningEvents = useCallback(
(events: PinningEvent | PinningEvent[]) => {
const data = Array.isArray(events) ? events : [events];
data.forEach((event: PinningEvent) => {
if (event.time == 0) {
// Remove the achievement from the player's list
setPins((prevPins) => {
const achievementIds = prevPins[event.playerId] || [];
return {
...prevPins,
[event.playerId]: achievementIds.filter(
(id: string) => id !== event.achievementId,
),
};
});
} else {
// Otherwise, add the achievement to the player's list
setPins((prevPins) => {
const achievementIds = prevPins[event.playerId] || [];
return {
...prevPins,
[event.playerId]: [...achievementIds, event.achievementId],
};
});
}
});
},
[],
);
const handlePinEvents = useCallback((events: PinEvent | PinEvent[]) => {
const data = Array.isArray(events) ? events : [events];
data.forEach((event: PinEvent) => {
if (event.time == 0) {
// Remove the achievement from the player's list
setPins((prevPins) => {
const achievementIds = prevPins[event.playerId] || [];
return {
...prevPins,
[event.playerId]: achievementIds.filter(
(id: string) => id !== event.achievementId,
),
};
});
} else {
// Otherwise, add the achievement to the player's list
setPins((prevPins) => {
const achievementIds = prevPins[event.playerId] || [];
return {
...prevPins,
[event.playerId]: [...achievementIds, event.achievementId],
};
});
}
});
}, []);

const handleGameModels = useCallback((models: GameModel | GameModel[]) => {
const data = Array.isArray(models) ? models : [models];
Expand All @@ -98,20 +96,20 @@ export const ArcadeProvider = ({ children }: { children: ReactNode }) => {
useEffect(() => {
if (initialized) return;
const initialize = async () => {
await Registry.init(CHAIN_ID);
await Social.init(CHAIN_ID);
setInitialized(true);
};
initialize();
}, [initialized, setInitialized]);

useEffect(() => {
if (!initialized) return;
Registry.Pinning.fetch(handlePinningEvents);
Registry.Pinning.sub(handlePinningEvents);
Social.Pin.fetch(handlePinEvents);
Social.Pin.sub(handlePinEvents);
return () => {
Registry.Pinning.unsub();
Social.Pin.unsub();
};
}, [initialized, handlePinningEvents]);
}, [initialized, handlePinEvents]);

useEffect(() => {
if (!initialized) return;
Expand Down
Loading

0 comments on commit 51893f6

Please sign in to comment.