Skip to content

Commit c1f06e8

Browse files
tarrencevgithub-actions[bot]kronosapiens
authored
docs: Update documentation for controller PR #1785 (#45)
* docs: Update documentation for controller PR #1785 Updates documentation to reflect changes made in: Prepare prerelease: 0.8.0-alpha.0 Related controller PR: cartridge-gg/controller#1785 * fix: update window variable handling for safety --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Daniel Kronovet <kronovet@gmail.com>
1 parent e1f32c1 commit c1f06e8

File tree

1 file changed

+114
-9
lines changed

1 file changed

+114
-9
lines changed

src/pages/controller/getting-started.mdx

Lines changed: 114 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,30 @@ Here's a simple example of how to initialize and use the controller:
4949

5050
```ts
5151
import Controller from "@cartridge/controller";
52+
import { SessionPolicies } from "@cartridge/controller";
53+
import { constants } from "starknet";
54+
55+
// Define your game's session policies
56+
const policies: SessionPolicies = {
57+
contracts: {
58+
"0x1234...": {
59+
name: "My Game Contract",
60+
methods: [
61+
{ name: "move_player", entrypoint: "move_player" },
62+
{ name: "attack", entrypoint: "attack" },
63+
],
64+
},
65+
},
66+
};
67+
68+
// Initialize the controller _with_ session policies
69+
const controller = new Controller({
70+
policies,
71+
// Optional: specify the default chain
72+
defaultChainId: constants.StarknetChainId.SN_SEPOLIA,
73+
});
5274

53-
// Initialize the controller without policies
75+
// Initialize the controller _without_ policies
5476
// This requires manual approval for each transaction
5577
const controller = new Controller();
5678

@@ -60,13 +82,27 @@ const account = await controller.connect();
6082
// Execute transactions - user will see approval dialog
6183
const tx = await account.execute([
6284
{
63-
contractAddress: "0x...",
64-
entrypoint: "my_function",
85+
contractAddress: "0x1234...",
86+
entrypoint: "move_player",
6587
calldata: ["0x1", "0x2"],
6688
}
6789
]);
6890
```
6991

92+
### Simple Controller (No Sessions)
93+
94+
For basic usage without session management:
95+
96+
```ts
97+
import Controller from "@cartridge/controller";
98+
99+
// Initialize with minimal configuration
100+
const controller = new Controller();
101+
102+
// Connect and use
103+
const account = await controller.connect();
104+
```
105+
70106
> **Note:** When no policies are provided, each transaction requires manual user approval through the Cartridge interface. This is suitable for simple applications or testing, but games typically benefit from using [session policies](#configuration-with-session-policies) for a smoother experience.
71107
72108
## Configuration with Session Policies
@@ -90,7 +126,7 @@ const controller = new Controller({
90126
},
91127
{
92128
name: "attack",
93-
entrypoint: "attack",
129+
entrypoint: "attack",
94130
description: "Attack enemy",
95131
},
96132
],
@@ -102,18 +138,51 @@ const controller = new Controller({
102138

103139
## Usage with Starknet React
104140

141+
### Controller Connector
142+
105143
```tsx
106144
import React from "react";
107145
import { sepolia, mainnet } from "@starknet-react/chains";
108146
import {
109147
StarknetConfig,
110148
jsonRpcProvider,
111-
starkscan,
149+
cartridge,
112150
} from "@starknet-react/core";
113151
import ControllerConnector from "@cartridge/connector/controller";
152+
import SessionConnector from "@cartridge/connector/session";
153+
import { SessionPolicies } from "@cartridge/controller";
154+
import { constants } from "starknet";
155+
156+
// Define session policies for your game
157+
const policies: SessionPolicies = {
158+
contracts: {
159+
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7": {
160+
methods: [
161+
{ name: "transfer", entrypoint: "transfer" },
162+
{ name: "approve", entrypoint: "approve" },
163+
],
164+
},
165+
},
166+
};
167+
168+
// Create the controller connector
169+
const controller = new ControllerConnector({
170+
policies,
171+
// Optional: customize chains and default chain
172+
chains: [
173+
{ rpcUrl: "https://api.cartridge.gg/x/starknet/sepolia" },
174+
{ rpcUrl: "https://api.cartridge.gg/x/starknet/mainnet" },
175+
],
176+
defaultChainId: constants.StarknetChainId.SN_SEPOLIA,
177+
});
114178

115-
// Create the connector outside the component to avoid recreation on renders
116-
const connector = new ControllerConnector();
179+
// Create the session connector for direct session management
180+
const session = new SessionConnector({
181+
policies,
182+
rpc: "https://api.cartridge.gg/x/starknet/sepolia",
183+
chainId: constants.StarknetChainId.SN_SEPOLIA,
184+
redirectUrl: typeof window !== "undefined" ? window.location.origin : "",
185+
});
117186

118187
// Configure the JSON RPC provider
119188
const provider = jsonRpcProvider({
@@ -134,15 +203,51 @@ export function StarknetProvider({ children }: { children: React.ReactNode }) {
134203
defaultChainId={sepolia.id}
135204
chains={[mainnet, sepolia]}
136205
provider={provider}
137-
connectors={[connector]}
138-
explorer={starkscan}
206+
connectors={[controller, session]}
207+
explorer={cartridge}
139208
>
140209
{children}
141210
</StarknetConfig>
142211
);
143212
}
144213
```
145214

215+
### Session Connector
216+
217+
The `SessionConnector` provides direct session management capabilities, allowing for more granular control over session handling:
218+
219+
```tsx
220+
import SessionConnector from "@cartridge/connector/session";
221+
import { SessionPolicies } from "@cartridge/controller";
222+
import { constants } from "starknet";
223+
224+
const policies: SessionPolicies = {
225+
contracts: {
226+
"0x1234...": {
227+
name: "My Game Contract",
228+
methods: [
229+
{ name: "move_player", entrypoint: "move_player" },
230+
{ name: "attack", entrypoint: "attack" },
231+
],
232+
},
233+
},
234+
};
235+
236+
const sessionConnector = new SessionConnector({
237+
policies,
238+
rpc: "https://api.cartridge.gg/x/starknet/sepolia",
239+
chainId: constants.StarknetChainId.SN_SEPOLIA,
240+
redirectUrl: typeof window !== "undefined" ? window.location.origin : "",
241+
// Optional: specify keychain URL for custom deployments
242+
keychainUrl: "https://x.cartridge.gg",
243+
});
244+
```
245+
246+
The SessionConnector is ideal for applications that need:
247+
- Direct control over session lifecycle
248+
- Custom session management flows
249+
- Integration with existing authentication systems
250+
146251
## Examples
147252

148253
For more detailed examples of how to use Cartridge Controller in different

0 commit comments

Comments
 (0)