web3-react icon indicating copy to clipboard operation
web3-react copied to clipboard

Add Torus connector

Open flux0uz opened this issue 3 years ago • 6 comments

It would be great to bring back the connector for Torus.

flux0uz avatar Mar 15 '22 10:03 flux0uz

for reference: https://github.com/NoahZinsmeister/web3-react#adding-connectors

NoahZinsmeister avatar Mar 15 '22 15:03 NoahZinsmeister

I try to add torus connector and i probably made something wrong but i don't understand why, when activate is called useAccount and provider is always undefined some one can help me :)

` import { TorusParams } from '@toruslabs/torus-embed' import type { Actions, AddEthereumChainParameter, Provider, ProviderConnectInfo, ProviderRpcError, } from '@web3-react/types'

import { Connector } from '@web3-react/types'

interface TorusConnectorArguments { chainId: number initOptions?: TorusParams constructorOptions?: TorusCtorArgs loginOptions?: any } interface TorusCtorArgs { buttonPosition?: 'top-left' | 'top-right' | 'bottom-right' | 'bottom-left' }

export class TorusConnector extends Connector { /** {@inheritdoc Connector.provider} */ public provider: Provider | undefined private readonly chainId: number private readonly initOptions: TorusParams private readonly constructorOptions: TorusCtorArgs private readonly loginOptions: any public torus: any constructor( actions: Actions, { chainId, constructorOptions = {}, initOptions = {} }: TorusConnectorArguments ) { super(actions) this.chainId = chainId this.constructorOptions = constructorOptions this.initOptions = initOptions } // the connected property, is bugged, but this works as a hack to check connection status public async activate(): Promise { this.actions.startActivation() if (!this.provider) { const Torus = await import('@toruslabs/torus-embed').then((m) => m?.default ?? m) this.torus = new Torus(this.constructorOptions) await this.torus.init(this.initOptions) await this.torus.ethereum.enable() this.provider = this.torus.ethereum } if (this.provider) return Promise.all([this.provider.request({ method: 'eth_accounts' }) as Promise<string[]>]) .then(([accounts]) => { if (accounts.length) { this.actions.update({ chainId: parseChainId(this.chainId), accounts }) } else { this.actions.reportError(new Error('No accounts returned')) } }) .catch((error) => { console.debug('Could not connect eagerly', error) this.actions.startActivation() }) } } function parseChainId(chainId: string | number) { return typeof chainId === 'number' ? chainId : Number.parseInt(chainId, chainId.startsWith('0x') ? 16 : 10) } `

OlremProj avatar Apr 21 '22 14:04 OlremProj

Sorry for the issue finaly it's works my import package in the client didn't update correctly now it sync

OlremProj avatar Apr 21 '22 15:04 OlremProj

Will provide a PR for this @NoahZinsmeister!

YZhenY avatar Apr 26 '22 14:04 YZhenY

My connector finally works. I need to add test and adapt type with abstract connector to be compliance to your practice manifest but if it can help to go faster I can send PR. It will allow me to import one npm package web3-react and not web3-react and my custom connector to handle torus. Thanks for your answer, best regards.

OlremProj avatar Apr 26 '22 16:04 OlremProj

PR

OlremProj avatar Apr 27 '22 11:04 OlremProj