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
Next Next commit
Update MM Connect EVM JS code samples with potential new usage
  • Loading branch information
alexandratran committed Nov 10, 2025
commit 1048fcef98017be20d2404877260880d1b156b12
2 changes: 1 addition & 1 deletion sdk/evm/connect/guides/connectkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You can [download the quickstart template](#set-up-using-a-template) or [manuall

## Set up manually

### 1. Install the SDK
### 1. Install MM Connect

Install MM Connect along with its peer dependencies to an existing React project:

Expand Down
2 changes: 1 addition & 1 deletion sdk/evm/connect/guides/dynamic.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ See how to [use the combined SDKs](#usage).

### 1. Install dependencies

Install the SDK and the required dependencies to an existing project:
Install MM Connect and the required dependencies to an existing project:

```bash npm2yarn
npm install @dynamic-labs/sdk-react-core @dynamic-labs/ethereum @dynamic-labs/wagmi-connector wagmi viem @tanstack/react-query
Expand Down
8 changes: 4 additions & 4 deletions sdk/evm/connect/guides/javascript/batch-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ When using `metamask_batch`, keep in mind the following:
The following is an example of batching JSON-RPC requests using `metamask_batch`:

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

const MMSDK = new MetaMaskSDK();
const provider = MMSDK.getProvider();
// Initialize SDK
const evmClient = createEVMClient();

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

try {
const results = await provider.request({
const results = await evmClient.request({
method: "metamask_batch",
params: [requests],
});
Expand Down
69 changes: 38 additions & 31 deletions sdk/evm/connect/guides/javascript/display-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,34 @@ extension (not on mobile).
To prompt users to add an ERC-20 token, you can add something like the following to your project script:

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

// Initialize SDK
const evmClient = createEVMClient();

const tokenAddress = "0xd00981105e61274c8a5cd5a88fe7e037d935b513"
const tokenSymbol = "TUT"
const tokenDecimals = 18
const tokenImage = "http://placekitten.com/200/300"

try {
// 'wasAdded' is a boolean. Like any RPC method, an error can be thrown.
const wasAdded = await provider // Or window.ethereum if you don't support EIP-6963.
.request({
method: "wallet_watchAsset",
params: {
type: "ERC20",
options: {
// The address of the token.
address: tokenAddress,
// A ticker symbol or shorthand, up to 5 characters.
symbol: tokenSymbol,
// The number of decimals in the token.
decimals: tokenDecimals,
// A string URL of the token logo.
image: tokenImage,
},
const wasAdded = await evmClient.request({
method: "wallet_watchAsset",
params: {
type: "ERC20",
options: {
// The address of the token.
address: tokenAddress,
// A ticker symbol or shorthand, up to 5 characters.
symbol: tokenSymbol,
// The number of decimals in the token.
decimals: tokenDecimals,
// A string URL of the token logo.
image: tokenImage,
},
})
},
})

if (wasAdded) {
console.log("Thanks for your interest!")
Expand Down Expand Up @@ -110,21 +114,25 @@ To prompt users to add a single NFT, add something like the following to your pr
`wallet_watchAsset` supports both ERC-721 and ERC-1155 NFT standards.

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

// Initialize SDK
const evmClient = createEVMClient();

try {
// wasAdded is a boolean. Like any RPC method, an error can be thrown.
const wasAdded = await provider // Or window.ethereum if you don't support EIP-6963.
.request({
method: "wallet_watchAsset",
params: {
type: "ERC721", // Or "ERC1155".
options: {
// The address of the token.
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
// ERC-721 or ERC-1155 token ID.
tokenId: "1",
},
const wasAdded = await evmClient.request({
method: "wallet_watchAsset",
params: {
type: "ERC721", // Or "ERC1155".
options: {
// The address of the token.
address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
// ERC-721 or ERC-1155 token ID.
tokenId: "1",
},
})
},
})

if (wasAdded) {
console.log("User successfully added the token!")
Expand All @@ -143,8 +151,7 @@ To prompt users to add multiple NFTs, use `sendAsync()` instead of
For example:

```javascript
provider // Or window.ethereum if you don't support EIP-6963.
.sendAsync([{
evmClient.sendAsync([{ // TO DO: Confirm if MM Connect supports sendAsync
method: "wallet_watchAsset",
params: {
type: "ERC721",
Expand All @@ -164,5 +171,5 @@ provider // Or window.ethereum if you don't support EIP-6963.
},
},
...
])
])
```
41 changes: 18 additions & 23 deletions sdk/evm/connect/guides/javascript/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,25 @@ You've successfully set up MM Connect.

## Set up manually

### 1. Install the SDK
### 1. Install MM Connect

Install the SDK in an existing JavaScript project:
Install MM Connect in an existing JavaScript project:

```bash npm2yarn
npm install @metamask/sdk
npm install @metamask/connect/evm
```

### 2. Initialize the SDK
### 2. Initialize MM Connect

The following are examples of using the SDK in various JavaScript environments:
The following are examples of using MM Connect in various JavaScript environments:

<Tabs>
<TabItem value="Web dapps">

```javascript
import { MetaMaskSDK } from "@metamask/sdk"
import { createEVMClient } from "@metamask/connect/evm"

const MMSDK = new MetaMaskSDK({
const evmClient = createEVMClient({
dappMetadata: {
name: "Example JavaScript dapp",
url: window.location.href,
Expand All @@ -119,7 +119,7 @@ const MMSDK = new MetaMaskSDK({
<head>
<script src="https://c0f4f41c-2f55-4863-921b-sdk-docs.github.io/cdn/metamask-sdk.js"></script>
<script>
const MMSDK = new MetaMaskSDK.MetaMaskSDK({
const evmClient = createEVMClient({
dappMetadata: {
name: "Example JavaScript dapp",
url: window.location.href,
Expand All @@ -145,21 +145,19 @@ These examples configure the SDK with the following options:
Connect to MetaMask and get the provider for RPC requests:

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

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

const result = await provider.request({
const result = await evmClient.request({
method: "eth_accounts",
params: [],
})
console.log("eth_accounts result:", result)
```

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

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`.
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`.

## Common SDK methods at a glance

Expand All @@ -175,24 +173,21 @@ Use `provider.request()` for arbitrary [JSON-RPC requests](../../reference/json-

```javascript
// 1. Connect and get accounts
const accounts = await MMSDK.connect()
const accounts = await evmClient.connect()

// 2. Connect and sign in one step
const signResult = await MMSDK.connectAndSign({
const signResult = await evmClient.connectAndSign({
msg: "Sign in to the dapp",
})

// 3. Get provider for RPC requests
const provider = MMSDK.getProvider()

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

// 5. Batch multiple RPC requests
const batchResults = await provider.request({
// 4. Batch multiple RPC requests
const batchResults = await evmClient.request({
method: "metamask_batch",
params: [{ method: "eth_accounts" }, { method: "eth_chainId" }],
})
Expand Down
13 changes: 9 additions & 4 deletions sdk/evm/connect/guides/javascript/interact-with-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ You can implement smart contract interactions directly in JavaScript.
The following example reads contract data using the [`eth_call`](../../reference/json-rpc-api/index.md) RPC method:

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

// Initialize SDK
const evmClient = createEVMClient();

async function getBalance(contractAddress, userAddress) {
try {
// Create function signature for balanceOf(address)
const functionSignature = "0x70a08231";
// Pad address to 32 bytes
const encodedAddress = userAddress.slice(2).padStart(64, "0");

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

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

// Send transaction
const txHash = await ethereum.request({
const txHash = await evmClient.request({
method: "eth_sendTransaction",
params: [{
from: accounts[0],
Expand All @@ -102,7 +107,7 @@ async function watchTransaction(txHash) {
return new Promise((resolve, reject) => {
const checkTransaction = async () => {
try {
const tx = await ethereum.request({
const tx = await evmClient.request({
method: "eth_getTransactionReceipt",
params: [txHash],
});
Expand Down
13 changes: 9 additions & 4 deletions sdk/evm/connect/guides/javascript/manage-networks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ The following example detects the current network using the
[`chainChanged`](../../reference/provider-api.md#chainchanged) provider event:

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

// Initialize SDK
const evmClient = createEVMClient();

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

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

try {
// Try to switch to the network
await ethereum.request({
await evmClient.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 ethereum.request({
await evmClient.request({
method: "wallet_addEthereumChain",
params: [{
chainId: network.chainId,
Expand Down
18 changes: 8 additions & 10 deletions sdk/evm/connect/guides/javascript/manage-user-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ and [`accountsChanged`](../../reference/provider-api.md#accountschanged) provide
For example:

```javascript
import { MetaMaskSDK } from "@metamask/sdk";
import { createEVMClient } from "@metamask/connect/evm";

// Initialize SDK
const MMSDK = new MetaMaskSDK();
const provider = MMSDK.getProvider();
const evmClient = createEVMClient();

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

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

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

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

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

const MMSDK = new MetaMaskSDK()
const provider = MMSDK.getProvider()
const evmClient = createEVMClient()

async function handleConnectAndSign() {
try {
const signature = await MMSDK.connectAndSign({ msg: "Hello in one go!" })
const signature = await evmClient.connectAndSign({ msg: "Hello in one go!" })
console.log("Signature:", signature)
} catch (err) {
console.error("Error with connectAndSign:", err)
Expand Down
Loading