// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import {Script} from "forge-std/Script.sol"; import {stdJson} from "forge-std/StdJson.sol"; import "@openzeppelin-contracts/utils/Strings.sol"; contract AddressFileRWScript is Script { using stdJson for string; function _readFromFile(string memory fileName) internal view returns (address) { string memory root = vm.projectRoot(); string memory path = string.concat( root, "/broadcast/", fileName, ".s.sol/", Strings.toString(block.chainid), "/run-latest.json" ); string memory json = vm.readFile(path); bytes memory contractAddress = stdJson.parseRaw( json, ".transactions[0].contractAddress" ); return _bytesToAddress(contractAddress); } function _uint2str(uint256 _i) internal pure returns (string memory str) { if (_i == 0) { return "0"; } uint256 j = _i; uint256 length; while (j != 0) { length++; j /= 10; } bytes memory bstr = new bytes(length); uint256 k = length; j = _i; while (j != 0) { bstr[--k] = bytes1(uint8(48 + j % 10)); j /= 10; } str = string(bstr); } function _bytesToAddress(bytes memory bys) internal pure returns (address addr) { assembly { addr := mload(add(bys, 32)) } } }