Contract Deployment Foundry Cast – How to Change tx.origin Address When Running Foundry Scripts

castcontract-deploymentfoundry

I am running a foundry script that deploys some contracts and calls them. I start my run function with

      function run() public {
            uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
            vm.startBroadcast(deployerPrivateKey);
...

            address(contract).call(
                    abi.encodeWithSignature(
                        "function(uint256,bytes8,address)",
                        var1,
                        var2,
                        var3
                    )
                );
                vm.stopBroadcast();
            }

Inside of the contract being called, I always get tx.origin as the address: 0x1804c8ab1f12e6bbf3894d4083f33e07309d1f38. I am assuming this is some kind of foundry script deployment address. The msg.sender is the correct address of mine, however.

I am able to set both the tx.origin and msg.sender properly if I use vm.prank(), but I need the script to interact with an existing contract on-chain. I think I need startBroadcast for that instead of vm.prank().

Is that address indeed a default deployer address?

How can I get the tx.origin to be my address instead?

Best Answer

While executing the forge script command, include --sender or --private-key wallet options to change the tx.origin address.

Related Topic