Skip to content

Commit

Permalink
feat(connectors): expose ready as static method
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Mar 14, 2022
1 parent cd788c7 commit c2b6aae
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/connectors/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AccountInterface } from 'starknet'

export abstract class Connector<Options = any> {
/** Unique connector id */
abstract readonly id: string
/** Connector name */
abstract readonly name: string
/** Whether connector is usable */
static readonly ready: boolean
/** Options to use with connector */
readonly options: Options

constructor({ options }: { options: Options }) {
this.options = options
}

abstract connect(): Promise<AccountInterface>
abstract account(): Promise<AccountInterface>
}
21 changes: 1 addition & 20 deletions packages/core/src/connectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
import { AccountInterface } from 'starknet'

export abstract class Connector<Options = any> {
/** Unique connector id */
abstract readonly id: string
/** Connector name */
abstract readonly name: string
/** Whether connector is usable */
abstract readonly ready: boolean
/** Options to use with connector */
readonly options: Options

constructor({ options }: { options: Options }) {
this.options = options
}

abstract connect(): Promise<AccountInterface>
abstract account(): Promise<AccountInterface>
}

export { Connector } from './base'
export { InjectedConnector } from './injected'
6 changes: 3 additions & 3 deletions packages/core/src/connectors/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type InjectedConnectorOptions = {
export class InjectedConnector extends Connector<InjectedConnectorOptions> {
readonly id = 'injected'
readonly name = 'argent'
readonly ready = typeof window != 'undefined' && !!window.starknet
static readonly ready = typeof window != 'undefined' && !!window.starknet

private starknet = getStarknet()

Expand All @@ -23,7 +23,7 @@ export class InjectedConnector extends Connector<InjectedConnectorOptions> {
}

async connect() {
if (!this.ready) {
if (!InjectedConnector.ready) {
throw new ConnectorNotFoundError()
}

Expand All @@ -43,7 +43,7 @@ export class InjectedConnector extends Connector<InjectedConnectorOptions> {
}

account() {
if (!this.ready) {
if (!InjectedConnector.ready) {
throw new ConnectorNotFoundError()
}

Expand Down
24 changes: 11 additions & 13 deletions packages/core/src/providers/starknet/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,17 @@ export function useStarknetManager(): StarknetState {

const { account, library, error } = state

const connect = useCallback(async (connector: Connector) => {
if (connector.ready) {
connector.connect().then(
(account) => {
dispatch({ type: 'set_account', account: account.address })
dispatch({ type: 'set_provider', provider: account })
},
(err) => {
console.error(err)
dispatch({ type: 'set_error', error: 'could not activate StarkNet' })
}
)
}
const connect = useCallback((connector: Connector) => {
connector.connect().then(
(account) => {
dispatch({ type: 'set_account', account: account.address })
dispatch({ type: 'set_provider', provider: account })
},
(err) => {
console.error(err)
dispatch({ type: 'set_error', error: 'could not activate StarkNet' })
}
)
}, [])

return { account, connect, library, error }
Expand Down
5 changes: 2 additions & 3 deletions website/docs/hooks/starknet.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ Hook to access the current instance of the underlying StarkNet library.
```typescript
import { useStarknet } from '@starknet-react/core'

const { account, hasStarknet, connectBrowserWallet, library, error } = useStarknet()
const { account, connect, library, error } = useStarknet()
```

## Return Values

```typescript
{
account?: string
hasStarknet: boolean
connectBrowserWallet: () => void
connect: (Connector) => Promise<void>
library: ProviderInterface
error?: string
}
Expand Down
17 changes: 10 additions & 7 deletions website/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ yarn add @starknet-react/core @argent/get-starknet starknet
```

Or with npm:

```
npm install @starknet-react/core @argent/get-starknet starknet
```
Expand All @@ -36,21 +37,23 @@ function App() {
```

3. Connect the wallet (needs Argent X StartkNet Wallet extension installed)

```typescript
import { useStarknet } from '@starknet-react/core';
import { useStarknet, InjectedConnector } from '@starknet-react/core'

function YourComponent() {
const { connectBrowserWallet } = useStarknet()
const { connect } = useStarknet()

return (
<button onClick={connectBrowserWallet}>
Connect Wallet
</button>
)
if (!InjectedConnector.ready) {
;<span>Injected connector not found</span>
}

return <button onClick={connect(new InjectedConnector())}>Connect Wallet</button>
}
```

4. Retrieve the account address

```typescript
import { useStarknet } from '@starknet-react/core'

Expand Down
7 changes: 4 additions & 3 deletions website/src/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useStarknetTransactionManager,
Transaction,
useStarknet,
InjectedConnector,
} from '@starknet-react/core'

import CounterAbi from '../abi/counter.json'
Expand Down Expand Up @@ -48,16 +49,16 @@ function useCounterContract() {
}

function DemoAccount() {
const { account, connectBrowserWallet, hasStarknet } = useStarknet()
const { account, connect } = useStarknet()
return (
<Section>
<SectionTitle>Account</SectionTitle>
<div>
<p>Connected Account: {account}</p>
</div>
{hasStarknet ? (
{InjectedConnector.ready ? (
<ActionRoot>
<Button onClick={connectBrowserWallet}>Connect Argent-X</Button>
<Button onClick={() => connect(new InjectedConnector())}>Connect Argent-X</Button>
</ActionRoot>
) : (
<div>
Expand Down

0 comments on commit c2b6aae

Please sign in to comment.