# getRpcUrls

```typescript
/* src/utils/getRpcUrls */
import { Rule } from "../types/block3d";
import { type Config } from "wagmi";
import { getClient } from "@wagmi/core";

interface RpcUrls {
  [chainId: number]: string;
}

/**
 * @dev This function views the wagmi config and retrieves any necessary provided 
 * RPC URLs, if none are provided we use the default values
 * @param rules is an array of valid rule types
 * @param wagmiConig is the current wagmi config object
 * @return `rpcUrls` is an KV object with chainIds mapped to RPC URLs
 */
export async function getRpcUrls(rules: Rule[], wagmiConfig: Config) {
  /* Step 1: Use Set to store only unique chainIds */
  const chainIds = new Set<number>();
  rules.forEach((rule) => {
    if (rule.contracts) {
      rule?.contracts.forEach((contract) => {
        chainIds.add(contract.chainId);
      });
    }
  });
  const chainIdsArray = Array.from(chainIds);

  /* Step 2: View RPC URLs and add them to KV object  */
  let rpcUrls: RpcUrls = {};

  chainIdsArray.forEach(async (chainId) => {
    const client = await getClient(wagmiConfig, {
      chainId: chainId,
    });

    const url = client?.transport.url;
    if (url) {
      rpcUrls[chainId] = url;
    }
  });

  /* Step 3: Return the KV object */
  return rpcUrls;
}
```


---

# 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://block3d.gitbook.io/block3d/utils/getrpcurls.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.
