简体   繁体   English

Foundry 使用 EOA 地址调用智能合约

[英]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 帐户(如alicebob )调用 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.

相关问题 测试智能合约(solidity/truffle) - Test for smart contract (solidity/truffle) 测试松露中的智能合约要求:如果功能不无效,则交易将被还原 - Testing Smart contract requires in Truffle: transaction reverted if function isnt void 运行测试用例后清理智能合约存储 - Cleaning smart contract storage after running a test case 如何在 Remix 中测试 Solidity-By-Example Mapping 智能合约? - How to test the Solidity-By-Example Mapping smart contract in Remix? 如何发送wei/eth到合约地址? (使用松露 javascript 测试) - How to send wei/eth to contract address? (using truffle javascript test) ExecutionError“:”智能合约恐慌:在运行测试时出现“应在使用前初始化”错误时恐慌:部署[NEAR PROTOCOL] - ExecutionError":"Smart contract panicked: panicked at 'Should be initialized before usage' error while running test:deploy [NEAR PROTOCOL] 如何通过 hardhat 验证自定义 tes.net 上的智能合约? (zkSync alpha tes.net) - How to verify smart contract on custom testnet via hardhat? (zkSync alpha testnet) 是否可以使用 Foundry/Forge 从之前的测试 state 开始按顺序继续测试? - Is it possible to continue tests sequentially from prior test state with Foundry/Forge? 将以太坊从账户转移到合约时“用完” - “Out of Gas” while transferring ethereum from account to contract 将 Pact 合约测试从 JavaScript 重写为 C# - Rewriting Pact contract test from JavaScript to C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM