Skip to content

Commit 5e5bcbb

Browse files
SDK: New Docs (#1766)
* feat: intro + wagmi quickstart * feat: versioning * remove versioning for now + sidebar structured * refactor * review fixes + tidy * reorg * improvments * ui refresh * tidy * better copy * tidy * tidy * updated redirects + cleaned up wallet+sdk separation * addressing review comments * pr comments * switch and rename * update example link * update to new link * force vercel to deploy again * test preview deployment * pm comments * partial edit * full edit * add, fix, clean up redirects + update what's new * add missing redirect --------- Co-authored-by: Alexandra Tran <[email protected]> Co-authored-by: Alexandra Carrillo <[email protected]>
1 parent b838a49 commit 5e5bcbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2400
-2771
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ To contribute changes:
7171

7272
> **Notes:**
7373
>
74-
> - All documentation content is located in the `wallet`, `snaps`, `services`, and
74+
> - All documentation content is located in the `wallet`, `sdk`, `snaps`, `services`, and
7575
> `developer-tools` directories.
76-
> - If you add a new documentation page, edit `wallet-sidebar.js`, `snaps-sidebar.js`,
76+
> - If you add a new documentation page, edit `wallet-sidebar.js`, `sdk-sidebar.js`, `snaps-sidebar.js`,
7777
> `services-sidebar.js`, or `dashboard-sidebar.js` to add the page to the
7878
> [sidebar](https://docs-template.consensys.io/create/configure-docusaurus#sidebar).
7979
> - If you delete, rename, or move a documentation file, add a
@@ -120,7 +120,7 @@ Refer to the [Consensys documentation style guide](https://docs-template.consens
120120

121121
## Add images
122122

123-
All images are located in the `wallet/assets`, `snaps/assets`, `services/images`, and
123+
All images are located in the `wallet/assets`, `sdk/_assets`, `snaps/assets`, `services/images`, and
124124
`developer-tools/images` directories.
125125
When adding a new image, such as a screenshot or diagram, make sure the image has a white or
126126
`#1b1b1d` color background in order for it to be compatible with the site's light and dark modes.

docs/whats-new.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ of the [MetaMask developer page](https://metamask.io/developer/).
1111

1212
## January 2025
1313

14+
- Added new [MetaMask SDK documentation section](/sdk).
15+
([#1766](https://github.com/MetaMask/metamask-docs/pull/1766))
1416
- Documented Snaps [`Banner`](/snaps/features/custom-ui/#banner), [`Container`](/snaps/features/custom-ui/#container), [`Footer`](/snaps/features/custom-ui/#footer), [`Skeleton`](/snaps/features/custom-ui/#skeleton), and [`Value`](/snaps/features/custom-ui/#value) UI components.
1517
([#1835](https://github.com/MetaMask/metamask-docs/pull/1835))
1618

@@ -26,7 +28,7 @@ of the [MetaMask developer page](https://metamask.io/developer/).
2628

2729
- Documented [updated error responses](/services/reference/ethereum/json-rpc-methods) when rate-limiting Infura JSON-RPC API calls. ([#1749](https://github.com/MetaMask/metamask-docs/pull/1749))
2830
- Documented [Unichain Sepolia](/services/reference/unichain) support. ([#1725](https://github.com/MetaMask/metamask-docs/pull/1725))
29-
- Updated Snaps [Custom UI documentation](/snaps/features/custom-ui/) for MetaMask Extension version 12.6.
31+
- Updated Snaps [custom UI documentation](/snaps/features/custom-ui/) for MetaMask Extension version 12.6.
3032
([#1715](https://github.com/MetaMask/metamask-docs/pull/1715))
3133
- Added tutorial for
3234
[creating a simple Starknet dapp](/wallet/how-to/use-non-evm-networks/starknet/create-a-simple-starknet-dapp).

docusaurus.config.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ const config = {
151151
}
152152
},
153153
],
154+
[
155+
"@docusaurus/plugin-content-docs",
156+
{
157+
id: "sdk",
158+
path: "sdk",
159+
routeBasePath: "sdk",
160+
editUrl: "https://github.com/MetaMask/metamask-docs/edit/main/",
161+
sidebarPath: require.resolve("./sdk-sidebar.js"),
162+
breadcrumbs: false,
163+
},
164+
],
154165
"./src/plugins/plugin-json-rpc.ts",
155166
isProd
156167
? [
@@ -178,9 +189,13 @@ const config = {
178189
width: 150,
179190
},
180191
items: [
192+
{
193+
to: "sdk",
194+
label: "SDK",
195+
},
181196
{
182197
to: "wallet",
183-
label: "Wallet",
198+
label: "Wallet API",
184199
},
185200
{
186201
to: "snaps",
@@ -248,6 +263,10 @@ const config = {
248263
label: "Wallet",
249264
to: "/wallet",
250265
},
266+
{
267+
label: "SDK",
268+
to: "/sdk",
269+
},
251270
{
252271
label: "Snaps",
253272
to: "/snaps",

sdk-sidebar.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// @ts-check
2+
3+
/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */
4+
const sidebar = {
5+
sdkSidebar: [
6+
{
7+
type: 'category',
8+
label: 'Introduction',
9+
collapsible: false,
10+
collapsed: false,
11+
items: [
12+
'introduction/welcome',
13+
'introduction/supported-platforms',
14+
'introduction/supported-networks',
15+
'introduction/llm-prompt',
16+
{
17+
type: 'link',
18+
label: 'Try demo dapp',
19+
href: 'https://metamask-sdk-examples-relink.vercel.app/',
20+
},
21+
],
22+
},
23+
{
24+
type: 'category',
25+
label: 'Quickstart',
26+
collapsible: false,
27+
collapsed: false,
28+
items: [
29+
'quickstart/javascript-wagmi',
30+
'quickstart/javascript',
31+
'quickstart/react-native',
32+
],
33+
},
34+
{
35+
type: 'category',
36+
label: 'Guides',
37+
collapsible: false,
38+
collapsed: false,
39+
items: [
40+
'guides/authenticate-users',
41+
'guides/manage-networks',
42+
'guides/handle-transactions',
43+
'guides/interact-with-contracts',
44+
{
45+
type: 'category',
46+
label: 'Advanced',
47+
collapsible: true,
48+
collapsed: true,
49+
items: [
50+
'guides/advanced/connect-and-sign',
51+
'guides/advanced/batch-requests',
52+
],
53+
},
54+
],
55+
},
56+
{
57+
type: 'category',
58+
label: 'Reference',
59+
collapsible: false,
60+
collapsed: false,
61+
items: [
62+
'reference/sdk-options'
63+
],
64+
}
65+
],
66+
};
67+
68+
module.exports = sidebar;

sdk/_assets/connect.gif

336 KB
Loading

sdk/_assets/network.gif

302 KB
Loading

sdk/_assets/quickstart.jpg

137 KB
Loading

sdk/guides/advanced/batch-requests.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
description: Batch multiple JSON-RPC requests.
3+
---
4+
5+
# Batch requests
6+
7+
You can batch multiple JSON-RPC requests in your JavaScript dapp.
8+
9+
The SDK's `metamask_batch` method enables you to batch multiple JSON-RPC requests in a single call,
10+
providing a streamlined approach for dapps to interact with EVM networks, and enabling complex
11+
sequences of actions.
12+
This method enhances performance, usability, and efficiency by reducing the number of network calls
13+
made to MetaMask.
14+
15+
Use cases include:
16+
17+
- **Batching multiple signatures** - Send multiple signing requests in one batch.
18+
19+
- **Switching networks** - Switch the EVM network, perform an action such as sending a transaction,
20+
and switch back, all in one batch.
21+
22+
- **Mixed transactions and signatures** - Combine transaction sending and signing requests in one batch.
23+
24+
`metamask_batch` opens up additional possibilities for sophisticated transaction flows in dapps,
25+
enhancing the user experience and operational efficiency.
26+
27+
## Prerequisites
28+
29+
Set up MetaMask SDK in your JavaScript dapp.
30+
31+
## Use the `metamask_batch` method
32+
33+
`metamask_batch` takes an array of JSON-RPC requests (`ChainRPC[]`) as its parameter.
34+
35+
Each request in the batch is independent.
36+
The user receives a prompt for each action within the batch, allowing them to approve or reject
37+
individual requests.
38+
If any request is rejected, the entire batch fails and an error is returned, ensuring integrity in
39+
transactional operations.
40+
41+
The method returns an array of results corresponding to each request.
42+
43+
### React / Next.js / React Native example
44+
45+
The following is an example of using `metamask_batch` to batch
46+
[`personal_sign`](/wallet/reference/json-rpc-methods/personal_sign) and
47+
[`eth_sendTransaction`](/wallet/reference/json-rpc-methods/eth_sendtransaction) in React, Next.js, or React Native/Expo:
48+
49+
```javascript title="index.js"
50+
import { metamask_batch } from "metamask-sdk"
51+
52+
function MyComponent() {
53+
const handleBatchRequest = async () => {
54+
const batchRequests = [
55+
{ method: "personal_sign", params: ["message", "address"] },
56+
{
57+
method: "eth_sendTransaction",
58+
params: [
59+
{
60+
/* Transaction parameters */
61+
},
62+
],
63+
},
64+
]
65+
66+
try {
67+
const results = await metamask_batch(batchRequests)
68+
console.log(results) // Process results.
69+
} catch (error) {
70+
console.error("Batch request failed", error)
71+
}
72+
}
73+
74+
return <button onClick={handleBatchRequest}>Send Batch Request</button>
75+
}
76+
```
77+
78+
### Vue.js example
79+
80+
The following is an example of using `metamask_batch` to batch
81+
[`personal_sign`](/wallet/reference/json-rpc-methods/personal_sign) and
82+
[`eth_sendTransaction`](/wallet/reference/json-rpc-methods/eth_sendtransaction) in Vue.js:
83+
84+
```javascript title="App.vue"
85+
<script>
86+
import { metamask_batch } from "metamask-sdk";
87+
88+
export default {
89+
methods: {
90+
async sendBatchRequest() {
91+
const batchRequests = [
92+
{ method: "personal_sign", params: ["message", "address"] },
93+
{
94+
method: "eth_sendTransaction",
95+
params: [
96+
{
97+
/* Transaction parameters */
98+
},
99+
],
100+
},
101+
];
102+
103+
try {
104+
const results = await metamask_batch(batchRequests);
105+
console.log(results);
106+
} catch (error) {
107+
console.error("Error in batch request", error);
108+
}
109+
}
110+
}
111+
}
112+
</script>
113+
```
114+
115+
### Best practices
116+
117+
Follow these best practices when using `metamask_batch`:
118+
119+
- **Ensure each request in the batch is properly formatted** according to the JSON-RPC specifications.
120+
121+
- **Handle errors appropriately**, especially when a batch request is partially approved.
122+
123+
- **Test batch operations** to ensure consistent behavior across different networks and accounts.
124+
125+
- **Be aware of the dependencies between chained requests.**
126+
Avoid creating a dependency where the outcome of one request directly influences the context or
127+
validity of a subsequent request within the same batch.
128+
For example, avoid chaining a [`wallet_switchEthereumChain`](/wallet/reference/json-rpc-methods/wallet_switchethereumchain)
129+
request with [`eth_signTypedData_v4`](/wallet/reference/json-rpc-methods/eth_signtypeddata_v4), because
130+
`eth_signTypedData_v4` relies on the current chain ID, which would be altered by `wallet_switchEthereumChain`.
131+
This approach ensures that each request in the batch operates independently and maintains its
132+
integrity, regardless of changes introduced by preceding requests in the batch.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
description: Connect and sign in a single interaction.
3+
---
4+
5+
# Connect and sign
6+
7+
You can connect to MetaMask and sign data in a single interaction from your JavaScript dapp.
8+
9+
The SDK's `connectAndSign` method provides a streamlined approach for dapps to interact with MetaMask.
10+
This method combines the [`eth_requestAccounts`](/wallet/reference/json-rpc-methods/eth_requestaccounts)
11+
and [`personal_sign`](/wallet/reference/json-rpc-methods/personal_sign) RPC methods, executing them sequentially.
12+
`connectAndSign` takes one parameter, the message string to be signed, and passes the message and
13+
the output of `eth_requestAccounts` directly to `personal_sign`.
14+
15+
This method enhances dapp user experience, especially on mobile platforms, by allowing users to
16+
connect to MetaMask and sign a message in a single interaction, requiring only one switch between
17+
the mobile dapp and MetaMask Mobile.
18+
This is useful for various purposes such as authentication and transaction verification.
19+
20+
<p align="center">
21+
<video width="350" controls>
22+
<source src="/connect-and-sign.mp4" type="video/mp4" />
23+
</video>
24+
</p>
25+
26+
## Prerequisites
27+
28+
- MetaMask SDK set up in your JavaScript dapp.
29+
30+
- MetaMask Mobile version 7.10 or later.
31+
Your users must have an updated version of MetaMask Mobile for this feature to work correctly.
32+
For older versions of MetaMask, this function may not work as expected.
33+
34+
## Use the `connectAndSign` method
35+
36+
Use the `connectAndSign` method as follows:
37+
38+
```javascript
39+
const connectAndSign = async () => {
40+
try {
41+
const signResult = await sdk?.connectAndSign({
42+
msg: "Connect + Sign message",
43+
})
44+
setResponse(signResult)
45+
} catch (err) {
46+
console.warn("failed to connect..", err)
47+
}
48+
}
49+
```
50+
51+
To invoke `connectAndSign`:
52+
53+
1. Ensure the `MetaMaskSDK` instance (`sdk` in this context) is properly initialized and available.
54+
2. Call `connectAndSign` with the message to be signed.
55+
3. Handle the promise to process the response or catch any errors.
56+
57+
## Examples
58+
59+
The following is an example of using the `connectAndSign` method in a React dapp, integrating it
60+
into a functional component:
61+
62+
```javascript
63+
import React, { useState } from "react"
64+
import { useSDK } from "@metamask/sdk-react"
65+
66+
function MyComponent() {
67+
const { sdk } = useSDK()
68+
const [signedMessage, setSignedMessage] = useState("")
69+
70+
const handleConnectAndSign = async () => {
71+
try {
72+
const message = "Your message here"
73+
const signature = await sdk.connectAndSign({ msg: message })
74+
setSignedMessage(signature)
75+
} catch (error) {
76+
console.error("Error in signing:", error)
77+
}
78+
}
79+
80+
return (
81+
<div>
82+
<button onClick={handleConnectAndSign}>Connect and Sign</button>
83+
{signedMessage && <p>Signed Message: {signedMessage}</p>}
84+
</div>
85+
)
86+
}
87+
```
88+
89+
For examples of using the `connectAndSign` function in Next.js and Vue.js, see the
90+
[example Next.js dapp](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples/nextjs-demo)
91+
and [example Vue.js dapp](https://github.com/MetaMask/metamask-sdk/tree/main/packages/examples/vuejs)
92+
on GitHub.

0 commit comments

Comments
 (0)