Skip to content

Commit

Permalink
Merge pull request #161 from lidofinance/fix/binance-wallet-follow-up-1
Browse files Browse the repository at this point in the history
Follow-up fixes for Binance wallet
  • Loading branch information
alx-khramov authored Aug 27, 2024
2 parents 9a8fb23 + bc22e29 commit 17fff8b
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 76 deletions.
7 changes: 7 additions & 0 deletions packages/connect-wallet-modal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @reef-knot/connect-wallet-modal

## 5.3.1

### Patch Changes

- Binance wallet: move up in the wallets display order, add deeplink, add detector fn
- @reef-knot/wallets-list@2.2.1

## 5.3.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/connect-wallet-modal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/connect-wallet-modal",
"version": "5.3.0",
"version": "5.3.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,7 +41,7 @@
"@ledgerhq/hw-app-eth": "^6.37.1",
"@ledgerhq/hw-transport-webhid": "^6.29.0",
"@lidofinance/lido-ui": "^3.18.0",
"@reef-knot/wallets-list": "^2.2.0",
"@reef-knot/wallets-list": "^2.2.1",
"@types/react": "18.2.45",
"@types/react-dom": "18.2.17"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const WALLETS_DISPLAY_CONFIG_DEFAULT: WalletsModalEthProps['walletsShown'] = [
'ledgerHID',
'ledgerLive',
'walletConnect',
'binanceWallet',
'coinbase',
'trust',
'okx',
Expand All @@ -41,7 +42,6 @@ const WALLETS_DISPLAY_CONFIG_DEFAULT: WalletsModalEthProps['walletsShown'] = [
'ambire',
'safe',
'dappBrowserInjected',
'binanceWallet',
];

const WALLETS_PINNED_CONFIG_DEFAULT: WalletsModalEthProps['walletsPinned'] = [
Expand Down
6 changes: 6 additions & 0 deletions packages/core-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/core-react

## 4.2.1

### Patch Changes

- Remove unsupported chain check from eager connect

## 4.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/core-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/core-react",
"version": "4.2.0",
"version": "4.2.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
72 changes: 9 additions & 63 deletions packages/core-react/src/hooks/useEagerConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,19 @@ import { useConfig } from 'wagmi';
import { useReefKnotContext } from './useReefKnotContext';
import { useReefKnotModal } from './useReefKnotModal';

import { connect, disconnect } from 'wagmi/actions';
import { getUnsupportedChainError } from '../helpers/getUnsupportedChainError';
import { connect } from 'wagmi/actions';
import { checkTermsAccepted } from '../helpers/checkTermsAccepted';

import type { WalletAdapterData } from '@reef-knot/types';
import type { Chain } from 'wagmi/chains';
import type { Config, CreateConnectorFn, ConnectReturnType } from '@wagmi/core';
import type { Config, ConnectReturnType } from '@wagmi/core';
import type { ReefKnotModalContextValue } from '../context';

type ConnectResult = ConnectReturnType;

const connectToAdapter = async (
config: Config,
connector: CreateConnectorFn,
supportedChains: readonly [Chain, ...Chain[]],
) => {
const connectResult = await connect(config, { connector });

if (
connectResult &&
supportedChains.findIndex(({ id }) => id === connectResult.chainId) === -1
) {
// No errors during connection, but the chain is unsupported.
// This case is considered as error for now, and we explicitly call disconnect() here.
// This logic comes from previously used web3-react connection logic, which wasn't reworked yet after web3-react removal.
// web3-react logic was: if a chain is unsupported – break the connection, throw an error
// wagmi logic is: if a chain is unsupported – connect anyway, without errors.
// So, here we are trying to mimic the legacy logic, because we are not ready to rework it yet.
const connectError = getUnsupportedChainError(supportedChains);
await disconnect(config);

// A user may change a chain in a wallet app, prepare for that event.
// There is a strong recommendation in the MetaMask documentation
// to reload the page upon chain changes, unless there is a good reason not to.
// This looks like a good general approach.

if (config.state.current) {
const activeConnector = config.state.connections.get(
config.state.current,
)?.connector;
if (activeConnector) {
const provider: any = await activeConnector.getProvider();
provider.once('chainChanged', () =>
globalThis.window?.location.reload(),
);
}
}

throw connectError;
}

return connectResult;
};

export const connectEagerly = async (
config: Config,
adapters: WalletAdapterData[],
openModalAsync: ReefKnotModalContextValue['openModalAsync'],
supportedChains: readonly [Chain, ...Chain[]],
): Promise<ConnectResult | null> => {
const isTermsAccepted = checkTermsAccepted();

Expand All @@ -70,11 +24,9 @@ export const connectEagerly = async (
// wallet is detected
let connectionResult: ConnectResult | null = null;
const tryConnection = async () => {
const result = await connectToAdapter(
config,
adapter.createConnectorFn,
supportedChains,
);
const result = await connect(config, {
connector: adapter.createConnectorFn,
});
connectionResult = result;
return result;
};
Expand All @@ -94,7 +46,7 @@ export const connectEagerly = async (
try {
await tryConnection();
} catch (e) {
// when failed open terms modal,allow user to see error and retry from it
// when failed, open the terms modal, allow user to see the error and retry the connection
await openModalAsync({
type: 'eager',
props: {
Expand All @@ -104,7 +56,6 @@ export const connectEagerly = async (
});
}
}
// TS doesn't know we assigned in tryConnection
return connectionResult;
}
}
Expand All @@ -114,20 +65,15 @@ export const connectEagerly = async (
export const useEagerConnect = () => {
const config = useConfig();
const { openModalAsync } = useReefKnotModal();
const { walletDataList, chains } = useReefKnotContext();
const { walletDataList } = useReefKnotContext();

const eagerConnect = useCallback(() => {
const autoConnectOnlyAdapters = walletDataList.filter(
({ autoConnectOnly }) => autoConnectOnly,
);

return connectEagerly(
config,
autoConnectOnlyAdapters,
openModalAsync,
chains,
);
}, [openModalAsync, walletDataList, chains, config]);
return connectEagerly(config, autoConnectOnlyAdapters, openModalAsync);
}, [openModalAsync, walletDataList, config]);

return { eagerConnect };
};
10 changes: 10 additions & 0 deletions packages/reef-knot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# reef-knot

## 5.5.1

### Patch Changes

- Binance wallet: move up in the wallets display order, add deeplink, add detector fn
- Updated dependencies
- @reef-knot/connect-wallet-modal@5.3.1
- @reef-knot/wallets-list@2.2.1
- @reef-knot/core-react@4.2.1

## 5.5.0

### Minor Changes
Expand Down
8 changes: 4 additions & 4 deletions packages/reef-knot/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reef-knot",
"version": "5.5.0",
"version": "5.5.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,11 +41,11 @@
"lint": "eslint --ext ts,tsx,js,mjs ."
},
"dependencies": {
"@reef-knot/connect-wallet-modal": "5.3.0",
"@reef-knot/core-react": "4.2.0",
"@reef-knot/connect-wallet-modal": "5.3.1",
"@reef-knot/core-react": "4.2.1",
"@reef-knot/web3-react": "4.0.1",
"@reef-knot/ui-react": "2.1.3",
"@reef-knot/wallets-list": "2.2.0",
"@reef-knot/wallets-list": "2.2.1",
"@reef-knot/wallets-helpers": "2.1.0",
"@reef-knot/types": "2.1.0",
"@reef-knot/ledger-connector": "4.1.0"
Expand Down
7 changes: 7 additions & 0 deletions packages/wallets-list/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @reef-knot/wallets-list

## 2.2.1

### Patch Changes

- Updated dependencies
- @reef-knot/wallet-adapter-binance-wallet@1.0.1

## 2.2.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/wallets-list/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/wallets-list",
"version": "2.2.0",
"version": "2.2.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -43,7 +43,7 @@
"@reef-knot/wallet-adapter-exodus": "2.1.0",
"@reef-knot/wallet-adapter-walletconnect": "2.0.1",
"@reef-knot/wallet-adapter-ambire": "2.0.1",
"@reef-knot/wallet-adapter-binance-wallet": "1.0.0",
"@reef-knot/wallet-adapter-binance-wallet": "1.0.1",
"@reef-knot/wallet-adapter-bitkeep": "2.1.0",
"@reef-knot/wallet-adapter-coin98": "2.1.0",
"@reef-knot/wallet-adapter-brave": "2.1.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/wallets/binance-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/wallet-adapter-binance-wallet

## 1.0.1

### Patch Changes

- Binance wallet: move up in the wallets display order, add deeplink, add detector fn

## 1.0.0

### Minor Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/wallets/binance-wallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/wallet-adapter-binance-wallet",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -43,6 +43,7 @@
"wagmi": "2.10.2"
},
"dependencies": {
"@binance/w3w-wagmi-connector-v2": "^1.2.3"
"@binance/w3w-wagmi-connector-v2": "^1.2.3",
"@binance/w3w-utils": "^1.1.6"
}
}
14 changes: 13 additions & 1 deletion packages/wallets/binance-wallet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { WalletAdapterType } from '@reef-knot/types';
import { WalletIcon } from './icons/index.js';
import { getWagmiConnectorV2 } from '@binance/w3w-wagmi-connector-v2';
import { isInBinance, getDeepLink } from '@binance/w3w-utils';

export const id = 'binanceWallet';
export const name = 'Binance Web3 Wallet';

export const BinanceWeb3Wallet: WalletAdapterType = () => {
const deeplinkDAppUrl = globalThis.window
? globalThis.window.location.host + globalThis.window.location.pathname
: '';

export const BinanceWeb3Wallet: WalletAdapterType = ({ defaultChain }) => {
const binanceWalletConnector = getWagmiConnectorV2();

const deeplink =
typeof window !== 'undefined'
? getDeepLink(deeplinkDAppUrl, defaultChain.id).http
: undefined;

return {
walletId: id,
walletName: name,
type: binanceWalletConnector.type,
icon: WalletIcon,
createConnectorFn: binanceWalletConnector(),
detector: isInBinance,
deeplink,
};
};
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,16 @@
hash.js "^1.1.7"
js-base64 "^3.7.5"

"@binance/w3w-utils@^1.1.6":
version "1.1.6"
resolved "https://registry.yarnpkg.com/@binance/w3w-utils/-/w3w-utils-1.1.6.tgz#0212e06efc71f3644601c9ca4ee7c841080fbcf2"
integrity sha512-NGT629vS9tRlbigtNn9wHtTYNB00oyDcsajO/kpAcDiQn4ktYs7+oTIr/qLvjP8Z3opTXpbooqMPITDY7DI0IA==
dependencies:
"@binance/w3w-types" "1.1.4"
eventemitter3 "^5.0.0"
hash.js "^1.1.7"
js-base64 "^3.7.5"

"@binance/w3w-wagmi-connector-v2@^1.2.3":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@binance/w3w-wagmi-connector-v2/-/w3w-wagmi-connector-v2-1.2.3.tgz#bd7733f8a12d2ee1bc348407198243244011f951"
Expand Down

0 comments on commit 17fff8b

Please sign in to comment.