dapptools icon indicating copy to clipboard operation
dapptools copied to clipboard

Simulate EOA for tests

Open CodeSandwich opened this issue 4 years ago • 4 comments

I have a smart contract which uses EIP-712 signatures in its API, which I want to test. The regular testing routine is to have a smart contract as a user, but then there are no private key involved, which is needed to create a signature.

I could pseudo-randomly generate a private key and derive its address, but then I won't be able to send a message where msg.sender is my generated address.

It would be great to have a possiblity to simulate being an EOA with a private key. Maybe with a cheatcode to set msg.sender?

CodeSandwich avatar Sep 06 '21 15:09 CodeSandwich

Yes, would like this too. I've been wanting to implement something like an hevm.callFrom for a while.

d-xo avatar Sep 07 '21 12:09 d-xo

Although I am not sure if this will help, I managed to "cheat" the msg.sender by deploying the test contract on the address that I wanted to simulate with DAPP_TEST_ADDRESS.

I imagine that it's already been suggested and tested, but I mention it just in case :)

odyslam avatar Sep 08 '21 19:09 odyslam

Although I am not sure if this will help, I managed to "cheat" the msg.sender by deploying the test contract on the address that I wanted to simulate with DAPP_TEST_ADDRESS.

I imagine that it's already been suggested and tested, but I mention it just in case :)

Yep that env var is great if you only need to impersonate one account. But dynamic impersonation at runtime is also helpful for a lot of workflows and is nor currently supported :(

transmissions11 avatar Sep 08 '21 20:09 transmissions11

At least for tests that depend on a certain value of msg.sender, creating a custom mock contract in the test file and then calling from the mock can help. I know it's not a simulated EOA but better than nothing:

contract Abc {
  address owner;
  constructor() {
    owner = msg.sender;
  }
  function methodcall() public {
    require(msg.sender == owner);
    // etc...
  }
}
contract MockCaller {
  // calling `callFrom` from within a test function will change msg.sender to the address of `MockCaller`.
  function callFrom(address token) public {
    Abc abc = Abc(token);
    abc.methodcall();
  }
}

I agree that a simulated EOA would be helpful.

TimDaub avatar Jan 19 '22 17:01 TimDaub