From 96fcd285f5a45b40ed6ce7fbaace7c05ff0d8120 Mon Sep 17 00:00:00 2001 From: Art Date: Tue, 24 Jan 2023 15:02:17 +0100 Subject: [PATCH] chore: move token's web3 url connector to web3 lib (#2655) Co-authored-by: Dexter Co-authored-by: Dariusz Majcherczyk --- apps/token/src/lib/web3-connectors.ts | 7 +- libs/web3/src/index.ts | 2 + .../web3/src/lib/eip-1193-custom-bridge.ts | 69 +------------ libs/web3/src/lib/url-connector.ts | 98 ++----------------- 4 files changed, 16 insertions(+), 160 deletions(-) rename apps/token/src/lib/url-connector.ts => libs/web3/src/lib/eip-1193-custom-bridge.ts (56%) diff --git a/apps/token/src/lib/web3-connectors.ts b/apps/token/src/lib/web3-connectors.ts index 26f5415a0..62d9c266e 100644 --- a/apps/token/src/lib/web3-connectors.ts +++ b/apps/token/src/lib/web3-connectors.ts @@ -3,16 +3,17 @@ import type { Web3ReactHooks } from '@web3-react/core'; import { initializeConnector } from '@web3-react/core'; import { MetaMask } from '@web3-react/metamask'; import { WalletConnect } from '@web3-react/walletconnect'; -import { Url } from './url-connector'; import type { Connector } from '@web3-react/types'; import { ENV } from '../config/env'; +import { UrlConnector } from '@vegaprotocol/web3'; const [metamask, metamaskHooks] = initializeConnector( (actions) => new MetaMask(actions) ); -const [urlConnector, urlHooks] = initializeConnector( - (actions) => new Url(actions, ENV.localProviderUrl) +const [urlConnector, urlHooks] = initializeConnector( + (actions) => + new UrlConnector(actions, ENV.localProviderUrl, ENV.ethWalletMnemonic) ); export const createDefaultProvider = (providerUrl: string, chainId: number) => { diff --git a/libs/web3/src/index.ts b/libs/web3/src/index.ts index a682e1d88..6e966f55b 100644 --- a/libs/web3/src/index.ts +++ b/libs/web3/src/index.ts @@ -20,3 +20,5 @@ export * from './lib/web3-connectors'; export * from './lib/web3-connect-dialog'; export * from './lib/web3-connect-store'; export * from './lib/web3-container'; +export * from './lib/url-connector'; +export * from './lib/eip-1193-custom-bridge'; diff --git a/apps/token/src/lib/url-connector.ts b/libs/web3/src/lib/eip-1193-custom-bridge.ts similarity index 56% rename from apps/token/src/lib/url-connector.ts rename to libs/web3/src/lib/eip-1193-custom-bridge.ts index 4eb1521ce..d69b666ce 100644 --- a/apps/token/src/lib/url-connector.ts +++ b/libs/web3/src/lib/eip-1193-custom-bridge.ts @@ -1,12 +1,7 @@ -import { ethers, Wallet } from 'ethers'; -import { Connector } from '@web3-react/types'; - import { Eip1193Bridge } from '@ethersproject/experimental'; -import type { ConnectionInfo } from '@ethersproject/web'; -import type { Actions } from '@web3-react/types'; -import { ENV } from '../config/env'; +import { ethers } from 'ethers'; -export class CustomizedBridge extends Eip1193Bridge { +export class Eip1193CustomBridge extends Eip1193Bridge { // eslint-disable-next-line @typescript-eslint/no-explicit-any async sendAsync(...args: any) { console.debug('sendAsync called', ...args); @@ -89,63 +84,3 @@ export class CustomizedBridge extends Eip1193Bridge { } } } - -type url = string | ConnectionInfo; - -export class Url extends Connector { - /** {@inheritdoc Connector.provider} */ - public override provider: Eip1193Bridge | undefined; - - private eagerConnection?: Promise; - private url: url; - - /** - * @param url - An RPC url. - * @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed. - */ - constructor(actions: Actions, url: url, connectEagerly = false) { - super(actions); - - if (connectEagerly && typeof window === 'undefined') { - throw new Error( - 'connectEagerly = true is invalid for SSR, instead use the activate method in a useEffect' - ); - } - - this.url = url; - - if (connectEagerly) void this.activate(); - } - - private async isomorphicInitialize() { - if (this.eagerConnection) return this.eagerConnection; - - await (this.eagerConnection = import('@ethersproject/providers') - .then(({ JsonRpcProvider }) => JsonRpcProvider) - .then((JsonRpcProvider) => { - const provider = new JsonRpcProvider(this.url); - const privateKey = Wallet.fromMnemonic( - ENV.ethWalletMnemonic, - `m/44'/60'/0'/0/0` - ).privateKey; - const signer = new Wallet(privateKey, provider); - this.provider = new CustomizedBridge(signer, provider); - this.actions.update({ accounts: [signer.address], chainId: 1440 }); - })); - } - - /** {@inheritdoc Connector.activate} */ - public async activate(): Promise { - this.actions.startActivation(); - - await this.isomorphicInitialize(); - - try { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const chainId = await this.provider!.request({ method: 'eth_chainId' }); - this.actions.update({ chainId: Number(chainId) }); - } catch (error) { - this.actions.reportError(error as Error); - } - } -} diff --git a/libs/web3/src/lib/url-connector.ts b/libs/web3/src/lib/url-connector.ts index 247b27a64..e813d1e60 100644 --- a/libs/web3/src/lib/url-connector.ts +++ b/libs/web3/src/lib/url-connector.ts @@ -1,97 +1,15 @@ -import { ethers, Wallet } from 'ethers'; +import { Wallet } from 'ethers'; import { Connector } from '@web3-react/types'; -import { Eip1193Bridge } from '@ethersproject/experimental'; + import type { ConnectionInfo } from '@ethersproject/web'; import type { Actions } from '@web3-react/types'; import { initializeConnector } from '@web3-react/core'; - -export class CustomizedBridge extends Eip1193Bridge { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - async sendAsync(...args: any) { - console.debug('sendAsync called', ...args); - return this.send(...args); - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - override async send(...args: any) { - console.debug('send called', ...args); - const isCallbackForm = - typeof args[0] === 'object' && typeof args[1] === 'function'; - let callback; - let method; - let params; - if (isCallbackForm) { - callback = args[1]; - method = args[0].method; - params = args[0].params; - } else { - method = args[0]; - params = args[1]; - } - try { - // Hacky, https://github.com/ethers-io/ethers.js/issues/1683#issuecomment-1016227588 - - // If from is present on eth_call it errors, removing it makes the library set - // from as the connected wallet which works fine - if (params && params.length && params[0].from && method === 'eth_call') { - delete params[0].from; - } - let result; - // For sending a transaction if we call send it will error - // as it wants gasLimit in sendTransaction but hexlify sets the property gas - // to gasLimit which makes sensd transaction error. - // This has taken the code from the super method for sendTransaction and altered - // it slightly to make it work with the gas limit issues. - if ( - params && - params.length && - params[0].from && - method === 'eth_sendTransaction' - ) { - // Hexlify will not take gas, must be gasLimit, set this property to be gasLimit - params[0].gasLimit = params[0].gas; - delete params[0].gas; - // If from is present on eth_sendTransaction it errors, removing it makes the library set - // from as the connected wallet which works fine - delete params[0].from; - const req = ethers.providers.JsonRpcProvider.hexlifyTransaction( - params[0] - ); - // Hexlify sets the gasLimit property to be gas again and send transaction requires gasLimit - req['gasLimit'] = req['gas']; - delete req['gas']; - - if (!this.signer) { - throw new Error('No signer'); - } - - // Send the transaction - const tx = await this.signer.sendTransaction(req); - result = tx.hash; - } else { - // All other transactions the base class works for - result = await super.send(method, params); - } - console.debug('result received', method, params, result); - if (isCallbackForm) { - callback(null, { result }); - } else { - return result; - } - } catch (error) { - console.error(error); - if (isCallbackForm) { - callback(error, null); - } else { - throw error; - } - } - } -} +import type { Eip1193Bridge } from '@ethersproject/experimental'; +import { Eip1193CustomBridge } from './eip-1193-custom-bridge'; type url = string | ConnectionInfo; -export class Url extends Connector { +export class UrlConnector extends Connector { /** {@inheritdoc Connector.provider} */ public override provider: Eip1193Bridge | undefined; @@ -135,7 +53,7 @@ export class Url extends Connector { `m/44'/60'/0'/0/0` ).privateKey; const signer = new Wallet(privateKey, provider); - this.provider = new CustomizedBridge(signer, provider); + this.provider = new Eip1193CustomBridge(signer, provider); this.actions.update({ accounts: [signer.address], chainId: 1440 }); })); } @@ -164,7 +82,7 @@ export const initializeUrlConnector = ( walletMnemonic && localProviderUrl.length > 0 && walletMnemonic.length > 0 - ? initializeConnector( - (actions) => new Url(actions, localProviderUrl, walletMnemonic) + ? initializeConnector( + (actions) => new UrlConnector(actions, localProviderUrl, walletMnemonic) ) : null;