Skip to content

Commit

Permalink
docs: add Ethereum Provider section
Browse files Browse the repository at this point in the history
  • Loading branch information
KipCrossing committed Aug 7, 2023
1 parent e4a870e commit 3cc78d2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions site/docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,25 @@ The following utilities support type inference when you use const assertions or
## Configuring Internal Types

For advanced use-cases, you may want to configure viem's internal types. Most of viem's types relating to ABIs and EIP-712 Typed Data are powered by [ABIType](https://abitype.dev). See ABIType's [documentation](https://abitype.dev/config.html) for more info on how to configure types.

## Ethereum Provider

Importing `viem/window` will type `window.ethereum` as `EIP1193Provider | undefined`, allowing type-safe interactions with the Ethereum provider in the browser. It will be `undefined` in case no browser extension (like MetaMask) is detected.


```ts
import "viem/window";

if (window.ethereum) {
window.ethereum
.request({ method: "eth_chainId" })
.then((chainId: string) => console.log(`Connected to chain: ${chainId}`))
.catch((error: Error) =>
console.error(`Error getting chainId: ${error.message}`)
);
} else {
console.log("Ethereum provider is not available");
}
```

This provides a type-safe interface when using the standard Ethereum [JSON-RPC API](https://ethereum.org/en/developers/docs/apis/json-rpc/) methods.

0 comments on commit 3cc78d2

Please sign in to comment.