-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🦊🐺🐭 Add connectors #754
Closed
Closed
🦊🐺🐭 Add connectors #754
Changes from 1 commit
Commits
Show all changes
106 commits
Select commit
Hold shift + click to select a range
475c84a
Add context
dmaretskyi f59899b
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
048c972
Add connector logic and exports
f883491
Add connector example
75f986a
Add wallet connect example
bd54c05
Apply stash changes
84e8a55
Update depedencies
c17beb4
Merge branch 'master' into HEAD
7873f43
Remove ethers/providers
4e8dc7f
Merge branch 'dmaretskyi/connectors' of https://github.com/TrueFiEng/…
ef2490f
Fix lint
520287b
Fix lint
9f6f1f7
Merge branch 'master' into dmaretskyi/connectors
5b77f2a
Apply connector rebase
7c93e36
Remove transaction blocker
7208448
Merge branch 'master' into dmaretskyi/connectors
mj426382 b13347a
Merge branch 'master' into dmaretskyi/connectors
mj426382 1a13f3d
Add metamask by default
a7ac27c
Add testing wallet
0e88662
Add coinbase and portis connectors
a2b48cc
Merge branch 'dmaretskyi/connectors' of https://github.com/TrueFiEng/…
ebadd12
Add fortmatic wallet
f5ce29d
Add torus connector
f067124
Add brave and injected connectors
d80c170
Merge origin/master into dmaretskyi/connectors
rzadp e39343b
premerge
rzadp 42b602c
Merge origin/master into dmaretskyi/connectors
rzadp 76dcbdd
postmerge
rzadp 0e2e45f
Remove types from deps
rzadp 47cd9a9
Merge origin/master into dmaretskyi/connectors
rzadp 0f2357b
Clean up a bit
yivlad 7c282fc
Remov some wallets
yivlad ecc9f72
Debugging tests
yivlad b551f49
Merge branch 'master' into dmaretskyi/connectors
yivlad 9c786dc
WIP
yivlad 29b76e8
Add readonly provider as default
yivlad 620761b
Green tests
yivlad c65cf20
Lint
yivlad 09a3d2f
Green playwright tests
yivlad af7a59f
Lint
yivlad ecc4328
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad 2f1d594
Lint
yivlad feb07ab
Update tests
yivlad 243cb46
Record example tests
yivlad 170494e
Revert "Record example tests"
yivlad ad36415
Test ambire availability
yivlad 13a343e
Bringa back playwright tests
yivlad f36cad8
Upload screenshot
yivlad 9ca2988
Debug CI
yivlad 4c43d06
Fix screenshot paths
yivlad 4463cd5
Fix path again
yivlad b975f02
Wait for ambire page to show up
yivlad 22710ce
Wait also for metamask page
yivlad c99be0a
Remove debugging stuff
yivlad bae12a6
Lint
yivlad 892ba36
Start fixing example-next
yivlad 80b7b36
Fix next playwright tests
yivlad 0ae8081
Debgging swithicng chains
yivlad 285034a
Fix docs tests
yivlad c265a72
Lint
yivlad f6d4ba4
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad ca7481a
Lint
yivlad 4a82820
Check websocket provider
yivlad 4bb5a6f
Fix timestamp overflow
yivlad 749b6e5
Lint
yivlad a5f4080
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad 3d96f46
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad f1c3d0e
Add test for switching accounts
yivlad bcb853e
Fix no multicall address error
yivlad de83617
Add logs
yivlad 1c9255c
Fix change account issue
yivlad 6397824
Lint
yivlad 6778dbc
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad 18cc041
Fix test order
yivlad 7015a80
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad 7da9027
Fix next tests
yivlad 6ed9b2b
Fix ethers version mismatch
yivlad 48c4869
Docs add timeout to playwright tests
yivlad 1a959e2
deactivate function for connectors
yivlad 670ef69
Example app add local provider only when testing
yivlad 5640226
Fix coinabse wallet
yivlad cdcd19e
Bring bakc portis connector
yivlad 16e599f
Lint
yivlad cdeb200
Reimplement polling interval logic
yivlad 9cb87ac
Lint
yivlad 9988309
USe typed events
yivlad e2d1db3
Don't use setImmediate
yivlad 53e1c6d
Add apiNode connector
yivlad 9dce9aa
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad aa7f725
Extract connectors to separate packages
yivlad 02384ef
Set workspace dependecies properly
yivlad 7ec4870
Fix CI
yivlad d55c53c
WIP
yivlad 6ea9884
Make all tests green
yivlad 05da578
Fix wallet provider non static calls tracking
yivlad 8934edd
clear local storage
yivlad a38f556
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad d7a908d
Upgrade waffle
yivlad 5fe407a
Delete tokens playwright tests
yivlad fda2c65
Fix playwright tests
yivlad 1d830a3
Fix localStorage issue
yivlad e55fbca
Fix playwright tests
yivlad 369ebde
Merge remote-tracking branch 'origin/master' into dmaretskyi/connectors
yivlad ee7d95d
Add transfer funds test
yivlad a6b375b
Lint
yivlad 1c2582b
Make useEthers API backwards-compatible
yivlad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
packages/core/src/providers/network/connector/connector.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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { BaseProvider, Web3Provider } from '@ethersproject/providers'; | ||
|
||
export enum ConnectorPriority { | ||
ApiNode = 1, | ||
Wallet = 2, | ||
} | ||
|
||
export interface Update { | ||
chainId: number | ||
accounts: string[] | ||
} | ||
|
||
export type UpdateFn = (data: Update) => void | ||
|
||
export interface Connector { | ||
name: string; | ||
priority: ConnectorPriority; | ||
|
||
provider?: BaseProvider; | ||
|
||
onUpdate?: UpdateFn; | ||
|
||
connectEagerly(): Promise<void> | ||
|
||
activate(): Promise<void> | ||
|
||
deactivate(): Promise<void> | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/core/src/providers/network/connector/connectorController.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Connector } from "./connector"; | ||
|
||
export class ConnectorController { | ||
constructor( | ||
public readonly connector: Connector, | ||
) { | ||
connector.onUpdate = ({ chainId, accounts }) => { | ||
this.chainId = chainId; | ||
this.accounts = accounts; | ||
} | ||
|
||
void this.connector.connectEagerly(); | ||
} | ||
|
||
public active = false; | ||
public accounts: string[] = [] | ||
public chainId: number = 0 | ||
|
||
getProvider() { | ||
return this.connector.provider; | ||
} | ||
|
||
async activate() { | ||
await this.connector.activate(); | ||
this.active = true; | ||
} | ||
|
||
async deactivate() { | ||
this.active = false; | ||
await this.connector.deactivate(); | ||
} | ||
} |
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,41 @@ | ||
import { createContext, ReactNode, useEffect, useState } from "react"; | ||
import { Connector } from "./connector"; | ||
import { ConnectorController } from "./connectorController"; | ||
|
||
interface ConnectorContextValue { | ||
connectors: ConnectorController[]; | ||
addConnector(connector: Connector): void; | ||
} | ||
|
||
export const ConnectorContext = createContext<ConnectorContextValue | undefined>(undefined) | ||
|
||
export interface ConnectorContextProviderProps { | ||
connectors: Connector[]; | ||
children?: ReactNode; | ||
} | ||
|
||
export function ConnectorContextProvider({ connectors, children }: ConnectorContextProviderProps) { | ||
const [controllers, setControllers] = useState<ConnectorController[]>([]) | ||
|
||
useEffect(() => { | ||
const newControllers = connectors.map(connector => { | ||
const existing = controllers.find(c => c.connector === connector) | ||
if (existing) { | ||
return existing | ||
} | ||
return new ConnectorController(connector) | ||
}) | ||
const otherControllers = controllers.filter(c => !newControllers.some(c2 => c2.connector === c.connector)) | ||
setControllers([...newControllers, ...otherControllers].sort((a, b) => a.connector.priority - b.connector.priority)) | ||
}, connectors) | ||
|
||
function addConnector(connector: Connector) { | ||
setControllers(controllers.concat(new ConnectorController(connector))) | ||
} | ||
|
||
return ( | ||
<ConnectorContext.Provider value={{ connectors: controllers, addConnector }}> | ||
{children} | ||
</ConnectorContext.Provider> | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/core/src/providers/network/connector/impls/metamask.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { BaseProvider, Web3Provider } from "@ethersproject/providers"; | ||
import { Connector, ConnectorPriority, UpdateFn } from "../connector"; | ||
import detectMetamask from '@metamask/detect-provider' | ||
|
||
export class MetamaskConnector implements Connector { | ||
public provider?: Web3Provider; | ||
public priority = ConnectorPriority.Wallet; | ||
public name = 'Metamask'; | ||
|
||
constructor() { | ||
|
||
} | ||
|
||
onUpdate?: UpdateFn; | ||
|
||
private async init() { | ||
const metamask = await detectMetamask(); | ||
this.provider = new Web3Provider(metamask as any); | ||
} | ||
|
||
async connectEagerly(): Promise<void> { | ||
await this.init(); | ||
|
||
const chainId: string = await this.provider!.send('eth_chainId', []) | ||
yivlad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const accounts: string[] = await this.provider!.send('eth_accounts', []) | ||
this.onUpdate?.({ chainId: parseInt(chainId), accounts }); | ||
} | ||
|
||
async activate(): Promise<void> { | ||
const chainId: string = await this.provider!.send('eth_chainId', []) | ||
const accounts: string[] = await this.provider!.send('eth_requestAccounts', []) | ||
this.onUpdate?.({ chainId: parseInt(chainId), accounts }); | ||
} | ||
|
||
async deactivate(): Promise<void> { | ||
|
||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/core/src/providers/network/connector/impls/rpc.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { BaseProvider, JsonRpcProvider, Web3Provider } from "@ethersproject/providers"; | ||
import { Connector, ConnectorPriority, UpdateFn } from "../connector"; | ||
|
||
export class RpcConnector implements Connector { | ||
public provider: BaseProvider; | ||
public priority = ConnectorPriority.ApiNode; | ||
public name = 'RPC'; | ||
|
||
public onUpdate?: UpdateFn; | ||
|
||
constructor( | ||
public url: string, | ||
) { | ||
this.provider = new JsonRpcProvider(url); | ||
} | ||
|
||
async connectEagerly(): Promise<void> { | ||
const { chainId } = await this.provider!.getNetwork(); | ||
this.onUpdate?.({ chainId, accounts: [] }); | ||
} | ||
async activate(): Promise<void> { | ||
const { chainId } = await this.provider!.getNetwork(); | ||
this.onUpdate?.({ chainId, accounts: [] }); | ||
} | ||
async deactivate(): Promise<void> { | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/core/src/providers/network/connector/useConnectorNetwork.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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { BaseProvider } from "@ethersproject/providers"; | ||
import { useContext } from "react"; | ||
import { ConnectorController } from "./connectorController"; | ||
import { ConnectorContext } from "./context"; | ||
|
||
export interface UseProviderOptions { | ||
chainId?: number; | ||
account?: boolean | string; | ||
} | ||
|
||
export interface ConnectorNetwork { | ||
chainId: number; | ||
accounts: string[]; | ||
provider: BaseProvider; | ||
} | ||
|
||
export function useConnectorNetwork(opts: UseProviderOptions = {}): ConnectorNetwork | undefined { | ||
const { connectors } = useContext(ConnectorContext)!; | ||
const connector = connectors.find(c => matchConnector(opts, c)); | ||
|
||
if(!connector || !connector.getProvider()) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
chainId: connector.chainId, | ||
accounts: connector.accounts, | ||
provider: connector.getProvider()!, | ||
} | ||
} | ||
|
||
function matchConnector(opts: UseProviderOptions, connector: ConnectorController) { | ||
if(opts.chainId) { | ||
if(connector.chainId !== opts.chainId) { | ||
return false; | ||
} | ||
} | ||
|
||
if(opts.account === true) { | ||
if(!connector.accounts.length) { | ||
return false; | ||
} | ||
} else if(opts.account) { | ||
if(!connector.accounts.includes(opts.account)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should handle the lifecycle of the connector including chainID, accounts, block number etc.
Should have safeguards to protect from bugs in the connector implementation