This repository was archived by the owner on Apr 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
New recipe Controller #102
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| --- | ||
| sidebar_position: 6 | ||
| title: Integrating Cartridge Controller | ||
| description: Comprehensive guide to integrating the Cartridge Controller in Scaffold-Stark | ||
| --- | ||
|
|
||
| # Integrating the Cartridge Controller | ||
|
|
||
| ## Overview | ||
|
|
||
| This guide provides a complete implementation for integrating the [Cartridge Controller](https://docs.cartridge.gg/controller/overview) into your Scaffold-Stark project, enabling advanced wallet connections. | ||
|
|
||
| ## Prerequisites and Deployment Considerations | ||
|
|
||
| ### Important Deployment Requirements | ||
|
|
||
| ⚠️ **Crucial Notes Before Implementation**: | ||
|
|
||
| **Local Development Limitations** | ||
|
|
||
| - The Cartridge Controller cannot be used in localhost environments without relying on ngrok or other alternatives. Additionally, developers must use Katana or [Slot](https://docs.cartridge.gg/slot/getting-started). | ||
| - This guide focuses only on Sepolia and Mainnet deployments and integration with Vercel. | ||
|
|
||
| ## Implementation Guide | ||
|
|
||
| ### Step 1: Add Controller Dependency | ||
|
|
||
| Update your `packages/nextjs/package.json` to include the Cartridge dependency: | ||
|
|
||
| <details open> | ||
| <summary>Add dependencies controller </summary> | ||
|
|
||
| ```json title="nextjs/package.json" | ||
| { | ||
| "dependencies": { | ||
| "@cartridge/connector": "^0.5.7" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ### Step 2: Create Controller Configuration | ||
|
|
||
| Create a new file `index.tsx` in `nextjs/services/web3/controller/index.tsx`. You can also review different configurations in the [Cartridge Controller documentation](https://docs.cartridge.gg/controller/overview) to understand how to integrate [policies or sessions](https://docs.cartridge.gg/controller/sessions) and other powerful features, especially for gaming or UX improvements. | ||
|
|
||
| <details> | ||
| <summary>View full code</summary> | ||
|
|
||
| ```tsx title="utils/scaffold-stark/controller.tsx" | ||
| "use client"; | ||
|
|
||
| import { Chain } from "@starknet-react/chains"; | ||
| import { jsonRpcProvider, publicProvider, starknetChainId, InjectedConnector } from "@starknet-react/core"; | ||
| import ControllerConnector from "@cartridge/connector/controller"; | ||
| import { constants } from "starknet"; | ||
| import scaffoldConfig from "~~/scaffold.config"; | ||
| import { SessionPolicies } from "@cartridge/controller"; | ||
|
|
||
| // Standard contract addresses | ||
| export const ETH_CONTRACT_ADDRESS = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; | ||
| export const STRK_CONTRACT_ADDRESS = "0x04718f5a0Fc34cC1AF16A1cdee98fFB20C31f5cD61D6Ab07201858f4287c938D"; | ||
|
|
||
| // Function to check for devnet networks | ||
| const containsDevnet = (networks: readonly Chain[]) => { | ||
| return networks.some(it => it.network === "devnet"); | ||
| }; | ||
|
|
||
| // Provider configuration based on Scaffold settings | ||
| export const getProvider = () => { | ||
| if (scaffoldConfig.rpcProviderUrl === "" || containsDevnet(scaffoldConfig.targetNetworks)) { | ||
| return publicProvider(); | ||
| } | ||
|
|
||
| return jsonRpcProvider({ | ||
| rpc: () => ({ | ||
| nodeUrl: scaffoldConfig.rpcProviderUrl, | ||
| chainId: starknetChainId(scaffoldConfig.targetNetworks[0].id), | ||
| }), | ||
| }); | ||
| }; | ||
|
|
||
| // Supported chains configuration | ||
| const chains = [ | ||
| { | ||
| id: constants.StarknetChainId.SN_SEPOLIA, | ||
| name: "Sepolia", | ||
| rpcUrl: process.env.NEXT_PUBLIC_RPC_SEPOLIA ?? "https://api.cartridge.gg/x/starknet/sepolia", | ||
| }, | ||
| { | ||
| id: constants.StarknetChainId.SN_MAIN, | ||
| name: "Mainnet", | ||
| rpcUrl: process.env.NEXT_PUBLIC_RPC_MAINNET ?? "https://api.cartridge.gg/x/starknet/mainnet", | ||
| }, | ||
| ]; | ||
|
|
||
| // Session policies for contracts | ||
| const policies: SessionPolicies = { | ||
| contracts: { | ||
| [ETH_CONTRACT_ADDRESS]: { | ||
| methods: [ | ||
| { name: "approve", entrypoint: "approve" }, | ||
| { name: "transfer", entrypoint: "transfer" }, | ||
| ], | ||
| }, | ||
| [STRK_CONTRACT_ADDRESS]: { | ||
| methods: [ | ||
| { name: "approve", entrypoint: "approve" }, | ||
| { name: "transfer", entrypoint: "transfer" }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // Create Cartridge Controller instance | ||
| export const controllerInstance = new ControllerConnector({ | ||
| policies, | ||
| defaultChainId: constants.StarknetChainId.SN_SEPOLIA, | ||
| chains: chains, | ||
| url: process.env.NEXT_PUBLIC_KEYCHAIN_DEPLOYMENT_URL, | ||
| profileUrl: process.env.NEXT_PUBLIC_PROFILE_DEPLOYMENT_URL, | ||
| }) as unknown as InjectedConnector; | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ### Step 3: Modify Connectors Configuration | ||
|
|
||
| Update `services/web/connectors.tsx` to include the Cartridge Controller instance. | ||
|
|
||
| <details> | ||
| <summary>View changes</summary> | ||
|
|
||
| ```tsx title="nextjs/services/web/connectors.tsx" | ||
| import { controllerInstance } from "~~/services/web3/controller/index"; | ||
|
|
||
| // Add Cartridge Controller for non-devnet networks | ||
| if (!targetNetworks.some(network => (network.network as string) === "devnet")) { | ||
| connectors.push(controllerInstance as unknown as InjectedConnector); | ||
| } | ||
| ``` | ||
|
|
||
| </details> | ||
|
|
||
| ### Step 4: Configure Deployment Settings | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this section should be somewhere else, is standalone for every app. |
||
|
|
||
| #### Update Network Configuration | ||
|
|
||
| Modify `scaffold.config.ts` to target `sepolia` or `mainnet`: | ||
|
|
||
| ```typescript title="scaffold.config.ts" | ||
| const scaffoldConfig = { | ||
| targetNetworks: [chains.sepolia], | ||
| }; | ||
| ``` | ||
|
|
||
| #### Prepare for Deployment | ||
|
|
||
| To deploy your application, install Vercel and log in: | ||
|
|
||
| ```bash | ||
| yarn install -g vercel | ||
| vercel login | ||
| yarn vercel | ||
| ``` | ||
|
|
||
| If you want to deploy without pre-checks, use: | ||
|
|
||
| ```bash | ||
| yarn vercel:yolo | ||
| ``` | ||
|
|
||
| For more details, refer to the [Vercel deployment guide](https://docs.scaffoldstark.com/deploying/deploy-nextjs-app). If you need to disable type linting checks, see [Disabling Type Checks](https://docs.scaffoldstark.com/disable-type-linting-error-checks). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.