diff --git a/lib/plugin.js b/lib/plugin.js index 7bd72fd..d5bb391 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -16,7 +16,6 @@ export default (ctx, inject) => { async initProvider(provider) { try { this.provider = provider - // this.web3 = new Web3(provider) await this._checkVersion() return await this._initProvider() @@ -25,6 +24,21 @@ export default (ctx, inject) => { } } + initWeb3(rpcUrl) { + try { + if (!rpcUrl) { + throw new Error(`Please set rpcUrl to params, current rpcUrl ${rpcUrl}`) + } + + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)) + this.web3 = web3 + + return web3 + } catch (err) { + throw new Error(`Provider method initWeb3 has error: ${err.message}`) + } + } + async sendRequest(params) { try { const request = (args) => (this.version === 'old' ? this._sendAsync(args) : this.provider.request(args)) @@ -182,7 +196,7 @@ export default (ctx, inject) => { const request = () => this.version === 'old' ? this.provider.enable() : this.sendRequest({ method: 'eth_requestAccounts' }) - const [account] = await request('') + const [account] = await request() if (!account) { throw new Error('Locked metamask') diff --git a/package.json b/package.json index 38fd673..2d52b39 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nuxtjs/provider", - "version": "0.0.8", + "version": "0.0.9", "description": "Provider integration with Nuxt.js", "repository": "provider-module", "license": "MIT", diff --git a/types/index.d.ts b/types/index.d.ts index d92ec21..fb4356c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -76,6 +76,8 @@ interface ProviderInstance { batchRequest(params: BatchRequestParams): Promise checkNetworkVersion(): Promise getWeb3(rpcUrl: string): Web3 + initWeb3(rpcUrl: string): Web3 + on(params: OnListenerParams): void }