Skip to content

Commit

Permalink
chore: wip embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed Aug 24, 2024
1 parent 3f93f1c commit 4e491e3
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 92 deletions.
6 changes: 1 addition & 5 deletions queue-manager/rango-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ export interface SwapQueueContext extends QueueContext {
switchNetwork: (
wallet: WalletType,
network: Network
) => Promise<ConnectResult | undefined> | undefined;
) => Promise<ConnectResult | ConnectResult[] | undefined> | undefined;
canSwitchNetworkTo: (type: WalletType, network: Network) => boolean;
connect: (
wallet: WalletType,
network: Network
) => Promise<ConnectResult> | undefined;
state: (type: WalletType) => WalletState;
isMobileWallet: (type: WalletType) => boolean;

Expand Down
4 changes: 4 additions & 0 deletions wallets/core/src/legacy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export enum Namespace {
Tron = 'Tron',
}

// TODO: Deprecated this.
export type NamespaceData = {
namespace: Namespace;
derivationPath?: string;
Expand Down Expand Up @@ -257,7 +258,9 @@ export type NamespacesWithDiscoverMode = Namespace | 'DISCOVER_MODE';
export type NamespaceWithDiscoverMode = {
namespace: 'DISCOVER_MODE';
network: string;
derivationPath?: string;
};
// TODO: Rename this?
export type NamespaceAndNetwork<T extends Namespace = Namespace> =
| {
/**
Expand All @@ -269,5 +272,6 @@ export type NamespaceAndNetwork<T extends Namespace = Namespace> =
* In some cases, we need to connect a specific network on a namespace. e.g. Polygon on EVM.
*/
network: NetworkTypeForNamespace<T>;
derivationPath?: string;
}
| NamespaceWithDiscoverMode;
7 changes: 7 additions & 0 deletions wallets/core/src/legacy/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ export type EventHandler = (
export type EventInfo = {
supportedBlockchains: BlockchainMeta[];
isContractWallet: boolean;
// This is for Hub and be able to make it compatible with legacy behavior.
isHub: boolean;
};

export interface State {
connected: boolean;
connecting: boolean;
/**
* @depreacted it always returns `false`. don't use it.
*/
reachable: boolean;
installed: boolean;
accounts: string[] | null;
Expand All @@ -57,6 +62,7 @@ class Wallet<InstanceType = any> {
this.info = {
supportedBlockchains: [],
isContractWallet: false,
isHub: false,
};
this.state = {
connected: false,
Expand Down Expand Up @@ -408,6 +414,7 @@ class Wallet<InstanceType = any> {
const eventInfo: EventInfo = {
supportedBlockchains: this.info.supportedBlockchains,
isContractWallet: this.info.isContractWallet,
isHub: false,
};
this.options.handler(
this.options.config.type,
Expand Down
72 changes: 38 additions & 34 deletions wallets/provider-all/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import * as tronLink from '@rango-dev/provider-tron-link';
import * as trustwallet from '@rango-dev/provider-trustwallet';
import * as walletconnect2 from '@rango-dev/provider-walletconnect-2';
import * as xdefi from '@rango-dev/provider-xdefi';
import {
legacyProviderImportsToVersionsInterface,
type Versions,
} from '@rango-dev/wallets-core/utils';
import { type WalletType, WalletTypes } from '@rango-dev/wallets-shared';

import { isWalletExcluded } from './helpers.js';
Expand All @@ -45,7 +49,7 @@ interface Options {
trezor?: TrezorEnvironments;
}

export const allProviders = (options?: Options) => {
export const allProviders = (options?: Options): Versions[] => {
const providers = options?.selectedProviders || [];

if (
Expand Down Expand Up @@ -75,38 +79,38 @@ export const allProviders = (options?: Options) => {
}

return [
safe,
defaultInjected,
metamask,
solflareSnap,
walletconnect2,
keplr,
phantom,
argentx,
tronLink,
trustwallet,
bitget,
enkrypt,
xdefi,
clover,
safepal,
brave,
coin98,
coinbase,
cosmostation,
exodus,
mathwallet,
okx,
tokenpocket,
tomo,
halo,
leapCosmos,
frontier,
taho,
braavos,
ledger,
rabby,
trezor,
solflare,
legacyProviderImportsToVersionsInterface(safe),
legacyProviderImportsToVersionsInterface(defaultInjected),
legacyProviderImportsToVersionsInterface(metamask),
legacyProviderImportsToVersionsInterface(solflareSnap),
legacyProviderImportsToVersionsInterface(walletconnect2),
legacyProviderImportsToVersionsInterface(keplr),
legacyProviderImportsToVersionsInterface(phantom),
legacyProviderImportsToVersionsInterface(argentx),
legacyProviderImportsToVersionsInterface(tronLink),
legacyProviderImportsToVersionsInterface(trustwallet),
legacyProviderImportsToVersionsInterface(bitget),
legacyProviderImportsToVersionsInterface(enkrypt),
legacyProviderImportsToVersionsInterface(xdefi),
legacyProviderImportsToVersionsInterface(clover),
legacyProviderImportsToVersionsInterface(safepal),
legacyProviderImportsToVersionsInterface(brave),
legacyProviderImportsToVersionsInterface(coin98),
legacyProviderImportsToVersionsInterface(coinbase),
legacyProviderImportsToVersionsInterface(cosmostation),
legacyProviderImportsToVersionsInterface(exodus),
legacyProviderImportsToVersionsInterface(mathwallet),
legacyProviderImportsToVersionsInterface(okx),
legacyProviderImportsToVersionsInterface(tokenpocket),
legacyProviderImportsToVersionsInterface(tomo),
legacyProviderImportsToVersionsInterface(halo),
legacyProviderImportsToVersionsInterface(leapCosmos),
legacyProviderImportsToVersionsInterface(frontier),
legacyProviderImportsToVersionsInterface(taho),
legacyProviderImportsToVersionsInterface(braavos),
legacyProviderImportsToVersionsInterface(ledger),
legacyProviderImportsToVersionsInterface(rabby),
legacyProviderImportsToVersionsInterface(trezor),
legacyProviderImportsToVersionsInterface(solflare),
];
};
10 changes: 5 additions & 5 deletions wallets/wallets-adapter/src/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {
ProviderInterface,
ProviderProps,
} from '@rango-dev/wallets-react';
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/legacy';
import type { ProviderProps } from '@rango-dev/wallets-react';

import { Provider } from '@rango-dev/wallets-react';
import React from 'react';
Expand All @@ -10,7 +8,9 @@ import Adapter from './adapter';

function AdapterProvider({ children, ...props }: ProviderProps) {
const list = props.providers.map(
(provider: ProviderInterface) => provider.config.type
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line
(provider: LegacyProviderInterface) => provider.config.type
);

return (
Expand Down
10 changes: 8 additions & 2 deletions widget/embedded/src/QueueManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ function QueueManager(props: PropsWithChildren<{ apiKey?: string }>) {
if (!canSwitchNetworkTo(wallet, network)) {
return undefined;
}
return connect(wallet, network);
const result = await connect(wallet, [
{
namespace: 'DISCOVER_MODE',
network,
},
]);

return result;
};

const isMobileWallet = (walletType: WalletType): boolean =>
Expand Down Expand Up @@ -88,7 +95,6 @@ function QueueManager(props: PropsWithChildren<{ apiKey?: string }>) {
providers: allProviders,
switchNetwork,
canSwitchNetworkTo,
connect,
state,
isMobileWallet,
};
Expand Down
7 changes: 6 additions & 1 deletion widget/embedded/src/components/SwapDetails/SwapDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export function SwapDetails(props: SwapDetailsProps) {
canSwitchNetworkTo(currentStepWallet.walletType, currentStepBlockchain));

const switchNetwork = showSwitchNetwork
? connect.bind(null, currentStepWallet.walletType, currentStepBlockchain)
? connect.bind(null, currentStepWallet.walletType, [
{
namespace: 'DISCOVER_MODE',
network: currentStepBlockchain,
},
])
: undefined;

const stepMessage = getSwapMessages(swap, currentStep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface SwapAlertsProps extends WaningAlertsProps {
}

export interface WaningAlertsProps extends FailedAlertsProps {
switchNetwork: (() => Promise<ConnectResult>) | undefined;
switchNetwork: (() => Promise<ConnectResult[]>) | undefined;
showNetworkModal: PendingSwapNetworkStatus | null | undefined;
setNetworkModal: (network: ModalState) => void;
}
Expand Down
28 changes: 18 additions & 10 deletions widget/embedded/src/containers/Wallets/Wallets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function Main(props: PropsWithChildren<PropTypes>) {
walletConnectListedDesktopWalletLink:
props.config.__UNSTABLE_OR_INTERNAL__
?.walletConnectListedDesktopWalletLink,
experimentalWallet: props.config.features?.experimentalWallet,
};
const { providers } = useWalletProviders(config.wallets, walletOptions);
const { connectWallet, disconnectWallet } = useWalletsStore();
Expand Down Expand Up @@ -85,6 +86,7 @@ function Main(props: PropsWithChildren<PropTypes>) {
meta.isContractWallet
);
if (data.length) {
console.log('EventHandler', { data });
connectWallet(data, findToken);
}
} else {
Expand All @@ -98,17 +100,17 @@ function Main(props: PropsWithChildren<PropTypes>) {
}
}
}
if (event === Events.ACCOUNTS && state.connected) {
if (
(event === Events.ACCOUNTS && state.connected) ||
// Hub works differently, and this check should be enough.
(event === Events.ACCOUNTS && meta.isHub)
) {
const key = `${type}-${state.network}-${value}`;

if (state.connected) {
if (!!onConnectWalletHandler.current) {
onConnectWalletHandler.current(key);
} else {
console.warn(
`onConnectWallet handler hasn't been set. Are you sure?`
);
}
if (!!onConnectWalletHandler.current) {
onConnectWalletHandler.current(key);
} else {
console.warn(`onConnectWallet handler hasn't been set. Are you sure?`);
}
}

Expand Down Expand Up @@ -146,7 +148,13 @@ function Main(props: PropsWithChildren<PropTypes>) {
allBlockChains={blockchains}
providers={providers}
onUpdateState={onUpdateState}
autoConnect={!!isActiveTab}>
autoConnect={!!isActiveTab}
configs={{
isExperimentalEnabled:
props.config.features?.experimentalWallet === 'enabled'
? true
: false,
}}>
{props.children}
</Provider>
</WidgetContext.Provider>
Expand Down
25 changes: 23 additions & 2 deletions widget/embedded/src/hooks/useStatefulConnect/useStatefulConnect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HandleConnectOptions, Result } from './useStatefulConnect.types';
import type { WalletInfoWithExtra } from '../../types';
import type { ExtendedModalWalletInfo } from '../../utils/wallets';
import type {
Namespace,
NamespaceData,
Expand Down Expand Up @@ -61,7 +62,10 @@ export function useStatefulConnect(): UseStatefulConnect {
});

try {
await connect(type, undefined, namespaces);
await connect(
type,
namespaces?.map((ns) => ({ ...ns, network: 'DISCOVER_MODE' }))
);

return { status: ResultStatus.Connected };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -75,14 +79,31 @@ export function useStatefulConnect(): UseStatefulConnect {
};

const handleConnect = async (
wallet: WalletInfoWithExtra,
wallet: ExtendedModalWalletInfo,
options?: HandleConnectOptions
): Promise<{
status: ResultStatus;
}> => {
const isDisconnected = wallet.state === WalletState.DISCONNECTED;

if (isDisconnected) {
const detachedInstances = wallet.properties?.find(
(item) => item.name === 'detached'
);
const hubCondition = detachedInstances && wallet.state !== 'connected';
if (hubCondition) {
dispatch({
type: 'needsNamespace',
payload: {
providerType: wallet.type,
providerImage: wallet.image,
availableNamespaces: wallet.namespaces,
singleNamespace: wallet.singleNamespace,
},
});
return { status: ResultStatus.Namespace };
}

if (!wallet.namespaces) {
return await runConnect(wallet.type, undefined, options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { WidgetConfig } from '../../types';
import type { ProvidersOptions } from '../../utils/providers';
import type { ProviderInterface } from '@rango-dev/wallets-react';

import { useEffect } from 'react';

Expand All @@ -14,10 +13,7 @@ export function useWalletProviders(
options?: ProvidersOptions
) {
const clearConnectedWallet = useWalletsStore.use.clearConnectedWallet();
let generateProviders: ProviderInterface[] = matchAndGenerateProviders(
providers,
options
);
let generateProviders = matchAndGenerateProviders(providers, options);

useEffect(() => {
clearConnectedWallet();
Expand Down
10 changes: 7 additions & 3 deletions widget/embedded/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Language, theme } from '@rango-dev/ui';
import type { ProviderInterface } from '@rango-dev/wallets-react';
import type { LegacyProviderInterface } from '@rango-dev/wallets-core/dist/legacy/mod';
import type { WalletType } from '@rango-dev/wallets-shared';
import type { Asset } from 'rango-sdk';

Expand Down Expand Up @@ -132,6 +132,9 @@ export type SignersConfig = {
*
* @property {'visible' | 'hidden'} [liquiditySource]
* - The visibility state for the liquiditySource feature. Optional property.
*
* @property {'disabled' | 'enabled'} [experimentalWallet]
* - Enable our experimental version of wallets. Default: disable on production, enabled on dev.
*/
export type Features = Partial<
Record<
Expand All @@ -143,7 +146,8 @@ export type Features = Partial<
'visible' | 'hidden'
>
> &
Partial<Record<'experimentalRoute', 'disabled' | 'enabled'>>;
Partial<Record<'experimentalRoute', 'disabled' | 'enabled'>> &
Partial<Record<'experimentalWallet', 'disabled' | 'enabled'>>;

export type TrezorManifest = {
appUrl: string;
Expand Down Expand Up @@ -218,7 +222,7 @@ export type WidgetConfig = {
from?: BlockchainAndTokenConfig;
to?: BlockchainAndTokenConfig;
liquiditySources?: string[];
wallets?: (WalletType | ProviderInterface)[];
wallets?: (WalletType | LegacyProviderInterface)[];
multiWallets?: boolean;
customDestination?: boolean;
defaultCustomDestinations?: { [blockchain: string]: string };
Expand Down
Loading

0 comments on commit 4e491e3

Please sign in to comment.