Skip to content

Commit

Permalink
Allow axios timeout configuration via hardhat config (#322)
Browse files Browse the repository at this point in the history
* [skip ci]
  • Loading branch information
notV4l committed Feb 23, 2023
1 parent f8e373c commit e5e0a9f
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const VOYAGER_MAINNET_VERIFIED_URL = "https://voyager.online/contract/";

export const CHECK_STATUS_TIMEOUT = 5000; // ms
export const CHECK_STATUS_RECOVER_TIMEOUT = 10000; // ms
export const REQUEST_TIMEOUT = 30000; // ms

export const LEN_SUFFIX = "_len";

Expand Down
3 changes: 1 addition & 2 deletions src/devnet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { StarknetPluginError } from "./starknet-plugin-error";
import { Devnet, HardhatRuntimeEnvironment } from "hardhat/types";

import { Block, MintResponse, L2ToL1Message } from "./starknet-types";
import { REQUEST_TIMEOUT } from "./constants";
import { hash } from "starknet";
import { numericToHexString } from "./utils";
import { Numeric } from "./types";
Expand Down Expand Up @@ -79,7 +78,7 @@ export interface PredeployedAccount {
export class DevnetUtils implements Devnet {
private axiosInstance = axios.create({
baseURL: this.endpoint,
timeout: REQUEST_TIMEOUT,
timeout: this.hre.config.starknet.requestTimeout,
timeoutErrorMessage: "Request timed out"
});

Expand Down
6 changes: 4 additions & 2 deletions src/external-server/external-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChildProcess } from "child_process";
import { StarknetPluginError } from "../starknet-plugin-error";
import { IntegratedDevnetLogger } from "./integrated-devnet-logger";
import { StringMap } from "../types";
import { REQUEST_TIMEOUT } from "../constants";
import { HardhatRuntimeEnvironment } from "hardhat/types";

function sleep(amountMillis: number): Promise<void> {
return new Promise((resolve) => {
Expand Down Expand Up @@ -161,9 +161,11 @@ export abstract class ExternalServer {
public async post<T>(data: StringMap): Promise<T> {
await this.ensureStarted();

const hre: HardhatRuntimeEnvironment = await import("hardhat");

try {
const response = await axios.post<T>(this.url, data, {
timeout: REQUEST_TIMEOUT,
timeout: hre.config.starknet.requestTimeout,
timeoutErrorMessage: "Request timed out"
});
return response.data;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
if (!config.starknet) {
config.starknet = {};
}
if (!config.starknet.requestTimeout) {
config.starknet.requestTimeout = 30_000;
}
});

// add sources path
Expand Down
1 change: 1 addition & 0 deletions src/starknet-venv-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChildProcess, spawn } from "child_process";
import { ExternalServer } from "./external-server";
import { getFreePort } from "./external-server/external-server";

import path from "path";

export class StarknetVenvProxy extends ExternalServer {
Expand Down
1 change: 1 addition & 0 deletions src/types/starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export type StarknetConfig = {
networkUrl?: string;
networkConfig?: NetworkConfig;
recompile?: boolean;
requestTimeout?: number;
};

export type WalletUserConfig = {
Expand Down
5 changes: 5 additions & 0 deletions test/configuration-tests/with-request-timeout-1ms/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hardhatStarknetTest } from "../../utils/cli-functions";
import { assertContains } from "../../utils/utils";

const execution = hardhatStarknetTest("--no-compile test/get-balance.test.ts".split(" "), true);
assertContains(execution.stdout, "Error: timeout of 1ms exceeded");
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "@shardlabs/starknet-hardhat-plugin";

module.exports = {
starknet: {
network: process.env.NETWORK,
requestTimeout: 1
},
networks: {
devnet: {
url: "http://127.0.0.1:5050",
stdout: "STDOUT"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "../../network.schema",
"devnet": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { hardhatStarknetTest } from "../../utils/cli-functions";

hardhatStarknetTest("--no-compile test/get-balance.test.ts".split(" "));
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "@shardlabs/starknet-hardhat-plugin";

module.exports = {
starknet: {
network: process.env.NETWORK
},
networks: {
devnet: {
url: "http://127.0.0.1:5050"
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "../../network.schema",
"devnet": true
}
13 changes: 13 additions & 0 deletions www/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,19 @@ module.exports = {
};
```
### Request Timeout
Default requestTimeout is 30s. It can be changed using the following configuration.
You may need to increase the timeout value in some situation (declaring large smart contract).
```typescript
module.exports = {
starknet: {
requestTimeout: 90_000, // 90s
}
};
```
### Paths
Prefer providing absolute paths when using the plugin. If a relative path is provided, it will be resolved relative to the root of the project.
Expand Down

0 comments on commit e5e0a9f

Please sign in to comment.