feat: getter for web3

This commit is contained in:
Nik Dement'ev 2020-11-12 20:01:07 +03:00
parent 7d17901c61
commit 418d238b01
No known key found for this signature in database
GPG Key ID: 769B05D57CF16FE2
2 changed files with 15 additions and 2 deletions

View File

@ -2,7 +2,7 @@ const Web3 = require('web3')
const { hexToNumberString, toChecksumAddress, numberToHex } = require('web3-utils')
export default (ctx, inject) => {
// const moduleOptions = <%= JSON.stringify(options) %>
const moduleOptions = <%= JSON.stringify(options) %>
const instance = class Provider {
constructor(options) {
@ -35,6 +35,18 @@ export default (ctx, inject) => {
}
}
getWeb3(rpcUrl) {
try {
if (!url) {
throw new Error(`Please set url to params, current url ${rpcUrl}`)
}
return new Web3(rpcUrl)
} catch (err) {
throw new Error(`Provider method getWeb3 has error: ${err.message}`)
}
}
getContract({ abi, address }) {
return new this.web3.eth.Contract(abi, address)
}

3
types/index.d.ts vendored
View File

@ -75,6 +75,7 @@ interface ProviderInstance {
waitForTxReceipt(params: WaitForTxReceiptParams): Promise<TransactionReceipt>
batchRequest(params: BatchRequestParams): Promise<string[]>
checkNetworkVersion(): Promise<string>
getWeb3(rpcUrl: string): Web3
on(params: OnListenerParams): void
}
@ -91,4 +92,4 @@ declare module 'vue/types/vue' {
interface Vue {
$provider: ProviderInstance
}
}
}