Skip to content

Commit

Permalink
v1.9.3: updated walletConnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
linkielink committed Jul 18, 2023
1 parent 2284172 commit 99ae658
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 14 deletions.
10 changes: 10 additions & 0 deletions dist/components/WalletConnectHandler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { WalletConnection } from "@delphi-labs/shuttle-react";
import { ChainInfoID } from "src/enums";
interface Props {
setConnected: () => void;
setConnectedWallet: (recentWallet: WalletConnection) => void;
chainId: ChainInfoID;
}
declare const WalletConnectHandler: ({ setConnected, setConnectedWallet, chainId, }: Props) => null;
export default WalletConnectHandler;
//# sourceMappingURL=WalletConnectHandler.d.ts.map
1 change: 1 addition & 0 deletions dist/components/WalletConnectHandler.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/components/WalletManagerProvider.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions example/components/ConnectButton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {
ChainInfoID,
WalletConnectionStatus,
useWalletManager,
} from "@marsprotocol/wallet-connector"
import { useEffect, useState } from "react"

interface Props {
chainId: ChainInfoID
}

export default function ConnectButton(props: Props) {
export default function ConnectButton() {
const { connect, status, connectedWallet, disconnect } = useWalletManager()
const [isConnected, setIsConnected] = useState(false)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@marsprotocol/wallet-connector",
"version": "1.9.2",
"version": "1.9.3",
"repository": {
"type": "git",
"url": "git+https://github.com/mars-protocol/wallet-connector.git"
Expand Down
47 changes: 47 additions & 0 deletions src/components/WalletConnectHandler.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useShuttle, WalletConnection } from "@delphi-labs/shuttle-react"
import { useEffect } from "react"
import { ChainInfoID } from "src/enums"

interface Props {
setConnected: () => void
setConnectedWallet: (recentWallet: WalletConnection) => void
chainId: ChainInfoID
}

const WalletConnectHandler = ({
setConnected,
setConnectedWallet,
chainId,
}: Props) => {
const { connect, extensionProviders, wallets } = useShuttle()

useEffect(() => {
if (wallets === null) return
const recentWallet = wallets.find((w) => w.network.chainId === chainId)

if (!recentWallet) return
if (!recentWallet.providerId.includes("mobile")) return
const recentProvider = extensionProviders.find(
(p) => p.id === recentWallet.providerId,
)

if (!recentProvider) return
recentProvider
.connect({ chainId: recentWallet.network.chainId })
.then(() => {
setConnected()
setConnectedWallet(recentWallet)
})
}, [
connect,
wallets,
extensionProviders,
setConnected,
setConnectedWallet,
chainId,
])

return null
}

export default WalletConnectHandler
12 changes: 8 additions & 4 deletions src/components/WalletManagerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import DisconnectHandler from "./DisconnectHandler"
import { SelectWalletModal } from "./ui"
import { EnablingWalletModal } from "./ui/EnablingWalletModal"
import { StationWalletErrorModal } from "./ui/StationWalletErrorModal"
import WalletConnectHandler from "./WalletConnectHandler"
import { WalletManagerContext } from "./WalletManagerContext"

export const WalletManagerProvider: FunctionComponent<
Expand Down Expand Up @@ -123,10 +124,6 @@ export const WalletManagerProvider: FunctionComponent<
)

const closePickerModal = () => {
if (status === WalletConnectionStatus.WalletConnect) {
setStatus(WalletConnectionStatus.Unconnected)
}

setPickerModalOpen(false)
}

Expand Down Expand Up @@ -206,6 +203,13 @@ export const WalletManagerProvider: FunctionComponent<
setConnectedWallet={setConnectedWallet}
/>
)}
{status === WalletConnectionStatus.WalletConnect && (
<WalletConnectHandler
chainId={defaultChainId}
setConnected={() => setStatus(WalletConnectionStatus.Connected)}
setConnectedWallet={setConnectedWallet}
/>
)}
{status === WalletConnectionStatus.Disconnecting && (
<DisconnectHandler
chainId={defaultChainId}
Expand Down

0 comments on commit 99ae658

Please sign in to comment.