WalletConnect fix

This commit is contained in:
Danil Kovtonyuk 2021-01-18 20:17:53 +10:00
parent 89f968959d
commit f9d01e17ca
3 changed files with 39 additions and 5 deletions

View File

@ -1,5 +1,9 @@
const Web3 = require('web3') const Web3 = require('web3')
const { hexToNumberString, toChecksumAddress, numberToHex } = require('web3-utils') const { hexToNumberString, toChecksumAddress, numberToHex } = require('web3-utils')
const VERSIONS = ['old', 'new']
// TODO: create constants, utils
// TODO: move _repeatUntilResult and _generateId to utils
export default (ctx, inject) => { export default (ctx, inject) => {
const moduleOptions = <%= JSON.stringify(options) %> const moduleOptions = <%= JSON.stringify(options) %>
@ -13,11 +17,11 @@ export default (ctx, inject) => {
this.web3 = new Web3(new Web3.providers.HttpProvider(options.config.rpcUrl)) this.web3 = new Web3(new Web3.providers.HttpProvider(options.config.rpcUrl))
} }
async initProvider(provider) { async initProvider(provider, { version }) {
try { try {
this.provider = provider this.provider = provider
await this._checkVersion() await this._setVersion(version)
return await this._initProvider() return await this._initProvider()
} catch (err) { } catch (err) {
throw new Error(`Provider method initProvider has error: ${err.message}`) throw new Error(`Provider method initProvider has error: ${err.message}`)
@ -245,10 +249,11 @@ export default (ctx, inject) => {
this.provider.sendAsync( this.provider.sendAsync(
{ {
from,
method, method,
params, params,
jsonrpc: '2.0', jsonrpc: '2.0',
from, id: this._generateId(),
}, },
callback, callback,
) )
@ -279,6 +284,23 @@ export default (ctx, inject) => {
} }
} }
async _setVersion(version) {
try {
if(version) {
if (!VERSIONS.includes(version)) {
throw new Error('Invalid version parameter.')
}
this.version = version
return
}
return await this._checkVersion()
} catch (err) {
throw new Error(`Provider method _setVersion has error: ${err.message}`)
}
}
_checkVersion() { _checkVersion() {
if (this.provider && this.provider.request) { if (this.provider && this.provider.request) {
this.version = 'new' this.version = 'new'
@ -311,6 +333,12 @@ export default (ctx, inject) => {
iteration() iteration()
}) })
} }
_generateId() {
const date = Date.now() * Math.pow(10, 3);
const extra = Math.floor(Math.random() * Math.pow(10, 3));
return date + extra;
}
} }
const provider = new instance({ config: moduleOptions }) const provider = new instance({ config: moduleOptions })

View File

@ -1,6 +1,6 @@
{ {
"name": "nuxt-web3-provider", "name": "nuxt-web3-provider",
"version": "0.1.1", "version": "0.1.2",
"description": "Provider integration with Nuxt.js", "description": "Provider integration with Nuxt.js",
"repository": { "repository": {
"type": "git", "type": "git",

8
types/index.d.ts vendored
View File

@ -61,11 +61,17 @@ interface ProviderOptions {
blockGasLimit?: number, blockGasLimit?: number,
} }
type ProviderVersions = 'new' | 'old'
type Config = {
version?: ProviderVersions
}
interface ProviderInstance { interface ProviderInstance {
readonly web3: typeof Web3 readonly web3: typeof Web3
readonly config: ProviderOptions readonly config: ProviderOptions
initProvider(provider: unknown): Promise<Address> initProvider(provider: unknown, config?: Config): Promise<Address>
sendRequest(params: RequestParams): Promise<TransactionReceipt> sendRequest(params: RequestParams): Promise<TransactionReceipt>
contractRequest(params: ContractRequestParams): Promise<TransactionReceipt> contractRequest(params: ContractRequestParams): Promise<TransactionReceipt>
getBalance(params: GetBalanceParams): Promise<number> getBalance(params: GetBalanceParams): Promise<number>