-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathclient.ts
54 lines (44 loc) · 1.62 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { type Connector, ConnectorController } from '@reown/appkit-core'
import { ApiController } from './controllers/ApiController.js'
import { WalletButtonController } from './controllers/WalletButtonController.js'
import { ConnectorUtil } from './utils/ConnectorUtil.js'
import { ConstantsUtil } from './utils/ConstantsUtil.js'
import type { SocialProvider, Wallet } from './utils/TypeUtil.js'
import { WalletUtil } from './utils/WalletUtil.js'
export class AppKitWalletButton {
constructor() {
if (!this.isReady()) {
ApiController.fetchWalletButtons()
}
}
public isReady() {
return WalletButtonController.state.ready
}
public subscribeIsReady(callback: ({ isReady }: { isReady: boolean }) => void) {
ApiController.subscribeKey('walletButtons', val => {
if (val.length) {
callback({ isReady: true })
} else {
callback({ isReady: false })
}
})
}
async connect(wallet: Wallet) {
const connectors = ConnectorController.state.connectors
if (ConstantsUtil.Socials.some(social => social === wallet)) {
return ConnectorUtil.connectSocial(wallet as SocialProvider)
}
const walletButton = WalletUtil.getWalletButton(wallet)
const connector = walletButton
? ConnectorController.getConnector(walletButton.id, walletButton.rdns)
: undefined
if (connector) {
return ConnectorUtil.connectExternal(connector)
}
return ConnectorUtil.connectWalletConnect({
walletConnect: wallet === 'walletConnect',
connector: connectors.find(c => c.id === 'walletConnect') as Connector | undefined,
wallet: walletButton
})
}
}