Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disconnect #84

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useStarknet, InjectedConnector } from '@starknet-react/core'

export function ConnectWallet() {
const { account, connect } = useStarknet()
const { account, connect, disconnect } = useStarknet()

if (account) {
return <p>Account: {account}</p>
return (
<div>
<p>Account: {account}</p>
<button onClick={() => disconnect(new InjectedConnector())}>Disconnect</button>
</div>
)
}

return <button onClick={() => connect(new InjectedConnector())}>Connect</button>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/connectors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export abstract class Connector<Options = any> {
/** Whether connector is already authorized */
abstract ready(): Promise<boolean>
abstract connect(): Promise<AccountInterface>
abstract disconnect(): Promise<void>
abstract account(): Promise<AccountInterface>
}
15 changes: 15 additions & 0 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Connector } from './index'
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
UserNotConnectedError,
UserRejectedRequestError,
} from '../errors'

Expand Down Expand Up @@ -54,6 +55,20 @@ export class InjectedConnector extends Connector<InjectedConnectorOptions> {
return starknet.account
}

async disconnect(): Promise<void> {
if (!this.available()) {
throw new ConnectorNotFoundError()
}

const starknet = getStarknet()

if (!starknet.isConnected) {
throw new UserNotConnectedError()
}

starknet.off('accountsChanged', () => {})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@janek26 is this the expected way to disconnect a connected Argent account?

Copy link

Choose a reason for hiding this comment

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

this is entirely up to the dapp. Connection status is not stored in Argent X, but just in the wallet.
In get-starknet we handle it like this: https://github.com/starknet-community-libs/get-starknet/blob/master/src/index.ts#L92

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for sharing! I'll defer to maintainers then

}

async account() {
if (!this.available()) {
throw new ConnectorNotFoundError()
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export class UserRejectedRequestError extends Error {
name = 'UserRejectedRequestError'
message = 'User rejected request'
}

export class UserNotConnectedError extends Error {
name = 'UserNotConnectedError'
message = 'User not connected'
}
15 changes: 14 additions & 1 deletion packages/core/src/providers/starknet/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ export function useStarknetManager({
)
}, [])

const disconnect = useCallback((connector: Connector) => {
connector.disconnect().then(
() => {
dispatch({ type: 'set_account', account: undefined })
dispatch({ type: 'set_provider', provider: undefined })
},
(err) => {
console.error(err)
dispatch({ type: 'set_error', error: new ConnectorNotFoundError() })
}
)
}, [])

useEffect(() => {
async function tryAutoConnect(connectors: Connector[]) {
// Autoconnect priority is defined by the order of the connectors.
Expand Down Expand Up @@ -107,5 +120,5 @@ export function useStarknetManager({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return { account, connect, connectors, library, error }
return { account, connect, disconnect, connectors, library, error }
}
2 changes: 2 additions & 0 deletions packages/core/src/providers/starknet/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Connector } from '../../connectors'
export interface StarknetState {
account?: string
connect: (connector: Connector) => void
disconnect: (connector: Connector) => void
library: ProviderInterface
connectors: Connector[]
error?: Error
Expand All @@ -12,6 +13,7 @@ export interface StarknetState {
export const STARKNET_INITIAL_STATE: StarknetState = {
account: undefined,
connect: () => undefined,
disconnect: () => undefined,
library: defaultProvider,
connectors: [],
}