# 9. Waku (optional)

### Imports

```typescript
import {
  NETWORK_CONFIG,
  type CachedTokenFee,
  type FeeTokenDetails,
  type NetworkName,
  type SelectedBroadcaster,
  type TransactionGasDetails,
} from "@railgun-community/shared-models";
import { calculateBroadcasterFeeERC20Amount } from "@railgun-community/wallet";
const wakuModule = import("@railgun-community/waku-broadcaster-client-node");
// let wakuModule: any;

let WakuBroadcasterClient: any;
let BroadcasterTransaction: any;

let waku: any; 
```

### Setup Waku&#x20;

```typescript
export const initializeBroadcasters = async (network: NetworkName) => {
  if (!waku) {
    waku = await wakuModule;
  }
  WakuBroadcasterClient = waku.WakuBroadcasterClient;
  BroadcasterTransaction = waku.BroadcasterTransaction;

  const { chain } = NETWORK_CONFIG[network];
  const options = {};
  const callback = (chain: any, status: any) => {
    if (status !== "Connected") {
      console.log(`WAKU ${chain.id}:${chain.type} ${status}`);
    }
  };
  const debugLogger = {
    log: (_msg: string) => {
      console.log(msg);
    },
    error: (error: Error) => {
      console.error(error);
    },
  };
  WakuBroadcasterClient.start(chain, options, callback, debugLogger);
};

```

### Utility Functions

```typescript
export const getBroadcasterFeeInfo = async (
  network: NetworkName,
  tokenAddress: string
): Promise<SelectedBroadcaster> => {
  const { chain } = NETWORK_CONFIG[network];
  return await WakuBroadcasterClient.findBestBroadcaster(
    chain,
    tokenAddress,
    true
  );
};

// TODO: write out broadcaster transaction cost calculation function

export const getBroadcasterFeeRecipientDetails = async (
  selectedBroadcaster: SelectedBroadcaster,
  estimatedGasDetails: TransactionGasDetails,
  feeTokenDetails: FeeTokenDetails
) => {
  const broadcasterFeeAmountDetails = calculateBroadcasterFeeERC20Amount(
    feeTokenDetails,
    estimatedGasDetails
  );

  const broadcasterFeeERC20AmountRecipient = {
    tokenAddress: broadcasterFeeAmountDetails.tokenAddress,
    amount: broadcasterFeeAmountDetails.amount,
    recipientAddress: selectedBroadcaster.railgunAddress,
  };

  return broadcasterFeeERC20AmountRecipient;
};

export const getFeeTokenDetails = async (
  network: NetworkName,
  tokenAddress: string,
  sendWithPublicWallet: boolean = true
) => {
  if (sendWithPublicWallet) {
    return {
      broadcaster: undefined,
      feeTokenDetails: undefined,
    };
  }
  const broadcaster = await getBroadcasterFeeInfo(network, tokenAddress);
  const feeTokenDetails: FeeTokenDetails = {
    tokenAddress: broadcaster.tokenAddress,
    feePerUnitGas: BigInt(broadcaster.tokenFee.feePerUnitGas),
  };

  return {
    broadcaster,
    feeTokenDetails,
  };
};

export const getBroadcasterDetails = async (
  estimatedGasDetails: TransactionGasDetails,
  selectedBroadcaster: SelectedBroadcaster | undefined,
  feeTokenDetails: FeeTokenDetails | undefined
) => {
  if (!selectedBroadcaster || !feeTokenDetails) {
    return undefined;
  }
  // gets a broadcaster from the waku client based on tokenAddress

  // const { broadcaster, feeTokenDetails } = await getFeeTokenDetails(
  //   network,
  //   tokenAddress
  // );

  const feeRecipientDetails = await getBroadcasterFeeRecipientDetails(
    selectedBroadcaster,
    estimatedGasDetails,
    feeTokenDetails
  );

  const broadcasterFeeERC20AmountRecipient = {
    tokenAddress: feeRecipientDetails.tokenAddress,
    amount: feeRecipientDetails.amount,
    recipientAddress: feeRecipientDetails.recipientAddress,
  };

  return {
    selectedBroadcaster,
    broadcasterFeeERC20AmountRecipient,
  };
};

```


---

# 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.railgun.org/developer-guide/wallet/getting-started/9.-waku-optional.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.
