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

feat(forge): prompt address and uint cheatcodes #7600

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,14 @@ interface Vm {
#[cheatcode(group = Filesystem)]
function promptSecret(string calldata promptText) external returns (string memory input);

/// Prompts the user for an address in the terminal.
#[cheatcode(group = Filesystem)]
function promptAddress(string calldata promptText) external returns (address);

/// Prompts the user for uint256 in the terminal.
#[cheatcode(group = Filesystem)]
function promptUint(string calldata promptText) external returns (uint256);

// ======== Environment Variables ========

/// Sets environment variables.
Expand Down
16 changes: 16 additions & 0 deletions crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Implementations of [`Filesystem`](crate::Group::Filesystem) cheatcodes.

use super::string::parse;
use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
use alloy_dyn_abi::DynSolType;
use alloy_json_abi::ContractObject;
use alloy_primitives::U256;
use alloy_sol_types::SolValue;
Expand Down Expand Up @@ -374,6 +376,20 @@ impl Cheatcode for promptSecretCall {
}
}

impl Cheatcode for promptAddressCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { promptText: text } = self;
parse(&prompt(state, text, prompt_input)?, &DynSolType::Address)
}
}

impl Cheatcode for promptUintCall {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { promptText: text } = self;
parse(&prompt(state, text, prompt_input)?, &DynSolType::Uint(256))
}
}

pub(super) fn write_file(state: &Cheatcodes, path: &Path, contents: &[u8]) -> Result {
let path = state.config.ensure_path_allowed(path, FsAccessKind::Write)?;
// write access to foundry.toml is not allowed
Expand Down
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions testdata/default/cheats/Prompt.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.18;

import "ds-test/test.sol";
import "cheats/Vm.sol";
import "../logs/console.sol";

contract PromptTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
Expand All @@ -15,4 +16,14 @@ contract PromptTest is DSTest {
vm._expectCheatcodeRevert();
vm.promptSecret("test");
}

function testPrompt_Address() public {
vm._expectCheatcodeRevert();
address test = vm.promptAddress("test");
}

function testPrompt_Uint() public {
vm._expectCheatcodeRevert();
uint256 test = vm.promptUint("test");
}
}
Loading