-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: walletconnect * feat: upgrade to wallet connect v2 * fix: formatting * fix: dependencies * chore: remove unused dependencies * chore: push lock * refactor: rename file * feat: added walletautomations for walletconnect * fix: linting * chore: sort imports Co-authored-by: Eugene Chybisov <imchybisov@gmail.com>
- Loading branch information
Showing
7 changed files
with
1,253 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 84 additions & 11 deletions
95
packages/wallet-management/src/connectors/walletConnect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,90 @@ | ||
import { supportedChains } from '@lifi/sdk'; | ||
import WalletConnectProvider from '@walletconnect/ethereum-provider'; | ||
import { initializeConnector } from '@web3-react/core'; | ||
import { WalletConnect } from '@web3-react/walletconnect'; | ||
import type { Actions } from '@web3-react/types'; | ||
import { Connector } from '@web3-react/types'; | ||
import type { EventEmitter } from 'node:events'; | ||
|
||
export const [walletConnect, hooks] = initializeConnector<WalletConnect>( | ||
interface WalletConnectOptions { | ||
rpc: { | ||
[chainId: number]: string; | ||
}; | ||
} | ||
|
||
interface MockWalletConnectProvider | ||
extends Omit<WalletConnectProvider, 'on' | 'off' | 'once' | 'removeListener'>, | ||
EventEmitter {} | ||
|
||
class WalletConnectV2 extends Connector { | ||
private readonly options?: WalletConnectOptions; | ||
|
||
public provider: MockWalletConnectProvider; | ||
|
||
public walletConnectProvider: WalletConnectProvider; | ||
|
||
public isCurrentlyUsed: boolean = false; | ||
|
||
constructor(actions: Actions, options?: WalletConnectOptions) { | ||
super(actions); | ||
this.options = options; | ||
|
||
const walletConnectProvider = new WalletConnectProvider({ | ||
rpc: this.options!.rpc, | ||
}); | ||
this.provider = | ||
walletConnectProvider as unknown as MockWalletConnectProvider; | ||
this.walletConnectProvider = walletConnectProvider; | ||
} | ||
|
||
private async startListening(): Promise<void> { | ||
// Subscribe to accounts change | ||
this.provider?.on('accountsChanged', (accounts: string[]) => { | ||
this.actions.update({ accounts }); | ||
}); | ||
|
||
// Subscribe to chainId change | ||
this.provider?.on('chainChanged', (chainId: number) => { | ||
this.actions.update({ chainId }); | ||
}); | ||
|
||
// Subscribe to session disconnection | ||
this.provider?.on('disconnect', (code: number, reason: string) => { | ||
this.actions.update({ accounts: [], chainId: undefined }); | ||
this.actions.resetState(); | ||
this.isCurrentlyUsed = false; | ||
}); | ||
} | ||
|
||
public connectEagerly = () => {}; | ||
|
||
public async activate(): Promise<void> { | ||
this.actions.startActivation(); | ||
this.isCurrentlyUsed = true; | ||
|
||
await this.provider?.enable(); // open modal | ||
this.startListening(); | ||
|
||
this.actions.update({ accounts: this.provider.accounts }); | ||
|
||
this.actions.update({ chainId: this.provider.chainId }); | ||
} | ||
|
||
public async deactivate(): Promise<void> { | ||
if (this.provider) { | ||
await this.provider?.disconnect(); | ||
this.isCurrentlyUsed = false; | ||
this.actions.resetState(); | ||
} | ||
} | ||
} | ||
|
||
export const [walletConnect, hooks] = initializeConnector<WalletConnectV2>( | ||
(actions) => | ||
new WalletConnect({ | ||
actions, | ||
options: { | ||
rpc: Object.fromEntries( | ||
supportedChains.map((chain) => { | ||
return [chain.id, chain.metamask.rpcUrls[0] || '']; | ||
}), | ||
), | ||
}, | ||
new WalletConnectV2(actions, { | ||
rpc: Object.fromEntries( | ||
supportedChains.map((chain) => { | ||
return [chain.id, chain.metamask.rpcUrls[0] || '']; | ||
}), | ||
), | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './LiFiWalletManagement'; | ||
export * from './walletAutomation'; | ||
export * from './wallets'; | ||
export * from './browserWallets'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.