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 { 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) => {
const moduleOptions = <%= JSON.stringify(options) %>
@ -13,11 +17,11 @@ export default (ctx, inject) => {
this.web3 = new Web3(new Web3.providers.HttpProvider(options.config.rpcUrl))
}
async initProvider(provider) {
async initProvider(provider, { version }) {
try {
this.provider = provider
await this._checkVersion()
await this._setVersion(version)
return await this._initProvider()
} catch (err) {
throw new Error(`Provider method initProvider has error: ${err.message}`)
@ -245,10 +249,11 @@ export default (ctx, inject) => {
this.provider.sendAsync(
{
from,
method,
params,
jsonrpc: '2.0',
from,
id: this._generateId(),
},
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() {
if (this.provider && this.provider.request) {
this.version = 'new'
@ -311,6 +333,12 @@ export default (ctx, inject) => {
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 })

View File

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

8
types/index.d.ts vendored
View File

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