Skip to content

Commit

Permalink
chore: restructure shared and react to use /core package
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed Aug 16, 2024
1 parent b1c8bb5 commit f0b3f07
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 260 deletions.
2 changes: 1 addition & 1 deletion examples/queue-manager-demo/src/flows/rango/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
} from 'rango-types';

import { SUPPORTED_ETH_CHAINS as XDEFI_WALLET_SUPPORTED_EVM_CHAINS } from '@rango-dev/provider-xdefi';
import { readAccountAddress } from '@rango-dev/wallets-react';
import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core';
import {
Networks,
WalletTypes,
Expand Down
3 changes: 2 additions & 1 deletion examples/wallets-demo/src/components/List/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
Tooltip,
Typography,
} from '@rango-dev/ui';
import { readAccountAddress, useWallets } from '@rango-dev/wallets-react';
import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core';
import { useWallets } from '@rango-dev/wallets-react';
import { detectInstallLink, Networks } from '@rango-dev/wallets-shared';
import React, { useState } from 'react';
import './styles.css';
Expand Down
2 changes: 1 addition & 1 deletion examples/wallets-demo/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Network } from '@rango-dev/wallets-shared';
import type { BlockchainMeta } from 'rango-sdk';

import { readAccountAddress } from '@rango-dev/wallets-react';
import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core';
import { Networks } from '@rango-dev/wallets-shared';
import { isEvmBlockchain } from 'rango-sdk';

Expand Down
2 changes: 1 addition & 1 deletion queue-manager/rango-preset/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type {

import { warn } from '@rango-dev/logging-core';
import { Status } from '@rango-dev/queue-manager-core';
import { readAccountAddress } from '@rango-dev/wallets-core';
import { legacyReadAccountAddress as readAccountAddress } from '@rango-dev/wallets-core/legacy';
import {
getBlockChainNameFromId,
getEvmProvider,
Expand Down
2 changes: 1 addition & 1 deletion queue-manager/rango-preset/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
QueueDef,
QueueStorage,
} from '@rango-dev/queue-manager-core';
import type { ConnectResult } from '@rango-dev/wallets-core';
import type { LegacyConnectResult as ConnectResult } from '@rango-dev/wallets-core/legacy';
import type {
Meta,
Network,
Expand Down
8 changes: 8 additions & 0 deletions wallets/core/legacy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@rango-dev/wallets-core/legacy",
"type": "module",
"main": "../dist/legacy/mod.js",
"module": "../dist/legacy/mod.js",
"types": "../dist/legacy/mod.d.ts",
"sideEffects": false
}
19 changes: 11 additions & 8 deletions wallets/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
"version": "0.37.0",
"license": "MIT",
"type": "module",
"source": "./src/legacy/index.ts",
"main": "./dist/index.js",
"typings": "./dist/legacy/index.d.ts",
"source": "./src/mod.ts",
"main": "./dist/mod.js",
"typings": "./dist/mod.d.ts",
"exports": {
".": {
"types": "./dist/legacy/index.d.ts",
"default": "./dist/index.js"
"types": "./dist/mod.d.ts",
"default": "./dist/mod.js"
},
"./legacy": {
"types": "./dist/legacy/mod.d.ts",
"default": "./dist/legacy/mod.js"
}
},
"files": [
"dist",
"src"
],
"scripts": {
"build": "node ../../scripts/build/command.mjs --path wallets/core --inputs src/legacy/index.ts",
"build": "node ../../scripts/build/command.mjs --path wallets/core --inputs src/mod.ts,src/legacy/mod.ts",
"ts-check": "tsc --declaration --emitDeclarationOnly -p ./tsconfig.json",
"clean": "rimraf dist",
"format": "prettier --write '{.,src}/**/*.{ts,tsx}'",
Expand All @@ -29,10 +33,9 @@
"react-dom": "^17.0.0 || ^18.0.0"
},
"dependencies": {
"@rango-dev/wallets-shared": "^0.36.0",
"rango-types": "^0.1.69"
},
"publishConfig": {
"access": "public"
}
}
}
38 changes: 37 additions & 1 deletion wallets/core/src/legacy/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Network } from './types.js';
import type { Options } from './wallet.js';
import type { Network } from '@rango-dev/wallets-shared';
import type { BlockchainMeta } from 'rango-types';

import { Networks } from './types.js';

export function formatAddressWithNetwork(
address: string,
Expand Down Expand Up @@ -37,3 +40,36 @@ export function needsCheckInstallation(options: Options) {
const { checkInstallation = true } = options.config;
return checkInstallation;
}

export const getBlockChainNameFromId = (
chainId: string | number,
blockchains: BlockchainMeta[]
): Network | null => {
chainId =
typeof chainId === 'string' && chainId.startsWith('0x')
? parseInt(chainId)
: chainId;

/*
* Sometimes providers are passing `Network` as chainId.
* If chainId is a `Network`, we return itself.
*/
const allNetworks = Object.values(Networks);
if (allNetworks.includes(String(chainId) as Networks)) {
return chainId as Networks;
}

if (chainId === 'Binance-Chain-Tigris') {
return Networks.BINANCE;
}
return (
blockchains
.filter((blockchainMeta) => !!blockchainMeta.chainId)
.find((blockchainMeta) => {
const blockchainChainId = blockchainMeta.chainId?.startsWith('0x')
? parseInt(blockchainMeta.chainId)
: blockchainMeta.chainId;
return blockchainChainId == chainId;
})?.name || null
);
};
6 changes: 0 additions & 6 deletions wallets/core/src/legacy/index.ts

This file was deleted.

39 changes: 39 additions & 0 deletions wallets/core/src/legacy/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* All the exported types/values from legacy should be prefixed with `Legacy`
* since they will be removed soon and isn't part of the main interface for this package.
*/
export type {
EventHandler as LegacyEventHandler,
State as LegacyState,
Options as LegacyOptions,
} from './wallet.js';

export type {
Connect as LegacyConnect,
Disconnect as LegacyDisconnect,
Subscribe as LegacySubscribe,
CanEagerConnect as LegacyCanEagerConnect,
SwitchNetwork as LegacySwitchNetwork,
Suggest as LegacySuggest,
CanSwitchNetwork as LegacyCanSwitchNetwork,
NamespaceData as LegacyNamespaceData,
ProviderInterface as LegacyProviderInterface,
Network as LegacyNetwork,
WalletType as LegacyWalletType,
InstallObjects as LegacyInstallObjects,
WalletInfo as LegacyWalletInfo,
ConnectResult as LegacyConnectResult,
} from './types.js';

export {
Events as LegacyEvents,
Namespace as LegacyNamespace,
Networks as LegacyNetworks,
} from './types.js';

export { Persistor } from './persistor.js';
export {
readAccountAddress as legacyReadAccountAddress,
getBlockChainNameFromId as legacyGetBlockChainNameFromId,
} from './helpers.js';
export { default as LegacyWallet } from './wallet.js';
108 changes: 102 additions & 6 deletions wallets/core/src/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,108 @@
import type { State as WalletState } from './wallet.js';
import type {
NamespaceData,
Network,
WalletInfo,
WalletType,
} from '@rango-dev/wallets-shared';
import type { BlockchainMeta, SignerFactory } from 'rango-types';

export enum Networks {
BTC = 'BTC',
BSC = 'BSC',
LTC = 'LTC',
THORCHAIN = 'THOR',
BCH = 'BCH',
BINANCE = 'BNB',
ETHEREUM = 'ETH',
POLYGON = 'POLYGON',
TERRA = 'TERRA',
POLKADOT = '',
TRON = 'TRON',
DOGE = 'DOGE',
HARMONY = 'HARMONY',
AVAX_CCHAIN = 'AVAX_CCHAIN',
FANTOM = 'FANTOM',
MOONBEAM = 'MOONBEAM',
ARBITRUM = 'ARBITRUM',
BOBA = 'BOBA',
OPTIMISM = 'OPTIMISM',
FUSE = 'FUSE',
CRONOS = 'CRONOS',
SOLANA = 'SOLANA',
MOONRIVER = 'MOONRIVER',
GNOSIS = 'GNOSIS',
COSMOS = 'COSMOS',
OSMOSIS = 'OSMOSIS',
AXELAR = 'AXELAR',
MARS = 'MARS',
STRIDE = 'STRIDE',
MAYA = 'MAYA',
AKASH = 'AKASH',
IRIS = 'IRIS',
PERSISTENCE = 'PERSISTENCE',
SENTINEL = 'SENTINEL',
REGEN = 'REGEN',
CRYPTO_ORG = 'CRYPTO_ORG',
SIF = 'SIF',
CHIHUAHUA = 'CHIHUAHUA',
JUNO = 'JUNO',
KUJIRA = 'KUJIRA',
STARNAME = 'STARNAME',
COMDEX = 'COMDEX',
STARGAZE = 'STARGAZE',
DESMOS = 'DESMOS',
BITCANNA = 'BITCANNA',
SECRET = 'SECRET',
INJECTIVE = 'INJECTIVE',
LUMNETWORK = 'LUMNETWORK',
BANDCHAIN = 'BANDCHAIN',
EMONEY = 'EMONEY',
BITSONG = 'BITSONG',
KI = 'KI',
MEDIBLOC = 'MEDIBLOC',
KONSTELLATION = 'KONSTELLATION',
UMEE = 'UMEE',
STARKNET = 'STARKNET',
TON = 'TON',

// Using instead of null
Unknown = 'Unkown',
}

export enum Namespace {
Solana = 'Solana',
Evm = 'EVM',
Cosmos = 'Cosmos',
Utxo = 'UTXO',
Starknet = 'Starknet',
Tron = 'Tron',
}

export type NamespaceData = {
namespace: Namespace;
derivationPath?: string;
};

export type WalletType = string;
export type Network = string;

export type InstallObjects = {
CHROME?: string;
FIREFOX?: string;
EDGE?: string;
BRAVE?: string;
DEFAULT: string;
};

export type WalletInfo = {
name: string;
img: string;
installLink: InstallObjects | string;
color: string;
supportedChains: BlockchainMeta[];
showOnMobile?: boolean;
isContractWallet?: boolean;
mobileWallet?: boolean;
namespaces?: Namespace[];
singleNamespace?: boolean;
needsDerivationPath?: boolean;
};

export type State = {
[key: string]: WalletState | undefined;
};
Expand Down
13 changes: 5 additions & 8 deletions wallets/core/src/legacy/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import type {
GetInstanceOptions,
WalletActions,
WalletConfig,
} from './types.js';
import type {
NamespaceData,
Network,
WalletActions,
WalletConfig,
WalletType,
} from '@rango-dev/wallets-shared';
} from './types.js';
import type { BlockchainMeta } from 'rango-types';

import { getBlockChainNameFromId, Networks } from '@rango-dev/wallets-shared';

import {
accountAddressesWithNetwork,
getBlockChainNameFromId,
needsCheckInstallation,
} from './helpers.js';
import { Events } from './types.js';
import { Events, Networks } from './types.js';

export type EventHandler = (
type: WalletType,
Expand Down
1 change: 1 addition & 0 deletions wallets/core/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file will be updated to export Hub methods and types.
3 changes: 0 additions & 3 deletions wallets/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ export * from './legacy/helpers.js';
export { default as Provider } from './provider.js';
export { useWallets } from './legacy/hooks.js';
export * from './legacy/types.js';

export type { EventHandler } from '@rango-dev/wallets-core';
export { Events, readAccountAddress } from '@rango-dev/wallets-core';
12 changes: 6 additions & 6 deletions wallets/react/src/legacy/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type {
WalletActions,
WalletProviders,
} from './types.js';
import type Wallet from '@rango-dev/wallets-core';
import type {
Options,
EventHandler as WalletEventHandler,
State as WalletState,
} from '@rango-dev/wallets-core';
LegacyOptions as Options,
LegacyWallet as Wallet,
LegacyEventHandler as WalletEventHandler,
LegacyState as WalletState,
} from '@rango-dev/wallets-core/legacy';
import type { WalletConfig, WalletType } from '@rango-dev/wallets-shared';

import { Persistor } from '@rango-dev/wallets-core';
import { Persistor } from '@rango-dev/wallets-core/legacy';

import { LAST_CONNECTED_WALLETS } from './constants.js';

Expand Down
4 changes: 2 additions & 2 deletions wallets/react/src/legacy/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ProviderContext, WalletActions, WalletConfig } from './types.js';
import type { EventHandler as WalletEventHandler } from '@rango-dev/wallets-core';
import type { LegacyEventHandler as WalletEventHandler } from '@rango-dev/wallets-core/legacy';

import Wallet from '@rango-dev/wallets-core';
import { LegacyWallet as Wallet } from '@rango-dev/wallets-core/legacy';
import { useContext, useRef } from 'react';

import { WalletContext } from './context.js';
Expand Down
16 changes: 7 additions & 9 deletions wallets/react/src/legacy/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type {
EventHandler as WalletEventHandler,
State as WalletState,
} from '@rango-dev/wallets-core';
import type {
NamespaceData,
Network,
WalletInfo,
WalletType,
} from '@rango-dev/wallets-shared';
LegacyNamespaceData as NamespaceData,
LegacyNetwork as Network,
LegacyEventHandler as WalletEventHandler,
LegacyWalletInfo as WalletInfo,
LegacyState as WalletState,
LegacyWalletType as WalletType,
} from '@rango-dev/wallets-core/legacy';
import type { BlockchainMeta, SignerFactory } from 'rango-types';
import type { PropsWithChildren } from 'react';

Expand Down
Loading

0 comments on commit f0b3f07

Please sign in to comment.