-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dima Brook
committed
Nov 19, 2023
1 parent
bd7326f
commit c5837e5
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
--- | ||
sidebar_label: '8. Provider' | ||
sidebar_position: 8 | ||
image: /cover-image.png | ||
description: The Most Powerful Multi-Chain NFT bridge | ||
--- | ||
|
||
# Provider | ||
|
||
|
||
## Definition | ||
|
||
A Web3 provider is a software component that facilitates communication between a decentralized application (DApp) and the Ethereum blockchain or other blockchain networks. In the context of Web3, which refers to the third generation of the web focused on decentralized and blockchain-based applications, a Web3 provider plays a crucial role in enabling interactions between the front end (user interface) of a DApp and the underlying blockchain. The wallet selected by a user injects a provider or a provider with a signer into a web3 dApp, allowing the application to query the blockchain of interest or sign transactions on behalf of the user once authorized by the user in the wallet. | ||
|
||
Key characteristics and functionalities of a Web3 provider include: | ||
|
||
1. **Connection to the Blockchain** | ||
The Web3 provider establishes a connection between the DApp and a blockchain network, allowing the DApp to send and receive data from the blockchain. | ||
|
||
2. **User Wallet Interaction** | ||
It enables the DApp to interact with the user's cryptocurrency wallet. It includes requesting the user's address, signing transactions, and performing other wallet-related operations. | ||
|
||
3. **Ethereum JavaScript Provider API** | ||
A Web3 provider typically implements or adheres to a standard API for interacting with Ethereum from JavaScript. EIP-1193 is an example of an Ethereum Improvement Proposal that aims to standardize the Ethereum JavaScript Provider API. | ||
|
||
4. Event Handling | ||
Providers often emit events to notify the DApp about changes in the user's wallet state or other relevant events. It allows the DApp to react dynamically to user actions. | ||
|
||
Web3 providers play a crucial role in ensuring the security of transactions and interactions with the blockchain. They handle tasks such as transaction signing, a critical security measure. | ||
|
||
## Provider related standards | ||
|
||
### EIP-1193 Ethereum Provider JavaScript API | ||
|
||
[EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) is a proposal for an Ethereum Improvement Proposal (EIP) that defines a standard Ethereum JavaScript Provider API. The primary goal of EIP-1193 is to standardize the way Ethereum decentralized applications (DApps) interact with user wallets and Ethereum providers in the browser. | ||
|
||
#### Connectivity | ||
|
||
A provider can interact with only one chain at a time. The provider is said to be `connected` when such a connection is established. Otherwise, the provider is said to be `disconnected`. | ||
|
||
#### Request API | ||
|
||
All provider APIs must implement the following interface: | ||
|
||
```ts | ||
interface RequestArguments { | ||
readonly method: string; | ||
readonly params?: readonly unknown[] | object; | ||
} | ||
|
||
Provider.request(args: RequestArguments): Promise<unknown>; | ||
``` | ||
|
||
```ts | ||
// Most Providers are available as window.ethereum on page load. | ||
const ethereum = window.ethereum; | ||
``` | ||
|
||
**Example requests**: | ||
|
||
```ts | ||
(async() => { | ||
const chainId = await ethereum.request({ | ||
method:'eth_chainId' | ||
}).catch(err => { | ||
console.error(`Error fetching chainId: ${err.code}: ${err.message}`) | ||
}) | ||
}) | ||
``` | ||
|
||
|
||
#### Provider Errors | ||
|
||
If a Remote Procedure Call (RPC) is rejected, the provider returns a `ProviderRpcError`. All such errors must implement the following interface: | ||
|
||
```ts | ||
interface ProviderRpcError extends Error { | ||
// A human-readable string | ||
message: string; | ||
// An integer number | ||
code: number; | ||
// Additional info | ||
data?: unknown; | ||
} | ||
``` | ||
|
||
|Status Code|Error Name|Problem descriptopn| | ||
|:-:|:-:|:-| | ||
|4001|User Rejected Request|The user rejected the request.| | ||
|4100|Unauthorized|The user has not authorized the requested method and account.| | ||
|4200|Unsupported Method|The Provider does not support the requested method.| | ||
|4900|Disconnected|The Provider is disconnected from all chains.| | ||
|4901| Chain Disconnected|The Provider is disconnected from the requested chain.| | ||
|
||
#### Events | ||
|
||
JS Provider events follow the [Node.js Class EventEmitter API](https://nodejs.org/api/events.html). | ||
|
||
Listening to events happen like this: | ||
|
||
```ts | ||
// Template | ||
Provider.on('event-name', listener:(<variable>:<type>) => void): Provider; | ||
|
||
// Examples: | ||
Provider.on('connect', listener: (connectInfo: {chainId: string}) => void): Provider; | ||
Provider.on('disconnect', listener: (error: ProviderRpcError) => void): Provider; | ||
Provider.on('chainChanged', listener: (chainId: string) => void): Provider; | ||
Provider.on('accountsChanged', listener: (accounts: string[]) => void): Provider; | ||
``` | ||
|
||
Other related EIPs | ||
- [EIP-1102 Opt-in account exposure](https://eips.ethereum.org/EIPS/eip-1102) | ||
- [EIP-1474 RPC Call Specification](https://eips.ethereum.org/EIPS/eip-1474) | ||
- [EIP-1767 GraphQL interface to Ethereum node data](https://eips.ethereum.org/EIPS/eip-1767) | ||
- [EIP-2255 Wallet Permissions](https://eips.ethereum.org/EIPS/eip-2255) |