-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds walletconnectv2 support (#279)
- Loading branch information
1 parent
d5f89ba
commit d488d7c
Showing
9 changed files
with
933 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Connector, RpcData } from '../types' | ||
import { ConnectorConfigError } from '../errors' | ||
import { | ||
WalletConnect, | ||
WalletConnectConstructorArgs, | ||
} from '@web3-react/walletconnect-v2' | ||
|
||
export default async function init(): Promise<Connector> { | ||
const { WalletConnect } = await import('@web3-react/walletconnect-v2') | ||
|
||
class WalletConnectExtended { | ||
private walletConnect: WalletConnect | ||
|
||
constructor(args: WalletConnectConstructorArgs) { | ||
this.walletConnect = new WalletConnect(args) | ||
} | ||
|
||
public async getProvider(): Promise<any> { | ||
return this.walletConnect.provider | ||
} | ||
|
||
public async getAccount(): Promise<null | string> { | ||
return this.walletConnect.provider?.accounts[0] || null | ||
} | ||
|
||
public async activate(desiredChainId?: number): Promise<any> { | ||
await this.walletConnect.activate(desiredChainId) | ||
return this.getProvider() | ||
} | ||
|
||
public async deactivate(): Promise<void> { | ||
return this.walletConnect.deactivate() | ||
} | ||
|
||
// Fake beeing an event emitter | ||
public on( | ||
event: string, | ||
callback: (args: any) => void | ||
): WalletConnectExtended { | ||
this.walletConnect.provider?.on(event as any, callback) | ||
return this | ||
} | ||
public off( | ||
event: string, | ||
callback: (args: any) => void | ||
): WalletConnectExtended { | ||
this.walletConnect.provider?.off(event as any, callback) | ||
return this | ||
} | ||
} | ||
|
||
return { | ||
web3ReactConnector({ | ||
rpc, | ||
projectId, | ||
}: { | ||
rpc: RpcData | ||
projectId: string | ||
}) { | ||
if (!rpc || !projectId) { | ||
throw new ConnectorConfigError( | ||
'The WalletConnect connector requires rpcUrl to be set.' | ||
) | ||
} | ||
Object.values(rpc).forEach((url: string) => { | ||
if (!/^https?:\/\//.test(url)) { | ||
throw new ConnectorConfigError( | ||
'The WalletConnect connector requires rpcUrl to be an HTTP URL.' | ||
) | ||
} | ||
return | ||
}) | ||
return new WalletConnectExtended({ | ||
options: { | ||
projectId, | ||
showQrModal: true, | ||
rpcMap: rpc, | ||
chains: Object.keys(rpc).map((chainId) => parseInt(chainId)), | ||
}, | ||
actions: { | ||
startActivation: () => () => {}, | ||
update: () => { | ||
return 'asdf' | ||
}, | ||
resetState: () => {}, | ||
}, | ||
}) | ||
}, | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.