我正在使用铸造厂编写测试用例。我想调用一个自定义智能契约函数,它可以更改从EOA调用的智能契约的状态,因此msg.sender将是EOA地址。这是我的测试代码:
// 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
}
}因此,在上面的代码中,customFunction是在MyERC20契约上定义的,它改变了智能契约状态。我想用不同的alice和bob帐户调用这个函数。它是否有可能,如果有,它的语法是什么?
发布于 2022-09-13 23:50:54
为此,我建议在铸造厂使用prank 作弊码。
挺直的。
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();
}
}这个恶作剧的调用者只会在一个电话中持续。然后,在对契约的未来调用中,必须再次使用prank cheatCode实例化调用方。或者,还有一个名为cheatCode的startPrank,它将允许自定义调用者持久化,直到调用stopPrank为止。希望这能有所帮助!
https://stackoverflow.com/questions/73670446
复制相似问题