vega-frontend-monorepo/apps/token/src/lib/web3-connectors.ts
Dexter Edwards 994bb4cf2b
test: allow connecting to a local ganache node by signing transactions in browser (#595)
* test: allow connecting to a local ganache node by signing transactions in brower

* chore: update environment variables

* fix: disassocaition

* chore: remove redundant code

* chore: rewrite promise as async/await

* fix: approval amount too low

* chore: address PR comments

* test: fix tets

* revert changes to env file

* chore: changes as per pr comments
2022-06-24 10:07:05 +01:00

42 lines
1.3 KiB
TypeScript

import { ethers } from 'ethers';
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';
const [metamask, metamaskHooks] = initializeConnector<MetaMask>(
(actions) => new MetaMask(actions)
);
const [urlConnector, urlHooks] = initializeConnector<Url>(
(actions) => new Url(actions, ENV.localProviderUrl)
);
export const createDefaultProvider = (providerUrl: string, chainId: number) => {
return new ethers.providers.JsonRpcProvider(providerUrl, chainId);
};
export const createConnectors = (providerUrl: string, chainId: number) => {
if (isNaN(chainId)) {
throw new Error('Invalid Ethereum chain ID for environment');
}
const [walletconnect, walletconnectHooks] =
initializeConnector<WalletConnect>(
(actions) =>
new WalletConnect(actions, {
rpc: {
[chainId]: providerUrl,
},
}),
[chainId]
);
return [
ENV.urlConnect ? [urlConnector, urlHooks] : null,
[metamask, metamaskHooks],
[walletconnect, walletconnectHooks],
].filter(Boolean) as [Connector, Web3ReactHooks][];
};