Skip to content

Commit

Permalink
Make HttpProvider constructor private
Browse files Browse the repository at this point in the history
  • Loading branch information
schaable committed Nov 27, 2024
1 parent c577d7d commit bf72bd8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class EdrProvider extends EventEmitter implements EthereumProvider {
#vmTracer?: VMTracerT;
#nextRequestId = 1;

/**
* Creates a new instance of `EdrProvider`.
*/
public static async create({
networkConfig,
loggerConfig = { enabled: false },
Expand Down Expand Up @@ -237,6 +240,13 @@ export class EdrProvider extends EventEmitter implements EthereumProvider {
return edrProvider;
}

/**
* @private
*
* This constructor is intended for internal use only.
* Use the static method {@link EdrProvider.create} to create an instance of
* `EdrProvider`.
*/
private constructor(
provider: Provider,
vmTraceDecoder: VmTraceDecoder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface HttpProviderConfig {
extraHeaders?: Record<string, string>;
timeout: number;
jsonRpcRequestWrapper?: JsonRpcRequestWrapperFunction;
testDispatcher?: Dispatcher;
}

export class HttpProvider extends EventEmitter implements EthereumProvider {
Expand All @@ -66,14 +67,16 @@ export class HttpProvider extends EventEmitter implements EthereumProvider {
extraHeaders = {},
timeout,
jsonRpcRequestWrapper,
testDispatcher,
}: HttpProviderConfig): Promise<HttpProvider> {
if (!isValidUrl(url)) {
throw new HardhatError(HardhatError.ERRORS.NETWORK.INVALID_URL, {
value: url,
});
}

const dispatcher = await getHttpDispatcher(url, timeout);
const dispatcher =
testDispatcher ?? (await getHttpDispatcher(url, timeout));

const httpProvider = new HttpProvider(
url,
Expand All @@ -93,8 +96,7 @@ export class HttpProvider extends EventEmitter implements EthereumProvider {
* Use the static method {@link HttpProvider.create} to create an instance of
* `HttpProvider`.
*/
// TODO: make the constructor private, but we need to fix the tests first
constructor(
private constructor(
url: string,
networkName: string,
extraHeaders: Record<string, string>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ describe("http-provider", () => {

/**
* To test the HttpProvider#request method, we need to use an interceptor to
* mock the network requests. As the HttpProvider.create does not allow to
* pass a custom dispatcher, we use the constructor directly.
* mock the network requests.
*/
describe("HttpProvider#request", async () => {
const interceptor = await initializeTestDispatcher();
Expand Down Expand Up @@ -78,12 +77,12 @@ describe("http-provider", () => {
})
.reply(200, jsonRpcResponse);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

const result = await provider.request({
method: "eth_chainId",
Expand All @@ -94,12 +93,12 @@ describe("http-provider", () => {
});

it("should throw if the params are an object", async () => {
const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

await assertRejectsWithHardhatError(
provider.request({
Expand Down Expand Up @@ -161,12 +160,12 @@ describe("http-provider", () => {
})
.reply(200, jsonRpcResponse);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

const result = await provider.request({
method: "eth_chainId",
Expand Down Expand Up @@ -202,12 +201,12 @@ describe("http-provider", () => {
})
.reply(200, jsonRpcResponse);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

const result = await provider.request({
method: "eth_chainId",
Expand Down Expand Up @@ -238,12 +237,12 @@ describe("http-provider", () => {
.reply(429);
}

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

try {
await provider.request({
Expand Down Expand Up @@ -276,12 +275,12 @@ describe("http-provider", () => {
.defaultReplyHeaders({ "retry-after": "6" })
.reply(429);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

try {
await provider.request({
Expand Down Expand Up @@ -316,12 +315,12 @@ describe("http-provider", () => {
})
.reply(200, invalidResponse);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

await assertRejectsWithHardhatError(
provider.request({
Expand Down Expand Up @@ -361,12 +360,12 @@ describe("http-provider", () => {
})
.reply(200, jsonRpcResponse);

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
);
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
testDispatcher: interceptor,
});

try {
await provider.request({
Expand Down Expand Up @@ -434,13 +433,13 @@ describe("http-provider", () => {
}
};

const provider = new HttpProvider(
"http://localhost",
"exampleNetwork",
{},
interceptor,
const provider = await HttpProvider.create({
url: "http://localhost",
networkName: "exampleNetwork",
timeout: 20_000,
jsonRpcRequestWrapper,
);
testDispatcher: interceptor,
});

// eth_chainId is handled by the wrapper and returns the hardhat chain ID
// instead of jsonRpcChainIdResponse.result
Expand Down

0 comments on commit bf72bd8

Please sign in to comment.