# viem & ethers.js

Both libraries treat AEREDIUM like any other EVM chain. The only thing you need to set up is a chain definition / provider with the right RPC and chain ID.

## viem

### Define the chain

```ts
// chain.ts
import { defineChain } from "viem";

export const aeredium = defineChain({
  id: 1000,
  name: "AEREDIUM",
  nativeCurrency: { name: "AER", symbol: "AER", decimals: 18 },
  rpcUrls: {
    default: {
      http: ["https://rpc.aeredium.io"],
      webSocket: ["wss://rpc.aeredium.io"],
    },
  },
});
```

### Public client (read-only)

```ts
import { createPublicClient, http } from "viem";
import { aeredium } from "./chain";

const client = createPublicClient({
  chain: aeredium,
  transport: http(),
});

const block = await client.getBlock({ blockTag: "latest" });
const code = await client.getBytecode({ address: "0xContract" });
```

### Wallet client (sign & send)

```ts
import { createWalletClient, http, parseEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { aeredium } from "./chain";

const wallet = createWalletClient({
  account: privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`),
  chain: aeredium,
  transport: http(),
});

const hash = await wallet.sendTransaction({
  to: "0xRecipient",
  value: parseEther("0.5"),
});
```

### Browser wallet (MetaMask)

```ts
import { createWalletClient, custom } from "viem";
import { aeredium } from "./chain";

const wallet = createWalletClient({
  chain: aeredium,
  transport: custom((window as any).ethereum),
});
```

### Subscriptions

```ts
import { createPublicClient, webSocket } from "viem";
import { aeredium } from "./chain";

const ws = createPublicClient({
  chain: aeredium,
  transport: webSocket(),
});

const unwatch = ws.watchBlocks({
  onBlock: (b) => console.log("new block", b.number),
});
```

## ethers.js v6

### Provider

```ts
import { JsonRpcProvider, WebSocketProvider } from "ethers";

export const provider = new JsonRpcProvider(
  "https://rpc.aeredium.io",
  { chainId: 1000, name: "aeredium" },
);

export const wsProvider = new WebSocketProvider(
  "wss://rpc.aeredium.io",
  { chainId: 1000, name: "aeredium" },
);
```

The second `network` argument is recommended — it lets ethers skip the initial `eth_chainId` round-trip on startup.

### Wallet

```ts
import { Wallet } from "ethers";
import { provider } from "./provider";

const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);

const tx = await wallet.sendTransaction({
  to: "0xRecipient",
  value: 10n ** 17n, // 0.1 AER
});
const receipt = await tx.wait();
```

### Contract

```ts
import { Contract } from "ethers";
import { wallet } from "./wallet";

const erc20 = new Contract(
  "0xToken",
  ["function transfer(address,uint256) returns (bool)", "function balanceOf(address) view returns (uint256)"],
  wallet,
);

await erc20.transfer("0xRecipient", 10n ** 18n);
const bal = await erc20.balanceOf("0xRecipient");
```

### Subscriptions

```ts
import { wsProvider } from "./provider";

wsProvider.on("block", (n) => console.log("new block", n));

wsProvider.on(
  { address: "0xToken", topics: [/* Transfer topic */] },
  (log) => console.log("transfer", log),
);
```

## ethers.js v5

If you're still on v5, the API is similar; the only difference relevant to AEREDIUM:

```ts
import { providers, Wallet } from "ethers";

const provider = new providers.JsonRpcProvider("https://rpc.aeredium.io", {
  chainId: 1000,
  name: "aeredium",
});
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);
```

## web3.py (Python)

```python
from web3 import Web3
from web3.middleware import construct_sign_and_send_raw_middleware
from eth_account import Account

w3 = Web3(Web3.HTTPProvider("https://rpc.aeredium.io"))
acct = Account.from_key("0x...")
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct))
w3.eth.default_account = acct.address

tx_hash = w3.eth.send_transaction({
    "to": "0xRecipient",
    "value": w3.to_wei(0.1, "ether"),
})
print(w3.eth.wait_for_transaction_receipt(tx_hash))
```


---

# 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://aeredium.gitbook.io/developer.aeredium.io/tooling/viem-ethers.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.
