Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gmx-io/gmx-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jun 29, 2023
2 parents 7d5a259 + 180ec83 commit 3ba69f8
Show file tree
Hide file tree
Showing 15 changed files with 1,191 additions and 10 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
"@types/react-router-dom": "5.3.3",
"@uniswap/sdk-core": "3.0.1",
"@uniswap/v3-sdk": "3.9.0",
"@walletconnect/ethereum-provider": "^2.8.0",
"@walletconnect/modal": "^2.4.6",
"@web3-react/abstract-connector": "6.0.7",
"@web3-react/core": "6.1.9",
"@web3-react/injected-connector": "6.0.7",
"@web3-react/types": "6.0.7",
"@web3-react/walletconnect-connector": "6.2.8",
"bigdecimal": "0.6.1",
"classnames": "2.3.1",
Expand Down
9 changes: 5 additions & 4 deletions src/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import {
clearWalletConnectData,
clearWalletLinkData,
getInjectedHandler,
getWalletConnectHandler,
getWalletConnectV2Handler,
hasCoinBaseWalletExtension,
hasMetaMaskWalletExtension,
useEagerConnect,
Expand Down Expand Up @@ -198,8 +198,9 @@ function FullApp() {
};

const connectInjectedWallet = getInjectedHandler(activate, deactivate);
const activateWalletConnect = () => {
getWalletConnectHandler(activate, deactivate, setActivatingConnector)();

const activateWalletConnectV2 = () => {
getWalletConnectV2Handler(activate, deactivate, setActivatingConnector)();
};

const userOnMobileDevice = "navigator" in window && isMobileDevice(window.navigator);
Expand Down Expand Up @@ -594,7 +595,7 @@ function FullApp() {
<Trans>Coinbase Wallet</Trans>
</div>
</button>
<button className="Wallet-btn WalletConnect-btn" onClick={activateWalletConnect}>
<button className="Wallet-btn WalletConnect-btn" onClick={activateWalletConnectV2}>
<img src={walletConnectImg} alt="WalletConnect" />
<div>
<Trans>WalletConnect</Trans>
Expand Down
1 change: 1 addition & 0 deletions src/config/localStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const SELECTED_NETWORK_LOCAL_STORAGE_KEY = "SELECTED_NETWORK";
export const WALLET_CONNECT_LOCALSTORAGE_KEY = "walletconnect";
export const WALLET_CONNECT_V2_LOCALSTORAGE_KEY = "walletconnect_v2";
export const WALLET_LINK_LOCALSTORAGE_PREFIX = "-walletlink";
export const SHOULD_EAGER_CONNECT_LOCALSTORAGE_KEY = "eagerconnect";
export const CURRENT_PROVIDER_LOCALSTORAGE_KEY = "currentprovider";
Expand Down
125 changes: 125 additions & 0 deletions src/lib/wallets/WalletConnectV2.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { AbstractConnector } from "@web3-react/abstract-connector";
import { ConnectorUpdate } from "@web3-react/types";
import {
EthereumProvider,
EthereumProviderOptions,
} from "@walletconnect/ethereum-provider/dist/types/EthereumProvider";

export const URI_AVAILABLE = "URI_AVAILABLE";

export class UserRejectedRequestError extends Error {
public constructor() {
super();
this.name = this.constructor.name;
this.message = "The user rejected the request!";
}
}

export class WalletConnectConnectorV2 extends AbstractConnector {
private readonly config: EthereumProviderOptions;

public walletConnectProvider?: EthereumProvider;

constructor({
projectId,
showQrModal,
chains,
optionalChains,
rpcMap,
}: {
projectId: string;
showQrModal: boolean;
chains: number[];
optionalChains?: number[];
rpcMap: { [networkId: number]: string };
}) {
super();

this.config = {
chains,
optionalChains,
rpcMap,
projectId,
showQrModal,
qrModalOptions: {
enableExplorer: true,
chainImages: undefined,
themeMode: "dark",
themeVariables: {
"--w3m-font-family": '"Relative",sans-serif',
"--w3m-z-index": "1100",
},
},
};

this.handleChainChanged = this.handleChainChanged.bind(this);
this.handleAccountsChanged = this.handleAccountsChanged.bind(this);
this.handleDisconnect = this.handleDisconnect.bind(this);
}

private handleChainChanged(chainId: number | string): void {
this.emitUpdate({ chainId });
}

private handleAccountsChanged(accounts: string[]): void {
this.emitUpdate({ account: accounts[0] });
}

private handleDisconnect(): void {
this.emitDeactivate();
}

private handleDisplayURI = (uri: string): void => {
this.emit(URI_AVAILABLE, uri);
};

public async activate(): Promise<ConnectorUpdate> {
if (!this.walletConnectProvider) {
const walletConnectProviderFactory = await import("@walletconnect/ethereum-provider").then(
(m) => m?.default ?? m
);
this.walletConnectProvider = await walletConnectProviderFactory.init(this.config);
}

this.walletConnectProvider.on("chainChanged", this.handleChainChanged);
this.walletConnectProvider.on("accountsChanged", this.handleAccountsChanged);
this.walletConnectProvider.on("disconnect", this.handleDisconnect);
this.walletConnectProvider.on("display_uri", this.handleDisplayURI);
try {
const accounts = await this.walletConnectProvider.enable();
const defaultAccount = accounts[0];
return { provider: this.walletConnectProvider, account: defaultAccount };
} catch (error) {
if (error.message === "User closed the modal!") {
throw new UserRejectedRequestError();
}
throw error;
}
}

public async getProvider(): Promise<typeof this.walletConnectProvider> {
return this.walletConnectProvider;
}

public async getChainId(): Promise<number | string> {
return Promise.resolve(this.walletConnectProvider!.chainId);
}

public async getAccount(): Promise<null | string> {
return Promise.resolve(this.walletConnectProvider!.accounts).then((accounts: string[]): string => accounts[0]);
}

public deactivate() {
if (this.walletConnectProvider) {
this.walletConnectProvider.removeListener("disconnect", this.handleDisconnect);
this.walletConnectProvider.removeListener("chainChanged", this.handleChainChanged);
this.walletConnectProvider.removeListener("accountsChanged", this.handleAccountsChanged);
this.walletConnectProvider.removeListener("display_uri", this.handleDisplayURI);
this.walletConnectProvider.disconnect();
}
}

public async close() {
this.emitDeactivate();
}
}
47 changes: 47 additions & 0 deletions src/lib/wallets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SELECTED_NETWORK_LOCAL_STORAGE_KEY,
SHOULD_EAGER_CONNECT_LOCALSTORAGE_KEY,
WALLET_CONNECT_LOCALSTORAGE_KEY,
WALLET_CONNECT_V2_LOCALSTORAGE_KEY,
WALLET_LINK_LOCALSTORAGE_PREFIX,
} from "config/localStorage";
import {
Expand All @@ -28,6 +29,10 @@ import { helperToast } from "../helperToast";
import { t, Trans } from "@lingui/macro";

import { Web3ReactManagerFunctions } from "@web3-react/core/dist/types";
import {
UserRejectedRequestError as UserRejectedRequestErrorWalletConnectV2,
WalletConnectConnectorV2,
} from "./WalletConnectV2";

export type NetworkMetadata = {
chainId: string;
Expand Down Expand Up @@ -103,8 +108,25 @@ export const getWalletConnectConnector = () => {
});
};

export const getWalletConnectConnectorV2 = () => {
const chainId = Number(localStorage.getItem(SELECTED_NETWORK_LOCAL_STORAGE_KEY)) || DEFAULT_CHAIN_ID;
return new WalletConnectConnectorV2({
rpcMap: {
[AVALANCHE]: getRpcUrl(AVALANCHE)!,
[ARBITRUM]: getRpcUrl(ARBITRUM)!,
[ARBITRUM_TESTNET]: getRpcUrl(ARBITRUM_TESTNET)!,
[AVALANCHE_FUJI]: getRpcUrl(AVALANCHE_FUJI)!,
},
showQrModal: true,
chains: [chainId],
optionalChains: SUPPORTED_CHAIN_IDS,
projectId: "8fceb548bea9a92efcb7c0230d70011b",
});
};

export function clearWalletConnectData() {
localStorage.removeItem(WALLET_CONNECT_LOCALSTORAGE_KEY);
localStorage.removeItem(WALLET_CONNECT_V2_LOCALSTORAGE_KEY);
}

export function clearWalletLinkData() {
Expand Down Expand Up @@ -290,6 +312,31 @@ export const getWalletConnectHandler = (
return fn;
};

export const getWalletConnectV2Handler = (
activate: Web3ReactManagerFunctions["activate"],
deactivate: Web3ReactManagerFunctions["deactivate"],
setActivatingConnector: (connector?: WalletConnectConnectorV2) => void
) => {
const fn = async () => {
const walletConnect = getWalletConnectConnectorV2();
setActivatingConnector(walletConnect);
activate(walletConnect, (ex) => {
if (ex instanceof UnsupportedChainIdError) {
helperToast.error(t`Unsupported chain. Switch to Arbitrum network on your wallet and try again`);
// eslint-disable-next-line no-console
console.warn(ex);
} else if (!(ex instanceof UserRejectedRequestErrorWalletConnectV2)) {
helperToast.error(ex.message);
// eslint-disable-next-line no-console
console.warn(ex);
}
clearWalletConnectData();
deactivate();
});
};
return fn;
};

export const getInjectedHandler = (
activate: Web3ReactManagerFunctions["activate"],
deactivate: Web3ReactManagerFunctions["deactivate"]
Expand Down
5 changes: 5 additions & 0 deletions src/locales/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr ""
msgid "Unstaking..."
msgstr "Unstaken..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "Nicht unterstützte Chain. Wechsle auf deiner Wallet zum Arbitrum-Netzwerk und versuche es erneut"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "Wallet noch nicht verbunden"
msgid "WalletConnect"
msgstr "WalletConnect"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "Gewichtung"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr "Unstaking will burn <0>{0} Multiplier Points</0>. {1}"
msgid "Unstaking..."
msgstr "Unstaking..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "Wallet not yet connected"
msgid "WalletConnect"
msgstr "WalletConnect"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr "WalletConnect V2"

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "Weight"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr ""
msgid "Unstaking..."
msgstr "Destakeando..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "Cadena no soportada. Cambia a la red de Arbitrum en tu monedero y vuelve a intentarlo"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "Monedero no conectado todavía"
msgid "WalletConnect"
msgstr "WalletConnect"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "Peso"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr ""
msgid "Unstaking..."
msgstr "Arrêt du staking..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "Chaîne non supportée. Passez au réseau Arbitrum sur votre portefeuille et réessayez"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "Le portefeuille n'est pas encore connecté"
msgid "WalletConnect"
msgstr "WalletConnect"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "Poids"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/ja/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr ""
msgid "Unstaking..."
msgstr "アンステーク中..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "サポートされていないチェーンです。ウォレットをArbitrumネットワークに切り替えて再試行してください"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "ウォレット未接続"
msgid "WalletConnect"
msgstr "WalletConnect"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "ウェイト"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/ko/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,7 @@ msgstr ""
msgid "Unstaking..."
msgstr "언스테이킹중..."

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr "지원하지 않는 체인입니다. 지갑을 Arbitrum 네트워크로 변경하여 다시 시도해주세요"
Expand Down Expand Up @@ -3792,6 +3793,10 @@ msgstr "지갑이 아직 연동되지 않았습니다"
msgid "WalletConnect"
msgstr "지갑연결"

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr "가중치"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/pseudo/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,7 @@ msgstr ""
msgid "Unstaking..."
msgstr ""

#: src/lib/wallets/index.tsx
#: src/lib/wallets/index.tsx
msgid "Unsupported chain. Switch to Arbitrum network on your wallet and try again"
msgstr ""
Expand Down Expand Up @@ -3789,6 +3790,10 @@ msgstr ""
msgid "WalletConnect"
msgstr ""

#: src/App/App.js
#~ msgid "WalletConnect V2"
#~ msgstr ""

#: src/pages/Dashboard/DashboardV2.js
msgid "Weight"
msgstr ""
Expand Down
Loading

0 comments on commit 3ba69f8

Please sign in to comment.