Skip to content
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

feat: wallet management v2 #69

Merged
merged 37 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f329b3c
refactor: remove web3-react
Addminus Mar 21, 2023
00c661e
refactor: external change propagation
Addminus Mar 21, 2023
6ca396e
fix: external disconnect behavior
Addminus Mar 21, 2023
c07c217
refactor: cleanup unnecessary files
Addminus Mar 21, 2023
bbf58a4
chore: cleanup unused code
Addminus Mar 21, 2023
c6987c9
feat: add autoConnect
Addminus Mar 21, 2023
77707f3
fix: auto connect
Addminus Mar 22, 2023
597bb98
fix: walletconnect
Addminus Mar 22, 2023
2d5ce27
feat: re-enable tally
Addminus Mar 22, 2023
b4e8d89
fix: type export
Addminus Mar 22, 2023
4b8d67e
fix: type import
Addminus Mar 22, 2023
f7a5bf8
chore: remove unused files
Addminus Mar 22, 2023
d0ec46f
chore: remove unused files
Addminus Mar 22, 2023
09ec03e
refactor: playground wallet provider
Addminus Mar 22, 2023
631a14e
fix: import
Addminus Mar 22, 2023
47efeb6
Merge branch 'main' into wallet-refactor
Addminus Mar 22, 2023
495f0ad
feat: add frontier
Addminus Mar 22, 2023
50ab849
feat: frontier logo
Addminus Mar 22, 2023
d530a36
feat: add apex wallet, optimize connectors
Addminus Mar 23, 2023
7996939
refactor: cleanup
chybisov Apr 12, 2023
57c82cb
Merge branch 'main' into wallet-refactor
chybisov Apr 12, 2023
29f6d7c
Merge branch 'main' into wallet-refactor
chybisov Apr 13, 2023
bc66578
refactor: unshift wallets into back of connected wallets array
Addminus Apr 17, 2023
2af7d62
chore: clean widget walletprovider
Addminus Apr 17, 2023
6664a30
feat: add default wallet
Addminus Apr 17, 2023
ed79275
fix: walletautomation
Addminus Apr 17, 2023
fd167bd
fix: playground wallet connect
Addminus Apr 17, 2023
23a5846
chore: remove logs
Addminus Apr 17, 2023
c55ae7e
fix: external disconnet sync
Addminus Apr 17, 2023
fefd248
refactor: widget-embedded walletprovider
Addminus Apr 17, 2023
20aa020
Merge branch 'main' into wallet-refactor
chybisov Apr 18, 2023
3a8170e
feat: improve default wallet handling
Addminus Apr 18, 2023
27e795a
Merge branch 'wallet-refactor' of https://github.com/lifinance/lifi-w…
Addminus Apr 18, 2023
89d9f8e
chore: simplify wallet select page
Addminus Apr 18, 2023
e3b35ea
feat: autoconnect based on specific wallets
Addminus Apr 18, 2023
b4734e9
chore: cleanup
chybisov Apr 19, 2023
51fff61
chore: bump packages
chybisov Apr 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions packages/wallet-management/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,13 @@
"lifi"
],
"dependencies": {
"@coinbase/wallet-sdk": "^3.6.5",
"@coinbase/wallet-sdk": "^3.7.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/experimental": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@lifi/sdk": "^2.0.0-beta.8",
"@walletconnect/ethereum-provider": "^1.8.0",
"@walletconnect/web3-provider": "^1.8.0",
"@web3-react/coinbase-wallet": "^8.0.35-beta.0",
"@web3-react/core": "^8.0.35-beta.0",
"@web3-react/eip1193": "^8.0.27-beta.0",
"@web3-react/empty": "^8.0.20-beta.0",
"@web3-react/metamask": "^8.0.30-beta.0",
"@web3-react/network": "^8.0.27-beta.0",
"@web3-react/types": "^8.0.20-beta.0",
"@web3-react/url": "^8.0.25-beta.0",
"react": "^18.2.0"
},
"devDependencies": {
Expand Down
165 changes: 48 additions & 117 deletions packages/wallet-management/src/LiFiWalletManagement.ts
Original file line number Diff line number Diff line change
@@ -1,130 +1,61 @@
import type { Signer } from '@ethersproject/abstract-signer';
import type { ExternalProvider } from '@ethersproject/providers';
import { Web3Provider } from '@ethersproject/providers';
import type { Connector } from '@web3-react/types';
import { useCallback, useEffect, useState } from 'react';
import { usePriorityConnector } from './priorityConnector';
// import { usePriorityConnector, usePriorityProvider } from './connectorHooks';
import { getInjectedAddress } from './injectedData';
import events from 'events';
import type { Wallet } from './types';
import {
addToActiveWallets,
addToDeactivatedWallets,
isWalletDeactivated,
removeFromActiveWallets,
removeFromDeactivatedWallets,
} from './walletPersistance';
import type { Wallet } from './walletProviders';

export const useLiFiWalletManagement = () => {
const priorityConnector = usePriorityConnector();
// "any" because of https://github.com/ethers-io/ethers.js/issues/866
// const priorityProvider = usePriorityProvider('any');
// const [currentProvider, setCurrentProvider] = useState<Web3Provider>();
const [currentConnector, setCurrentConnector] = useState<Connector>();
const [signer, setSigner] = useState<Signer>();

const flushCurrentWalletData = () => {
setCurrentConnector(undefined);
// setCurrentProvider(undefined);
setSigner(undefined);
};

// eslint-disable-next-line react-hooks/exhaustive-deps
const calcWalletData = (connector?: Connector) => {
if (connector) {
const provider = new Web3Provider(
(connector.provider as ExternalProvider) || (window as any).ethereum,
'any', // fallback
);
// setCurrentProvider(() => provider);
setCurrentConnector(() => connector);
setSigner(() => provider?.getSigner?.());
export class LiFiWalletManagement extends events.EventEmitter {
connectedWallets: Wallet[] = [];

public connect = async (wallet: Wallet) => {
try {
await wallet.connect();
wallet.addListener('walletAccountChanged', this.handleAccountDataChange);
this.connectedWallets.unshift(wallet);
removeFromDeactivatedWallets({
address: wallet.account?.address || '',
name: wallet.name,
});
addToActiveWallets({
address: wallet.account?.address || '',
name: wallet.name,
});
} catch (e) {
throw e;
}
};

const connect = useCallback(
async (wallet?: Wallet) => {
try {
if (wallet) {
const { connector } = wallet.web3react;
await connector.activate();
calcWalletData(connector);
} else {
await priorityConnector.activate();
}
} catch (e) {
console.log(e);
public async autoConnect(wallets: Wallet[]) {
for (const wallet of wallets) {
if (wallet.autoConnect) {
await wallet.autoConnect();
wallet.addListener(
'walletAccountChanged',
this.handleAccountDataChange,
);
this.connectedWallets.unshift(wallet);
}
const selectedAddress = getInjectedAddress(wallet);
removeFromDeactivatedWallets(selectedAddress);
addToActiveWallets(selectedAddress);
},
[calcWalletData, priorityConnector],
);

const disconnect = useCallback(
async (wallet?: Wallet) => {
const selectedAddress = getInjectedAddress(wallet);
removeFromActiveWallets(selectedAddress);
addToDeactivatedWallets(selectedAddress);
if (wallet) {
await currentConnector?.deactivate?.();
flushCurrentWalletData();
} else if (priorityConnector.deactivate) {
await priorityConnector.deactivate?.();
flushCurrentWalletData();
} else {
await priorityConnector.resetState();
flushCurrentWalletData();
}
},
[priorityConnector, currentConnector],
);

// eager connect
useEffect(() => {
const selectedAddress = getInjectedAddress();
if (!isWalletDeactivated(selectedAddress) && priorityConnector) {
priorityConnector.connectEagerly?.();
calcWalletData(priorityConnector);
}
}, [
priorityConnector,
// eslint-disable-next-line react-hooks/exhaustive-deps
(window as any).ethereum?.selectedAddress,
// eslint-disable-next-line react-hooks/exhaustive-deps
(window as any).tally?.selectedAddress,
]);

// injected wallet listeners
useEffect(() => {
const { ethereum } = window as any;
const handleChainChanged = async (chainId: string | number) => {
await currentConnector?.activate();
calcWalletData(currentConnector);
};
const handleAccountsChanged = async (accounts: string[]) => {
if (!accounts.length) {
await currentConnector?.deactivate?.();
flushCurrentWalletData();
}
};

ethereum?.on('chainChanged', handleChainChanged);
ethereum?.on('accountsChanged', handleAccountsChanged);

return () => {
if (ethereum?.removeListener) {
ethereum.removeListener('chainChanged', handleChainChanged);
ethereum.removeListener('accountsChanged', handleAccountsChanged);
}
};
}, [currentConnector]);

return {
connect,
disconnect,
signer,
provider: signer?.provider,
}

public disconnect = async (wallet: Wallet) => {
wallet.removeAllListeners();
removeFromActiveWallets({
address: wallet.account?.address || '',
name: wallet.name,
});
addToDeactivatedWallets({
address: wallet.account?.address || '',
name: wallet.name,
});

wallet.disconnect();
};
};

private handleAccountDataChange() {
this.emit('walletChanged', this.connectedWallets);
}
}
12 changes: 0 additions & 12 deletions packages/wallet-management/src/connectors/coinbaseWallet.ts

This file was deleted.

68 changes: 0 additions & 68 deletions packages/wallet-management/src/connectors/eip1193.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/wallet-management/src/connectors/empty.ts

This file was deleted.

Loading