dapptools
dapptools copied to clipboard
Simulate EOA for tests
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?
Yes, would like this too. I've been wanting to implement something like an hevm.callFrom for a while.
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 :)
Although I am not sure if this will help, I managed to "cheat" the
msg.senderby deploying the test contract on the address that I wanted to simulate withDAPP_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 :(
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.