-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from levelkdev/develop
Develop
- Loading branch information
Showing
27 changed files
with
273 additions
and
252 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
Binary file not shown.
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,40 @@ | ||
import React from 'react' | ||
import styled from 'styled-components' | ||
import { useDisclaimerBar } from '../../hooks/useShowDisclaimerBar' | ||
|
||
export const DisclaimerBar = () => { | ||
if (!useDisclaimerBar()) return null | ||
|
||
return ( | ||
<Bar> | ||
<Disclaimer> | ||
This is an unofficial build meant for development and testing purposes.{' '} | ||
<a href="https://swapr.eth.link/" target="_blank" rel="noopener noreferrer"> | ||
Please visit Swapr.eth | ||
</a> | ||
</Disclaimer> | ||
</Bar> | ||
) | ||
} | ||
|
||
const Bar = styled.div` | ||
width: 100%; | ||
padding: 8px; | ||
background: rgba(242, 153, 74, 0.15); | ||
border-bottom: 1px solid rgba(242, 153, 74, 0.5); | ||
` | ||
|
||
const Disclaimer = styled.p` | ||
margin: 0; | ||
font-weight: 500; | ||
font-size: 12px; | ||
line-height: 15px; | ||
text-align: center; | ||
color: #f2994a; | ||
a { | ||
font-weight: 700; | ||
color: inherit; | ||
text-decoration: none; | ||
} | ||
` |
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
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
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,120 @@ | ||
// largely taken from https://github.com/NoahZinsmeister/web3-react/blob/v6/packages/walletconnect-connector/src/index.ts | ||
// Updated to always be in sync with network connector's chain id | ||
|
||
import { ConnectorUpdate } from '@web3-react/types' | ||
import { AbstractConnector } from '@web3-react/abstract-connector' | ||
import { IWalletConnectProviderOptions } from '@walletconnect/types' | ||
import { network } from '.' | ||
|
||
export const URI_AVAILABLE = 'URI_AVAILABLE' | ||
|
||
export interface WalletConnectConnectorArguments extends IWalletConnectProviderOptions { | ||
supportedChainIds?: number[] | ||
} | ||
|
||
export class UserRejectedRequestError extends Error { | ||
public constructor() { | ||
super() | ||
this.name = this.constructor.name | ||
this.message = 'The user rejected the request.' | ||
} | ||
} | ||
|
||
function getSupportedChains({ supportedChainIds, rpc }: WalletConnectConnectorArguments): number[] | undefined { | ||
if (supportedChainIds) { | ||
return supportedChainIds | ||
} | ||
|
||
return rpc ? Object.keys(rpc).map(k => Number(k)) : undefined | ||
} | ||
|
||
export class CustomWalletConnectConnector extends AbstractConnector { | ||
private readonly config: WalletConnectConnectorArguments | ||
|
||
public walletConnectProvider?: any | ||
|
||
constructor(config: WalletConnectConnectorArguments) { | ||
super({ supportedChainIds: getSupportedChains(config) }) | ||
|
||
this.config = config | ||
|
||
this.handleChainChanged = this.handleChainChanged.bind(this) | ||
this.handleAccountsChanged = this.handleAccountsChanged.bind(this) | ||
this.handleDisconnect = this.handleDisconnect.bind(this) | ||
} | ||
|
||
private handleChainChanged(chainId: number | string): void { | ||
this.emitUpdate({ chainId }) | ||
} | ||
|
||
private handleAccountsChanged(accounts: string[]): void { | ||
this.emitUpdate({ account: accounts[0] }) | ||
} | ||
|
||
private handleDisconnect(): void { | ||
this.emitDeactivate() | ||
// we have to do this because of a @walletconnect/web3-provider bug | ||
if (this.walletConnectProvider) { | ||
this.walletConnectProvider.stop() | ||
this.walletConnectProvider.removeListener('chainChanged', this.handleChainChanged) | ||
this.walletConnectProvider.removeListener('accountsChanged', this.handleAccountsChanged) | ||
this.walletConnectProvider = undefined | ||
} | ||
|
||
this.emitDeactivate() | ||
} | ||
|
||
public async activate(): Promise<ConnectorUpdate> { | ||
const WalletConnectProvider = await import('@walletconnect/web3-provider').then(m => m?.default ?? m) | ||
this.walletConnectProvider = new WalletConnectProvider(this.config) | ||
|
||
// ensure that the uri is going to be available, and emit an event if there's a new uri | ||
if (!this.walletConnectProvider.wc.connected) { | ||
await this.walletConnectProvider.wc.createSession({ chainId: await network.getChainId() }) | ||
this.emit(URI_AVAILABLE, this.walletConnectProvider.wc.uri) | ||
} | ||
|
||
const account = await this.walletConnectProvider | ||
.enable() | ||
.then((accounts: string[]): string => accounts[0]) | ||
.catch((error: Error): void => { | ||
// TODO ideally this would be a better check | ||
if (error.message === 'User closed modal') { | ||
throw new UserRejectedRequestError() | ||
} | ||
|
||
throw error | ||
}) | ||
|
||
this.walletConnectProvider.on('disconnect', this.handleDisconnect) | ||
this.walletConnectProvider.on('chainChanged', this.handleChainChanged) | ||
this.walletConnectProvider.on('accountsChanged', this.handleAccountsChanged) | ||
|
||
return { provider: this.walletConnectProvider, account } | ||
} | ||
|
||
public async getProvider(): Promise<any> { | ||
return this.walletConnectProvider | ||
} | ||
|
||
public async getChainId(): Promise<number | string> { | ||
return this.walletConnectProvider.send('eth_chainId') | ||
} | ||
|
||
public async getAccount(): Promise<null | string> { | ||
return this.walletConnectProvider.send('eth_accounts').then((accounts: string[]): string => accounts[0]) | ||
} | ||
|
||
public deactivate() { | ||
if (this.walletConnectProvider) { | ||
this.walletConnectProvider.stop() | ||
this.walletConnectProvider.removeListener('disconnect', this.handleDisconnect) | ||
this.walletConnectProvider.removeListener('chainChanged', this.handleChainChanged) | ||
this.walletConnectProvider.removeListener('accountsChanged', this.handleAccountsChanged) | ||
} | ||
} | ||
|
||
public async close() { | ||
await this.walletConnectProvider?.close() | ||
} | ||
} |
Oops, something went wrong.