Skip to content

Commit

Permalink
feat(forge): prompt address and uint cheatcodes (#7600)
Browse files Browse the repository at this point in the history
* feat: prompt address and uint cheatcode

* nits

* chore: change test to pass ci
  • Loading branch information
kamuik16 committed Apr 10, 2024
1 parent f0ea57a commit 9a0f0c2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
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 @@ -1482,6 +1482,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 @@ -426,6 +428,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");
}
}

0 comments on commit 9a0f0c2

Please sign in to comment.