Skip to content

Commit

Permalink
common & evm public
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Dec 19, 2023
1 parent 87f66f5 commit 413f12e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 128 deletions.
14 changes: 5 additions & 9 deletions packages/tools/src/event-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import type {
} from '@/types'

export class EventEmitter<T extends EventMap> {
#handlers: EventHandlersMap<T> = {}

get handlers(): EventHandlersMap<T> {
return this.#handlers
}
handlers: EventHandlersMap<T> = {}

public on<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
this.#handlers[key] = (this.#handlers[key] || [])?.concat(fn)
this.handlers[key] = (this.handlers[key] || [])?.concat(fn)
}

public once<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
Expand All @@ -25,16 +21,16 @@ export class EventEmitter<T extends EventMap> {
}

public off<K extends EventMapKey<T>>(key: K, fn: EventHandler<T[K]>): void {
this.#handlers[key] = (this.#handlers[key] || [])?.filter(f => f !== fn)
this.handlers[key] = (this.handlers[key] || [])?.filter(f => f !== fn)
}

public emit<K extends EventMapKey<T>>(key: K, data?: T[K]): void | never {
;(this.#handlers[key] || [])?.forEach((fn: EventHandler<T[K]>) => {
;(this.handlers[key] || [])?.forEach((fn: EventHandler<T[K]>) => {
fn(data)
})
}

public clear(): void {
this.#handlers = {}
this.handlers = {}
}
}
52 changes: 24 additions & 28 deletions packages/w3p/src/provider-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ type ProviderDetectorConfig = {
}

export class ProviderDetector<T extends keyof Record<string, string>> {
#providers: ProviderInstance<T>[]
#rawProviders: RawProvider[]
#isInitiated = false
providersInstances: ProviderInstance<T>[]
rawProviders: RawProvider[]
isInitiated = false

public static errorHandlers?: ErrorHandlersMap
public static cfg: ProviderDetectorConfig

constructor() {
this.#providers = []
this.#rawProviders = []
this.providersInstances = []
this.rawProviders = []

ProviderDetector.setCfg({
isWrapDefaultProviders: true,
Expand All @@ -55,18 +55,14 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
}

public async init(): Promise<ProviderDetector<T>> {
this.#detectRawProviders()
await this.#defineProviders()
this.#isInitiated = true
this.detectRawProviders()
await this.defineProviders()
this.isInitiated = true
return this
}

get isInitiated(): boolean {
return this.#isInitiated
}

public get providers(): Record<PROVIDERS | T, ProviderInstance> {
return this.#providers.reduce((acc, el) => {
return this.providersInstances.reduce((acc, el) => {
const name = el.name.toLowerCase() as PROVIDERS

acc[name] = {
Expand All @@ -78,18 +74,18 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
}

public get isEnabled(): boolean {
return Boolean(this.#providers.length)
return Boolean(this.providersInstances.length)
}

public getProvider(provider: PROVIDERS | T): ProviderInstance | undefined {
return this.providers[provider]
}

public addProvider(provider: ProviderInstance<T>): void {
this.#providers.push(provider)
this.providersInstances.push(provider)
}

#detectRawProviders(): void {
detectRawProviders(): void {
const ethProviders = window?.ethereum
? window?.ethereum?.providers || [window?.ethereum]
: []
Expand All @@ -103,7 +99,7 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
),
)

this.#rawProviders = [
this.rawProviders = [
...(ProviderDetector.cfg?.isWrapDefaultProviders && proxyEthProviders
? proxyEthProviders
: ethProviders),
Expand All @@ -112,35 +108,35 @@ export class ProviderDetector<T extends keyof Record<string, string>> {
] as RawProvider[]
}

async #defineProviders(): Promise<void> {
if (this.#rawProviders.length) {
this.#designateProviders()
async defineProviders(): Promise<void> {
if (this.rawProviders.length) {
this.designateProviders()
} else {
await sleep(3000)
await this.#detectRawProviders()
this.#designateProviders()
this.detectRawProviders()
this.designateProviders()
}
}

#designateProviders(): void {
if (!this.#rawProviders.length) return
designateProviders(): void {
if (!this.rawProviders.length) return

const browserProviders = this.#rawProviders.map(el => {
const browserProviders = this.rawProviders.map(el => {
const appropriatedProviderName: PROVIDERS =
this.#getAppropriateProviderName(el)
this.getAppropriateProviderName(el)

return {
name: appropriatedProviderName,
instance: el,
} as ProviderInstance
})

this.#providers = browserProviders.filter(
this.providersInstances = browserProviders.filter(
(el, idx, arr) => arr.findIndex(sec => sec.name === el.name) === idx,
)
}

#getAppropriateProviderName(provider: RawProvider): PROVIDERS {
getAppropriateProviderName(provider: RawProvider): PROVIDERS {
const providerName = Object.entries(PROVIDER_CHECKS).find(el => {
const [, value] = el

Expand Down
70 changes: 35 additions & 35 deletions packages/w3p/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,40 @@ export type CreateProviderOpts<T extends keyof Record<string, string>> = {
* ```
*/
export class Provider implements IProvider {
readonly #proxyConstructor: ProviderProxyConstructor
#selectedProvider?: PROVIDERS
#proxy?: ProviderProxy
readonly proxyConstructor: ProviderProxyConstructor
selectedProvider?: PROVIDERS
proxy?: ProviderProxy

public static chainsDetails?: Record<ChainId, Chain>

constructor(proxyConstructor: ProviderProxyConstructor) {
this.#selectedProvider = undefined
this.#proxy = undefined
this.#proxyConstructor = proxyConstructor
this.selectedProvider = undefined
this.proxy = undefined
this.proxyConstructor = proxyConstructor
}

public get rawProvider() {
return this.#proxy?.rawProvider
return this.proxy?.rawProvider
}

public get chainType() {
return this.#proxy?.chainType
return this.proxy?.chainType
}

public get providerType() {
return this.#selectedProvider
return this.selectedProvider
}

public get isConnected() {
return Boolean(this.chainId && this.address)
}

public get address() {
return this.#proxy?.address
return this.proxy?.address
}

public get chainId() {
return this.#proxy?.chainId
return this.proxy?.chainId
}

public get chainDetails() {
Expand All @@ -83,40 +83,40 @@ export class Provider implements IProvider {
if (!provider.instance)
throw new errors.ProviderInjectedInstanceNotFoundError()

this.#proxy = new this.#proxyConstructor(provider.instance)
this.proxy = new this.proxyConstructor(provider.instance)

Object.entries(listeners || {}).forEach(([key, value]) => {
this.#proxy?.[key as keyof ProviderListeners]?.(
this.proxy?.[key as keyof ProviderListeners]?.(
value as (e?: ProviderEventPayload) => void,
)
})

this.#selectedProvider = provider.name
await this.#proxy?.init()
this.selectedProvider = provider.name
await this.proxy?.init()
return this
}

public async connect() {
if (!this.#proxy) throw new errors.ProviderNotInitializedError()
if (!this.proxy) throw new errors.ProviderNotInitializedError()

await this.#proxy?.connect?.()
await this.proxy?.connect?.()
}

public async switchChain(chainId: ChainId) {
await this.#proxy?.switchChain?.(chainId)
await this.proxy?.switchChain?.(chainId)
}

public async addChain(chain: Chain) {
await this.#proxy?.addChain?.(chain)
await this.proxy?.addChain?.(chain)
}

public static setChainsDetails(chains: Record<ChainId, Chain>) {
this.chainsDetails = chains
}

public async signAndSendTx(txRequestBody: TxRequestBody) {
if (this.#proxy?.signAndSendTx) {
return this.#proxy?.signAndSendTx?.(
if (this.proxy?.signAndSendTx) {
return this.proxy?.signAndSendTx?.(
txRequestBody,
) as Promise<TransactionResponse>
}
Expand All @@ -125,59 +125,59 @@ export class Provider implements IProvider {
}

public getHashFromTx(txResponse: TransactionResponse) {
return this.#proxy?.getHashFromTx?.(txResponse) ?? ''
return this.proxy?.getHashFromTx?.(txResponse) ?? ''
}

public getTxUrl(chain: Chain, txHash: string) {
return this.#proxy?.getTxUrl?.(chain, txHash) ?? ''
return this.proxy?.getTxUrl?.(chain, txHash) ?? ''
}

public getAddressUrl(chain: Chain, address: string) {
return this.#proxy?.getAddressUrl?.(chain, address) ?? ''
return this.proxy?.getAddressUrl?.(chain, address) ?? ''
}

public async signMessage(message: string) {
return this.#proxy?.signMessage?.(message) ?? ''
return this.proxy?.signMessage?.(message) ?? ''
}

public onAccountChanged(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onAccountChanged(cb)
this.proxy?.onAccountChanged(cb)
}

public onChainChanged(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onChainChanged?.(cb)
this.proxy?.onChainChanged?.(cb)
}

public onConnect(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onConnect(cb)
this.proxy?.onConnect(cb)
}

public onDisconnect(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onDisconnect(cb)
this.proxy?.onDisconnect(cb)
}

public onInitiated(cb: (e?: ProviderEventPayload) => void): void {
this.#proxy?.onInitiated(cb)
this.proxy?.onInitiated(cb)
}

public clearHandlers(): void {
this.#proxy?.clearHandlers()
this.proxy?.clearHandlers()
}

public onBeforeTxSent(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onBeforeTxSent(cb)
this.proxy?.onBeforeTxSent(cb)
}

public onTxSent(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onTxSent(cb)
this.proxy?.onTxSent(cb)
}

public onTxConfirmed(cb: (e?: ProviderEventPayload) => void) {
this.#proxy?.onTxConfirmed(cb)
this.proxy?.onTxConfirmed(cb)
}

public async disconnect() {
await this.#proxy?.disconnect?.()
await this.proxy?.disconnect?.()
}
}

Expand Down
Loading

0 comments on commit 413f12e

Please sign in to comment.