1

I deployed with one script contract and I wonder there is a way to read that deployment from foundry env and its data like contract address to use it in another script?

I managed to do below with this cheatcodes https://book.getfoundry.sh/cheatcodes/parse-json

contract CheckScript is Script {
    function run() external returns (Tx1559 memory) {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        vm.startBroadcast(deployerPrivateKey);
        ITH404 th = ITH404(0xc5D38Bcc8E0B8a7C61bef5b1cc8945118a0Fd712);

        string memory root = vm.projectRoot();
        string memory path = string.concat(
            root,
            "/broadcast/deploy.sol/666666666/run-latest.json"
        );

        Tx1559[] memory transactions = readTx1559s(path);

        console.log(transactions[0].contractAddress);
        vm.stopBroadcast();

        return transactions[0];
    }
}

but in the end all I get is this error

script failed:

1 Answer 1

2

Storing and reading as environment variables:

One way is to store the details corresponding to a particular contract in .env file as environment variables, and later access them in other scripts.

Here’s a sample code to achieve the same:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Script} from "forge-std/Script.sol";
import {NFT} from "../src/NFT.sol";

contract NFTScript is Script {
    function run() public {
        vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
        NFT nft = new NFT("NonFun", "NFT");
        vm.stopBroadcast();

        // Store the contract address in an environment variable
        vm.envSet("NFT_CONTRACT_ADDRESS", address(nft));
    }
}

Then, access the NFT_CONTRACT_ADDRESS in another script:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

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

contract AnotherScript is Script {
    function run() public {
        // Read the contract address from the environment variable
        address nftContractAddress = vm.envAddress("NFT_CONTRACT_ADDRESS");

        // Use the contract address as needed
        // For example, interact with the NFT contract
    }
}

Reading from broadcast folder:

To read the files in your script, you need to add this line into your foundry.toml, granting access to read files:

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

Then, the script goes as follows:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.24;

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

contract CheckScript is Script {
    function run() external view {
        string memory root = vm.projectRoot();
        string memory path = string.concat(
            root,
            "/broadcast/deploy.sol/666666666/run-latest.json"
        );
        string memory json = vm.readFile(path);
        bytes memory contractName = stdJson.parseRaw(
            json,
            ".transactions[0].contractName"
        );
        bytes memory contractAddress = stdJson.parseRaw(
            json,
            ".transactions[0].contractAddress"
        );
        bytes memory transactionHash = stdJson.parseRaw(
            json,
            ".receipts[0].transactionHash"
        );

        console.log(string(contractName));
        console.logAddress(bytesToAddress(contractAddress));
        console.logBytes(transactionHash);
    }

    function bytesToAddress(
        bytes memory bys
    ) private pure returns (address addr) {
        assembly {
            addr := mload(add(bys, 32))
        }
    }
}
3
  • Nice example, the problem is that this is only available for that session, so if you need to reuse it, like for example read data from a contract this is no use, we just need to HC address into the script or in .env
    – Blissful
    Mar 30 at 22:26
  • @Blissful, I've added the code for reading the json file from broadcast folder. Please check. Apr 3 at 7:32
  • 1
    Thnx, this works great.
    – Blissful
    Apr 3 at 16:46

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.