# Get Started with Ethers.js (/docs/builders/frontend/ethers/get-started)

Learn how to set up and configure Ethers.js for building on RISE.

## Installation

Install Ethers.js v6 using your preferred package manager:

```bash
npm install ethers
```

## Project Setup

Create a new project:

```bash
mkdir my-rise-ethers-project
cd my-rise-ethers-project
npm init -y
npm install ethers dotenv
```

## Connect to RISE

### Using JsonRpcProvider

Connect to RISE Testnet:

```javascript
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');

// Get network info
const network = await provider.getNetwork();
console.log('Connected to:', network.name);
console.log('Chain ID:', network.chainId);

// Get latest block
const blockNumber = await provider.getBlockNumber();
console.log('Latest block:', blockNumber);
```

### Create a Wallet

Create a wallet from a private key:

```javascript
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');

// From private key
const wallet = new ethers.Wallet(
  'YOUR_PRIVATE_KEY',
  provider
);

console.log('Address:', wallet.address);

// Get balance
const balance = await provider.getBalance(wallet.address);
console.log('Balance:', ethers.formatEther(balance), 'ETH');
```

### Generate New Wallet

Create a new random wallet:

```javascript
// Generate new wallet
const newWallet = ethers.Wallet.createRandom();

console.log('Address:', newWallet.address);
console.log('Private Key:', newWallet.privateKey);
console.log('Mnemonic:', newWallet.mnemonic.phrase);

// Connect to provider
const connectedWallet = newWallet.connect(provider);
```

### From Mnemonic

Restore wallet from mnemonic phrase:

```javascript
const mnemonic = 'your twelve word mnemonic phrase here ...';
const wallet = ethers.Wallet.fromPhrase(mnemonic, provider);

console.log('Address:', wallet.address);
```

## Environment Variables

Create a `.env` file:

```bash
PRIVATE_KEY=0x...
RPC_URL=https://testnet.riselabs.xyz
```

Load and use environment variables:

```javascript
import { ethers } from 'ethers';
import { config } from 'dotenv';

config();

const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

console.log('Connected as:', wallet.address);
```

## Network Configuration

Create a config file for RISE network:

```javascript
// config.js
export const RISE_TESTNET = {
  chainId: 11155931,
  name: 'RISE Testnet',
  rpcUrl: 'https://testnet.riselabs.xyz',
  explorer: 'https://explorer.testnet.riselabs.xyz',
  symbol: 'ETH',
  decimals: 18
};

export function getProvider() {
  return new ethers.JsonRpcProvider(RISE_TESTNET.rpcUrl);
}

export function getWallet(privateKey) {
  const provider = getProvider();
  return new ethers.Wallet(privateKey, provider);
}
```

Usage:

```javascript
import { getProvider, getWallet } from './config.js';

const provider = getProvider();
const wallet = getWallet(process.env.PRIVATE_KEY);
```

## Basic Operations

### Check Account Balance

```javascript
const address = '0x...';
const balance = await provider.getBalance(address);

console.log('Balance (wei):', balance.toString());
console.log('Balance (ETH):', ethers.formatEther(balance));
```

### Get Transaction Count (Nonce)

```javascript
const nonce = await provider.getTransactionCount(wallet.address);
console.log('Transaction count:', nonce);
```

### Get Gas Price

```javascript
const feeData = await provider.getFeeData();

console.log('Gas Price:', ethers.formatUnits(feeData.gasPrice, 'gwei'), 'Gwei');
console.log('Max Fee:', ethers.formatUnits(feeData.maxFeePerGas, 'gwei'), 'Gwei');
console.log('Max Priority Fee:', ethers.formatUnits(feeData.maxPriorityFeePerGas, 'gwei'), 'Gwei');
```

### Send ETH

```javascript
const tx = await wallet.sendTransaction({
  to: '0x...',
  value: ethers.parseEther('0.1') // 0.1 ETH
});

console.log('Transaction hash:', tx.hash);

// Wait for confirmation
const receipt = await tx.wait();
console.log('Transaction confirmed in block:', receipt.blockNumber);
```

## Example: Complete Setup

```javascript
// index.js
import { ethers } from 'ethers';
import { config } from 'dotenv';

config();

// Setup
const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);

async function main() {
  // Network info
  const network = await provider.getNetwork();
  console.log('Network:', network.name);
  console.log('Chain ID:', network.chainId.toString());

  // Account info
  console.log('\nAccount:', wallet.address);

  const balance = await provider.getBalance(wallet.address);
  console.log('Balance:', ethers.formatEther(balance), 'ETH');

  const nonce = await provider.getTransactionCount(wallet.address);
  console.log('Nonce:', nonce);

  // Block info
  const blockNumber = await provider.getBlockNumber();
  console.log('\nLatest block:', blockNumber);

  const block = await provider.getBlock('latest');
  console.log('Block timestamp:', new Date(block.timestamp * 1000).toISOString());

  // Gas info
  const feeData = await provider.getFeeData();
  console.log('\nGas Price:', ethers.formatUnits(feeData.gasPrice, 'gwei'), 'Gwei');
}

main().catch(console.error);
```

Run the script:

```bash
node index.js
```

## Next Steps

* [Reading from Contracts](/docs/builders/ethers/reading) - Query contract data
* [Writing to Contracts](/docs/builders/ethers/writing) - Send transactions and interact with contracts
