Sender Wallet Developer Documentation
  • Guide
    • Introduction
    • Getting Started
    • Sign in Applications
      • Sign in Applications in Near
      • Sign in Applications in Ethereum
    • Access Accounts
      • Access Accounts in Near
      • Access Account in Ethereum
    • Send Transactions
      • Send Transactions in Near
      • Send Transactions in Ethereum
  • API Reference
    • Near
      • NEAR Provider API
      • Deprecated APIs
    • Ethereum
      • Sign Data
      • Ethereum Provider API
    • Ton
      • Mobile Dapp Provider
  • Best Practices
    • Define App's Icon
Powered by GitBook
On this page
  • Properties
  • Methods
  • Events
  1. API Reference
  2. Ethereum

Ethereum Provider API

PreviousSign DataNextTon

Last updated 2 years ago

Sender injects a global JavaScript API into websites visited by its users using the window.sender.ethereum provider object. This API allows websites to request users' Ethereum accounts, read data from blockchains the user is connected to, and suggest that the user sign messages and transactions.

Properties

window.sender.ethereum.isSender

This property is true if the user has Sender installed.

Methods

eth_requestAccounts

Requests that the user provide an Ethereum address to be identified by. Use this method to . This method is specified by .

Example:

// eth_requestAccounts code snippet
document.getElementById('connectButton', connect);

function connect() {
  sender.ethereum
    .request({ method: 'eth_requestAccounts' })
    .then(handleAccountsChanged)
    .catch((error) => {
      if (error.code === 4001) {
        // EIP-1193 userRejectedRequest error
        console.log('Please connect to MetaMask.');
      } else {
        console.error(error);
      }
    });
}

wallet_getPermissions

wallet_requestPermissions

Example:

// wallet_requestPermissions code snippet

document.getElementById('requestPermissionsButton', requestPermissions);

function requestPermissions() {
  sender.ethereum
    .request({
      method: 'wallet_requestPermissions',
      params: [{ eth_accounts: {} }],
    })
    .then((permissions) => {
      const accountsPermission = permissions.find(
        (permission) => permission.parentCapability === 'eth_accounts'
      );
      if (accountsPermission) {
        console.log('eth_accounts permission successfully requested!');
      }
    })
    .catch((error) => {
      if (error.code === 4001) {
        // EIP-1193 userRejectedRequest error
        console.log('Permissions needed to continue.');
      } else {
        console.error(error);
      }
    });
}

window.sender.ethereum.request(args)

// RequestArguments interface
interface RequestArguments {
  method: string;
  params?: unknown[] | object;
}

window.sender.ethereum.request(args: RequestArguments): Promise<unknown>;
// Using window.sender.ethereum.request(args) to call eth_sendTransaction example
params: [
  {
    from: '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
    to: '0xd46e8dd67c5d32be8058bb8eb970870f07244567',
    gas: '0x76c0', // 30400
    gasPrice: '0x9184e72a000', // 10000000000000
    value: '0x9184e72a', // 2441406250
    data:
      '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675',
  },
];

sender.ethereum
  .request({ method: 'eth_sendTransaction', params })
  .then((result) => {
    // The result varies by RPC method.
    // For example, this method returns a transaction hash hexadecimal string upon success.
  })
  .catch((error) => {
    // If the request fails, the Promise rejects with an error.
  });

Events

// handle accounts changed
function handleAccountsChanged(accounts) {
  // Handle new accounts, or lack thereof.
}

window.sender.ethereum.on('accountsChanged', handleAccountsChanged);

// Later
window.sender.ethereum.removeListener('accountsChanged', handleAccountsChanged);

The first argument of window.sender.ethereum.removeListener is the event name, and the second argument is a reference to the function passed to window.sender.ethereum.on for the event.

accountsChanged

window.sender.ethereum.on('accountsChanged', handler: (accounts: Array<string>) => void);

chainChanged

window.sender.ethereum.on('chainChanged', handler: (chainId: string) => void);

connect

interface ConnectInfo {
  chainId: string;
}

window.sender.ethereum.on('connect', handler: (connectInfo: ConnectInfo) => void);

The provider emits this event when it's first able to submit RPC requests to a chain.

disconnect

window.sender.ethereum.on('disconnect', handler: (error: ProviderRpcError) => void);

The provider emits this event if it becomes unable to submit RPC requests to a chain. In general, this only happens due to network connectivity issues or some unforeseen error.

message

interface ProviderMessage {
  type: string;
  data: unknown;
}

window.sender.ethereum.on('message', handler: (message: ProviderMessage) => void);

The provider emits this event when it receives a message that the user should be notified of. The type property identifies the kind of message.

Gets the caller's current . This method returns an array of the caller's permission objects. If the caller has no permissions, the array is empty.

Requests from the user. The request causes a Sender popup to appear. You should only request permissions in response to a direct user action, such as a button click.

Use this method to submit requests to Ethereum using Sender It returns a promise that resolves to the result of the RPC method call.

The following is an example of using window.sender.ethereum.request(args) to call :

The Sender provider emits events using the Node.js API. The following is an example of listening to the event. You should remove listeners once you're done listening to an event (for example, on component unmount in React).

The Sender provider emits this event when the return value of the RPC method changes. eth_accounts returns either an empty array, or an array that contains the address of the most recently used account the caller is permitted to access. Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.

This means that the provider emits accountsChanged when the user's exposed account address changes. Listen to this event to .

The provider emits this event when the currently connected chain changes. Listen to this event to .

access a user's accounts
EIP-1102
permissions
permissions
RPC API
eth_sendTransaction
EventEmitter
accountsChanged
eth_accounts
handle accounts
detect a user's network