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

args in script? #1012

Closed
pedrohba1 opened this issue Sep 23, 2023 · 1 comment
Closed

args in script? #1012

pedrohba1 opened this issue Sep 23, 2023 · 1 comment

Comments

@pedrohba1
Copy link

pedrohba1 commented Sep 23, 2023

I have a script like this:

	forge script scripts/DeployFromDeployer.s.sol:Deploy --fork-url ${RPC_URL}  --broadcast -vvvv

can I pass --constructor-args in a script? Or any args at all? I couldn't find much about constructor args or passing args to scripts. I can read them from a file, although. But I would like to set them from the command line. Is this achievable?

I know that hardhat has this concept called tasks, which does pretty much what I'm defining: https://hardhat.org/hardhat-runner/docs/advanced/create-task#creating-a-task

@pedrohba1 pedrohba1 changed the title constructor args in script? args in script? Sep 23, 2023
@0xf1b0
Copy link

0xf1b0 commented Sep 25, 2023

I also was looking for a way to do this. As far as I understand, now this can only be done using the --sig option.

-s, --sig <SIG>
        The signature of the function you want to call in the contract, or raw
        calldata

        [default: run()]

By default when you run a script, the run() function in this script is called. But you can specify any function you want using --sig option. To call a function with input parameters you need to use raw calldata.

To make it easier to obtain calldata, you can use another tool that comes with foundry - cast.

For example your deploy script has a function like this:

function run(uint256 arg1, bool arg2) external { ... }

To get calldata to run it, use the command:

cast calldata "run(uint256,bool)" 1 true

Now you can run your script like this:

forge script scripts/DeployFromDeployer.s.sol:Deploy \
    --fork-url ${RPC_URL}  --broadcast -vvvv \
    --sig 0x0a3395f500000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001

Can be combined into oneliner:

forge script scripts/DeployFromDeployer.s.sol:Deploy \
    --fork-url ${RPC_URL}  --broadcast -vvvv \
    --sig $(cast calldata "run(uint256,bool)" 1 true)

@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Book Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

No branches or pull requests

3 participants