Questions tagged [constructor]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
16
votes
2answers
5k 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
567 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 ...
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
4k 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
2k 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 ...
5
votes
2answers
3k 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'...
5
votes
2answers
3k 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() { ...
4
votes
2answers
494 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
165 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
791 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
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
2answers
719 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
521 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 ...
3
votes
1answer
114 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 ...
2
votes
2answers
3k 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 ...
2
votes
2answers
764 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. ...
2
votes
1answer
635 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
926 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 ...
2
votes
1answer
479 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
219 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
208 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
120 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
3answers
109 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: ...
2
votes
0answers
285 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
27 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
2answers
529 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 { ...
1
vote
2answers
138 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
88 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
135 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
294 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
338 views

Why use constructor function?

What is the big difference between: contract Crowdsale { MintableToken public token; uint256 public someInt; [...] function Crowdsale (uint256 _constructorArgument){ someInt = ...
1
vote
1answer
2k 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
180 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 ...
1
vote
2answers
255 views

Encode constructor arguments used when deploying a contract

How can I encode the constructor arguments when deploying a contract? Here is my deployment function: async function deploy(abi, bin, args) { let contract = new this.web3.eth.Contract(JSON.parse(...
1
vote
1answer
120 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
94 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 ...
1
vote
1answer
47 views

Solidity Syntax question

What happens if a function is being passed before { into another function? Like DetailedERC20(_name, _symbol, TOKEN_DECIMALS) in this example: function MyToken(Registry _registry, string _name, ...
1
vote
1answer
100 views

Why would data be passed into a contract constructor rather than just hardcoding it, which would be less costly?

Doesn't it cost more to pass in variable assignment via a constructor due to changing state? Wouldn't it be cheaper to just assign them in the code? I am wondering if there are advantages to using ...
1
vote
1answer
197 views

Checking the length of bytes32[] in Contract constructor throws with Invalid Opcode

This is my Contract constructor: function MyContract(address _owner, bytes32[] _array) { owner = _owner; require(_array.length > 1); } The require(_array.length > 1) causes ...
1
vote
1answer
25 views

Payable constructor gives revert message

I am trying to execute the following contract on remixe IDE: pragma solidity ^0.5.4; contract GuessTheNumberChallenge { constructor() public payable { require(msg.value == 1 ether); } ...
1
vote
2answers
29 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 = ...
1
vote
1answer
55 views

Can you emit events in a contract constructor?

I have a constructor that is emitting an event, but cannot seem to see/catch/filter that event after deployment.
1
vote
1answer
124 views

Solidity : Error when pushing an address into a dynamic address type array

I'm trying to insert an address into an address type dynamic array. It works well when I do the operation inside a function. There are no errors and the elements are properly pushed into the array. ...
1
vote
1answer
352 views

Ethereum Solidity how to deploy a contract from another contract with constructor function

The old way: constructor function is the same as the contract name: contract CtrtFactory { function makeCtrt() { address ctrtAddress = new Ctrt(...); } } contract Ctrt() { function Ctrt(..) ...
1
vote
1answer
389 views

Is it ok to pass function's call as arguments to functions in Solidity?

So basically, is it ok to call fun() as an argument like ethToWeiConverter in the example below? Otherwise, what style is preferable? contract RefundableCrowdsale { uint256 cap; ...
1
vote
1answer
647 views

How to pass arguments of contract constructor in EVM?

In geth console, I know I can pass arguments via new. However if I just wanna use evm, how should it be done? For example, contract SetNumber { int public number; function SetNumber(int ...