-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from lidofinance/feature/si-1300-update-reef-…
…knot-to-v2-on-eth-stake-widget-wagmi-autoconnect Update reef-knot to v2
- Loading branch information
Showing
6 changed files
with
231 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { ProviderSDK } from '@lido-sdk/react'; | ||
import { getStaticRpcBatchProvider } from '@lido-sdk/providers'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { Chain, useAccount } from 'wagmi'; | ||
import { mainnet } from 'wagmi/chains'; | ||
import { useWeb3 } from 'reef-knot/web3-react'; | ||
|
||
const POLLING_INTERVAL = 12_000; | ||
|
||
export const SDKLegacyProvider = (props: { | ||
children?: React.ReactNode; | ||
defaultChainId: number; | ||
supportedChains: Chain[]; | ||
rpc: Record<number, string>; | ||
pollingInterval?: number; | ||
}) => { | ||
const { | ||
children, | ||
defaultChainId, | ||
rpc, | ||
supportedChains, | ||
pollingInterval = POLLING_INTERVAL, | ||
} = props; | ||
const { chainId = defaultChainId, account } = useWeb3(); | ||
const { connector, isConnected } = useAccount(); | ||
|
||
const [providerWeb3, setProviderWeb3] = useState<Web3Provider>(); | ||
|
||
// Reset web3 provider if the provider was set previously, | ||
// and currently no wallet is connected. | ||
// Gets triggered on a wallet disconnection, for example. | ||
if (!isConnected && providerWeb3) { | ||
setProviderWeb3(undefined); | ||
} | ||
|
||
useEffect(() => { | ||
void (async () => { | ||
if (!providerWeb3 && connector && isConnected) { | ||
const provider = await connector.getProvider(); | ||
const wrappedProvider = new Web3Provider(provider); | ||
wrappedProvider.pollingInterval = pollingInterval; | ||
setProviderWeb3(wrappedProvider); | ||
} | ||
})(); | ||
}, [connector, isConnected, pollingInterval, providerWeb3]); | ||
|
||
const supportedChainIds = supportedChains.map((chain) => chain.id); | ||
|
||
const providerRpc = getStaticRpcBatchProvider( | ||
chainId, | ||
rpc[chainId], | ||
0, | ||
pollingInterval, | ||
); | ||
|
||
const providerMainnetRpc = getStaticRpcBatchProvider( | ||
mainnet.id, | ||
rpc[mainnet.id], | ||
0, | ||
pollingInterval, | ||
); | ||
|
||
return ( | ||
// @ts-expect-error Property children does not exist on type | ||
<ProviderSDK | ||
chainId={chainId} | ||
supportedChainIds={supportedChainIds} | ||
providerWeb3={providerWeb3} | ||
providerRpc={providerRpc} | ||
providerMainnetRpc={providerMainnetRpc} | ||
account={account ?? undefined} | ||
> | ||
{children} | ||
</ProviderSDK> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.