2019-08-25 09:39:20 +03:00
|
|
|
"use strict";
|
2021-05-17 23:19:36 +03:00
|
|
|
import { deepCopy, defineReadOnly } from "@ethersproject/properties";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
import { JsonRpcProvider } from "./json-rpc-provider";
|
2020-04-27 13:31:43 +03:00
|
|
|
let _nextId = 1;
|
|
|
|
function buildWeb3LegacyFetcher(provider, sendFunc) {
|
2021-05-17 23:19:36 +03:00
|
|
|
const fetcher = "Web3LegacyFetcher";
|
2020-04-27 13:31:43 +03:00
|
|
|
return function (method, params) {
|
|
|
|
const request = {
|
|
|
|
method: method,
|
|
|
|
params: params,
|
|
|
|
id: (_nextId++),
|
|
|
|
jsonrpc: "2.0"
|
|
|
|
};
|
2019-08-25 09:39:20 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-05-17 23:19:36 +03:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher,
|
|
|
|
request: deepCopy(request),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
sendFunc(request, (error, response) => {
|
2019-08-25 09:39:20 +03:00
|
|
|
if (error) {
|
2021-05-17 23:19:36 +03:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher,
|
|
|
|
error,
|
|
|
|
request,
|
|
|
|
provider: this
|
|
|
|
});
|
2020-04-27 13:31:43 +03:00
|
|
|
return reject(error);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2021-05-17 23:19:36 +03:00
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher,
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
if (response.error) {
|
|
|
|
const error = new Error(response.error.message);
|
|
|
|
error.code = response.error.code;
|
|
|
|
error.data = response.error.data;
|
2020-04-27 13:31:43 +03:00
|
|
|
return reject(error);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2021-05-17 23:19:36 +03:00
|
|
|
resolve(response.result);
|
2019-08-25 09:39:20 +03:00
|
|
|
});
|
|
|
|
});
|
2020-04-27 13:31:43 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildEip1193Fetcher(provider) {
|
|
|
|
return function (method, params) {
|
|
|
|
if (params == null) {
|
|
|
|
params = [];
|
|
|
|
}
|
2021-05-17 23:19:36 +03:00
|
|
|
const request = { method, params };
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "request",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request: deepCopy(request),
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
return provider.request(request).then((response) => {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request,
|
|
|
|
response,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
return response;
|
|
|
|
}, (error) => {
|
|
|
|
this.emit("debug", {
|
|
|
|
action: "response",
|
|
|
|
fetcher: "Eip1193Fetcher",
|
|
|
|
request,
|
|
|
|
error,
|
|
|
|
provider: this
|
|
|
|
});
|
|
|
|
throw error;
|
|
|
|
});
|
2020-04-27 13:31:43 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
export class Web3Provider extends JsonRpcProvider {
|
|
|
|
constructor(provider, network) {
|
|
|
|
logger.checkNew(new.target, Web3Provider);
|
|
|
|
if (provider == null) {
|
|
|
|
logger.throwArgumentError("missing provider", "provider", provider);
|
|
|
|
}
|
|
|
|
let path = null;
|
|
|
|
let jsonRpcFetchFunc = null;
|
|
|
|
let subprovider = null;
|
|
|
|
if (typeof (provider) === "function") {
|
|
|
|
path = "unknown:";
|
|
|
|
jsonRpcFetchFunc = provider;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
path = provider.host || provider.path || "";
|
|
|
|
if (!path && provider.isMetaMask) {
|
|
|
|
path = "metamask";
|
|
|
|
}
|
|
|
|
subprovider = provider;
|
|
|
|
if (provider.request) {
|
|
|
|
if (path === "") {
|
|
|
|
path = "eip-1193:";
|
|
|
|
}
|
|
|
|
jsonRpcFetchFunc = buildEip1193Fetcher(provider);
|
|
|
|
}
|
|
|
|
else if (provider.sendAsync) {
|
|
|
|
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.sendAsync.bind(provider));
|
|
|
|
}
|
|
|
|
else if (provider.send) {
|
|
|
|
jsonRpcFetchFunc = buildWeb3LegacyFetcher(provider, provider.send.bind(provider));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.throwArgumentError("unsupported provider", "provider", provider);
|
|
|
|
}
|
|
|
|
if (!path) {
|
|
|
|
path = "unknown:";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super(path, network);
|
|
|
|
defineReadOnly(this, "jsonRpcFetchFunc", jsonRpcFetchFunc);
|
|
|
|
defineReadOnly(this, "provider", subprovider);
|
|
|
|
}
|
|
|
|
send(method, params) {
|
|
|
|
return this.jsonRpcFetchFunc(method, params);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=web3-provider.js.map
|