chore: move token's web3 url connector to web3 lib (#2655)
Co-authored-by: Dexter <dexter.edwards93@gmail.com> Co-authored-by: Dariusz Majcherczyk <dariusz.majcherczyk@gmail.com>
This commit is contained in:
committed by
Dariusz Majcherczyk
co-authored by
Dexter
Dariusz Majcherczyk
parent
e82e4f91ac
commit
96fcd285f5
@@ -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<MetaMask>(
|
||||
(actions) => new MetaMask(actions)
|
||||
);
|
||||
|
||||
const [urlConnector, urlHooks] = initializeConnector<Url>(
|
||||
(actions) => new Url(actions, ENV.localProviderUrl)
|
||||
const [urlConnector, urlHooks] = initializeConnector<UrlConnector>(
|
||||
(actions) =>
|
||||
new UrlConnector(actions, ENV.localProviderUrl, ENV.ethWalletMnemonic)
|
||||
);
|
||||
|
||||
export const createDefaultProvider = (providerUrl: string, chainId: number) => {
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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<void>;
|
||||
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<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Url>(
|
||||
(actions) => new Url(actions, localProviderUrl, walletMnemonic)
|
||||
? initializeConnector<UrlConnector>(
|
||||
(actions) => new UrlConnector(actions, localProviderUrl, walletMnemonic)
|
||||
)
|
||||
: null;
|
||||
|
||||
Reference in New Issue
Block a user