2

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?

3 Answers 3

2

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

1
  • Where did you see that in the docs? Exactly the issue I was having. Thanks! Commented Jan 22, 2023 at 4:08
1

Answering to Yan's question in the comments.

There's only two references to this address 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38 in the Foundry docs.

You can check them out here:

https://book.getfoundry.sh/reference/config/testing

It's the default address for msg.sender and tx.origin.

sender
Type: string (address)
Default: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Environment: FOUNDRY_SENDER or DAPP_SENDER
The value of msg.sender in tests.

tx_origin
Type: string (address)
Default: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
Environment: FOUNDRY_TX_ORIGIN or DAPP_TX_ORIGIN
The value of tx.origin in tests.

There're no other references to this address anywhere else.

1

I was also struggling with same problem. I discovered that you can override tx.origin address in foundry.toml file. I don't know why this is not working with vm.prank() or vm.startPrank().

I override both these variables to assign desired address to tx.origin in tests.

[profile.default]
src = "src"
out = "out"
libs = ["lib"]
// added lines
sender = '0x9dF0C6b0066D5317aA5b38B36850548DaCCa6B4e'
tx_origin = '0x9dF0C6b0066D5317aA5b38B36850548DaCCa6B4e'

There is full default foundry.toml file with default values:

https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.