Skip to content

Commit

Permalink
chore(rpc): hardcoded gnosis rpc for indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbeckers committed Aug 30, 2023
1 parent 9ab7972 commit 98561ef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useIndexer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const useIndexer = () => {
// check if the indexer has not been initialized
const indexer = new CookieJarIndexer(
storageEntities,
publicClient,
"https://rpc.ankr.com/gnosis",
handleEvent
);
await indexer.init();
Expand Down
15 changes: 7 additions & 8 deletions src/utils/CookieJarIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { createIndexer, IdbStorage } from "chainsauce-web";
import type { EventHandler, Indexer } from "chainsauce-web";
import { Abi, PublicClient } from "viem";
import { Contract, providers, utils } from "ethers";
import { useEthersProvider } from "./ethersAdapters";
import { Contract, utils, providers } from "ethers";
import { JsonFragment } from "@daohaus/utils";

interface CookieJarIndexerInterface {
storage: IdbStorage;
publicClient: PublicClient;
rpc: string;
eventHandler: EventHandler<IdbStorage>;
indexer?: Indexer<IdbStorage>;
init: () => Promise<void>;
Expand All @@ -21,24 +20,24 @@ interface CookieJarIndexerInterface {

class CookieJarIndexer implements CookieJarIndexerInterface {
storage: IdbStorage;
publicClient: PublicClient;
rpc: string;
eventHandler: EventHandler<IdbStorage>;
indexer?: Indexer<IdbStorage>;

constructor(
storageEntities: string[],
publicClient: PublicClient,
rpc: string,
eventHandler: EventHandler<IdbStorage>
) {
this.storage = new IdbStorage(storageEntities);
this.publicClient = publicClient;
this.rpc = rpc;
this.eventHandler = eventHandler;
}

init = async () => {
const provider = useEthersProvider({ chainId: 100 });
const provider = new providers.JsonRpcProvider(this.rpc, 100);
this.indexer = await createIndexer(
provider as providers.JsonRpcProvider,
provider,
this.storage,
this.eventHandler
);
Expand Down
21 changes: 5 additions & 16 deletions src/utils/ethersAdapters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as React from "react";
import { type PublicClient, usePublicClient } from "wagmi";
import { type PublicClient, getPublicClient } from "@wagmi/core";
import { providers } from "ethers";
import { type HttpTransport } from "viem";

export function publicClientToProvider(publicClient: PublicClient) {
const { chain, transport } = publicClient;
Expand All @@ -10,20 +8,11 @@ export function publicClientToProvider(publicClient: PublicClient) {
name: chain.name,
ensAddress: chain.contracts?.ensRegistry?.address,
};
if (transport.type === "fallback")
return new providers.FallbackProvider(
(transport.transports as ReturnType<HttpTransport>[]).map(
({ value }) => new providers.JsonRpcProvider(value?.url, network)
)
);
return new providers.JsonRpcProvider(transport.url, network);
}

/** Hook to convert a viem Public Client to an ethers.js Provider. */
export function useEthersProvider({ chainId }: { chainId?: number } = {}) {
const publicClient = usePublicClient({ chainId });
return React.useMemo(
() => publicClientToProvider(publicClient),
[publicClient]
);
/** Action to convert a viem Public Client to an ethers.js Provider. */
export function getEthersProvider({ chainId }: { chainId?: number } = {}) {
const publicClient = getPublicClient({ chainId });
return publicClientToProvider(publicClient);
}

0 comments on commit 98561ef

Please sign in to comment.