Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time manipulation functions #114

Merged
merged 5 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ Devnet can be restarted by calling `starknet.devnet.restart()`. All of the deplo
await starknet.devnet.restart();
```

#### Time Advancing

The time offset for each generated block can be increased by calling `starknet.devnet.increaseTime()`, and the time for the next block can be set by calling `starknet.devnet.setTime()` and subsequent blocks will keep the set offset.

Warning: *block time can be set in the past and lead to unexpected behaviour!*

```typescript
await starknet.devnet.setTime(1000); // time in seconds
await starknet.devnet.increaseTime(1000); // time in seconds
```

## Configure the plugin

Specify custom configuration by editing your project's `hardhat.config.ts` (or `hardhat.config.js`).
Expand Down
32 changes: 31 additions & 1 deletion src/devnet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,16 @@ export interface LoadL1MessagingContractResponse {
l1_provider: string;
}

export interface SetTimeResponse {
next_block_timestamp: number;
}

export interface IncreaseTimeResponse {
timestamp_increased_by: number;
}

export class DevnetUtils implements Devnet {
constructor(private hre: HardhatRuntimeEnvironment) {}
constructor(private hre: HardhatRuntimeEnvironment) { }

private get endpoint() {
return `${this.hre.config.starknet.networkUrl}`;
Expand Down Expand Up @@ -79,4 +87,26 @@ export class DevnetUtils implements Devnet {
return response.data;
}, "Request failed. Make sure your network has the /postman endpoint");
}

public async increaseTime(seconds: number) {
return this.withErrorHandler<IncreaseTimeResponse>(async () => {
const response = await axios.post<IncreaseTimeResponse>(
`${this.endpoint}/increase_time`,
{
time: seconds
});
return response.data;
}, "Request failed. Make sure your network has the /increase_time endpoint");
}

public async setTime(seconds: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would make sense to provide an option to set time in Date as well? Or does it make more sense to expect users to do the date manipulation anyway?

return this.withErrorHandler<SetTimeResponse>(async () => {
const response = await axios.post<SetTimeResponse>(
`${this.endpoint}/set_time`,
{
time: seconds
});
return response.data;
}, "Request failed. Make sure your network has the /set_time endpoint");
}
}
19 changes: 17 additions & 2 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
StringMap
} from "./types";
import { StarknetWrapper } from "./starknet-wrappers";
import { FlushResponse, LoadL1MessagingContractResponse } from "./devnet-utils";
import { FlushResponse, IncreaseTimeResponse, LoadL1MessagingContractResponse, SetTimeResponse } from "./devnet-utils";
import { Account, ArgentAccount, OpenZeppelinAccount } from "./account";
import { Transaction, TransactionReceipt, Block } from "./starknet-types";
import { HardhatNetworkConfig, NetworkConfig } from "hardhat/types/config";
Expand Down Expand Up @@ -121,6 +121,21 @@ declare module "hardhat/types/runtime" {
address?: string,
networkId?: string
) => Promise<LoadL1MessagingContractResponse>;


/**
* Increases the block timestamp offset.
* @param seconds the new timestamp in seconds
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
* @returns a timestamp
*/
increaseTime: (seconds: number) => Promise<IncreaseTimeResponse>;

/**
* Sets the block timestamp offset.
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
* @param seconds the new timestamp offset in seconds
* @returns a timestamp offset
*/
setTime: (seconds: number) => Promise<SetTimeResponse>;
}

interface HardhatRuntimeEnvironment {
Expand Down Expand Up @@ -213,7 +228,7 @@ declare module "hardhat/types/runtime" {

/**
* Returns an entire block and the transactions contained within it.
* @param optional block identifier (by block number or hash). To query the latest block, remove the identifier.
* @param identifier optional block identifier (by block number or hash). To query the latest block, remove the identifier.
* @returns a block object
*/
getBlock: (identifier?: BlockIdentifier) => Promise<Block>;
Expand Down