Skip to content

Instantly share code, notes, and snippets.

@arilotter
Created August 18, 2022 18:35
Show Gist options
  • Save arilotter/d14e4539b69572f0fe2c1494a30a508a to your computer and use it in GitHub Desktop.
Save arilotter/d14e4539b69572f0fe2c1494a30a508a to your computer and use it in GitHub Desktop.
sequence connector (not fully implemented)
import { sequence } from '0xsequence'
import { mainnetNetworks, testnetNetworks } from '@0xsequence/network'
import type { ConnectOptions, ProviderConfig, Web3Provider } from '@0xsequence/provider'
import { Wallet } from '@0xsequence/provider'
import { Chain } from '@rainbow-me/rainbowkit'
import { Connector, ConnectorData, ConnectorNotFoundError, UserRejectedRequestError } from 'wagmi'
interface Options {
provider?: Partial<ProviderConfig>
connect?: ConnectOptions
}
sequence.initWallet('polygon')
export class SequenceConnector extends Connector<Web3Provider, Options | undefined> {
id = 'sequence'
name = 'Sequence'
// chains = chainConfigList
ready = true
#provider: Web3Provider | null = null
#wallet: Wallet
#connected = false
constructor({ chains, options }: { chains?: Chain[]; options?: Options }) {
super({ chains, options })
this.#wallet = sequence.getWallet()
}
async connect(): Promise<Required<ConnectorData<Web3Provider>>> {
if (!this.#wallet.isConnected()) {
const e = await this.#wallet.connect(this.options?.connect)
if (e.error) {
throw new UserRejectedRequestError(e.error)
}
}
console.log('connect')
const chainId = await this.getChainId()
const provider = await this.getProvider()
const account = await this.getAccount()
// provider.on("accountsChanged", this.onAccountsChanged);
// provider.on("chainChanged", this.onChainChanged);
provider.on('disconnect', this.onDisconnect)
this.#connected = true
return {
account,
chain: {
id: chainId,
unsupported: this.isChainUnsupported(chainId)
},
provider
}
}
async disconnect() {
this.#wallet.disconnect()
}
getAccount() {
return this.#wallet.getAddress()
}
getChainId() {
// in mobile, when connecting with sequence Rainbowkit first tried to get ChainID for some reason, but in sequence you can't get ChainID before being connected, so forcing here to connect if you want to get ChainID
if (!this.#wallet.isConnected()) {
return this.connect().then(() => this.#wallet.getChainId())
}
return this.#wallet.getChainId()
}
async getProvider() {
if (!this.#provider) {
const provider = this.#wallet.getProvider()
if (!provider) {
throw new ConnectorNotFoundError('Failed to get Sequence Wallet provider.')
}
this.#provider = provider
}
return this.#provider
}
async getSigner() {
return this.#wallet.getSigner()
}
async isAuthorized() {
try {
const account = await this.getAccount()
return !!account
} catch {
return false
}
}
protected onAccountsChanged = (accounts: string[]) => {}
protected onChainChanged = (chain: number | string) => {}
protected onDisconnect = () => {
this.emit('disconnect')
}
isChainUnsupported(chainId: number): boolean {
return !(mainnetNetworks.some(c => c.chainId === chainId) || testnetNetworks.some(c => c.chainId === chainId))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment