Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tx.origin are overwritten in forge script #4434

Closed
2 tasks done
teddav opened this issue Feb 26, 2023 · 2 comments · Fixed by #4469
Closed
2 tasks done

tx.origin are overwritten in forge script #4434

teddav opened this issue Feb 26, 2023 · 2 comments · Fixed by #4469
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-bug Type: bug

Comments

@teddav
Copy link
Contributor

teddav commented Feb 26, 2023

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (c5dd9a6 2023-02-26T00:12:08.486636Z)

What command(s) is the bug in?

forge script

Operating System

macOS (Apple Silicon)

Describe the bug

I'm using forge script to run transactions and deploy contracts. Usually everything works well but I'm running into an issue where the tx.origin seems to be changing in the middle of a script.
Here is my code:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import "forge-std/Script.sol";

import { console, Script } from "forge-std/Script.sol";

contract MyScript is Script {
    string network = "local";

    function setUp() public {
        vm.createSelectFork(network);
        uint256 pk = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(pk);
    }

    function run() public {
        ExternalContract ext = new ExternalContract();

        MyContract my = new MyContract();
        my.test(address(ext));

        vm.stopBroadcast();
    }
}

contract MyContract {
    function test(address _external) public {
        ExternalContract ext = ExternalContract(_external);

        console.log(tx.origin);
        ext.extFunc();
        console.log(tx.origin);
    }
}

contract ExternalContract {
    function extFunc() public {}
}

The first log shows the correct value: the address generated from the PRIVATE_KEY but the second log prints 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38

I have anvil running and the script is connecting to it.
The command i'm running is forge script ./script/myscript.s.sol --tc MyScript
And I have the networks setup in foundry.toml:

fs_permissions = [{ access = "read", path = ".env"}]

[rpc_endpoints]
local = "http://127.0.0.1:8545"
@teddav teddav added the T-bug Type: bug label Feb 26, 2023
@onbjerg onbjerg added C-anvil Command: anvil C-forge Command: forge Cmd-forge-script Command: forge script and removed C-anvil Command: anvil labels Feb 27, 2023
@mds1
Copy link
Collaborator

mds1 commented Feb 28, 2023

Hmm this does seem like a bug, cc @joshieDo.

Ran both below examples with PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 forge script ./script/Counter.s.sol --tc MyScript which is the first anvil private key, but just running it locally, not against a fork, and you'll see the logs differ as shown.

But if you run both of the examples as forge script ./script/Counter.s.sol --tc MyScript --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 instead, tx.origin is correctly logged as 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 in all cases

Example 1

A slightly simpler version of the above, which shows different tx.origin logs.

contract MyScript is Script {
    function run() public {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(pk);
        ExternalContract ext = new ExternalContract();
        MyContract my = new MyContract();
        my.myFunc(ext);
        vm.stopBroadcast();
    }
}

contract MyContract {
    function myFunc(ExternalContract ext) public {
        console.log("MyContract.myFunc-1                          ", tx.origin);
        ext.extFunc();
        console.log("MyContract.myFunc-2                          ", tx.origin);
    }
}

contract ExternalContract {
    function extFunc() public {
        console.log("MyContract.myFunc -> ExternalContract.extFunc", tx.origin);
    }
}
  MyContract.myFunc-1                           0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
  MyContract.myFunc -> ExternalContract.extFunc 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
  MyContract.myFunc-2                           0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38

Example 2

Moving the contract deploying within the myFunc method and the logs show the foundry default sender

contract MyScript is Script {
    function run() public {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(pk);
        MyContract my = new MyContract();
        my.myFunc();
        vm.stopBroadcast();
    }
}

contract MyContract {
    function myFunc() public {
        ExternalContract ext = new ExternalContract();

        console.log("MyContract.myFunc-1                          ", tx.origin);
        ext.extFunc();
        console.log("MyContract.myFunc-2                          ", tx.origin);
    }
}

contract ExternalContract {
    function extFunc() public {
        console.log("MyContract.myFunc -> ExternalContract.extFunc", tx.origin);
    }
}
== Logs ==
  MyContract.myFunc-1                           0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
  MyContract.myFunc -> ExternalContract.extFunc 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38
  MyContract.myFunc-2                           0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38

@SuperHero123123
Copy link

I still have this problem. After calling callback function in my contract, tx.origin changes back to 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38(default user?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-script Command: forge script T-bug Type: bug
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants