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

Aleph Zero Wallets added #3

Merged
merged 19 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"author": "Jayesh Bhole <[email protected]>",
"dependencies": {
"@nightlylabs/wallet-selector-polkadot": "^0.2.6",
"@safe-global/safe-apps-sdk": "^9.0.0",
"@solana/wallet-adapter-base": "^0.9.23",
"@solana/wallet-adapter-react": "^0.15.35",
Expand Down
23 changes: 23 additions & 0 deletions packages/react/src/chains/aleph_zero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ChainData } from '../types/index.js';

export const alephZero: ChainData = {
id: 'aleph-zero',
name: 'Aleph Zero',
type: 'aleph_zero',
nativeCurrency: {
name: 'AZERO',
symbol: 'AZERO',
decimals: 18,
zmrp marked this conversation as resolved.
Show resolved Hide resolved
},
rpcUrls: {
default: {
http: [''],
},
},
blockExplorers: {
default: {
name: 'Subscan',
url: 'https://alephzero.subscan.io/',
},
},
} as const;
1 change: 1 addition & 0 deletions packages/react/src/chains/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './aleph_zero.js';
export * from './evm.js';
export * from './evm.testnet.js';
export * from './solana.js';
Expand Down
392 changes: 392 additions & 0 deletions packages/react/src/connectors/aleph_zero/connector.ts
zmrp marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion packages/react/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
alephZero,
arbitrum,
avalanche,
base,
Expand Down Expand Up @@ -56,7 +57,7 @@ export const CHAIN_ID = {

// casper

// aleph_zero
aleph_zero: 'aleph_zero',

// testnets
// goerli: '5',
Expand Down Expand Up @@ -90,6 +91,9 @@ export const CHAIN_DATA: Record<ChainId, ChainData> = {
// [CHAIN_ID.tronShasta]: tronShasta,
// [CHAIN_ID.tronNile]: tronNile,

// aleph
[CHAIN_ID.aleph_zero]: alephZero,

// testnets
// 5: goerli,
// 80001: polygonMumbai,
Expand All @@ -116,4 +120,5 @@ export const DEFAULT_CHAINS = [
'polygon_zkevm',
'scroll',
'zksync',
'aleph_zero',
] as Chain[];
6 changes: 6 additions & 0 deletions packages/react/src/hooks/useAlephContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useContext } from 'react';
import { AlephContext } from '../providers/AlephProvider.js';

export const useAlephContext = () => {
return useContext(AlephContext);
};
10 changes: 10 additions & 0 deletions packages/react/src/hooks/useAlephStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useContext } from 'react';
import { useStore } from 'zustand';
import { AlephContext } from '../providers/AlephProvider.js';
import { AlephState } from '../store/Aleph.js';

export function useAlephStore<T>(selector: (state: AlephState) => T): T {
const { store } = useContext(AlephContext);
if (!store) throw new Error('Missing Aleph Provider in the tree');
return useStore(store, selector);
}
10 changes: 9 additions & 1 deletion packages/react/src/hooks/useConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useCallback } from 'react';
import { useConnect as useWagmiConnect } from 'wagmi';
import { ChainType } from '../types/index.js';
import { Wallet, WalletInstance } from '../types/wallet.js';
import { useAlephContext } from './useAlephContext.js';
import { useTronContext } from './useTronContext.js';
import { useWallets } from './useWallets.js';

Expand All @@ -13,12 +14,14 @@ export const useConnect = () => {
const { connectAsync: connectEVM } = useWagmiConnect();
const { connect: connectSolanaWallet } = useSolanaWallet();
const { connect: connectTronWallet } = useTronContext();
const { connect: connectAlephWallet } = useAlephContext();

const connectWallet = useCallback(
async (params: { walletId: string; chainType: ChainType }) => {
const walletInstance: Wallet | undefined = wallets[params.chainType].find(
(wallet) => wallet.id === params.walletId,
);
console.log('Connecting wallet', params, wallets[params.chainType], walletInstance);

if (!walletInstance) {
throw new Error('Wallet not found');
Expand All @@ -34,13 +37,16 @@ export const useConnect = () => {
await connectTronWallet(walletInstance.id);
} else if (params.chainType === 'evm') {
await connectEVM({ connector: walletInstance.connector as WalletInstance<'evm'> });
} else if (params.chainType === 'aleph_zero') {
console.log('Connecting to Aleph Zero', walletInstance);
await connectAlephWallet(walletInstance.name);
} else {
await walletInstance.connector.connect();
}

return { walletInstance, name: walletInstance.name, id: params.walletId };
},
[connectEVM, connectSolanaWallet, connectTronWallet, wallets],
[connectAlephWallet, connectEVM, connectSolanaWallet, connectTronWallet, wallets],
);

const mutation = useMutation({
Expand All @@ -54,6 +60,8 @@ export const useConnect = () => {
// },
});

// console.log("connectWallet connectWallet - ", connectWallet)

jayeshbhole-rp marked this conversation as resolved.
Show resolved Hide resolved
return {
connect: connectWallet,
isLoading: mutation.isPending,
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/hooks/useConnections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ConnectedWallet } from '../types/wallet.js';
export const useConnections = (type?: ChainType): ConnectedWallet[] => {
const connectedWalletsByChain = useWalletsStore((store) => store.connectedWalletsByChain);

console.log('connectedWalletsByChain - ', connectedWalletsByChain);

zmrp marked this conversation as resolved.
Show resolved Hide resolved
return useMemo(() => {
if (type) {
return Object.values(connectedWalletsByChain[type]) ?? [];
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/hooks/useDisconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback } from 'react';
import { useDisconnect as useEVMDisconnect } from 'wagmi';
import { ChainType } from '../types/index.js';
import { Wallet, WalletInstance } from '../types/wallet.js';
import { useAlephContext } from './useAlephContext.js';
import { useTronContext } from './useTronContext.js';
import { useWallets } from './useWallets.js';

Expand All @@ -12,6 +13,7 @@ export const useDisConnect = () => {
const { disconnectAsync: disconnectEVM } = useEVMDisconnect();
const { disconnect: disconnectSolanaWallet } = useSolanaWallet();
const { disconnect: disconnectTronWallet } = useTronContext();
const { disconnect: disconnectAlephWallet } = useAlephContext();

const disconnectWallet = useCallback(
async (params: { walletId: string; chainType: ChainType }) => {
Expand All @@ -33,13 +35,15 @@ export const useDisConnect = () => {
await disconnectTronWallet();
} else if (params.chainType === 'evm') {
await disconnectEVM({ connector: walletInstance.connector as WalletInstance<'evm'> });
} else if (params.chainType === 'aleph_zero') {
await disconnectAlephWallet();
} else {
await walletInstance.connector.connect();
}

return { walletInstance, name: walletInstance.name, id: params.walletId };
},
[disconnectEVM, disconnectSolanaWallet, disconnectTronWallet, wallets],
[disconnectAlephWallet, disconnectEVM, disconnectSolanaWallet, disconnectTronWallet, wallets],
);

const mutation = useMutation({
Expand Down
33 changes: 29 additions & 4 deletions packages/react/src/hooks/useWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useConnectors as useEVMConnectors } from 'wagmi';
import { ChainType } from '../types/index.js';
import { Wallet } from '../types/wallet.js';
import { isEVMWalletInstalled } from '../utils/isEVMWalletInstalled.js';
import { useAlephStore } from './useAlephStore.js';
import { useTangledConfig } from './useTangledConfig.js';
import { useTronStore } from './useTronStore.js';

Expand All @@ -18,6 +19,10 @@ export const useWallets = (): { [key in ChainType]: Wallet<key>[] } => {

const { wallets: solanaWallets } = useSolanaWallet();

// const alephConnectors = useAlephStore((state) => state.connectors);
const alephAdapter = useAlephStore((state) => state.connectedAdapter);
// console.log("aleconnectors - ", AlephConnectors)

const { connectors: configuredConnectors } = useTangledConfig();

const tronConnectors = useTronStore((state) => state.connectors);
Expand All @@ -30,7 +35,7 @@ export const useWallets = (): { [key in ChainType]: Wallet<key>[] } => {
icon: connector.icon ?? '',
type: 'evm',
installed: isEVMWalletInstalled(connector.id),
downloadUrl: undefined,
url: undefined,
}));
}, [evmConnectors]);

Expand All @@ -44,7 +49,7 @@ export const useWallets = (): { [key in ChainType]: Wallet<key>[] } => {
icon: wallet.adapter.icon,
type: 'solana',
installed: wallet.readyState !== 'NotDetected' && wallet.readyState !== 'Unsupported',
downloadUrl: wallet.adapter.url,
url: wallet.adapter.url,
}));

const suggested =
Expand All @@ -63,15 +68,35 @@ export const useWallets = (): { [key in ChainType]: Wallet<key>[] } => {
icon: connector.adapter.icon,
type: 'tron',
installed: connector.adapter.readyState !== 'NotFound' && connector.adapter.readyState !== 'Loading',
downloadUrl: connector.adapter.url,
url: connector.adapter.url,
}));
}, [tronConnectors]);

const extendedAlephWallets = useMemo<Wallet<'aleph_zero'>[]>(() => {
const walletList = alephAdapter?.walletsList;
// console.log('wallet list --- ', alephAdapter?.walletsFromRegistry);

const detected: Wallet<'aleph_zero'>[] =
alephAdapter?.walletsFromRegistry.map((wallet) => ({
id: wallet.slug.toLowerCase(),
name: wallet.name,
connector: alephAdapter,
icon: wallet.image.default,
type: 'aleph_zero',
installed: walletList?.find((w) => w.slug == wallet.slug)?.detected,
url: wallet.homepage,
})) ?? [];

return detected;
}, [alephAdapter]);

// console.log("extendedAlephWallets - ", extendedAlephWallets)

return {
evm: extendedEvmWallets,
solana: extendedSolanaWallets,
tron: extendedTronWallets,
aleph_zero: [],
aleph_zero: extendedAlephWallets,
bitcoin: [],
casper: [],
cosmos: [],
Expand Down
Loading
Loading