Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [constructor]

The tag has no usage guidance.

2
votes
3answers
18 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: ...
0
votes
1answer
20 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{ ...
1
vote
2answers
20 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
30 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
23 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 ...
0
votes
1answer
28 views

Making a contract with new keyword address is always 0x0000000000000000000000000000000000000000

I'm making a contract that makes other contracts. Once the contract is made using address tokenAddress = new MyToken('Name'); I want to save the address of the contract, tokenAddress, in MyStruct. ...
1
vote
1answer
29 views

Solidity Constructor Parameter List

can someone list the parameter types accepted by a constructor in solidity .. in particular, can we do something like passing an actual contract in a constructor's parameter list, for example: ...
0
votes
1answer
20 views

Constructor Function: How can I tell which function is the constructor?

How can I tell which function is the constructor function? In the OpenZeppelin implementation of the MultisigWallet, OpenZeppelin claims this is the constructor function: /** * Constructor, sets ...
1
vote
1answer
36 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
39 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
1answer
74 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 { ...
1
vote
1answer
45 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
0answers
50 views

the constructor should be payable if you send

I have been testing the code and for some reason I am having a block. The error is described as "the constructor should be payable if you send value". The debugger points to " uint256 weiAmount = ...
0
votes
1answer
46 views

Smart Contract Testing

I am writing test driven smart contracts. There is one smart contract with parameterized constructor. And I am using the require statement in the constructor. I want to catch the error in my test ...
0
votes
1answer
27 views

When extending a contract how to call the external constructor and pass parameters?

I have the following contract import "zeppelin-solidity/contracts/token/ERC721/ERC721Token.sol"; contract Contract is ERC721Token { function Contract(string _name, string _symbol) public { ...
0
votes
1answer
173 views

Deploying a contract with a “payable” constructor and including input parameters?

There is a useful Solidity contract HERE. However, there are two things at time of its deployment: (1) The first thing is the contract balance since its constructor is payable. (2) And the second ...
0
votes
1answer
64 views

Solidity state variable resets to default inside function

For this project, I am coding smart contracts for Ethereum using Solidity. In the following code, the variable numCertificates should initially be 1. Nonetheless, newCertificateId is 0 when assigned. ...
0
votes
1answer
23 views

Advantage of using a contract contructor with parameters

What is the advantage of using a constructor with parameters instead of hard coding the values? Is there a possibility to reuse a once deployed contract?
0
votes
1answer
216 views

Fixing warning about “constructors” causes new error referencing the old function name [duplicate]

I'm using the solidity code for deploying new ERC20 tokens found here and quoted below: pragma solidity ^0.4.16; contract owned { address public owner; function owned() public { ...
3
votes
1answer
75 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 { ^ (...
0
votes
1answer
220 views

address.transfer(value) is throwing error in solidity contract.

I am building an approval contract in Remix and have been stuck on this issue. When I try to transfer wei using address.transfer(value), it throws an error. I have marked it in the code below. ...
2
votes
0answers
192 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
1answer
230 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 ...
1
vote
1answer
229 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
68 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; } } ...
0
votes
2answers
70 views

Correcting Code for Constructor

This StackExchange question ALMOST answered my question. However I don't know how to exactly correct my code so that the function named after the contract is replaced by the constructor. How ...
0
votes
0answers
51 views

Solidity library assignable variable

I have a library which contains a constant variable with a specific token address. Now I want to assign the address to this variable during the deploy of the library. So, is it possible to create a ...
1
vote
1answer
297 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(..) ...
0
votes
1answer
535 views

Using constructor() and the fallback function in the same contract gives error

I'm trying to compile something that mirror a pattern like this and getting the following error from remix: "browser/ballot.sol:26:3: DeclarationError: Function with same name and arguments defined ...
0
votes
1answer
472 views

Transfer ownership of a contract in the constructor

I'm trying to create a contract, and set its owner at the same time contract ERC721CrowdSale is Ownable { ... }//contract is in the same file and has access to methods contract CS_Creator is Ownable{...
0
votes
1answer
551 views

The constructor should be payable if you send value

I have written a contract as follows: pragma solidity ^0.4.0; /* Simple contract that mediates disputes using a trusted arbiter */ contract Taxi { enum State {IDLE, FINDING_DRIVER, ...
1
vote
1answer
560 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
1answer
334 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 ...
1
vote
2answers
944 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 ...
0
votes
0answers
96 views

How to find the constructor arguments ABI-encoded. to deploy a contract, ABI is hex encoded at the end of the compiled contract code

How to find the constructor arguments ABI-encoded. to deploy a contract, ABI is hex encoded at the end of the compiled contract code that you receive once contract is deployed but I can not seem to ...
2
votes
0answers
23 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: ...
0
votes
1answer
22 views

Setting mapping value in constructor has no effect?

In the following smart contract: pragma solidity ^0.4.20; contract MyCoin { uint _totalSupply; mapping(address => uint) public balances; function MyCoin() public { _totalSupply = 1; ...
1
vote
1answer
323 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. ...
1
vote
1answer
221 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 ...
0
votes
2answers
96 views

Access deployed contract's constructor's parameters from address

I'm using ethers.js but any answer will be okay ! I'm trying to access a contract's constructor's parameters that was used when deployed to make sure the contract has not been modified before being ...
1
vote
1answer
75 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 ...
0
votes
1answer
389 views

Constructor Arguments for verification on Etherscan.io

I want to verify a contract on Etherscan.io, but I am not able to find the correct Constructor Arguments. I tried everything which is explained on StackExchange, but cannot get it to verify... The ...
1
vote
1answer
305 views

Why use constructor function?

What is the big difference between: contract Crowdsale { MintableToken public token; uint256 public someInt; [...] function Crowdsale (uint256 _constructorArgument){ someInt = ...
15
votes
2answers
4k 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,...
4
votes
2answers
2k 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'...
1
vote
1answer
296 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; ...
2
votes
1answer
394 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 ...
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 ...
0
votes
1answer
1k 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 ...
0
votes
1answer
158 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 ...