-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
89 changed files
with
1,247 additions
and
671 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
80 changes: 80 additions & 0 deletions
80
packages/wallet-management/src/components/EVMListItemButton.tsx
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,80 @@ | ||
import { ChainType } from '@lifi/sdk'; | ||
import { Avatar, ListItemAvatar } from '@mui/material'; | ||
import type { Connector } from 'wagmi'; | ||
import { useConfig } from 'wagmi'; | ||
import { connect, disconnect, getAccount } from 'wagmi/actions'; | ||
import { ListItemButton } from '../components/ListItemButton.js'; | ||
import { ListItemText } from '../components/ListItemText.js'; | ||
import type { CreateConnectorFnExtended } from '../connectors/types.js'; | ||
import { useLastConnectedAccount } from '../hooks/useAccount.js'; | ||
import { useWalletManagementEvents } from '../hooks/useWalletManagementEvents.js'; | ||
import { WalletManagementEvent } from '../types/events.js'; | ||
import { getConnectorIcon } from '../utils/getConnectorIcon.js'; | ||
import { isWalletInstalledAsync } from '../utils/isWalletInstalledAsync.js'; | ||
import type { WalletListItemButtonProps } from './types.js'; | ||
|
||
interface EVMListItemButtonProps extends WalletListItemButtonProps { | ||
connector: CreateConnectorFnExtended | Connector; | ||
} | ||
|
||
export const EVMListItemButton = ({ | ||
ecosystemSelection, | ||
connector, | ||
onNotInstalled, | ||
onConnected, | ||
onConnecting, | ||
onError, | ||
}: EVMListItemButtonProps) => { | ||
const emitter = useWalletManagementEvents(); | ||
const config = useConfig(); | ||
const { setLastConnectedAccount } = useLastConnectedAccount(); | ||
|
||
const handleEVMConnect = async () => { | ||
try { | ||
const identityCheckPassed = await isWalletInstalledAsync( | ||
(connector as Connector).id, | ||
); | ||
if (!identityCheckPassed) { | ||
onNotInstalled?.(connector as Connector); | ||
return; | ||
} | ||
const connectedAccount = getAccount(config); | ||
onConnecting?.(); | ||
const data = await connect(config, { connector }); | ||
if (connectedAccount.connector) { | ||
await disconnect(config, { connector: connectedAccount.connector }); | ||
} | ||
setLastConnectedAccount(connector); | ||
emitter.emit(WalletManagementEvent.WalletConnected, { | ||
address: data.accounts[0], | ||
chainId: data.chainId, | ||
chainType: ChainType.EVM, | ||
}); | ||
onConnected?.(); | ||
} catch (error) { | ||
onError?.(error); | ||
} | ||
}; | ||
|
||
const connectorName: string = ecosystemSelection | ||
? 'Ethereum' | ||
: (connector as CreateConnectorFnExtended).displayName || connector.name; | ||
|
||
return ( | ||
<ListItemButton key={connector.id} onClick={handleEVMConnect}> | ||
<ListItemAvatar> | ||
<Avatar | ||
src={ | ||
ecosystemSelection | ||
? 'https://raw.githubusercontent.com/lifinance/types/main/src/assets/icons/chains/ethereum.svg' | ||
: getConnectorIcon(connector as Connector) | ||
} | ||
alt={connectorName} | ||
> | ||
{connectorName?.[0]} | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText primary={connectorName} /> | ||
</ListItemButton> | ||
); | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/wallet-management/src/components/ListItemButton.tsx
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,24 @@ | ||
import { | ||
alpha, | ||
ListItemButton as MuiListItemButton, | ||
styled, | ||
} from '@mui/material'; | ||
|
||
export const ListItemButton = styled(MuiListItemButton)(({ | ||
theme, | ||
disabled, | ||
}) => { | ||
const backgroundHoverColor = | ||
theme.palette.mode === 'light' | ||
? alpha(theme.palette.common.black, 0.04) | ||
: alpha(theme.palette.common.white, 0.04); | ||
return { | ||
borderRadius: theme.shape.borderRadius, | ||
paddingLeft: theme.spacing(1.5), | ||
height: 56, | ||
'&:hover': { | ||
backgroundColor: !disabled && backgroundHoverColor, | ||
}, | ||
...(disabled ? { opacity: 0.5, cursor: 'auto' } : {}), | ||
}; | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/wallet-management/src/components/ListItemText.tsx
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,11 @@ | ||
import { | ||
ListItemText as MuiListItemText, | ||
listItemTextClasses, | ||
styled, | ||
} from '@mui/material'; | ||
|
||
export const ListItemText = styled(MuiListItemText)(({ theme }) => ({ | ||
[`.${listItemTextClasses.primary}`]: { | ||
fontWeight: 500, | ||
}, | ||
})); |
71 changes: 71 additions & 0 deletions
71
packages/wallet-management/src/components/SVMListItemButton.tsx
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,71 @@ | ||
import { ChainId, ChainType } from '@lifi/sdk'; | ||
import { Avatar, ListItemAvatar } from '@mui/material'; | ||
import type { WalletAdapter } from '@solana/wallet-adapter-base'; | ||
import { useWallet } from '@solana/wallet-adapter-react'; | ||
import { ListItemButton } from '../components/ListItemButton.js'; | ||
import { ListItemText } from '../components/ListItemText.js'; | ||
import { useLastConnectedAccount } from '../hooks/useAccount.js'; | ||
import { useWalletManagementEvents } from '../hooks/useWalletManagementEvents.js'; | ||
import { WalletManagementEvent } from '../types/events.js'; | ||
import type { WalletListItemButtonProps } from './types.js'; | ||
|
||
interface SVMListItemButtonProps extends WalletListItemButtonProps { | ||
walletAdapter: WalletAdapter; | ||
} | ||
|
||
export const SVMListItemButton = ({ | ||
ecosystemSelection, | ||
walletAdapter, | ||
onConnected, | ||
onConnecting, | ||
onError, | ||
}: SVMListItemButtonProps) => { | ||
const emitter = useWalletManagementEvents(); | ||
const { select, disconnect, connected } = useWallet(); | ||
const { setLastConnectedAccount } = useLastConnectedAccount(); | ||
|
||
const connect = async () => { | ||
try { | ||
onConnecting?.(); | ||
if (connected) { | ||
await disconnect(); | ||
} | ||
select(walletAdapter.name); | ||
// We use autoConnect on wallet selection | ||
// await solanaConnect(); | ||
walletAdapter.once('connect', (publicKey) => { | ||
setLastConnectedAccount(walletAdapter); | ||
emitter.emit(WalletManagementEvent.WalletConnected, { | ||
address: publicKey?.toString(), | ||
chainId: ChainId.SOL, | ||
chainType: ChainType.SVM, | ||
}); | ||
}); | ||
onConnected?.(); | ||
} catch (error) { | ||
onError?.(error); | ||
} | ||
}; | ||
|
||
const connectorName: string = ecosystemSelection | ||
? 'Solana' | ||
: walletAdapter.name; | ||
|
||
return ( | ||
<ListItemButton key={connectorName} onClick={connect}> | ||
<ListItemAvatar> | ||
<Avatar | ||
src={ | ||
ecosystemSelection | ||
? 'https://raw.githubusercontent.com/lifinance/types/main/src/assets/icons/chains/solana.svg' | ||
: walletAdapter.icon | ||
} | ||
alt={connectorName} | ||
> | ||
{connectorName[0]} | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText primary={connectorName} /> | ||
</ListItemButton> | ||
); | ||
}; |
80 changes: 80 additions & 0 deletions
80
packages/wallet-management/src/components/UTXOListItemButton.tsx
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,80 @@ | ||
import { ChainType } from '@lifi/sdk'; | ||
import { Avatar, ListItemAvatar } from '@mui/material'; | ||
import type { Connector } from 'wagmi'; | ||
import { connect, disconnect, getAccount } from 'wagmi/actions'; | ||
import { ListItemButton } from '../components/ListItemButton.js'; | ||
import { ListItemText } from '../components/ListItemText.js'; | ||
import type { CreateConnectorFnExtended } from '../connectors/types.js'; | ||
import { useLastConnectedAccount } from '../hooks/useAccount.js'; | ||
import { useWalletManagementEvents } from '../hooks/useWalletManagementEvents.js'; | ||
import { WalletManagementEvent } from '../types/events.js'; | ||
import { getConnectorIcon } from '../utils/getConnectorIcon.js'; | ||
import { isWalletInstalledAsync } from '../utils/isWalletInstalledAsync.js'; | ||
import { useConfig } from '../utxo/hooks/useConfig.js'; | ||
import type { WalletListItemButtonProps } from './types.js'; | ||
|
||
interface UTXOListItemButtonProps extends WalletListItemButtonProps { | ||
connector: CreateConnectorFnExtended | Connector; | ||
} | ||
|
||
export const UTXOListItemButton = ({ | ||
ecosystemSelection, | ||
connector, | ||
onNotInstalled, | ||
onConnected, | ||
onConnecting, | ||
onError, | ||
}: UTXOListItemButtonProps) => { | ||
const emitter = useWalletManagementEvents(); | ||
const config = useConfig(); | ||
const { setLastConnectedAccount } = useLastConnectedAccount(); | ||
|
||
const handleUTXOConnect = async () => { | ||
try { | ||
const identityCheckPassed = await isWalletInstalledAsync( | ||
(connector as Connector).id, | ||
); | ||
if (!identityCheckPassed) { | ||
onNotInstalled?.(connector as Connector); | ||
return; | ||
} | ||
const connectedAccount = getAccount(config); | ||
onConnecting?.(); | ||
const data = await connect(config, { connector }); | ||
if (connectedAccount.connector) { | ||
await disconnect(config, { connector: connectedAccount.connector }); | ||
} | ||
setLastConnectedAccount(connector); | ||
emitter.emit(WalletManagementEvent.WalletConnected, { | ||
address: data.accounts[0], | ||
chainId: data.chainId, | ||
chainType: ChainType.UTXO, | ||
}); | ||
onConnected?.(); | ||
} catch (error) { | ||
onError?.(error); | ||
} | ||
}; | ||
|
||
const connectorName: string = ecosystemSelection | ||
? 'Bitcoin' | ||
: (connector as CreateConnectorFnExtended).displayName || connector.name; | ||
|
||
return ( | ||
<ListItemButton key={connector.id} onClick={handleUTXOConnect}> | ||
<ListItemAvatar> | ||
<Avatar | ||
src={ | ||
ecosystemSelection | ||
? 'https://raw.githubusercontent.com/lifinance/types/main/src/assets/icons/chains/bitcoin.svg' | ||
: getConnectorIcon(connector as Connector) | ||
} | ||
alt={connectorName} | ||
> | ||
{connectorName?.[0]} | ||
</Avatar> | ||
</ListItemAvatar> | ||
<ListItemText primary={connectorName} /> | ||
</ListItemButton> | ||
); | ||
}; |
Oops, something went wrong.