0

I got this error when I wanted to add config to the deploy script

Compiler run failed:
Error (9582): Member "priceFeedETHUSD" not found or not visible after argument-dependent lookup in function () view external returns (address,address).
  --> script/DeployAMMTest.s.sol:12:29:
   |
12 |         address priceFeed = helperConfig.config.priceFeedETHUSD;

The HelperConfig Code was

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.22;

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

contract HelperConfig is Script {
    NetworkConfig public config;

    uint8 public constant DECIMALS = 8;
    int256 public constant INITIAL_PRICE = 2000e8;

    struct NetworkConfig {
        address priceFeedETHUSD;
        address priceFeedBTCUSD;
    }

    constructor() {
        if (block.chainid == 1) {
            config = getMainnetConfig();
        } else if (block.chainid == 11155111) {
            config = getSepoliaConfig();
        } else {
            config = getOrCreateAnvilEthConfig();
        }
    }

    function getSepoliaConfig() public pure returns (NetworkConfig memory) {
        return
            NetworkConfig({
                priceFeedETHUSD: 0x694AA1769357215DE4FAC081bf1f309aDC325306,
                priceFeedBTCUSD: 0x694AA1769357215DE4FAC081bf1f309aDC325306
            });
    }

    function getMainnetConfig() public pure returns (NetworkConfig memory) {
        return
            NetworkConfig({
                priceFeedETHUSD: 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419,
                priceFeedBTCUSD: 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419
            });
    }

    function getOrCreateAnvilEthConfig() public returns (NetworkConfig memory) {
        if (config.priceFeedETHUSD != address(0)) {
            return config;
        }

        vm.startBroadcast();
        MockV3Aggregator mockPriceFeed = new MockV3Aggregator(
            DECIMALS,
            INITIAL_PRICE
        );
        vm.stopBroadcast();
        return
            NetworkConfig({
                priceFeedETHUSD: address(mockPriceFeed),
                priceFeedBTCUSD: address(mockPriceFeed)
            });
    }
}

And the Deploy Code was below:

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.22;

import {Script} from "forge-std/Script.sol";
import {AMMTest} from "../src/AMMTest.sol";
import {HelperConfig} from "./HelperConfig.s.sol";

contract DeployAMMTest is Script {
    function run() external {
        HelperConfig helperConfig = new HelperConfig();
        address priceFeed = helperConfig.config.priceFeedETHUSD;
        vm.startBroadcast();
        AMMTest ammTest = new AMMTest(priceFeed);
        vm.stopBroadcast();
    }
}

1 Answer 1

0

Solidity has limitations in handling direct member access of a struct returned from a function in another contract. This is due to visibility and scope rules in Solidity concerning structs.

Solution:

Update HelperConfig Contract:

Add a function to return the priceFeedETHUSD directly, avoiding direct struct member access.

function getPriceFeedETHUSD() public view returns (address) {
    return config.priceFeedETHUSD;
}

Update DeployAMMTest Contract:

Modify the contract to call the newly added function instead of accessing the struct member directly.

contract DeployAMMTest is Script {
    function run() external {
        HelperConfig helperConfig = new HelperConfig();
        address priceFeed = helperConfig.getPriceFeedETHUSD(); // Updated line
        vm.startBroadcast();
        AMMTest ammTest = new AMMTest(priceFeed);
        vm.stopBroadcast();
    }
}

This change resolves the visibility issue by accessing the price feed address through a function call, which is a more standard and safer method in Solidity for accessing data across different contracts.

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.