[Lesson 32] [Fund subscription] Question about importing abstract contract containing constant variables #2954
-
Hello everyone, I'm encountering an issue when trying to access the When I build the project, I get the following error: ~/foundry-f23/foundry-smart-contract-lottery$ forge build
[⠊] Compiling...
[⠰] Compiling 45 files with Solc 0.8.19
[⠔] Solc 0.8.19 finished in 253.68ms
Error:
Compiler run failed:
Error (7576): Undeclared identifier.
--> script/Interactions.s.sol:54:30:
|
54 | if (block.chainid == LOCAL_CHAIN_ID) {
| ^^^^^^^^^^^^^^ What I have tried:
❓ ❓Can anyone help me understand why the Relevant Code:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
From what I know so far, this has to do with how constants are stored in Solidity - at compile-time they become part of the bytecode - reference to Solidity docs This means that in your example, contract FundSubscription is Script, CodeConstants {
...
} If you wish to have something that you do not need to inherit, check out libraries. |
Beta Was this translation helpful? Give feedback.
-
Hey @tcrin, you can see that in the error you have provided it states that uint256 local_chain_id = HelperConfig.LOCAL_CHAIN_ID();```
and also you need to deploy the helper config in ```interactions.s.sol```, so the code might look like this
```solidity
contract FundSubscription is Script {
uint256 public constant FUND_AMOUNT = 3 ether; // 3 LINK
HelperConfig helper;
function fundSubscription(address vrfCoordinator, uint256 subscriptionId, address linkTokenAddress) public {
console.log("Funding subscription: ", subscriptionId);
console.log("On chainId: ", block.chainid);
helper = new HelperConfig();
uint256 local_chain_id = HelperConfig.LOCAL_CHAIN_ID();
if (block.chainid == local_chain_id) { // This line causes build fail // If you want you can do like this or just pass saving the variable in here in the interaction.s.sol, my advice is just store the variable in here
// My code here
}
}
function run() external {
fundSubscriptionUsingConfig();
}
} |
Beta Was this translation helpful? Give feedback.
From what I know so far, this has to do with how constants are stored in Solidity - at compile-time they become part of the bytecode - reference to Solidity docs
This means that in your example,
LOCAL_CHAIN_ID
which is part ofCodeConstants
contract is nowhere in the bytecode of theFundSubscription
contract. I think that if you inheritCodeConstants
your code will compileIf you wish to have something that you do not need to inherit, check out libraries.