[英]Foundry call smart contract from with EOA address
I am wtiting test case using Foundry .我正在使用Foundry测试用例。 I want to call a custom smart contract function that changes the state of the smart contract called from a EOA so the msg.sender
would be the EOA address.我想调用一个自定义智能合约 function 来更改从 EOA 调用的智能合约的 state,因此msg.sender
将是 EOA 地址。 Here is my testing code:这是我的测试代码:
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import {console} from "forge-std/console.sol";
import {stdStorage, StdStorage, Test} from "forge-std/Test.sol";
import {Utils} from "./utils/Utils.sol";
import {MyERC20} from "../MyERC20.sol";
contract BaseSetup is MyERC20, DSTest {
Utils internal utils;
address payable[] internal users;
address internal alice;
address internal bob;
function setUp() public virtual {
utils = new Utils();
users = utils.createUsers(5);
alice = users[0];
vm.label(alice, "Alice");
bob = users[1];
vm.label(bob, "Bob");
}
function test() {
this.customFunction(); // want to call this function as Alice as the caller
}
}
So, in the code above, customFunction
is defined on the MyERC20
contract, and it changes the smart contract state.因此,在上面的代码中,在customFunction
合约上定义了MyERC20
,它改变了智能合约 state。 I want to call the function with different EOA accounts like alice
and bob
.我想用不同的 EOA 帐户(如alice
和bob
)调用 function 。 Is it possible, and if so, what it the syntax for that?有没有可能,如果有,它的语法是什么?
I would recommend using the prank
cheatcode in foundry for this.为此,我建议在 Foundry 中使用prank
作弊码。
It's pretty straight forward.这很简单。
interface CheatCodes {
function prank(address) external;
}
contract Test is DSTest {
CheatCodes cheatCodes;
function setUp() public {
cheatCodes = CheatCodes(HEVM_ADDRESS);
}
function test() public {
// address(1337) is now the caller of customFunction
cheatCodes.prank(address(1337));
address(contract).customFunction();
}
}
This pranked caller will only persist for a single call.这个被恶作剧的来电者只会持续一个电话。 Then you will have to instantiate the caller again with the prank
cheatCode on future calls to the contract.然后,您将不得不在未来调用合约时使用prank
cheatCode 再次实例化调用者。 Alternatively there is also a cheatCode called startPrank
which will allow the custom caller to persist until stopPrank
is called.或者,还有一个名为startPrank
的作弊代码,它将允许自定义调用者持续存在,直到调用stopPrank
。 Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.