# Ethereum provider API

The NeoLine extension wallet injects an Ethereum provider, as specified by[ EIP-1193](https://eips.ethereum.org/EIPS/eip-1193), into the browser at window\.NEOLineNeoX.

You can use this provider in your dapp to request users' Ethereum accounts, read on-chain data, and have the user sign messages and transactions.

{% hint style="info" %}
NOTE

NeoLine supports [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963), You can access the provider API using the selected EIP-6963 provider object. Throughout this documentation, we refer to the selected provider using provider.
{% endhint %}

<pre class="language-javascript"><code class="lang-javascript"><strong>let provider;
</strong><strong>window.addEventListener('eip6963:announceProvider', (event) => {
</strong>  if (event.detail.provider.isNEOLine) {
    provider = event.detail.provider;
  }
});
window.dispatchEvent(new Event('eip6963:requestProvider'));
</code></pre>

## Properties

### `isNEOLine`

This property is `true` if the user has NeoLine installed, and `false` otherwise.

#### Example

```javascript
provider.isNEOLine // Or window.NEOLineNeoX.isNEOLine if you don't support EIP-6963.
```

## Methods

### `request()`

This method is used to submit [JSON-RPC API requests](https://tutorial.neoline.io/reference/json-rpc-api) to Ethereum using NeoLine.

#### Parameters

An object containing:

* method: string - The [JSON-RPC API](https://tutorial.neoline.io/reference/json-rpc-api) method name.
* params: array or object - (Optional) Parameters of the RPC method. In practice, if a method has parameters, they're almost always of type array.

#### Returns

A promise that resolves to the result of the RPC method call. If the request fails, the promise rejects with an error.

#### Example

The following is an example of using `request()` to call [`eth_sendTransaction`](https://tutorial.neoline.io/reference/json-rpc-api/eth_sendtransaction):

```javascript
provider // Or window.NEOLineNeoX if you don't support EIP-6963.
  .request({
    method: "eth_sendTransaction",
    params: [
      {
        from: "0xE45e9AdC2B51514849ea5B38dF37d1a65e6D52f5",
        to: "0x7Cd07BCbcCF30d71E768F5228b56d5B7Cc07f674",
        value: "0x3b9aca00", // 1_000_000_000
      },
    ],
  })
  .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.
  })
```

#### NeoLine Wallet supports many standardized Ethereum RPC methods, including:

* [wallet\_addEthereumChain](https://tutorial.neoline.io/reference/json-rpc-api/wallet_addethereumchain)
* [wallet\_switchEthereumChain](https://tutorial.neoline.io/reference/json-rpc-api/wallet_switchethereumchain)
* [wallet\_watchAsset](https://tutorial.neoline.io/reference/json-rpc-api/wallet_watchasset)
* [personal\_sign](https://tutorial.neoline.io/reference/json-rpc-api/personal_sign)
* [eth\_signTypedData\_v4](https://tutorial.neoline.io/reference/json-rpc-api/eth_signtypeddata_v4)
* [eth\_chainId](https://tutorial.neoline.io/reference/json-rpc-api/eth_chainid)&#x20;
* [eth\_accounts](https://tutorial.neoline.io/reference/json-rpc-api/eth_accounts)
* [eth\_requestAccounts](https://tutorial.neoline.io/reference/json-rpc-api/eth_requestaccounts)
* [eth\_sendTransaction](https://tutorial.neoline.io/reference/json-rpc-api/eth_sendtransaction)
* eth\_sendRawTransaction
* eth\_blockNumber
* eth\_call
* eth\_estimateGas
* ...

## Events

The NeoLine provider emits events using the Node.js [`EventEmitter`](https://nodejs.org/api/events.html) API. The following is an example of listening to the [`accountsChanged`](https://tutorial.neoline.io/reference/ethereum-provider-api#accountschanged) event.

You should [remove listeners](https://tutorial.neoline.io/reference/ethereum-provider-api#remove-event-listeners) after you're done listening to an event (for example, on component `unmount` in React).

```javascript
function handleAccountsChanged(accounts) {
  // Handle new accounts, or lack thereof.
}

provider // Or window.NEOLineNeoX if you don't support EIP-6963.
  .on("accountsChanged", handleAccountsChanged)

// Later

provider // Or window.NEOLineNeoX if you don't support EIP-6963.
  .removeListener("accountsChanged", handleAccountsChanged)
```

### `accountsChanged` <a href="#accountschanged" id="accountschanged"></a>

```javascript
provider // Or window.NEOLineNeoX if you don't support EIP-6963.
  .on("accountsChanged", handler: (accounts: string[]) => void);
```

The provider emits this event when the return value of the [`eth_accounts`](https://tutorial.neoline.io/reference/json-rpc-api/eth_accounts) RPC method changes. `eth_accounts` returns either an empty array, or an array that contains the addresses of the accounts the caller is permitted to access with the most recently used account first. Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.

### `chainChanged`

```javascript
provider // Or window.NEOLineNeoX if you don't support EIP-6963.
  .on("chainChanged", handler: (chainId: string) => void);
```

The provider emits this event when the currently connected chain changes. Listen to this event to [detect a user's network](https://tutorial.neoline.io/reference/json-rpc-api/eth_chainid).

### Remove event listeners <a href="#remove-event-listeners" id="remove-event-listeners"></a>

#### `removeListener`

Use the `removeListener` method to remove specific event listeners from an `EventEmitter` object. In the following example `removeListener` is used to remove the `chainChanged` and `accountsChanged` events:

```javascript
// Use window.NEOLineNeoX instead of provider if EIP-6963 is not supported.

// Add listeners
provider.on("accountsChanged", updateWallet)
provider.on("chainChanged", updateWalletAndAccounts)

// Remove individual listeners
provider.removeListener("accountsChanged", updateWallet)
provider.removeListener("chainChanged", updateWalletAndAccounts)
```

## Errors

All errors returned by the NeoLine provider follow this interface:

```javascript
interface ProviderRpcError extends Error {
  message: string
  code: number
  data?: unknown
}
```

The `request()` provider method throws errors eagerly. You can use the error `code` property to determine why the request failed. Common codes and their meaning include:

* `4001` - The request is rejected by the user.
* `-32602` - The parameters are invalid.
* `-32603` - Internal error.

For the complete list of errors, see [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193#provider-errors) and [EIP-1474](https://eips.ethereum.org/EIPS/eip-1474#error-codes).
