mango-ui-v3/utils/wallet-adapters/bitpie.ts

117 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-11-23 04:25:31 -08:00
import EventEmitter from 'eventemitter3'
import { PublicKey, Transaction } from '@solana/web3.js'
import { notify } from '../../utils/notifications'
import { WalletAdapter } from '../../@types/types'
interface BitpieWallet {
2021-11-23 11:10:58 -08:00
getAccount(): Promise<string>
signTransaction(transaction: Transaction): Promise<Transaction>
signAllTransactions(transactions: Transaction[]): Promise<Transaction[]>
2021-11-23 04:25:31 -08:00
}
interface BitpieWalletWindow extends Window {
2021-11-23 11:10:58 -08:00
bitpie?: BitpieWallet
2021-11-23 04:25:31 -08:00
}
2021-11-23 11:10:58 -08:00
declare const window: BitpieWalletWindow
2021-11-23 04:25:31 -08:00
export class BitpieWalletAdapter extends EventEmitter implements WalletAdapter {
2021-11-23 11:10:58 -08:00
private _connecting: boolean
private _wallet: BitpieWallet | null
private _publicKey: PublicKey | null
2021-11-23 04:25:31 -08:00
constructor() {
2021-11-23 11:10:58 -08:00
super()
this._connecting = false
this._wallet = null
this._publicKey = null
2021-11-23 04:25:31 -08:00
}
get publicKey(): PublicKey | null {
2021-11-23 11:10:58 -08:00
return this._publicKey
2021-11-23 04:25:31 -08:00
}
get ready(): boolean {
2021-11-23 11:10:58 -08:00
return typeof window !== 'undefined' && !!window.bitpie
2021-11-23 04:25:31 -08:00
}
get connecting(): boolean {
2021-11-23 11:10:58 -08:00
return this._connecting
2021-11-23 04:25:31 -08:00
}
get connected(): boolean {
2021-11-23 11:10:58 -08:00
return !!this._wallet
2021-11-23 04:25:31 -08:00
}
get autoApprove() {
return true
}
async connect(): Promise<void> {
try {
2021-11-23 11:10:58 -08:00
if (this.connected || this.connecting) return
this._connecting = true
2021-11-23 04:25:31 -08:00
2021-11-23 11:10:58 -08:00
const wallet = typeof window !== 'undefined' && window.bitpie
if (!wallet) return
2021-11-23 04:25:31 -08:00
2021-11-23 11:10:58 -08:00
let account: string
2021-11-23 04:25:31 -08:00
try {
2021-11-23 11:10:58 -08:00
account = await wallet.getAccount()
2021-11-23 04:25:31 -08:00
} catch (error: any) {
notify({
title: 'Connection Error',
type: 'error',
2021-11-23 11:10:58 -08:00
description:
'Please install Bitpie wallet and then reload this page.',
2021-11-23 04:25:31 -08:00
})
}
2021-11-23 11:10:58 -08:00
this._wallet = wallet
this._publicKey = new PublicKey(account)
2021-11-23 04:25:31 -08:00
2021-11-23 11:10:58 -08:00
this.emit('connect')
2021-11-23 04:25:31 -08:00
} catch (error: any) {
2021-11-23 11:10:58 -08:00
this.emit('error', error)
throw error
2021-11-23 04:25:31 -08:00
} finally {
2021-11-23 11:10:58 -08:00
this._connecting = false
2021-11-23 04:25:31 -08:00
}
}
async disconnect(): Promise<void> {
if (this._wallet) {
2021-11-23 11:10:58 -08:00
this._wallet = null
this._publicKey = null
2021-11-23 04:25:31 -08:00
}
2021-11-23 11:10:58 -08:00
this.emit('disconnect')
2021-11-23 04:25:31 -08:00
}
async signTransaction(transaction: Transaction): Promise<Transaction> {
try {
2021-11-23 11:10:58 -08:00
const wallet = this._wallet
if (!wallet) return
2021-11-23 04:25:31 -08:00
2021-11-23 11:10:58 -08:00
return (await wallet.signTransaction(transaction)) || transaction
2021-11-23 04:25:31 -08:00
} catch (error: any) {
2021-11-23 11:10:58 -08:00
this.emit('error', error)
throw error
2021-11-23 04:25:31 -08:00
}
}
2021-11-23 11:10:58 -08:00
async signAllTransactions(
transactions: Transaction[]
): Promise<Transaction[]> {
2021-11-23 04:25:31 -08:00
try {
2021-11-23 11:10:58 -08:00
const wallet = this._wallet
if (!wallet) return
2021-11-23 04:25:31 -08:00
2021-11-23 11:10:58 -08:00
return (await wallet.signAllTransactions(transactions)) || transactions
2021-11-23 04:25:31 -08:00
} catch (error: any) {
2021-11-23 11:10:58 -08:00
this.emit('error', error)
throw error
2021-11-23 04:25:31 -08:00
}
}
2021-11-23 11:10:58 -08:00
}