feat: add init web3 method

This commit is contained in:
Nik Dement'ev 2020-11-18 11:18:02 +03:00
parent 0ab416ea58
commit 2649be0d65
No known key found for this signature in database
GPG Key ID: 769B05D57CF16FE2
3 changed files with 19 additions and 3 deletions

View File

@ -16,7 +16,6 @@ export default (ctx, inject) => {
async initProvider(provider) { async initProvider(provider) {
try { try {
this.provider = provider this.provider = provider
// this.web3 = new Web3(provider)
await this._checkVersion() await this._checkVersion()
return await this._initProvider() 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) { async sendRequest(params) {
try { try {
const request = (args) => (this.version === 'old' ? this._sendAsync(args) : this.provider.request(args)) const request = (args) => (this.version === 'old' ? this._sendAsync(args) : this.provider.request(args))
@ -182,7 +196,7 @@ export default (ctx, inject) => {
const request = () => const request = () =>
this.version === 'old' ? this.provider.enable() : this.sendRequest({ method: 'eth_requestAccounts' }) this.version === 'old' ? this.provider.enable() : this.sendRequest({ method: 'eth_requestAccounts' })
const [account] = await request('') const [account] = await request()
if (!account) { if (!account) {
throw new Error('Locked metamask') throw new Error('Locked metamask')

View File

@ -1,6 +1,6 @@
{ {
"name": "@nuxtjs/provider", "name": "@nuxtjs/provider",
"version": "0.0.8", "version": "0.0.9",
"description": "Provider integration with Nuxt.js", "description": "Provider integration with Nuxt.js",
"repository": "provider-module", "repository": "provider-module",
"license": "MIT", "license": "MIT",

2
types/index.d.ts vendored
View File

@ -76,6 +76,8 @@ interface ProviderInstance {
batchRequest(params: BatchRequestParams): Promise<string[]> batchRequest(params: BatchRequestParams): Promise<string[]>
checkNetworkVersion(): Promise<string> checkNetworkVersion(): Promise<string>
getWeb3(rpcUrl: string): Web3 getWeb3(rpcUrl: string): Web3
initWeb3(rpcUrl: string): Web3
on(params: OnListenerParams): void on(params: OnListenerParams): void
} }