-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a `defaultChain` arg to `StarknetConfig` that allows changing the default chain used when no wallet is connected
- Loading branch information
Showing
10 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@starknet-react-core-2cb32fda-224a-4b36-86e2-acffaa17d520.json
This file contains 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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Add provider option to select default chain when no wallet is connected", | ||
"packageName": "@starknet-react/core", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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,70 @@ | ||
import { type Chain, mainnet, sepolia } from "@starknet-react/chains"; | ||
import { publicProvider, useAccount, useNetwork } from "@starknet-react/core"; | ||
import { useState } from "react"; | ||
import { Button } from "../ui/button"; | ||
import { DemoContainer } from "../starknet"; | ||
|
||
export function ChangeDefaultNetwork() { | ||
const [defaultChain, setDefaultChain] = useState<Chain>(sepolia); | ||
const chains = [mainnet, sepolia]; | ||
const provider = publicProvider(); | ||
|
||
return ( | ||
<DemoContainer hasWallet defaultChainId={defaultChain.id}> | ||
<ChangeNetworkInner | ||
defaultChain={defaultChain} | ||
setDefaultChain={setDefaultChain} | ||
/> | ||
</DemoContainer> | ||
); | ||
} | ||
|
||
function ChangeNetworkInner({ | ||
defaultChain, | ||
setDefaultChain, | ||
}: { | ||
defaultChain: Chain; | ||
setDefaultChain: (chain: Chain) => void; | ||
}) { | ||
const { chain } = useNetwork(); | ||
const chains = [sepolia, mainnet]; | ||
const { isConnected } = useAccount(); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<div className="h-full flex flex-col justify-center"> | ||
<p className="font-medium">Current Chain: </p> | ||
<pre>{chain.name}</pre> | ||
</div> | ||
|
||
<div className="h-full flex flex-col justify-center"> | ||
<p className="font-medium">Default Chain: </p> | ||
<pre>{defaultChain.name}</pre> | ||
</div> | ||
|
||
<div> | ||
<p className="font-medium">Change Default Chain:</p> | ||
<div className="flex gap-2 my-2"> | ||
{chains.map((chain) => ( | ||
<Button | ||
key={chain.id} | ||
onClick={() => setDefaultChain(chain)} | ||
variant={defaultChain.id === chain.id ? "default" : "outline"} | ||
> | ||
{chain.name} | ||
</Button> | ||
))} | ||
</div> | ||
</div> | ||
|
||
<div className="h-full flex flex-col justify-center"> | ||
<p className="font-medium">Behavior Explanation: </p> | ||
<pre> | ||
{isConnected | ||
? "Connected: Changing default chain won't affect current chain" | ||
: "Not Connected: Changing default chain will update current chain"} | ||
</pre> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains 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 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 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 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,5 @@ | ||
import Demo from "../../components/demo"; | ||
|
||
# Change Default Network (Todo) | ||
|
||
<Demo.ChangeDefaultNetwork /> |
This file contains 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 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,65 @@ | ||
# StarknetConfig | ||
|
||
The React Context provider for Starknet. | ||
|
||
## Usage | ||
|
||
```tsx twoslash | ||
"use client"; | ||
import React from "react"; | ||
|
||
import { mainnet } from "@starknet-react/chains"; | ||
import { StarknetConfig, publicProvider } from "@starknet-react/core"; | ||
|
||
function App() { | ||
return ( | ||
<StarknetConfig chains={[mainnet]} provider={publicProvider()}> | ||
{/* your app here */} | ||
</StarknetConfig> | ||
); | ||
} | ||
``` | ||
|
||
## Arguments | ||
|
||
### chains | ||
|
||
- Type: `Chain[]` | ||
|
||
List of supported chains. | ||
|
||
### provider | ||
|
||
- Type: `ChainProviderFactory` | ||
|
||
The JSON-RPC provider you want to use. See [the RPC providers page](/docs/providers) for more information. | ||
|
||
### connectors | ||
|
||
- Type: `Connector[]` | ||
|
||
List of wallet connectors you want to use. See [the wallets page](/docs/wallets) for more information. | ||
|
||
### explorer | ||
|
||
- Type: `ExplorerFactory` | ||
|
||
Explorer factory to use. See [the explorers page](/docs/explorers) for more information. | ||
|
||
### autoConnect | ||
|
||
- Type: `boolean | undefined` | ||
|
||
Whether to automatically connect to the first available wallet. | ||
|
||
### queryClient | ||
|
||
- Type: `QueryClient` | ||
|
||
React Query client to use. | ||
|
||
### defaultChainId | ||
|
||
- Type: `bigint | undefined` | ||
|
||
Default chain to use when no wallet is connected. This chain must be included in the `chains` array. |
This file contains 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 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