How to Initialise the Epoch SDK

Simple Account API

import { useEffect, useState } from "react";
import { HttpRpcClient, SimpleAccountAPI } from "@epoch-protocol/sdk";
import { useEthersProvider, useEthersSigner } from "~~/utils/scaffold-eth/common";

export const useBundler = () => {
  const ENTRY_POINT = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
  const FACTORY_ADDRESS = "0x4A4fC0bF39D191b5fcA7d4868C9F742B342a39c1";
  const bundlerUrl: string = process.env.BUNDLER_URL ?? "http://localhost:14337/80001";

  const [walletAPI, setWalletAPI] = useState<SimpleAccountAPI | null>(null);
  const [bundler, setBundler] = useState<HttpRpcClient | null>(null);

  const provider = useEthersProvider(); // Ethers Provider
  const signer = useEthersSigner(); // Ethers Signer

  useEffect(() => {
    (async () => {
      if (signer && provider) {
        const network = await provider.getNetwork();
x
        const walletAPIInstance = new SimpleAccountAPI({
          provider,
          entryPointAddress: ENTRY_POINT,
          owner: signer,
          factoryAddress: FACTORY_ADDRESS,
        });
        setWalletAPI(walletAPIInstance);

        const bundlerInstance = new HttpRpcClient(bundlerUrl, ENTRY_POINT, parseInt(network.chainId.toString()));
        setBundler(bundlerInstance);
      }
    })();
  }, [signer, provider, bundlerUrl]);

  return { walletAPI, bundler };
};

SAFE Account API

import { useEffect, useState } from "react";
import { HttpRpcClient, SafeAccountAPI } from "@epoch-protocol/sdk";
import { safeDefaultConfig } from "@epoch-protocol/sdk/dist/src/SafeDefaultConfig";
import { useEthersProvider, useEthersSigner } from "~~/utils/scaffold-eth/common";

export const useBundler = () => {
  const ENTRY_POINT = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789";
  const bundlerUrl: string = process.env.BUNDLER_URL ?? "http://localhost:14337/80001";

  const [walletAPI, setWalletAPI] = useState<SafeAccountAPI | null>(null);
  const [bundler, setBundler] = useState<HttpRpcClient | null>(null);

  const provider = useEthersProvider();
  const signer = useEthersSigner();

  useEffect(() => {
    (async () => {
      if (signer && provider) {
        const network = await provider.getNetwork();

        const walletAPIInstance = new SafeAccountAPI({
          provider,
          entryPointAddress: ENTRY_POINT,
          owner: signer,
          safeConfig: safeDefaultConfig[network.chainId],
          salt: safeDefaultConfig[network.chainId].salt,
        });
        setWalletAPI(walletAPIInstance);

        const bundlerInstance = new HttpRpcClient(bundlerUrl, ENTRY_POINT, parseInt(network.chainId.toString()));
        setBundler(bundlerInstance);
      }
    })();
  }, [signer, provider, bundlerUrl]);

  return { walletAPI, bundler };
};

Note - The Address in the above examples are for Polygon Mumbai

Last updated