From ee7b215f24e35d096c4a84ce196da7e3d08c3d46 Mon Sep 17 00:00:00 2001 From: yann300 Date: Tue, 29 Oct 2024 15:09:13 +0100 Subject: [PATCH] fix external provider --- .../src/app/providers/abstract-provider.tsx | 10 +--- apps/remix-ide/src/app/udapp/run-tab.tsx | 59 +++++++++++++------ 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/apps/remix-ide/src/app/providers/abstract-provider.tsx b/apps/remix-ide/src/app/providers/abstract-provider.tsx index 5e645ffe81..6c0d866265 100644 --- a/apps/remix-ide/src/app/providers/abstract-provider.tsx +++ b/apps/remix-ide/src/app/providers/abstract-provider.tsx @@ -102,17 +102,15 @@ export abstract class AbstractProvider extends Plugin implements IProvider { sendAsync(data: JsonDataRequest): Promise { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { - if (!this.provider) return reject({ jsonrpc: '2.0', id: data.id, error: { message: 'provider node set', code: -32603 } } as JsonDataResult) + if (!this.provider) return reject({ jsonrpc: '2.0', id: data.id, error: { message: 'provider not set', code: -32603 } } as JsonDataResult) this.sendAsyncInternal(data, resolve, reject) }) } private async switchAway(showError: boolean, msg: string) { if (!this.provider) return - this.provider = null - this.connected = false if (showError) { - this.call('notification', 'toast', 'Error while querying the provider: ' + msg) + this.call('terminal', 'log', { type: 'error', value: 'Error while querying the provider: ' + msg }) } return } @@ -138,10 +136,8 @@ export abstract class AbstractProvider extends Plugin implements IProvider { } catch (error) { this.switchAway(true, error.message ? error.message : error.error ? error.error : error) } - } - error.code = -32603 - reject({ jsonrpc: '2.0', error, id: data.id }) + reject({ jsonrpc: '2.0', error: { message: error.message, code: -32603 }, id: data.id }) } } else { const result = data.method === 'net_listening' ? 'canceled' : [] diff --git a/apps/remix-ide/src/app/udapp/run-tab.tsx b/apps/remix-ide/src/app/udapp/run-tab.tsx index b94d6cfb80..31125b4910 100644 --- a/apps/remix-ide/src/app/udapp/run-tab.tsx +++ b/apps/remix-ide/src/app/udapp/run-tab.tsx @@ -194,24 +194,7 @@ export class RunTab extends ViewPlugin { if (options['fork']) this.fork = options['fork'] } }, - provider: { - sendAsync (payload) { - return udapp.call(name, 'sendAsync', payload) - }, - request (payload) { - return new Promise((resolve, reject) => { - udapp.call(name, 'sendAsync', payload).then((response) => { - if (response.error) { - reject(response.error.message) - } else { - resolve(response) - } - }).catch((err) => { - reject(err) - }) - }) - } - } + provider: new Provider(udapp, name) }) } @@ -304,3 +287,43 @@ export class RunTab extends ViewPlugin { this.addInstance(address, contractObject.abi, contractObject.name) } } + +class Provider { + udapp: RunTab + name: string + constructor(udapp, name) { + this.udapp = udapp + this.name = name + } + sendAsync (payload) { + return this.udapp.call(this.name, 'sendAsync', payload) + } + request (payload): Promise { + return new Promise((resolve, reject) => { + this.udapp.call(this.name, 'sendAsync', payload).then((response) => { + if (response.error) { + reject(response.error.message) + } else { + resolve(response) + } + }).catch((err) => { + if (typeof err === 'string') { + reject(err) + } else if (err.error && err.error.message) { + reject(err.error.message) + } else if (err.error && typeof err.error === 'string') { + reject(err.error) + } else { + let e + try { + e = JSON.stringify(err) + } catch (e) { + reject('unknown error') + return + } + reject(e) + } + }) + }) + } +}