-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f3756c
commit 2e0ebab
Showing
26 changed files
with
1,298 additions
and
76 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
80 changes: 80 additions & 0 deletions
80
src/components/WalletConnectProvider/WalletConnectProvider.tsx
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,80 @@ | ||
import { createContext, useContext, useEffect, useMemo, useState } from "react" | ||
import { | ||
BaseWallet, | ||
WalletAggregator, | ||
WalletConnect, | ||
} from "utils/walletConnect" | ||
|
||
const walletConnectParams = { | ||
projectId: import.meta.env.VITE_WC_PROJECT_ID, | ||
relayUrl: "wss://relay.walletconnect.com", | ||
metadata: { | ||
name: "Basilisk", | ||
description: "Basilisk", | ||
url: import.meta.env.VITE_DOMAIN_URL, | ||
icons: ["https://walletconnect.com/walletconnect-logo.png"], | ||
}, | ||
} | ||
|
||
const walletAggregator = new WalletAggregator( | ||
new WalletConnect(walletConnectParams, "HydraDX"), | ||
) | ||
|
||
type OnboardProviderProps = { children: JSX.Element } | ||
|
||
interface PolkadotWalletsContextProviderProps { | ||
children: JSX.Element | ||
walletAggregator: WalletAggregator | ||
initialWaitMs?: number | ||
} | ||
|
||
interface PolkadotWalletsContextProps { | ||
wallet: BaseWallet | undefined | ||
} | ||
|
||
const PolkadotWalletsContext = createContext<PolkadotWalletsContextProps>({ | ||
wallet: undefined, | ||
}) | ||
|
||
export const useWalletConnect = () => useContext(PolkadotWalletsContext) | ||
|
||
const PolkadotWalletsContextProvider = ({ | ||
children, | ||
walletAggregator, | ||
initialWaitMs = 5 /* the default is set to 5ms to give extensions enough lead time to inject their providers */, | ||
}: PolkadotWalletsContextProviderProps) => { | ||
const [wallet, setWallet] = useState<BaseWallet | undefined>() | ||
|
||
useEffect(() => { | ||
const timeoutId = setTimeout(async () => { | ||
const wallet = walletAggregator.getWallet() | ||
const isWalletConnected = walletAggregator.getWallet().isConnected() | ||
|
||
if (!isWalletConnected) wallet.autoConnect() | ||
|
||
setWallet(wallet) | ||
}, initialWaitMs) | ||
return () => clearTimeout(timeoutId) | ||
}, [walletAggregator, initialWaitMs]) | ||
|
||
const contextData = useMemo( | ||
() => ({ | ||
wallet, | ||
}), | ||
[wallet], | ||
) | ||
|
||
return ( | ||
<PolkadotWalletsContext.Provider value={contextData}> | ||
{children} | ||
</PolkadotWalletsContext.Provider> | ||
) | ||
} | ||
|
||
export const WalletConnectProvider = ({ children }: OnboardProviderProps) => { | ||
return ( | ||
<PolkadotWalletsContextProvider walletAggregator={walletAggregator}> | ||
{children} | ||
</PolkadotWalletsContextProvider> | ||
) | ||
} |
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
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
Oops, something went wrong.