Questions tagged [constructor]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
17
votes
3answers
8k views

Should the constructor function be public?

In all the tutorials I'm watching nobody specifies public, but remix presents warnings that function visibility has not been specified, and public is assumed. Since I don't like warnings if possible,...
15
votes
1answer
4k views

Deploying contract with constructor arguments via Geth RPC

How can I deploy a compiled contract with constructor arguments using geth rpc? I have tested my call deployed the compiled contract bytecode just fine, I just don't understand how I can pass ...
11
votes
1answer
633 views

Constructor not working properly

After I mine the following contract giving the array [true, true, true] as _bools I would expect [true, true, true] to be returned by getBools(). Instead [false, true, true] is returned. Is there ...
10
votes
2answers
5k views

Is address(this) a valid address in a contract's constructor?

When does a smart contract's address first become valid? Is the address during the execution of the contract, or only after it's been fully instantiated and initialized? In other words, is a contract'...
9
votes
1answer
3k views

How to manually initialise an array in Solidity?

I can't find a way to manually initialize an array. string[] public cool; function constructor() { cool[0] = "one"; } Won't execute, while function constructor(){ cool[cool.length++] = "...
9
votes
4answers
6k views

Passing constructor arguments while deploying contract from byte code

I am trying to deploy a contract via MyEtherWallet from byte code generated by browser-solidity. I unlocked my wallet using private key,but I didn't find any way of passing parameters to contract's ...
7
votes
1answer
2k views

Constructor auto generation and property visibility

Suppose I have the folloing: contract Owned { address owner; function owned() { owner = msg.sender; } modifier onlyOwner() { if (msg.sender == owner) _ } } ...
7
votes
2answers
3k views

How do I determine the constructor ABI for Etherscan contract verification?

I am facing issues trying to verify a contract that I deployed on Rinkeby. My contract constructor has 3 parameters, how do I pass values of these parameters in the contract verification form? How ...
6
votes
2answers
3k views

What is the significance of making constructor payable?

Aren`t constructors used to initialize variable at the time of contract creation? In common patterns section in solidity documentation, payable constructor is used for withdrawal pattern example. ...
5
votes
2answers
2k views

Contract inheritance

I really hope this is not a dumb question. I am trying to call the constructors of the inherited contracts in a structure like this: contract A{ uint a; constructor (uint _a) public { ...
5
votes
2answers
4k views

Mapping member isn't initialized when creating a struct

pragma solidity ^0.4.0; contract TestcaseResetObject { MyObject ob; struct MyObject { mapping(uint8 => uint) map; } event Output(uint number); function makeNew() { ...
5
votes
1answer
1k views

Multiple inheritance and order of chaining of multiple base constructors [duplicate]

I am trying to understand the order of invocation of base class constructors in the presence of multiple inheritance. As far as I can tell, the order appears under-specified in the Solidity ...
4
votes
3answers
2k views

Passing constructor arguments to the CREATE assembly instruction in solidity

I have a contract which deploys other contracts by bytecode: contract DeployContract { function deploy(bytes calldata _bytecode) external returns(address addr) { bytes memory bytecode = ...
4
votes
2answers
588 views

Contract created by another contract and exceptions

I have two questions regarding contract creation and exceptions: If contract A creates contract B and then throws in the same function, is B also deleted? If contract A creates contract B, and B ...
4
votes
1answer
254 views

Purpose of the function with the contract's name?

What is the purpose of the WithdrawalContract function in this case? If becomeRichest doing exactly same but with a certain condition and adding a pending withdrawal to the pendingWithdrawals.
4
votes
1answer
991 views

solc returns empty bin for contracts with constructor and inheritance

Having a simple contract structure like this: pragma solidity ^0.4.8; contract A { uint public a1; } contract B is A { uint8 public b1; } I can compile it with: > solc --combined-json "abi,...
3
votes
2answers
7k views

Does Solidity support passing an array of strings to a contract's constructor yet?

I am going through a tutorial from 2017 that says it was updated in January of 2018. It says that currently you can not pass an array of strings directly to the constructor of a contract and that you ...
3
votes
1answer
1k views

How are the arguments of the constructor encoded in the contract creation transaction?

The Ethereum ABI defines how arguments are encoded and passed when a method is called. But how are the arguments to the constructor encoded in the contract creation transaction which creates the smart ...
3
votes
1answer
2k views

Dynamic Arrays in solidity Structs

I have question about creating Struct where size of array passed to that struct could have different lengths. As in code below I could pass to this Recipe 2 ingredients, 3 ingredients or whatever ...
3
votes
2answers
864 views

Contract creating another contract in a single source file

When deployed as a single source file is the address stored in variable creator = TokenCreator(msg.sender) within function OwnedToken referring to the address of the contract TokenCreator or the ...
3
votes
1answer
188 views

How to get the actual runtime bytecode from creation bytecode and constructor arguments without deploying

Possible outputs of the Solidity compiler include creation bytecode and deployed (runtime) bytecode. However, this compiled deployed bytecode might not be the same as the one that will actually be ...
3
votes
3answers
701 views

Is a constructor needed to set a contract owner?

until now I have set the owner in a contract like so: pragma solidity ^0.5.2; contract SampleContract{ address payable owner = msg.sender; } in the docs it is suggested to do it this way: ...
3
votes
1answer
134 views

v0.4.22 solc compile failing - not predictable behavior

Error: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead. function CappedCrowdsale(uint256 _cap) public { ^ (...
3
votes
1answer
2k views

"The constructor should be payable if you send value" but constructor is NOT payable and didn't send any value

When copiling an ICO contract using http://remix.ethereum.org/ and JS VM environment the following error is thrown: "VM error: invalid opcode. The constructor should be payable if you send value. The ...
3
votes
0answers
676 views

Passing constructor arguments from child contract to parent contract

The ERC721 standard contract requires a token name and symbol in its constructor. I have inheritance pattern that looks likes Contract A is B and Contract B is ERC721. If I want to set my token name ...
2
votes
2answers
3k views

Contract deployment using web3.eth.contract(abi) with constructor arguments

I'm trying to deploy the following smart contract: pragma solidity ^0.5.00; contract TestContract { address public owner; string public word; constructor(string memory _word) public{ ...
2
votes
1answer
1k views

Can a function with the same name as the contract be declared both as a function and a constructor?

A function with the same name as the contract is normally a constructor and it’s bytecode isn’t included in the resulting contract. But is there a way to declare a function with the same name as the ...
2
votes
1answer
569 views

How do I set the constructor arguments when deploying using OpenZeppelin CLI?

My Token contract compiles fine using solidity 0.5.0: import "/Users/lennard/Desktop/GGToken/node_modules/@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol"; import "/Users/lennard/Desktop/...
2
votes
1answer
4k views

Error: This contract may be abstract

My solidity contract compiles in Remix, but when I try to deploy it it fails and throws Error: This contract may be abstract, not implement an abstract parent's methods completely or not invoke an ...
2
votes
1answer
558 views

How to pass arguments to token smart contract generator?

I'm trying to build a 'token generator' smart contract that accepts 3 arguments to then deploy a new token smart contract with those inputs ('strings' for symbol, name, 'address' for owner). I am a ...
2
votes
1answer
517 views

How can you create a contract within a contract and pass the value / payable amount?

I want to create another contract in a contract function call and pass the associated ether into that new contract. Seems like it makes sense to want to do but... Why doesn't this work? contract B ...
2
votes
1answer
2k views

Does 0x0 have a balance, and if so is it always 0?

I have a condition in a contract that checks if msg.value > patron.balance. At the top of the contract, I declare patron with address patron;. However, it is not set in the constructor, so patron ...
2
votes
1answer
76 views

can a hacker call constructor() function after deploy if it is public? [duplicate]

Is below function can be called by a hacker after deploy constructor() public { test = true; }
2
votes
1answer
29 views

Unexpected behaviour. Huge size increase when adding new SubContract()

I have a contract similar to: contract Contract01 { SubContract _refContract; ... constructor() public { refContract = new SubContract(); } function setSubContractRef(SubContract ...
2
votes
1answer
696 views

How can I retrieve the constructor arguments used during contract deployment

I know how to extract the constructor arguments out of the transaction object returned from a contract-deployment operation. It is explain in web3.js official documentation here. But this is not an ...
2
votes
1answer
558 views

How does a contract's constructor work and load input values?

I set up the following contract: pragma solidity ^0.4.25; contract Simplest { uint x; constructor(bool _lucky) public { if (_lucky == true) { x = 7; } else { ...
2
votes
1answer
591 views

Initializing fixed-size storage array given function parameter

I would like to store an array of fixed size in my contract storage, with a user-defined size, initialized to all 0s. Here's an example contract: pragma solidity ^0.4.24; contract Test { ...
2
votes
1answer
378 views

Solidity Constructor Parameter List

Which variables types are accepted by a constructor in Solidity? In particular, is it possible to pass an actual contract as an constructor input? For example: contract ContractA { constructor (...
2
votes
1answer
276 views

Getting and storing address of erc20 contract after dynamically creating it

I have a function which dynamically creates an ERC20 contract on calling a function in another contract as follows: function createDao(string _tokenName) public payable { tokenNumberIndex = ...
2
votes
0answers
244 views

Error! Invalid constructor arguments provided. Please verify that they are in ABI-encoded format, I tried (https://abi.hashex.org/) but still no luck

Good afternoon! I want to create a NFT and my code is working fine in remix. An error occurred while adding source code to ropsten: Error! Invalid constructor arguments provided. Please verify that ...
2
votes
0answers
448 views

Passing struct into new contract through constructor

I have a function in my ChangeManager contract which should create multiple ChangeRequests. I tried to model a Change by using a struct and pass this from my ChangeManager to the ChangeRequest: Code ...
2
votes
0answers
32 views

Why not possible limiting gas but forwarding ether in creating this contract from Solidity documentation

In the Solidity docs Creating contracts via new there is an example: contract D { uint x; function D(uint a) public payable { x = a; } } and it states in the accompanying text: ...
1
vote
1answer
5k views

How to verify a contract having constructor arguments?

This is a contract that requires an argument to be deployed. The Contract Name is "Asset". The compiler used is "Compiler 0.4.16+commit.d7661dd9.Emscripten.clang" and Optimization was Disabled. To ...
1
vote
1answer
722 views

Invalid implicit conversion from address[2] memory to address[] memory requested

This is so frustrating and I don't know why these errors don't give more information. I compiled and deployed a contract in remix using the left panel and everything is working fine. I then proceeded ...
1
vote
2answers
587 views

Remix: 0x0 Transaction mined but execution failed due to construction

I have the following example smart contract, where I cannot deploy using remix into my private network using environment Web3 provider. The error I am getting is Transaction mined but execution failed ...
1
vote
1answer
556 views

What does etherscan mean by "Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)"?

I'm inspecting a contract on etherscan, at the following URL: https://etherscan.io/address/0x5cb5f46a655c02889172323760d12d0e5d83cdaf#code At the bottom of the web-page, I see two sections: ...
1
vote
1answer
148 views

problem with constructor parameter in rawtransaction

I am using remix and ganache to test out my contract. The contract deploys successfully, however, I do not see the state variables initialized with the constructor parameters when I look at the ...
1
vote
2answers
187 views

Multiple constructor functions - Will they be executed once?

let's say I have the following contract/solidity file: pragma solidity ^0.4.20; contract partOne { address public owner; function partOne() public { owner = msg.sender; } } ...
1
vote
1answer
398 views

Best way to send an address to the constructor in Remix?

I know how to pass an ETH address to the constructor in truffle. What is the best way to do it in Remix? Say contract A is in Remix to create Tokens and contract B (another solidity file) is the ...
1
vote
1answer
352 views

Why use constructor function?

What is the big difference between: contract Crowdsale { MintableToken public token; uint256 public someInt; [...] function Crowdsale (uint256 _constructorArgument){ someInt = ...