# MoonProvider

<br>

## @moonup/ethers Package Documentation

### Summary

The `@moonup/ethers` package is a utility package that provides a `MoonProvider` class for interacting with Ethereum-based blockchains using the MoonSDK from the `@moonup/moon-sdk` package. This provider can be used to sign messages, transactions, and typed data using the MoonSDK, and it implements the `Provider` and `IEthereumProvider` interfaces from the `ethers` library.

### Usage Examples

Here's a basic example of how to use the `MoonProvider` class:

```javascript
import { MoonSDK } from '@moonup/moon-sdk';
import { MoonProvider } from '@moonup/ethers';
import { ethers } from 'ethers';

const SDK = new MoonSDK({ /* your configuration */ });
const address = '0xYourEthereumAddress';
const chainId = 1; // mainnet

const provider = new MoonProvider({ SDK, address, chainId });

// Now you can use the provider with ethers
const signer = provider.getSigner();
const balance = await signer.getBalance();
console.log(`Balance: ${ethers.utils.formatEther(balance)} ETH`);
```

### API Reference

#### `MoonProvider`

A provider for interacting with Ethereum-based blockchains using the MoonSDK.

**Constructor**

* `options` (Object)
  * `SDK` (MoonSDK): An instance of the MoonSDK.
  * `address` (string): The Ethereum address of the account.
  * `chainId` (number): The ID of the Ethereum chain.

**Methods**

* `request(args: RequestArguments)`: Sends a JSON-RPC request.

  ```javascript
  const blockNumber = await provider.request({ method: 'eth_blockNumber' });
  console.log(`Current block number: ${blockNumber}`);
  ```
* `updateConfig(options: MoonProviderOptions)`: Updates the configuration options.

  ```javascript
  provider.updateConfig({ chainId: 4, address: '0xNewEthereumAddress' });
  ```
* `connect()`: Connects to the MoonSDK.

  ```javascript
  await provider.connect();
  ```
* `disconnect()`: Disconnects from the MoonSDK.

  ```javascript
  await provider.disconnect();
  ```
* `sendAsync(args: RequestArguments, callback: (error: Error | null, response: any) => void)`: Sends a JSON-RPC request asynchronously.

  ```javascript
  provider.sendAsync({ method: 'eth_blockNumber' }, (error, result) => {
    if (error) {
      console.error(error);
    } else {
      console.log(`Current block number: ${result}`);
    }
  });
  ```
* `enable()`: Enables the provider.

  ```javascript
  const accounts = await provider.enable();
  console.log(`Connected accounts: ${accounts}`);
  ```
* `isMoonProvider()`: Returns `true` if the provider is a `MoonProvider`.

  ```javascript
  console.log(`Is MoonProvider: ${provider.isMoonProvider()}`);
  ```
* `getChainId()`: Returns the ID of the Ethereum chain.

  ```javascript
  console.log(`Chain ID: ${provider.getChainId()}`);
  ```
* `getSigner()`: Returns the `MoonSigner` instance.

  ```javascript
  const signer = provider.getSigner();
  ```
* `getNetwork()`: Returns the network information.

  ```javascript
  const network = await provider.getNetwork();
  console.log(`Network name: ${network.name}`);
  ```
* `getBlockNumber()`: Returns the current block number.

  ```javascript
  const blockNumber = await provider.getBlockNumber();
  console.log(`Current block number: ${blockNumber}`);
  ```
* `getGasPrice()`: Returns the current gas price.

  ```javascript
  const gasPrice = await provider.getGasPrice();
  console.log(`Current gas price: ${gasPrice.toString()} wei`);
  ```
* `getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)`: Returns the balance of an account.

  ```javascript
  const balance = await provider.getBalance('0xYourEthereumAddress');
  console.log(`Balance: ${ethers.utils.formatEther(balance)} ETH`);
  ```
* `getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)`: Returns the number of transactions sent from an account.

  ```javascript
  const transactionCount = await provider.getTransactionCount('0xYourEthereumAddress');
  console.log(`Transaction count: ${transactionCount}`);
  ```
* `getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)`: Returns the code of a contract.

  ```javascript
  const code = await provider.getCode('0xYourContractAddress');
  console.log(`Contract code: ${code}`);
  ```
* `getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag> | undefined)`: Returns the value of a storage slot of a contract.

  ```javascript
  const storageValue = await provider.getStorageAt('0xYourContractAddress', 0);
  console.log(`Storage value: ${storageValue}`);
  ```
* `sendTransaction(signedTransaction: string | Promise<string>)`: Sends a transaction.

  ```javascript
  const signer = provider.getSigner();
  const transaction = {
    to: '0xRecipientAddress',
    value: ethers.utils.parseEther('1.0'),
  };
  const tx = await signer.sendTransaction(transaction);
  console.log(`Transaction hash: ${tx.hash}`);
  ```
* `call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag> | undefined)`: Calls a contract method.

  ```javascript
  const contract = new ethers.Contract('0xYourContractAddress', abi, provider);
  const result = await contract.myMethod();
  console.log(`Method result: ${result}`);
  ```
* `estimateGas(transaction: Deferrable<TransactionRequest>)`: Estimates the gas required for a transaction.

  ```javascript
  const signer = provider.getSigner();
  const transaction = {
    to: '0xRecipientAddress',
    value: ethers.utils.parseEther('1.0'),
  };
  const gasLimit = await signer.estimateGas(transaction);
  console.log(`Estimated gas limit: ${gasLimit.toString()}`);
  ```
* `getBlock(blockHashOrBlockTag: BlockTag | Promise<BlockTag>)`: Returns a block.

  ```javascript
  const block = await provider.getBlock('latest');
  console.log(`Block number: ${block.number}`);
  ```
* `getBlockWithTransactions(blockHashOrBlockTag: BlockTag | Promise<BlockTag>)`: Returns a block with its transactions.

  ```javascript
  const block = await provider.getBlockWithTransactions('latest');
  console.log(`Block number: ${block.number}`);
  console.log(`Transactions: ${block.transactions.length}`);
  ```
* `getTransaction(transactionHash: string)`: Returns a transaction.

  ```javascript
  const transaction = await provider.getTransaction('0xYourTransactionHash');
  console.log(`Transaction hash: ${transaction.hash}`);
  ```
* `getTransactionReceipt(transactionHash: string)`: Returns a transaction receipt.

  ```javascript
  const receipt = await provider.getTransactionReceipt('0xYourTransactionHash');
  console.log(`Transaction status: ${receipt.status}`);
  ```
* `getLogs(filter: Filter)`: Returns logs that match a filter.

  ```javascript
  const filter = {
    address: '0xYourContractAddress',
    topics: [ethers.utils.id('MyEvent()')],
  };
  const logs = await provider.getLogs(filter);
  console.log(`Number of logs: ${logs.length}`);
  ```
* `resolveName(name: string | Promise<string>)`: Resolves an ENS name to an address.

  ```javascript
  const address = await provider.resolveName('myname.eth');
  console.log(`Address: ${address}`);
  ```
* `lookupAddress(address: string | Promise<string>)`: Resolves an address to an ENS name.

  ```javascript
  const name = await provider.lookupAddress('0xYourEthereumAddress');
  console.log(`Name: ${name}`);
  ```
* `emit(eventName: EventType, ...args: any[])`: Emits an event.

  ```javascript
  provider.on('block', (blockNumber) => {
    console.log(`New block: ${blockNumber}`);
  });
  provider.emit('block', 1234567);
  ```
* `listenerCount(eventName?: EventType | undefined)`: Returns the number of listeners for an event.

  ```javascript
  const listenerCount = provider.listenerCount('block');
  console.log(`Number of listeners: ${listenerCount}`);
  ```
* `listeners(eventName?: EventType | undefined)`: Returns the listeners for an event.

  ```javascript
  const listeners = provider.listeners('block');
  console.log(`Number of listeners: ${listeners.length}`);
  ```
* `removeAllListeners(eventName?: EventType | undefined)`: Removes all listeners for an event.

  ```javascript
  provider.removeAllListeners('block');
  ```
* `waitForTransaction(transactionHash: string, confirmations?: number | undefined, timeout?: number | undefined)`: Waits for a transaction to be mined.

  ```javascript
  const receipt = await provider.waitForTransaction('0xYourTransactionHash');
  console.log(`Transaction status: ${receipt.status}`);
  ```
* `on(eventName: EventType, listener: Listener)`: Adds a listener for an event.

  ```javascript
  provider.on('block', (blockNumber) => {
    console.log(`New block: ${blockNumber}`);
  });
  ```
* `once(eventName: EventType, listener: Listener)`: Adds a one-time listener for an event.

  ```javascript
  provider.once('block', (blockNumber) => {
    console.log(`New block: ${blockNumber}`);
  });
  ```
* `off(eventName: EventType, listener?: Listener | undefined)`: Removes a listener for an event.

  ```javascript
  const listener = (blockNumber) => {
    console.log(`New block: ${blockNumber}`);
  };
  provider.on('block', listener);
  provider.off('block', listener);
  ```

**Example**

```javascript
const provider = new MoonProvider({ SDK, address, chainId });
window.ethereum = moon;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usemoon.ai/additional-moon-packages-7ca327c0bb2846d88b8ba5bc6caeaead/moon-ethers-f381fbf881c647e1aab3d43fb4ad0600/moonprovider-98424fc361554e529b42c6618739e9be.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
