我正在使用Foundry測驗用例。我想呼叫一個自定義智能合約函式,該函式會更改從 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,它改變了智能合約的狀態。我想使用不同的 EOA 帳戶(例如alice和)呼叫該函式bob。有沒有可能,如果有,它的語法是什么?
uj5u.com熱心網友回復:
為此,我建議在 Foundry 中使用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 再次實體化呼叫者。或者,還有一個名為 cheatCodestartPrank將允許自定義呼叫者持續到stopPrank被呼叫。希望這可以幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507614.html
上一篇:在React中模擬GeoLocationAPIHook
下一篇:使用測驗庫測驗Redux時出現此錯誤:警告:React.createElement:typeisinvalid:butgot:undefined
