Questions tagged [inheritance]
Inheritance in this stackexchange refers to parent child contract relationship where the child contract is considered to be a type of the parent contract and hence implement the parent contract functions. This is same like inheritance in OOP in generic programming except for the fact that in this community its used in a contract oriented programming domain.
176
questions
0
votes
1
answer
9
views
How to resolve "Invalid type for argument in modifier invocation. Invalid implicit conversion from struct" error?
I want my contract to inherit another contract, the issue is that the contract uses a struct inside its constructor, and I'm not sure how to pass that data type from my contract.
Method 1
Contract I ...
0
votes
0
answers
9
views
Compilation of inheritance chain
I came to know that given contracts
fileA.sol
pragma solidity >= 0.7.0;
contract A {}
is imported by
fileB.sol
import './fileA.sol';
pragma solidity 0.8.0;
contract B {}
where B is the top-level ...
0
votes
1
answer
22
views
Inheritance in solidity
Are private variables in solidity inherited ?
Please I need a bit of clarification here from most text I have read about inheritance in solidity they state clearly that private variables of parent ...
1
vote
1
answer
20
views
How does calling parent functions using "super" work?
When I call the function fun inside the contract D, I expect the Logs to be "D.foo" -> "C.foo" -> "A.foo" -> "B.foo" -> "A.foo". However, ...
1
vote
2
answers
41
views
How to Resolve TypeError with Overriding Functions in Solidity Inheriting from Two Contracts?
I have a situation where my contract (C) inherits from two other contracts (A and B). Both of these base contracts have a function with the same name and parameters but different return types. Here's ...
0
votes
1
answer
29
views
Can a Smart Contract call itself?
Suppose I have contract A. Contract B inherits Contract A. Suppose updateB() is called in contract A from an EOA. Now inside update(), a function in contract B is called. Will msg.sender be that of ...
0
votes
1
answer
27
views
What is the purpose of the import statement in Solidity?
Could someone explain to me what the pupose of the import statement is, in Solidity? Is it the similar to inheriting from another contract where you have access to the state variables and function of ...
1
vote
3
answers
552
views
Trying to create a soulbound token. Keep getting two errors
I'm trying to create a soulbound (non-transferable NFT) token on Solidity. Here is my code so far.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.17 <0.9.0;
import "@openzeppelin/...
0
votes
0
answers
30
views
Who is the msg.sender in the context when calling the "super.{....}" contract method on the inherited parent contract?
The child contract's method is: (1)
function redeem(
uint256 shares,
address receiver,
address owner
) public override andSync returns (uint256 assets) {
return ...
1
vote
1
answer
24
views
How to cache the result of an inherited function before overriding it
contract A {
function getUINT() public virtual view returns (uint) {
return 100000;
}
}
contract B is A {
uint trycache;
uint public trycache2 = getUINT(); // still 200000
...
1
vote
1
answer
30
views
Need help with debugging Error: Definition of base has to precede definition of derived contract
I'm working on a social account recovery feature for smart accounts (ERC-4337) on top of Thirdweb's smart account contract.
Link to the codebase
I'm getting the error Base has to precede definition of ...
1
vote
0
answers
34
views
Free functions cannot have function visibility
I am developing my Dapp and as I am trying to compile and test my contract I keep getting an error saying that free functions cannot have visibility and I am very frustrated and confused as to what is ...
1
vote
1
answer
34
views
Re declaration question - Inheritance & overriding
I need help clearing somethings up when it comes down to redeclaration and implementation of inherited functions. I have an example below. I am building an nft erc1155 marketplace and inherited the ...
1
vote
1
answer
41
views
How do i directly write in my ERC721 parent's private mapping?
MyContract is ERC721
I want to adapt the _update function to a specific usecase.
In order to keep its core functionality, the adapted function needs to write into _owners, _balances and ...
1
vote
1
answer
70
views
question about interfaces and inheritance
I am building my own application for an erc1155 dapp but have some questions about interfaces and inheritance. How do you re-declare functionality that you inherit from another contract? Also, how do ...
2
votes
0
answers
23
views
How to inherite modifier into multiple contracts ? Best practice?
I have a project with severals contracts.
To put some security, I've created a "onlyContract" modifier on a Roles contract so some functions in diferents contracts can only be called if the ...
1
vote
0
answers
27
views
Inheriting a contract to match state layout in a delegatecall
What is the best way to manage the state layout of a contract will exclusively be used via a delegatecall?
Let's say that there is a Parent contract that uses delegatecall on a Child contract. Within ...
0
votes
1
answer
231
views
Can you change an overridden function's visibility/mutability?
Say I have a parent and a child contract, in the child I don't want to use one of the parent's functions, so I changed the logic to just revert if it is ever called.
Now the terminal output shows me ...
0
votes
1
answer
68
views
Solidity Inheritance - Changing state variable content
I am new to Solidity and when I tried this code below and try to change the value from the child contract, it did not work.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;
contract ...
0
votes
1
answer
23
views
How can I inheritance an interface without abstract?
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import "./IERC20.sol";
abstract contract Lock is IERC20 {
string name;
string symbol;
uint8 decimals;
...
0
votes
1
answer
81
views
Is it possible to have access to an internal function of a smart contract already deployed?
I have a smart contract A which inheritates B :
contract A is B
In A, there is a function "test() onlyowner" calling B.otherFunction().
In B, otherFunction is just internal. Dont have the ...
1
vote
1
answer
103
views
Why do some developers make their base contract inherit its Interface?
Some protocols and developers inherit the contract's Interface inside of the base contract and then have the interface contain the actual NatSpec, is this simply for human readability/code ...
0
votes
1
answer
99
views
How do modifiers effect function signatures?
I want to change a function that my contract inherits to have a modifier, but are you allowed to add modifiers to overridden functions that didn't previously have a one?
1
vote
0
answers
21
views
Vyper: How to test internal helper functions?
I'm writing a Roulette contract as a learning activity in Vyper. My contract seems to be working through manual testing, and now I'm starting to write unit tests.
I have an internal helper function ...
0
votes
1
answer
14
views
I'm getting error every time I'm calling withdraw function from treasury contract?
//Treasury.sol
SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "./Lending.sol";
contract Treasury is Lending{
Lending lending = new Lending();
bool internal ...
0
votes
0
answers
29
views
Function Visibility specifiers during inheritance
when am overriding a function in solidity must my child function have the same visibility as the parent function? can the parent function be public and the child function be internal
0
votes
0
answers
23
views
Inheritance, leaving crypto to a child, drip by drip
How would be best to lock,up some eth (or other crypto, BTC?) for example, for my sons future ? Ie so that his wallet receives a certain percentage of the contents of my account over a certain period ...
0
votes
2
answers
134
views
Constructor Inheritance: : Wrong argument count for constructor call
In the following, why is the constructor of C not inherited by CExt so that it can be used by another extending contract CExtChild?
contract C {
uint id;
constructor(uint _id) {
id = ...
1
vote
0
answers
160
views
For function overriding, why can visibility be made less restrictive, but mutability only more restrictive?
From the solidity docs:
Function Overriding
Base functions can be overridden by inheriting contracts to change
their behavior if they are marked as virtual. The overriding function
must then use the ...
0
votes
1
answer
50
views
Overriding in multiple inheritance
If I have the following contracts:
contract A {
function _delegate() internal virtual {
//some code
}
}
contract B is A {
//some code not including _delegate()
}
contract C is B {
...
0
votes
1
answer
112
views
Difference between storing an address versus a contract instance?
What is the difference between storing an address of a contract and using a interface to interact with it, versus storing the type of the contract itself (i.e. IPriceFeed v. PriceFeed) within the ...
0
votes
1
answer
550
views
How to call a function that is inherited from another contract using Hardhat?
I have deployed a smart contract that inherits ERC721EnumerableUpgradeable which inherits ERC721Upgradeable.
When I deploy using Remix, for example, I see the function safeTransferFrom, which is a ...
1
vote
2
answers
155
views
Contract storing array returning addresses
Below is the contract which imports another contract
pragma solidity ^0.8.7;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
...
0
votes
1
answer
50
views
Is it possible to write an inheritable extension contract that supports multiple parent implementations?
I'm writing an extension to be used with a ERC721 contract. Is it possible to call a function like balanceOf() which should be defined in any implementation of ERC721, without having to define the ...
1
vote
2
answers
715
views
Upgradable contract with new inheritance
I've deployed a smart contract that inherits a few other contracts, simply:
contract A is B,C,D {}
Now, i have no issue with upgrades when it comes to upgrading A,B,C or D. My question is, what will ...
0
votes
1
answer
120
views
Inheritance issue using virtual and override (probably): Member not found
I'm trying to work through a course that is covering virtual and override and seem to be getting an error due to inheritance. Below is the code:
//SPDX-License-Identifier: GPL-3.0
pragma solidity >...
1
vote
3
answers
834
views
Why do we need interfaces in solidity?
Let's say contractA has a special function. And made an interface that contains the special function without implementation. To use the function in the other contractB, we can use the interface but ...
0
votes
1
answer
29
views
How Linearization is possible in first block of code but not in the second block of code?
First block of code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol";
...
1
vote
0
answers
92
views
`transferOwnership` is not a function
I am following the challenges from the speedrunEthereum and right now I am stuck on challenge 2 and not getting why the transferOwnership is not a function since I am also inheriting the Ownable from ...
1
vote
1
answer
85
views
modifiers overriding simple question with inheritance
just wanting to be sure of this. If I have a contract that inherits from another contract and overrides a function of that contract with NonReentrant modifier on it. Will that NonReentrant modifier ...
2
votes
1
answer
215
views
What is the practical purpose of interface contracts?
I wanted to make sure I understood the purpose of interface contracts. I understand that they are contracts with unimplemented functions, but wanted to make sure I understood all the ways programmers ...
0
votes
1
answer
172
views
Multiple inheritance in Solidity
I have a contract C that is Ownable so I can use ownerOnly on its admin functions.
Recently, I added some functionality that I'm going to start using in other projects. I refactored the functionality ...
1
vote
1
answer
173
views
Does the compiler ignore internal functions that are never called?
On inheriting a contract that contains some internal functions that are never used in the child contract. eg.
contract Parent {
function _someInternalFunction() internal{
}
}
contract Child ...
4
votes
1
answer
753
views
How does Solidity implement C3 linearization?
The solidity documentation does not offer a lot of insights into their implementation of the C3 linearization to resolve methods in a multiple inheritance scenario. There it states that
You have to ...
0
votes
1
answer
38
views
Create contract with contract
I want to create a contract that can create contract itself
I find something like this
pragma solidity ^0.5.0;
contract DaughterContract {
string public name;
uint public age;
constructor(
...
5
votes
1
answer
916
views
Interface overriding giving error. Overriding public state variable return types differ [duplicate]
I am trying to import interface in it's contract and overriding function. But 1 mapping is giving error.
Error:
Overriding public state variable return types differ.
Interface:
interface ...
3
votes
2
answers
557
views
What is the preferred Inheritance for ERC20 token and why?
I tried to deploy both ERC20 contracts with burn functionality over the remix and both seems to work fine, I am confused if one way should be preferred over the other. Since, openzeppelin contract ...
0
votes
1
answer
323
views
Solidity: calling a imported contract's function without inheritance, Calling a imported contract interface and passing its implemented contract add
This is the error I'm getting, I'm trying to implement ERC20's _transfer function without inheritance, calling its Interface IERC20 and passing the ERC20 deployed contracts address and then calling ...
0
votes
2
answers
1k
views
How to import Openzep ERC20: Identifier not found
Can I get someone's perspective on why I'm unable to find the ERC20 contract I've npm install @openzeppelin/contracts.
After import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; I get a ...
0
votes
3
answers
1k
views
Why is my Open Zeppelin import in Remix throwing an error?
I am importing ERC20 from OpenZeppelin but it's not working!
Here is the code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/...