Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add provider to code snippets
  • Loading branch information
alexandratran committed Nov 11, 2025
commit a4daed39d42beb898aa34b7945f14ccd9ed09975
3 changes: 2 additions & 1 deletion sdk/evm/connect/guides/javascript/batch-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following is an example of batching JSON-RPC requests using `metamask_batch`
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

async function handleBatchRequests() {
// Example batch: one personal_sign call and one eth_sendTransaction call.
Expand All @@ -55,7 +56,7 @@ async function handleBatchRequests() {
];

try {
const results = await evmClient.request({
const results = await provider.request({
method: "metamask_batch",
params: [requests],
});
Expand Down
8 changes: 5 additions & 3 deletions sdk/evm/connect/guides/javascript/display-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ To prompt users to add an ERC-20 token, you can add something like the following
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

const tokenAddress = "0xd00981105e61274c8a5cd5a88fe7e037d935b513"
const tokenSymbol = "TUT"
Expand All @@ -49,7 +50,7 @@ const tokenImage = "http://placekitten.com/200/300"

try {
// 'wasAdded' is a boolean. Like any RPC method, an error can be thrown.
const wasAdded = await evmClient.request({
const wasAdded = await provider.request({
method: "wallet_watchAsset",
params: {
type: "ERC20",
Expand Down Expand Up @@ -116,10 +117,11 @@ To prompt users to add a single NFT, add something like the following to your pr
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

try {
// wasAdded is a boolean. Like any RPC method, an error can be thrown.
const wasAdded = await evmClient.request({
const wasAdded = await provider.request({
method: "wallet_watchAsset",
params: {
type: "ERC721", // Or "ERC1155".
Expand Down Expand Up @@ -149,7 +151,7 @@ To prompt users to add multiple NFTs, use `sendAsync()` instead of
For example:

```javascript
evmClient.sendAsync([{ // TO DO: Confirm if MM Connect supports sendAsync
provider.sendAsync([{
method: "wallet_watchAsset",
params: {
type: "ERC721",
Expand Down
17 changes: 11 additions & 6 deletions sdk/evm/connect/guides/javascript/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,12 @@ These examples configure the SDK with the following options:
Connect to MetaMask and get the provider for RPC requests:

```javascript
const provider = evmClient.getProvider()

const accounts = await evmClient.connect()
console.log("Connected account:", accounts[0])

const result = await evmClient.request({
const result = await provider.request({
method: "eth_accounts",
params: [],
})
Expand All @@ -157,7 +159,7 @@ console.log("eth_accounts result:", result)

`evmClient.connect()` handles cross-platform connection (desktop and mobile), including deeplinking.

Use `evmClient.request()` for arbitrary [JSON-RPC requests](../../reference/json-rpc-api/index.md) like `eth_chainId` or `eth_getBalance`, or for [batching requests](batch-requests.md) via `metamask_batch`.
Use `provider.request()` for arbitrary [JSON-RPC requests](../../reference/json-rpc-api/index.md) like `eth_chainId` or `eth_getBalance`, or for [batching requests](batch-requests.md) via `metamask_batch`.

## Common SDK methods at a glance

Expand All @@ -180,14 +182,17 @@ const signResult = await evmClient.connectAndSign({
msg: "Sign in to the dapp",
})

// 3. Make an RPC request
const result = await evmClient.request({
// 3. Get provider for RPC requests
const provider = evmClient.getProvider()

// 4. Make an RPC request
const result = await provider.request({
method: "eth_accounts",
params: [],
})

// 4. Batch multiple RPC requests
const batchResults = await evmClient.request({
// 5. Batch multiple RPC requests
const batchResults = await provider.request({
method: "metamask_batch",
params: [{ method: "eth_accounts" }, { method: "eth_chainId" }],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following example reads contract data using the [`eth_call`](../../reference
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

async function getBalance(contractAddress, userAddress) {
try {
Expand All @@ -34,7 +35,7 @@ async function getBalance(contractAddress, userAddress) {
// Pad address to 32 bytes
const encodedAddress = userAddress.slice(2).padStart(64, "0");

const result = await evmClient.request({
const result = await provider.request({
method: "eth_call",
params: [{
to: contractAddress,
Expand Down Expand Up @@ -73,7 +74,7 @@ RPC methods:
async function mintNFT(contractAddress, tokenId) {
try {
// Get user's account
const accounts = await evmClient.request({
const accounts = await provider.request({
method: "eth_requestAccounts"
});

Expand All @@ -83,7 +84,7 @@ async function mintNFT(contractAddress, tokenId) {
const encodedTokenId = tokenId.toString(16).padStart(64, "0");

// Send transaction
const txHash = await evmClient.request({
const txHash = await provider.request({
method: "eth_sendTransaction",
params: [{
from: accounts[0],
Expand Down
9 changes: 5 additions & 4 deletions sdk/evm/connect/guides/javascript/manage-networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ The following example detects the current network using the
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

// Get current chain ID
async function getCurrentChain() {
try {
const chainId = await evmClient.request({
const chainId = await provider.request({
method: "eth_chainId"
});
console.log("Current chain ID:", chainId);
Expand All @@ -47,7 +48,7 @@ async function getCurrentChain() {
}

// Listen for network changes
ethereum.on("chainChanged", (chainId) => { // TO DO: Update with MM Connect usage
provider.on("chainChanged", (chainId) => {
console.log("Network changed to:", chainId);
// We recommend reloading the page
window.location.reload();
Expand Down Expand Up @@ -84,15 +85,15 @@ async function switchNetwork(networkKey) {

try {
// Try to switch to the network
await evmClient.request({
await provider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: network.chainId }]
});
} catch (err) {
// If the error code is 4902, the network needs to be added
if (err.code === 4902) {
try {
await evmClient.request({
await provider.request({
method: "wallet_addEthereumChain",
params: [{
chainId: network.chainId,
Expand Down
11 changes: 6 additions & 5 deletions sdk/evm/connect/guides/javascript/manage-user-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ For example:
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

// Connect wallet
async function connectWallet() {
try {
// Disable button while request is pending
document.getElementById("connectBtn").disabled = true;

const accounts = await evmClient.request({
const accounts = await provider.request({
method: "eth_requestAccounts"
});

Expand All @@ -66,14 +67,14 @@ async function connectWallet() {
// Disconnect wallet
async function disconnectWallet() {
try {
await evmClient.terminate()
await provider.terminate()
} catch (err) {
console.error("Error with disconnecting:", err)
}
}

// Handle account changes
provider.on("accountsChanged", (accounts) => { // TO DO: Update with MM Connect usage
provider.on("accountsChanged", (accounts) => {
if (accounts.length === 0) {
// User disconnected
document.getElementById("status").textContent = "Not connected";
Expand Down Expand Up @@ -104,9 +105,9 @@ You can use MM Connect's [`connectAndSign`](../../reference/methods.md#connectan
For example:

```js
import { createEVMClient } from "@metamask/connect/evm"
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient()
const evmClient = createEVMClient();

async function handleConnectAndSign() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ For example:
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

const capabilities = await evmClient.request({
const capabilities = await provider.request({
method: "wallet_getCapabilities",
params: [
"0xd46e8dd67c5d32be8058bb8eb970870f07244567", // The user's wallet address.
Expand Down Expand Up @@ -123,7 +124,7 @@ Use [`wallet_sendCalls`](../../../reference/json-rpc-api/index.md) to submit a b
For example:

```js title="index.js"
const result = await evmClient.request({
const result = await provider.request({
method: "wallet_sendCalls",
params: [
{
Expand Down Expand Up @@ -168,7 +169,7 @@ the status of the submitted batch of transactions, using the batch ID returned b
For example:

```js title="index.js"
const status = await evmClient.request({
const status = await provider.request({
method: "wallet_getCallsStatus",
params: [
"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331" // Batch ID.
Expand Down
10 changes: 6 additions & 4 deletions sdk/evm/connect/guides/javascript/send-transactions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ RPC methods.
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

async function sendTransaction(recipientAddress, amount) {
try {
// Get current account
const accounts = await evmClient.request({
const accounts = await provider.request({
method: "eth_requestAccounts"
});
const from = accounts[0];
Expand All @@ -51,7 +52,7 @@ async function sendTransaction(recipientAddress, amount) {
};

// Send transaction
const txHash = await evmClient.request({
const txHash = await provider.request({
method: "eth_sendTransaction",
params: [transaction],
});
Expand All @@ -70,7 +71,7 @@ function watchTransaction(txHash) {
return new Promise((resolve, reject) => {
const checkTransaction = async () => {
try {
const tx = await evmClient.request({
const tx = await provider.request({
method: "eth_getTransactionReceipt",
params: [txHash],
});
Expand Down Expand Up @@ -135,10 +136,11 @@ RPC method.
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

async function estimateGas(transaction) {
try {
const gasEstimate = await evmClient.request({
const gasEstimate = await provider.request({
method: "eth_estimateGas",
params: [transaction]
});
Expand Down
6 changes: 4 additions & 2 deletions sdk/evm/connect/guides/javascript/sign-data/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ The following is an example of using `eth_signTypedData_v4` with MetaMask:
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

signTypedDataV4Button.addEventListener("click", async function (event) {
event.preventDefault()
Expand Down Expand Up @@ -141,7 +142,7 @@ signTypedDataV4Button.addEventListener("click", async function (event) {
var params = [from[0], msgParams]
var method = "eth_signTypedData_v4"

evmClient.sendAsync( // TO DO: Confirm if MM Connect supports sendAsync
provider.sendAsync(
{
method,
params,
Expand Down Expand Up @@ -216,6 +217,7 @@ The following is an example of using `personal_sign` with MetaMask:
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

personalSignButton.addEventListener("click", async function (event) {
event.preventDefault()
Expand All @@ -225,7 +227,7 @@ personalSignButton.addEventListener("click", async function (event) {
// For historical reasons, you must submit the message to sign in hex-encoded UTF-8.
// This uses a Node.js-style buffer shim in the browser.
const msg = `0x${Buffer.from(exampleMessage, "utf8").toString("hex")}`
const sign = await evmClient.request({
const sign = await provider.request({
method: "personal_sign",
params: [msg, from],
})
Expand Down
3 changes: 2 additions & 1 deletion sdk/evm/connect/guides/javascript/sign-data/siwe.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ The following is an example of setting up SIWE with MetaMask using
import { createEVMClient } from "@metamask/connect/evm";

const evmClient = createEVMClient();
const provider = evmClient.getProvider();

const siweSign = async (siweMessage) => {
try {
const from = accounts[0]
const msg = `0x${Buffer.from(siweMessage, "utf8").toString("hex")}`
const sign = await evmClient.request({
const sign = await provider.request({
method: "personal_sign",
params: [msg, from],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ import SimplifiedApiReference from '@site/src/components/SimplifiedApiReference'
description: "Invalid typed data structure"
}
]}
exampleRequest={`await evmClient.request({
exampleRequest={`await provider.request({
method: "eth_signTypedData_v4",
params: [
"0x3b7252d007059ffc82d16d022da3cbf9992d2f70",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ import SimplifiedApiReference from '@site/src/components/SimplifiedApiReference'
description: "Upgrade rejected"
}
]}
exampleRequest={`await evmClient.request({
exampleRequest={`await provider.request({
method: "wallet_sendCalls",
params: [{
"version": "2.0.0",
Expand Down