Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Explicitly add the chain Id to the JSONRPCPROVIDER #62

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/use-contractkit/src/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { useContractKit } from './use-contractkit';
import { useIsMounted } from './utils/useIsMounted';

export const useProvider = (): Web3Provider => {
const { kit } = useContractKit();
const { kit, network } = useContractKit();
const provider = kit.web3.currentProvider as unknown as ExternalProvider;
const { chainId, name } = network;
return useMemo(() => {
return new Web3Provider(provider);
}, [provider]);
return new Web3Provider(provider, { chainId, name });
}, [provider, chainId, name]);
};

export const useProviderOrSigner = (): Web3Provider | JsonRpcSigner => {
Expand All @@ -24,17 +25,22 @@ export const useProviderOrSigner = (): Web3Provider | JsonRpcSigner => {
};

export const useGetConnectedSigner = (): (() => Promise<JsonRpcSigner>) => {
const { kit, getConnectedKit } = useContractKit();
const { kit, getConnectedKit, network } = useContractKit();
const signer = useProviderOrSigner();
const { chainId, name } = network;

return useCallback(async () => {
if (kit.defaultAccount) {
return signer as JsonRpcSigner;
}

const nextKit = await getConnectedKit();
const nextProvider = nextKit.web3
.currentProvider as unknown as ExternalProvider;
return new Web3Provider(nextProvider).getSigner(nextKit.defaultAccount);
}, [kit.defaultAccount, getConnectedKit, signer]);
return new Web3Provider(nextProvider, { chainId, name }).getSigner(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyone know why this needs to be a new provider? why cant we reuse provider from userProvider?

@dckesler @jmrossy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexBHarley would probably be the best person to ask. I haven't reviewed this part thoroughly but it's entirely possible it was an oversight. No disrespect to Alex who overall did a great job but some parts were written hastily I think!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it a bit more, do you mean reuse the kit's provider? That's a Web3 provider, this creates an ethers.js provider

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Web3Provider(nextProvider, { chainId, name }) looks to give us same kind of thing as useProvider no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite understanding you I think. This is inside the useProvider hook.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is modifying 2 places. the useProvider and inside useGetConnectedSigner this line is inside the later

nextKit.defaultAccount
);
}, [kit.defaultAccount, getConnectedKit, signer, chainId, name]);
};

export const useLazyConnectedSigner = (): {
Expand Down