Skip to content

Commit

Permalink
fix: init wc transport only 1 time
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaccoSordo committed Oct 8, 2024
1 parent a7a34d3 commit 31efb9c
Showing 1 changed file with 44 additions and 39 deletions.
83 changes: 44 additions & 39 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class DAppClient extends Client {

protected postMessageTransport: DappPostMessageTransport | undefined
protected p2pTransport: DappP2PTransport | undefined
protected walletConnectTransport: DappWalletConnectTransport | undefined
protected walletConnectTransport: DappWalletConnectTransport = {} as DappWalletConnectTransport

protected wcProjectId?: string
protected wcRelayUrl?: string
Expand Down Expand Up @@ -452,6 +452,40 @@ export class DAppClient extends Client {
)

this.initUserID().catch((err) => logger.error(err.message))

this.multiTabChannel.isLeader()
.then(async (isLeader) => {
if (!isLeader) {
return
}

const wcOptions = {
projectId: this.wcProjectId,
relayUrl: this.wcRelayUrl,
metadata: {
name: this.name,
description: this.description ?? '',
url: this.appUrl ?? '',
icons: this.iconUrl ? [this.iconUrl] : []
}
}

this.walletConnectTransport = new DappWalletConnectTransport(
this.name,
await this.keyPair,
this.storage,
{
network: this.network.type,
opts: wcOptions
},
() => this.multiTabChannel.isLeader()
)

this.initEvents()

await this.addListener(this.walletConnectTransport)

})
}

private async onElectedLeaderhandler() {
Expand Down Expand Up @@ -544,7 +578,7 @@ export class DAppClient extends Client {
public async initInternalTransports(): Promise<void> {
const keyPair = await this.keyPair

if (this.postMessageTransport || this.p2pTransport || this.walletConnectTransport) {
if (this.postMessageTransport || this.p2pTransport || (this.walletConnectTransport && this.walletConnectTransport.connectionStatus === TransportStatus.CONNECTED)) {
return
}

Expand All @@ -561,39 +595,9 @@ export class DAppClient extends Client {
)

await this.addListener(this.p2pTransport)

const wcOptions = {
projectId: this.wcProjectId,
relayUrl: this.wcRelayUrl,
metadata: {
name: this.name,
description: this.description ?? '',
url: this.appUrl ?? '',
icons: this.iconUrl ? [this.iconUrl] : []
}
}

this.walletConnectTransport = new DappWalletConnectTransport(
this.name,
keyPair,
this.storage,
{
network: this.network.type,
opts: wcOptions
},
() => this.multiTabChannel.isLeader()
)

this.initEvents()

await this.addListener(this.walletConnectTransport)
}

private initEvents() {
if (!this.walletConnectTransport) {
return
}

this.walletConnectTransport.setEventHandler(ClientEvents.CLOSE_ALERT, () => {
this.hideUI(['alert', 'toast'])
})
Expand Down Expand Up @@ -690,7 +694,7 @@ export class DAppClient extends Client {

await this.initInternalTransports()

if (!this.postMessageTransport || !this.p2pTransport || !this.walletConnectTransport) {
if (!this.postMessageTransport || !this.p2pTransport) {
return
}

Expand Down Expand Up @@ -778,7 +782,11 @@ export class DAppClient extends Client {
return p2pTransport.getPairingRequestInfo()
},
postmessagePeerInfo: () => postMessageTransport.getPairingRequestInfo(),
walletConnectPeerInfo: () => walletConnectTransport.getPairingRequestInfo(),
walletConnectPeerInfo: async () => {
const res = await walletConnectTransport.getPairingRequestInfo()
console.log('RES:', res)
return res
},
networkType: this.network.type,
abortedHandler: async () => {
logger.log('init', 'ABORTED')
Expand All @@ -792,7 +800,6 @@ export class DAppClient extends Client {
walletConnectTransport.disconnect()
])
this.postMessageTransport =
this.walletConnectTransport =
this.p2pTransport =
undefined
this._activeAccount.isResolved() && this.clearActiveAccount()
Expand Down Expand Up @@ -834,7 +841,7 @@ export class DAppClient extends Client {
this.postMessageTransport?.disconnect(),
this.walletConnectTransport?.disconnect()
])
this.postMessageTransport = this.p2pTransport = this.walletConnectTransport = undefined
this.postMessageTransport = this.p2pTransport = undefined
await this.setActivePeer(undefined)
await this.setTransport(undefined)
this._initPromise = undefined
Expand Down Expand Up @@ -874,7 +881,7 @@ export class DAppClient extends Client {
if (!this.debounceSetActiveAccount && transport instanceof WalletConnectTransport) {
this.debounceSetActiveAccount = true
this._initPromise = undefined
this.postMessageTransport = this.p2pTransport = this.walletConnectTransport = undefined
this.postMessageTransport = this.p2pTransport = undefined
if (await this.multiTabChannel.isLeader()) {
await transport.disconnect()
this.openRequestsOtherTabs.clear()
Expand Down Expand Up @@ -2010,7 +2017,6 @@ export class DAppClient extends Client {
this._initPromise = undefined
this.postMessageTransport = undefined
this.p2pTransport = undefined
this.walletConnectTransport = undefined
await this.setTransport()
await this.setActivePeer()
}
Expand Down Expand Up @@ -2570,7 +2576,6 @@ export class DAppClient extends Client {
}
this.postMessageTransport = undefined
this.p2pTransport = undefined
this.walletConnectTransport = undefined
this.sendMetrics('performance-metrics/save', await this.buildPayload('disconnect', 'success'))
}

Expand Down

0 comments on commit 31efb9c

Please sign in to comment.