public getWalletConnector() {
const walletConnect = new WalletConnectConnector({
infuraId: this.config.blockchain.network.getInfuraToken(),
rpc: {
'1': `https://mainnet.infura.io/v3/${this.config.blockchain.network.getInfuraToken()}`,
// '137': 'https://polygon-mainnet.infura.io/v3/<API_KEY>',
},
supportedChainIds: this.config.blockchain.network.getSupportedNetworkIds(),
bridge: "https://bridge.walletconnect.org",
qrcode: true,
});
return walletConnect;
}
/**
* Selector for provider
* https://docs.cloud.coinbase.com/wallet-sdk/docs/web3-react#step-2-import-and-setup-web3reactprovider
* @param provider is the provided initialized
*/
public getLibrary(provider: ExternalProvider | JsonRpcFetchFunc) {
return new Web3Provider(provider);
}
π¨βπ» Modal Header where Button is located
We store the selected provider (Metamask or WalletConnect) in local storage, whatever the user chooses
export const Modal = () => {
const sendGoogleAnalyticsEvent = marketingService.googleAnalyticsService.makeEventTracker( "Wallet Profile");
// This must be called from inside a React Component
const { activate, active:isActive, error:networkError } = useWeb3React();
// @ts-ignore
let ref = useRef<HTMLDivElement>(null);
const dispatch = useDispatch();
useOutsideClick(ref, () => dispatch(appAC.setShowModal(false)));
/**
* https://stackoverflow.com/questions/68345619/how-to-keep-metamask-connection-to-the-ui-persistent-with-web3-react/70364470#70364470
* @param type the name of the type of provider to be persisted in local storage
*/
const setProvider = (type:string) => {
window.localStorage.setItem("provider", type);
};
/**
* Connects the user account from the provider
* https://docs.cloud.coinbase.com/wallet-sdk/docs/web3-react#connect-and-disconnect-from-wallet
*/
const activateWalletHandler = async (handlerName:string, handler:SupercashBlockchainWallet) => {
console.log(`Activation network: current networkError=${networkError} ... isActive=${isActive}`)
console.log(`Open wallet collector for '${handlerName}'`)
const connector = handler.getWalletConnector();
console.log(`Will active connector=${JSON.stringify(connector)}`)
return await activate(connector).then(() => {
//here we use activate to create the connection
console.log(`connected to ${handlerName}`)
}).catch((errorActivating) => {
console.log(`Error activating wallet connector '${handlerName}': ${errorActivating}`)
})
}
π¨βπ» Button Component with Activate Wallet Handler call
<div className={style.btns}>
{
buttons.map(({icon, label, handler}, index) => (
<button key={index}
onClick={async () => {
console.log(`Activating the connector '${label}'`);
await activateWalletHandler(label, handler).then(() => {
console.log(`Activated the connector '${label}'`)
dispatch(appAC.setConnect(true));
console.log(`Setting the provider as '${label}' on local storage`)
setProvider(label);
// Remove the modal
dispatch(appAC.setShowModal(false));
}).catch((reason) => {
console.log(`Error activating connector '${label}': ${reason}`)
});
// Send the event to google analytics in parallel
sendGoogleAnalyticsEvent("button clicked", label);
}}
>
{icon}
<p className={style.label}>{label}</p>
</button>
))
}
</div>
π¨ Metamask Works properly
It loads the configs properly
It loads the Metamask wallet
It loads the info
NOTE: networkError=undefined when calling Metamask
Activating the connector 'Metamask' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/landing/ppd-qal/static/js/main.1646c262.js)
any networkError=undefined or isActive=false main.1646c262.js:2:29543
Open wallet collector for 'Metamask' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/landing/ppd-qal/static/js/main.1646c262.js)
connected to Metamask [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/landing/ppd-qal/static/js/main.1646c262.js)
Activated the connector 'Metamask' main.1646c262.js:2:29543
Setting the provider as 'Metamask' on local storage
π WalletConnect doesn't load: ERROR: networkError=ReferenceError: Buffer is not defined
It goes through the same process
This is the log of the Wallet Config that's loaded
ERROR: networkError=ReferenceError: Buffer is not defined
How to solve this properly?
Activating the connector 'Walletconnect' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
any networkError=undefined or isActive=false main.1646c262.js:2:29543
Open wallet collector for 'Walletconnect' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
connected to Walletconnect [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
Activated the connector 'Walletconnect' main.1646c262.js:2:29543
Setting the provider as 'Walletconnect' on local storage
Reloading the page, and trying to get the same object, the error is as follows:
Activating the connector 'Walletconnect' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
any networkError=ReferenceError: Buffer is not defined ... isActive=false main.1646c262.js:2:29543
Open wallet collector for 'Walletconnect' [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
connected to Walletconnect [main.1646c262.js:2:29543](https://supercash.gitlab.io/products/blockchain/cro-cactus/web/cro-cactus-landing-page/ppd-qal/static/js/main.1646c262.js)
Activated the connector 'Walletconnect' main.1646c262.js:2:29543
Setting the provider as 'Walletconnect' on local storage
ποΈ package.json
NOTE: The reference from Coinbase suggests to install a couple of libraries
π Problem
It looks like the problem is somewhere at the following:
π¨βπ» WalletConnect Config
π¨βπ» Modal Header where Button is located
π¨βπ» Button Component with Activate Wallet Handler call
π¨ Metamask Works properly
π WalletConnect doesn't load: ERROR: networkError=ReferenceError: Buffer is not defined
ποΈ package.json