Skip to content

Commit

Permalink
Merge pull request #58 from lidofinance/fix/reduce-wc-connectors-amount
Browse files Browse the repository at this point in the history
Fix: reduce WC connectors amount, reef-knot 1.4.2
  • Loading branch information
alx-khramov authored Jun 26, 2023
2 parents c029db1 + 70ce618 commit 8800283
Show file tree
Hide file tree
Showing 30 changed files with 236 additions and 1,538 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
},
"dependencies": {},
"resolutions": {
"@types/react": "17.0.53"
"@types/react": "17.0.53",
"@walletconnect/core": "^2.8.3",
"@walletconnect/ethereum-provider": "^2.8.3"
},
"engines": {
"node": ">=16.0.0"
Expand Down
6 changes: 6 additions & 0 deletions packages/connect-wallet-modal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/connect-wallet-modal

## 1.4.2

### Patch Changes

- Fix: rework getWalletConnectConnector and its usage (reduce the amount of WC connectors), remove WC v1 imports and usage

## 1.4.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion 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": "1.4.1",
"version": "1.4.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react';
import { helpers } from '@reef-knot/web3-react';
import { useReefKnotContext } from '@reef-knot/core-react';
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';
import { WalletConnectLegacyConnector } from 'wagmi/connectors/walletConnectLegacy';
import { InjectedConnector } from 'wagmi/connectors/injected';
import { WalletAdapterData } from '@reef-knot/types';
import {
ConnectBraveWallet,
Expand All @@ -25,8 +22,8 @@ import { WalletsModalForEthProps } from './types';
import { WALLET_IDS, WalletId } from '../../constants';

const walletsButtons: { [K in WalletId | string]: React.ComponentType } = {
Injected: ConnectInjected,
WalletConnect: ConnectWC,
injected: ConnectInjected,
walletConnect: ConnectWC,
[WALLET_IDS.METAMASK]: ConnectMetamask,
[WALLET_IDS.LEDGER]: ConnectLedger,
[WALLET_IDS.COINBASE]: ConnectCoinbase,
Expand Down Expand Up @@ -104,18 +101,7 @@ function getWalletsButtons(
(data) => data.walletId === walletId,
);
if (walletData) {
let connectorId = '';
// Multiple wagmi version can cause problems here (as objects will not directly equal)
// TODO: rework to a better solution
if (
walletData.connector instanceof WalletConnectLegacyConnector ||
walletData.connector instanceof WalletConnectConnector
) {
connectorId = 'WalletConnect';
}
if (walletData.connector instanceof InjectedConnector) {
connectorId = 'Injected';
}
const connectorId = walletData.connector.id;
return getWalletButton(connectorId, walletId, {
...commonProps,
...walletData,
Expand Down
25 changes: 8 additions & 17 deletions packages/connect-wallet-modal/src/connectButtons/ConnectWC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,19 @@ export const ConnectWC: FC<ConnectWCProps> = (props: ConnectWCProps) => {

useEffect(() => {
// BEGIN Handle WalletConnect v2 redirection (connection without QR modal)
const redirect = (uri: string) => {
if (WCURIRedirectLink)
setRedirectionWindowLocation(WCURIRedirectLink, uri);
};
let provider: any;
(async () => {
const provider = await WCURIConnector?.getProvider();
provider?.once('display_uri', (uri: string) => {
if (WCURIRedirectLink)
setRedirectionWindowLocation(WCURIRedirectLink, uri);
});
provider = await WCURIConnector?.getProvider();
provider?.on('display_uri', redirect);
})();
// END Handle WalletConnect v2 redirection (connection without QR modal)

// BEGIN Handle legacy WalletConnect v1 redirection (connection without QR modal)
// TODO: remove after migration to v2
const redirect = async () => {
if (WCURIConnector && WCURICondition && WCURIRedirectLink) {
const WCURI = (await WCURIConnector.getProvider())?.connector?.uri;
if (WCURI) setRedirectionWindowLocation(WCURIRedirectLink, WCURI);
}
};
WCURIConnector?.on('message', redirect);
// END Handle legacy WalletConnect v1 redirection (connection without QR modal)

return () => {
WCURIConnector?.off('message', redirect);
provider?.off('display_uri', redirect);
};
}, [WCURICondition, WCURIConnector, WCURIRedirectLink]);

Expand Down
9 changes: 9 additions & 0 deletions packages/reef-knot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# reef-knot

## 1.4.2

### Patch Changes

- Updated dependencies
- @reef-knot/connect-wallet-modal@1.4.2
- @reef-knot/wallets-helpers@1.1.2
- @reef-knot/wallets-list@1.4.2

## 1.4.1

### Patch 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": "1.4.1",
"version": "1.4.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,13 +41,13 @@
"lint": "eslint"
},
"dependencies": {
"@reef-knot/connect-wallet-modal": "1.4.1",
"@reef-knot/connect-wallet-modal": "1.4.2",
"@reef-knot/core-react": "1.4.1",
"@reef-knot/web3-react": "1.2.1",
"@reef-knot/ui-react": "1.0.3",
"@reef-knot/wallets-icons": "1.0.0",
"@reef-knot/wallets-list": "1.4.1",
"@reef-knot/wallets-helpers": "1.1.1",
"@reef-knot/wallets-list": "1.4.2",
"@reef-knot/wallets-helpers": "1.1.2",
"@reef-knot/types": "1.2.1"
}
}
6 changes: 6 additions & 0 deletions packages/wallets-helpers/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @reef-knot/wallets-helpers

## 1.1.2

### Patch Changes

- Fix: rework getWalletConnectConnector and its usage (reduce the amount of WC connectors), remove WC v1 imports and usage

## 1.1.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/wallets-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/wallets-helpers",
"version": "1.1.1",
"version": "1.1.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down
1 change: 0 additions & 1 deletion packages/wallets-helpers/src/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './walletConnectConnector';
export * from './walletConnectMobileLinks';
79 changes: 25 additions & 54 deletions packages/wallets-helpers/src/factories/walletConnectConnector.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,43 @@
import { WalletConnectLegacyConnector } from 'wagmi/connectors/walletConnectLegacy';
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect';
import { Chain } from 'wagmi/chains';
import { isValidHttpUrl } from '../utils';
import { walletConnectMobileLinks } from './walletConnectMobileLinks';

export const prepareWalletConnectRPC = (rpc: Record<number, string> = {}) => {
const BASE_URL = typeof window === 'undefined' ? '' : window.location.origin;
// adds BASE_URL to `rpc` object's string values
return Object.entries(rpc).reduce(
(result, [key, value]) => ({
...result,
[key]: isValidHttpUrl(value) ? value : BASE_URL + value,
}),
{} as typeof rpc,
);
};
let walletconnectConnector: WalletConnectConnector;
let walletconnectConnectorNoQR: WalletConnectConnector;

export const getWalletConnectConnector = ({
rpc,
projectId = '',
noMobileLinks = false,
qrcode = true,
v2: _v2 = false,
chains,
}: {
rpc?: Record<number, string>;
projectId?: string;
noMobileLinks?: boolean;
qrcode?: boolean;
v2?: boolean;
chains: Chain[];
}) => {
let v2EnabledByLS = false;
if (typeof window !== 'undefined') {
v2EnabledByLS =
window.localStorage.getItem('reefknot_wcv2_enable') === 'true';
}
// WalletConnect v2 will automatically replace legacy v1 after this date:
const v2TransitionDate = new Date('2023-06-24T00:00:00Z');
const v2 = _v2 || v2EnabledByLS || new Date() > v2TransitionDate;
if (v2) {
return new WalletConnectConnector({
chains,
options: {
projectId,
showQrModal: qrcode,
qrModalOptions: {
// @walletconnect library currently requires the "chainImages" option, looks like their mistake
chainImages: undefined,
themeVariables: {
'--w3m-z-index': '1000',
// @ts-expect-error walletconnect suddenly renamed w3m to wcm in @walletconnect/modal v2.5
// It is a breaking change in a minor version update. So now I have to support both options here
'--wcm-z-index': '1000',
},
},
},
});
}
return new WalletConnectLegacyConnector({
const params = {
chains,
options: {
rpc: prepareWalletConnectRPC(rpc),
qrcode,
qrcodeModalOptions: {
mobileLinks: noMobileLinks ? [] : walletConnectMobileLinks,
desktopLinks: [],
projectId,
showQrModal: qrcode,
qrModalOptions: {
themeVariables: {
'--w3m-z-index': '1000',
// walletconnect suddenly renamed w3m to wcm in @walletconnect/modal v2.5
// It is a breaking change in a minor version update. So now I have to support both options here
'--wcm-z-index': '1000',
},
},
},
});
};

if (!qrcode) {
if (!walletconnectConnectorNoQR) {
walletconnectConnectorNoQR = new WalletConnectConnector(params);
}
return walletconnectConnectorNoQR;
}

if (!walletconnectConnector) {
walletconnectConnector = new WalletConnectConnector(params);
}
return walletconnectConnector;
};
45 changes: 0 additions & 45 deletions packages/wallets-helpers/src/factories/walletConnectMobileLinks.ts

This file was deleted.

11 changes: 11 additions & 0 deletions packages/wallets-list/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @reef-knot/wallets-list

## 1.4.2

### Patch Changes

- Updated dependencies
- @reef-knot/wallet-adapter-ambire@1.2.2
- @reef-knot/wallet-adapter-blockchaincom@1.2.2
- @reef-knot/wallet-adapter-walletconnect@1.2.2
- @reef-knot/wallet-adapter-zengo@1.2.2
- @reef-knot/wallet-adapter-zerion@1.2.2

## 1.4.1

### Patch Changes
Expand Down
12 changes: 6 additions & 6 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": "1.4.1",
"version": "1.4.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -41,11 +41,11 @@
"@reef-knot/wallet-adapter-taho": "1.2.1",
"@reef-knot/wallet-adapter-okx": "1.2.1",
"@reef-knot/wallet-adapter-phantom": "1.2.1",
"@reef-knot/wallet-adapter-walletconnect": "1.2.1",
"@reef-knot/wallet-adapter-blockchaincom": "1.2.1",
"@reef-knot/wallet-adapter-zerion": "1.2.1",
"@reef-knot/wallet-adapter-zengo": "1.2.1",
"@reef-knot/wallet-adapter-ambire": "1.2.1"
"@reef-knot/wallet-adapter-walletconnect": "1.2.2",
"@reef-knot/wallet-adapter-blockchaincom": "1.2.2",
"@reef-knot/wallet-adapter-zerion": "1.2.2",
"@reef-knot/wallet-adapter-zengo": "1.2.2",
"@reef-knot/wallet-adapter-ambire": "1.2.2"
},
"devDependencies": {
"@reef-knot/types": "^1.2.1"
Expand Down
8 changes: 8 additions & 0 deletions packages/wallets/ambire/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @reef-knot/wallet-adapter-ambire

## 1.2.2

### Patch Changes

- Fix: rework getWalletConnectConnector and its usage (reduce the amount of WC connectors), remove WC v1 imports and usage
- Updated dependencies
- @reef-knot/wallets-helpers@1.1.2

## 1.2.1

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/wallets/ambire/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reef-knot/wallet-adapter-ambire",
"version": "1.2.1",
"version": "1.2.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
Expand Down Expand Up @@ -33,12 +33,12 @@
},
"devDependencies": {
"@reef-knot/types": "^1.2.1",
"@reef-knot/wallets-helpers": "^1.1.1",
"@reef-knot/wallets-helpers": "^1.1.2",
"@svgr/rollup": "^6.5.1"
},
"peerDependencies": {
"wagmi": "^0.12.17",
"@reef-knot/wallets-helpers": "^1.1.1",
"@reef-knot/wallets-helpers": "^1.1.2",
"@reef-knot/types": "^1.2.1"
}
}
Loading

0 comments on commit 8800283

Please sign in to comment.