Socket
Socket
Sign inDemoInstall

@metamask/sdk

Package Overview
Dependencies
Maintainers
0
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/sdk


Version published
Weekly downloads
145K
decreased by-10.8%
Maintainers
0
Weekly downloads
 
Created

What is @metamask/sdk?

@metamask/sdk is a JavaScript library that allows developers to integrate MetaMask functionalities into their web applications. It provides a set of tools to interact with the Ethereum blockchain, manage user accounts, and facilitate transactions.

What are @metamask/sdk's main functionalities?

Connecting to MetaMask

This feature allows you to connect to the MetaMask wallet and request access to the user's Ethereum accounts. The code sample demonstrates how to initialize the MetaMask SDK, get the provider, and request account access.

const { MetaMaskSDK } = require('@metamask/sdk');
const sdk = new MetaMaskSDK();
const ethereum = sdk.getProvider();

async function connect() {
  try {
    const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
    console.log('Connected account:', accounts[0]);
  } catch (error) {
    console.error('Error connecting to MetaMask:', error);
  }
}

connect();

Sending Transactions

This feature allows you to send transactions through the MetaMask wallet. The code sample demonstrates how to create a transaction object and send it using the MetaMask provider.

const { MetaMaskSDK } = require('@metamask/sdk');
const sdk = new MetaMaskSDK();
const ethereum = sdk.getProvider();

async function sendTransaction() {
  try {
    const transactionParameters = {
      to: '0xRecipientAddress',
      value: '0x29a2241af62c0000', // 0.1 ETH in hexadecimal
      gas: '0x5208', // 21000 GWEI in hexadecimal
    };
    const txHash = await ethereum.request({
      method: 'eth_sendTransaction',
      params: [transactionParameters],
    });
    console.log('Transaction hash:', txHash);
  } catch (error) {
    console.error('Error sending transaction:', error);
  }
}

sendTransaction();

Listening for Events

This feature allows you to listen for events such as account changes and network changes. The code sample demonstrates how to set up event listeners for 'accountsChanged' and 'chainChanged' events.

const { MetaMaskSDK } = require('@metamask/sdk');
const sdk = new MetaMaskSDK();
const ethereum = sdk.getProvider();

ethereum.on('accountsChanged', (accounts) => {
  console.log('Accounts changed:', accounts);
});

ethereum.on('chainChanged', (chainId) => {
  console.log('Chain changed:', chainId);
});

Other packages similar to @metamask/sdk

FAQs

Package last updated on 25 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc