-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow for alternative wallet injections (#34)
* fix: wallet popup on page open * refactor: rename walletconnct connector * feat: allow alternative injections * refactor: rename * fix: merge * fix: import error: remove logs
- Loading branch information
Showing
8 changed files
with
478 additions
and
188 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,69 @@ | ||
// eslint-disable-next-line max-classes-per-file | ||
import { Eip1193Bridge } from '@ethersproject/experimental'; | ||
import { initializeConnector } from '@web3-react/core'; | ||
import { EIP1193 } from '@web3-react/eip1193'; | ||
import { EIP1193, EIP1193ConstructorArgs } from '@web3-react/eip1193'; | ||
import type { Empty } from '@web3-react/empty'; | ||
import { EMPTY } from '@web3-react/empty'; | ||
import { ProviderConnectInfo, ProviderRpcError } from '@web3-react/types'; | ||
import { providers } from 'ethers'; | ||
|
||
class Eip1193BridgeWithoutAccounts extends Eip1193Bridge { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
request(request: { method: string; params?: any[] }): Promise<any> { | ||
if ( | ||
request.method === 'eth_requestAccounts' || | ||
request.method === 'eth_accounts' | ||
) { | ||
return Promise.resolve([]); | ||
} | ||
return super.request(request); | ||
} | ||
} | ||
const { ethereum } = window as any; | ||
const currentProvider = ethereum ? new providers.Web3Provider(ethereum) : null; | ||
|
||
const eip1193Provider = currentProvider | ||
? new Eip1193BridgeWithoutAccounts( | ||
currentProvider?.getSigner(), | ||
currentProvider, | ||
) | ||
: null; | ||
|
||
export const [eip1193, hooks, store] = initializeConnector<EIP1193 | Empty>( | ||
(actions) => | ||
eip1193Provider | ||
? new EIP1193({ | ||
actions, | ||
provider: eip1193Provider, | ||
onError: (error) => console.warn(error), | ||
}) | ||
: EMPTY, | ||
// supportedChains.map((chain) => chain.id), | ||
); | ||
class EIP1193Listener extends EIP1193 { | ||
constructor({ actions, provider, onError }: EIP1193ConstructorArgs) { | ||
super({ actions, provider, onError }); | ||
|
||
this.provider = provider; | ||
|
||
this.provider.on('connect', ({ chainId }: ProviderConnectInfo): void => { | ||
console.log('connect:', chainId); | ||
}); | ||
|
||
this.provider.on('disconnect', (error: ProviderRpcError): void => { | ||
this.actions.resetState(); | ||
this.onError?.(error); | ||
}); | ||
|
||
this.provider.on('chainChanged', (chainId: string): void => { | ||
console.log(chainId); | ||
}); | ||
|
||
this.provider.on('accountsChanged', (accounts: string[]): void => { | ||
console.log('eip1193 accounts', accounts); | ||
}); | ||
} | ||
} | ||
|
||
export const createEip1193Connector = () => { | ||
const { ethereum } = window as any; | ||
const currentProvider = ethereum | ||
? new providers.Web3Provider(ethereum) | ||
: null; | ||
|
||
const eip1193Provider = new Eip1193BridgeWithoutAccounts( | ||
currentProvider!.getSigner(), | ||
currentProvider!, | ||
); | ||
const [eip1193, hooks] = initializeConnector<EIP1193Listener | Empty>( | ||
(actions) => | ||
eip1193Provider | ||
? new EIP1193({ | ||
actions, | ||
provider: eip1193Provider, | ||
onError: (error) => console.warn(error), | ||
}) | ||
: EMPTY, | ||
// supportedChains.map((chain) => chain.id), | ||
); | ||
|
||
console.log('eip1193:', eip1193); | ||
|
||
return { | ||
connector: eip1193, | ||
hooks, | ||
}; | ||
}; |
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.