ethers.js/packages/providers/lib/web3-provider.js

155 lines
5.6 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-08 02:24:04 +03:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-15 01:48:48 +03:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-08 02:24:04 +03:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-15 01:48:48 +03:00
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-08 02:24:04 +03:00
exports.Web3Provider = void 0;
2019-05-15 01:48:48 +03:00
var properties_1 = require("@ethersproject/properties");
2019-08-02 09:10:58 +03:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-15 01:48:48 +03:00
var json_rpc_provider_1 = require("./json-rpc-provider");
2020-04-27 13:31:43 +03:00
var _nextId = 1;
function buildWeb3LegacyFetcher(provider, sendFunc) {
2021-05-17 23:19:36 +03:00
var fetcher = "Web3LegacyFetcher";
2020-04-27 13:31:43 +03:00
return function (method, params) {
2021-05-17 23:19:36 +03:00
var _this = this;
2020-04-27 13:31:43 +03:00
var request = {
method: method,
params: params,
id: (_nextId++),
jsonrpc: "2.0"
};
2019-05-15 01:48:48 +03:00
return new Promise(function (resolve, reject) {
2021-05-17 23:19:36 +03:00
_this.emit("debug", {
action: "request",
fetcher: fetcher,
2021-10-16 09:29:27 +03:00
request: (0, properties_1.deepCopy)(request),
2021-05-17 23:19:36 +03:00
provider: _this
});
sendFunc(request, function (error, response) {
2019-05-15 01:48:48 +03:00
if (error) {
2021-05-17 23:19:36 +03:00
_this.emit("debug", {
action: "response",
fetcher: fetcher,
error: error,
request: request,
provider: _this
});
2020-04-27 13:31:43 +03:00
return reject(error);
2019-05-15 01:48:48 +03:00
}
2021-05-17 23:19:36 +03:00
_this.emit("debug", {
action: "response",
fetcher: fetcher,
request: request,
response: response,
provider: _this
});
if (response.error) {
var error_1 = new Error(response.error.message);
error_1.code = response.error.code;
error_1.data = response.error.data;
2020-04-27 13:31:43 +03:00
return reject(error_1);
2019-05-15 01:48:48 +03:00
}
2021-05-17 23:19:36 +03:00
resolve(response.result);
2019-05-15 01:48:48 +03:00
});
});
};
2020-04-27 13:31:43 +03:00
}
function buildEip1193Fetcher(provider) {
return function (method, params) {
2021-05-17 23:19:36 +03:00
var _this = this;
2020-04-27 13:31:43 +03:00
if (params == null) {
params = [];
}
2021-05-17 23:19:36 +03:00
var request = { method: method, params: params };
this.emit("debug", {
action: "request",
fetcher: "Eip1193Fetcher",
2021-10-16 09:29:27 +03:00
request: (0, properties_1.deepCopy)(request),
2021-05-17 23:19:36 +03:00
provider: this
});
return provider.request(request).then(function (response) {
_this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request: request,
response: response,
provider: _this
});
return response;
}, function (error) {
_this.emit("debug", {
action: "response",
fetcher: "Eip1193Fetcher",
request: request,
error: error,
provider: _this
});
throw error;
});
2020-04-27 13:31:43 +03:00
};
}
var Web3Provider = /** @class */ (function (_super) {
__extends(Web3Provider, _super);
function Web3Provider(provider, network) {
var _this = this;
if (provider == null) {
logger.throwArgumentError("missing provider", "provider", provider);
}
var path = null;
var jsonRpcFetchFunc = null;
var 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:";
}
}
_this = _super.call(this, path, network) || this;
2021-10-16 09:29:27 +03:00
(0, properties_1.defineReadOnly)(_this, "jsonRpcFetchFunc", jsonRpcFetchFunc);
(0, properties_1.defineReadOnly)(_this, "provider", subprovider);
2020-04-27 13:31:43 +03:00
return _this;
}
Web3Provider.prototype.send = function (method, params) {
return this.jsonRpcFetchFunc(method, params);
};
2019-05-15 01:48:48 +03:00
return Web3Provider;
}(json_rpc_provider_1.JsonRpcProvider));
exports.Web3Provider = Web3Provider;
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=web3-provider.js.map