Skip to content

Commit 5b6e1cf

Browse files
committed
apply suggestions
1 parent b915696 commit 5b6e1cf

File tree

8 files changed

+117
-103
lines changed

8 files changed

+117
-103
lines changed

wallet/how-to/use-non-evm-networks/_category_.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

wallet/how-to/use-non-evm-networks/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
description: Interact with users' accounts on non-EVM networks
2+
description: Interact with users' accounts on non-EVM networks.
3+
sidebar_position: 9
34
---
45

56
import CardList from "@site/src/components/CardList"

wallet/how-to/use-non-evm-networks/starknet/_category_.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

wallet/how-to/use-non-evm-networks/starknet/connect-to-starknet.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ This library simplifies the process of connecting to the Starknet Snap, allowing
2222

2323
## Connect using `get-starknet`
2424

25+
:::note
26+
27+
We recommend using the `get-starknet method for most uses.
28+
29+
:::
30+
2531
### 1. Add `get-starknet` to your project
2632

2733
Add the [`get-starknet`](https://github.com/starknet-io/get-starknet) `version 3.3.0` library and the version `6.11.0` of the `starknet.js` library to your project's dependencies:
@@ -46,11 +52,10 @@ Add the [`get-starknet`](https://github.com/starknet-io/get-starknet) `version 3
4652

4753
### 2. Connect to the Snap
4854

49-
Create a new file named `WalletConnectButton.js` in your project's `src/components` folder. Create this folder if it doesn't exist. Add the following code to the file:.
50-
51-
```javascript
52-
import React, { useState } from 'react';
53-
import { connect, disconnect } from 'get-starknet';
55+
Create a `src/components` directory, and add a new file named `WalletConnectButton.js` to the directory. Add the following code to the file:
56+
```javascript title="WalletConnectButton.js"
57+
import React, { useState } from "react";
58+
import { connect, disconnect } from "get-starknet";
5459
import { encode } from "starknet";
5560
function WalletConnectButton() {
5661
const [walletAddress, setWalletAddress] = useState("");
@@ -110,7 +115,7 @@ To connect to Starknet, the dapp user must ensure the [Starknet Snap](https://sn
110115
111116
### 3. Start the dapp
112117
113-
Start the dapp, which allow users to click **Connect Wallet** and interact with the Starknet network using MetaMask:
118+
Start the dapp, which allows users to select **Connect Wallet** and interact with the Starknet network using MetaMask:
114119
115120
<Tabs>
116121
<TabItem value="yarn" label="Yarn" default>
@@ -142,14 +147,15 @@ Alternatively, you can manage the Snap invocation manually. Use the `wallet_invo
142147
143148
:::
144149
145-
In the `src/utils/snapHelper.js` file add the `connect` function and the `callSnap` helper function. This file handles the interactions with the Starknet Snap.
150+
Create a `src/utils` directory, and add a new file named `snapHelper.js` to the directory.
151+
In `snapHelper.js`, add a `connect` function and a `callSnap` helper function as follows. This file handles the interactions with the Starknet Snap:
146152
147-
```javascript title="src/utils/snapHelper.js"
148-
const snapId = 'npm:starknet-snap';
153+
```javascript title="snapHelper.js"
154+
const snapId = "npm:starknet-snap";
149155

150156
export async function connect() {
151157
await ethereum.request({
152-
method: 'wallet_requestSnaps',
158+
method: "wallet_requestSnaps",
153159
params: {
154160
[snapId]: {},
155161
},
@@ -159,7 +165,7 @@ export async function connect() {
159165
export async function callSnap(method, params) {
160166
try {
161167
const response = await ethereum.request({
162-
method: 'wallet_invokeSnap',
168+
method: "wallet_invokeSnap",
163169
params: {
164170
snapId,
165171
request: {
@@ -180,18 +186,20 @@ export async function callSnap(method, params) {
180186
181187
### 2. Call a specific Snap method
182188
183-
To call a specific Snap method, for example `createAccount` use the following:
189+
Use the `callSnap` function to call a specific Snap method.
190+
The following example calls `starkNet_createAccount`:
184191
185192
```javascript
186193
const deploy = false; // Set to true to deploy the actual account
187194
const addressIndex = 0; // Specify which address to derive
188-
const chainId = "0x534e5f5345504f4c4941"; // Specify which chain to use (Sepolia testnet)
189-
// For mainnet, use: "0x534e5f4d41494e"
195+
const chainId = "0x534e5f5345504f4c4941"; // Specify which chain to use (in this instance the Sepolia testnet)
190196

191197
const accountInfo = await callSnap('starkNet_createAccount', { addressIndex, deploy, chainId });
192198
```
193199
194-
#### Example in HTML and JavaScript (Vanilla JS)
200+
#### Example in HTML and Vanilla JS
201+
202+
The following is a full example of a dapp with a button that, when clicked, connects to a Starknet wallet using a MetaMask Snap, creates a Starknet account, and displays the account address:
195203
196204
```html
197205
<html lang="en">
@@ -234,9 +242,9 @@ const accountInfo = await callSnap('starkNet_createAccount', { addressIndex, dep
234242
try {
235243
const snapId = 'npm:@consensys/starknet-snap'; // Snap ID
236244
await connect(snapId);
237-
const deploy = false; // whether to deploy the actual account
238-
const addressIndex = 0; // which address to derive
239-
const chainId = "0x534e5f5345504f4c4941"; // which chain to use mainnet id "0x534e5f4d41494e", sepolia id "0x534e5f5345504f4c4941"
245+
const deploy = false; // Whether to deploy the actual account.
246+
const addressIndex = 0; // The address to derive.
247+
const chainId = "0x534e5f5345504f4c4941"; // Chain ID of the network to use.
240248
const account = await callSnap(snapId, 'starkNet_createAccount', { addressIndex, deploy, chainId });
241249
console.log(account)
242250
document.getElementById('accountInfo').innerText = `Connected Starknet Account: ${account.address}`;
@@ -251,27 +259,29 @@ const accountInfo = await callSnap('starkNet_createAccount', { addressIndex, dep
251259
252260
#### Example in a React component
253261
262+
The following is a full example of connecting to the Starknet Snap using `wallet_invokeSnap` in a React component:
263+
254264
```javascript
255-
import React, { useState } from 'react';
265+
import React, { useState } from "react";
256266
const ConnectWallet = () => {
257267
const [accountInfo, setAccountInfo] = useState('');
258268
const connect = async (snapId) => {
259269
try {
260270
await window.ethereum.request({
261-
method: 'wallet_requestSnaps',
271+
method: "wallet_requestSnaps",
262272
params: {
263273
[snapId]: {},
264274
},
265275
});
266276
} catch (err) {
267-
console.error('Snap connection error:', err);
277+
console.error("Snap connection error:", err);
268278
alert(`Error connecting to Snap: ${err.message || err}`);
269279
}
270280
};
271281
const callSnap = async (snapId, method, params) => {
272282
try {
273283
const response = await window.ethereum.request({
274-
method: 'wallet_invokeSnap',
284+
method: "wallet_invokeSnap",
275285
params: {
276286
snapId,
277287
request: {
@@ -288,15 +298,15 @@ const ConnectWallet = () => {
288298
};
289299
const handleConnectClick = async () => {
290300
try {
291-
const snapId = 'npm:@consensys/starknet-snap'; // Snap ID
301+
const snapId = "npm:@consensys/starknet-snap"; // Snap ID
292302
await connect(snapId);
293-
const deploy = false; // whether to deploy the actual account
294-
const addressIndex = 0; // which address to derive
295-
const chainId = '0x534e5f5345504f4c4941'; // chain (sepolia)
296-
const account = await callSnap(snapId, 'starkNet_createAccount', { addressIndex, deploy, chainId });
303+
const deploy = false; // Whether to deploy the actual account.
304+
const addressIndex = 0; // The address to derive.
305+
const chainId = "0x534e5f5345504f4c4941"; // Chain ID of the network to use.
306+
const account = await callSnap(snapId, "starkNet_createAccount", { addressIndex, deploy, chainId });
297307
setAccountInfo(`Connected Starknet Account: ${account.address}`);
298308
} catch (error) {
299-
console.error('Error connecting to Starknet Snap:', error);
309+
console.error("Error connecting to Starknet Snap:", error);
300310
}
301311
};
302312
return (

wallet/how-to/use-non-evm-networks/starknet/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sidebar_position: 1
88
Starknet is a non-EVM Layer 2 network. To interact with Starknet accounts in MetaMask, you need to use the Starknet Snap.
99

1010
You can use the `get-starknet` library or the `wallet_invokeSnap` JSON-RPC method to connect to the Starknet Snap.
11-
The choice depends on your specific use case and development preferences.
11+
1212

1313
`get-starknet`:
1414
- Provides a high-level API that abstracts complex operations.
@@ -27,7 +27,7 @@ The choice depends on your specific use case and development preferences.
2727

2828
:::note
2929

30-
We recommend using the `get-starknet` library for a most use cases.
30+
We recommend using the `get-starknet` library for most use cases.
3131

3232
:::
3333

@@ -59,7 +59,7 @@ sequenceDiagram
5959
participant get as get-starknet
6060
participant mm as MetaMask
6161
participant Snap as Starknet Snap
62-
participant network as Starknet Network
62+
participant network as Starknet network
6363
6464
dapp->>get: Initialize connection
6565
get->>mm: Request connection
@@ -87,11 +87,11 @@ sequenceDiagram
8787
get->>dapp: Notify change
8888
```
8989

90-
The `get-starknet` library offers several key features that improve how dapps interact with the StarkNet network through MetaMask."
90+
The `get-starknet` library offers several key features that improve how dapps interact with the Starknet network through MetaMask:
9191

92-
- The `WalletAccount` uses a specified provider to access data from the StarkNet network.
93-
- For transactions, `get-starknet` prepares the data and sends it to MetaMask for signing via StarkNet Snap.
92+
- The `WalletAccount` uses a specified provider to access data from the Starknet network.
93+
- For transactions, `get-starknet` prepares the data and sends it to MetaMask for signing through the Starknet Snap.
9494
- `get-starknet` enables the dapp to create contract instances connected to the `WalletAccount`, allowing smart contract functions to be invoked, with MetaMask handling the signatures.
95-
- It sets up listeners for account and network changes in MetaMask, so the dapp can subscribe and update its state accordingly.
96-
- `get-starknet` can request network changes through MetaMask, allowing users to switch between StarkNet networks, such as Mainnet or Sepolia testnet.
97-
- It can also request MetaMask to display specific tokens, improving the user experience.
95+
- `get-starknet` sets up listeners for account and network changes in MetaMask, so the dapp can subscribe and update its state accordingly.
96+
- `get-starknet` can request network changes through MetaMask, allowing users to switch between Starknet networks, such as Mainnet or Sepolia testnet.
97+
- `get-starknet` can also request MetaMask to display specific tokens, improving the user experience.

wallet/how-to/use-non-evm-networks/starknet/manage-starknet-accounts.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ sidebar_position: 2
55

66
# Manage Starknet accounts
77

8-
## Account creation
9-
10-
Account creation in Starknet is handled by the wallet provider. As a dapp developer, you do not create accounts directly. Instead, you guide users to create an account with their preferred wallet provider.
11-
12-
:::note
13-
14-
Currently, multiple Starknet accounts are not supported in the Starknet Snap.
15-
16-
:::
8+
You can manage Starknet accounts to display account information, handle transactions, and respond to account changes in your dapp.
179

1810
## View account information
1911

@@ -23,7 +15,7 @@ After a user connects, you can display the account details, such as the account
2315
const showAccountInfo = async () => {
2416
const account = await connectStarknetAccount();
2517
if (account) {
26-
document.getElementById('accountAddress').innerText = `Account Address: ${account}`;
18+
document.getElementById("accountAddress").innerText = `Account Address: ${account}`;
2719
}
2820
};
2921
```
@@ -33,8 +25,8 @@ const showAccountInfo = async () => {
3325
To retrieve and display connected Starknet accounts, use the `get-starknet` library in combination with React hooks:
3426

3527
```javascript
36-
import { useStarknet, useConnectors } from '@starknet-react/core';
37-
import { useState, useEffect } from 'react';
28+
import { useStarknet, useConnectors } from "@starknet-react/core";
29+
import { useState, useEffect } from "react";
3830

3931
function AccountDisplay() {
4032
const { account } = useStarknet();
@@ -67,6 +59,16 @@ function AccountDisplay() {
6759
6860
This component displays the connected account address if available, and provides buttons to connect or disconnect accounts.
6961
62+
## Account creation
63+
64+
Account creation in Starknet is handled by the wallet provider. As a dapp developer, you do not create accounts directly. Instead, you guide users to create an account with their preferred wallet provider.
65+
66+
:::note
67+
68+
Currently, multiple Starknet accounts are not supported in the Starknet Snap.
69+
70+
:::
71+
7072
## Manage account transactions
7173
7274
You can manage transactions with `get-starknet`:
@@ -78,8 +80,8 @@ const invokeStarknetContract = async () => {
7880
const starknet = getStarknet();
7981
await starknet.enable(); // Make sure the wallet is enabled
8082

81-
const contractAddress = '0xYourContractAddress'; // Replace with your contract address
82-
const entrypoint = 'function_name'; // The function you want to call
83+
const contractAddress = "0xYourContractAddress"; // Replace with your contract address
84+
const entrypoint = "function_name"; // The function you want to call
8385
const calldata = [/* your function arguments */]; // Replace with calldata
8486

8587
const result = await starknet.invoke({
@@ -88,21 +90,22 @@ const invokeStarknetContract = async () => {
8890
calldata: calldata
8991
});
9092

91-
console.log('Transaction result:', result);
93+
console.log("Transaction result: ", result);
9294
} catch (error) {
93-
console.error('Error invoking contract:', error);
95+
console.error("Error invoking contract:", error);
9496
}
9597
};
9698
```
9799
This invokes a specific function on a Starknet smart contract, handling wallet connection and transaction submission, and logs the result or any errors.
98100
101+
99102
## Handle account changes and disconnections
100103
101104
To handle account changes and disconnections, you can use event listeners provided by `get-starknet`:
102105
103106
```javascript
104-
import { getStarknet } from 'get-starknet';
105-
import { useEffect, useState } from 'react';
107+
import { getStarknet } from "get-starknet";
108+
import { useEffect, useState } from "react";
106109

107110
function AccountChangeHandler() {
108111
const [account, setAccount] = useState<string | null>(null);
@@ -111,29 +114,29 @@ function AccountChangeHandler() {
111114
const starknet = getStarknet();
112115

113116
const handleAccountsChanged = (accounts: string[]) => {
114-
console.log('Accounts changed:', accounts);
117+
console.log("Accounts changed:", accounts);
115118
setAccount(accounts[0] || null);
116-
// Update your app's state here
119+
// Update your app"s state here
117120
};
118121

119122
const handleDisconnect = () => {
120-
console.log('Disconnected from wallet');
123+
console.log("Disconnected from wallet");
121124
setAccount(null);
122125
// Handle disconnection (e.g., reset app state, show connect button)
123126
};
124127

125128
if (starknet) {
126-
starknet.on('accountsChanged', handleAccountsChanged);
127-
starknet.on('networkChanged', handleDisconnect);
129+
starknet.on("accountsChanged", handleAccountsChanged);
130+
starknet.on("networkChanged", handleDisconnect);
128131

129132
// Initial account setup
130133
starknet.enable().then((accounts: string[]) => {
131134
setAccount(accounts[0] || null);
132135
});
133136

134137
return () => {
135-
starknet.off('accountsChanged', handleAccountsChanged);
136-
starknet.off('networkChanged', handleDisconnect);
138+
starknet.off("accountsChanged", handleAccountsChanged);
139+
starknet.off("networkChanged", handleDisconnect);
137140
};
138141
}
139142
}, []);
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
---
2-
description: Sign Starknet data in MetaMask.
2+
description: Sign Starknet transactions in MetaMask.
33
sidebar_position: 5
44
---
55

6-
# Sign Starknet data
6+
# Sign Starknet transactions
7+
8+
After the account is connected, you can sign a transaction using the `wallet.account.signer.signTransaction` function:
9+
```typescript
10+
const signStarknetTransaction = async (wallet, contractAddress, entrypoint, calldata) => {
11+
try {
12+
if(wallet?.isConnected !== true){
13+
throw('Wallet not connected');
14+
}
15+
16+
// Send the transaction
17+
const result = await wallet?.account?.signer.signTransaction({
18+
contractAddress: contractAddress, // The address of the contract
19+
entrypoint: entrypoint, // The function to call in the contract
20+
calldata: calldata // The parameters to pass to the function
21+
});
22+
console.log('Transaction signed successfully:', result);
23+
return result;
24+
} catch (error) {
25+
console.error('Error signing transaction:', error);
26+
}
27+
};

0 commit comments

Comments
 (0)