19

It is quite complicated to get this correct, but here's some information on how to work it out. I'm using a simple contract as an example: pragma solidity ^0.4.2; contract Test { uint256 public v1; string public v2; function Test(uint256 _v1, string _v2) { v1 = _v1; v2 = _v2; } } Here is my deployment to a --dev ...


13

When you are trying to compile contract with private constructor, for example this one: pragma solidity ^0.4.15; contract PrivateConstructor { string public title; function PrivateConstructor() private { title = "Private Constructor"; } } you receive this error: TypeError: Constructor must be public or internal. So looks like ...


11

You can't assign a value to an array of size 0, you need to have enough space to write your value. It is true, you declared a variable size array but you still need to tell the VM to increase the array size before assign it. You can increase array size and assign values in different ways: All-in-one (increase and set, my preferred) using push method, like ...


6

If you look at Web3's documentation, you can get the full deployment bytecode with: var bytecodeWithParam = MyContract.new.getData( param1, param2, { data: compiledByteCode }); It is this bytecodeWithParam that you paste into the "Byte Code" field. If you look at it in detail, you will see param1 and param2 32-byte packed at the end.


5

A constructor can be internal or public; however, if the constructor has a 'payable' modifier, then the constructor can only be public. Regarding internal visibility, it makes it possible for an abstract contract to have a constructor.


5

Yes, a contract constructor can access it's own address. This code works just fine to both log the address, and set it in storage: pragma solidity ^0.4.0; contract Test { address public thisAddress; event LogAddr(address); function Test() { thisAddress = address(this); LogAddr(address(this)); } } This makes sense per this answer: The ...


5

Good Question! Lets explore the constructor inheritance: Lets look at the following example: import "dapple/test.sol"; contract A { bool aHit; bool public aPublicHit; bool public aConstructorArg; bool cHit; function A(bool _aConstructorArg) { aHit = true; aPublicHit = true; aConstructorArg = _aConstructorArg; } }...


5

payable means that you can transfer ether with the transaction. If the contract is designed that it needs an ether deposit on construction, then you can not hard code this. You have to allow the transaction sender to indeed send this ether to the contract.


5

You can actually use an array of byte32 as an argument to a solidity function as with this constructor function: constructor(bytes32[] memory proposalNames) public { chairperson = msg.sender; voters[chairperson].weight = 1; // For each of the provided proposal names, // create a new proposal object and add it // to the end of the array. ...


4

It's a critical bug in solc. Ref: https://github.com/ethereum/solidity/issues/485


4

1. Yes B will not be created if the transaction containing the creation of B has an exception. Example: pragma solidity ^0.4.8; contract A { B public b; function foo() { b = new B(); throw; } function getXfromB() returns (uint) { return b.x(); } } contract B { uint public x; function B() { x =...


4

According to the Ethereum Yellow Paper (section 4.2), the contract creation transaction does not have a data byte array where the arguments to method calls are placed. The contract creation only has the init byte array so the arguments to the constructor must go in there. The init byte array is also where the byte code of the smart contract logic is placed. ...


3

You need to correctly ABI-encode constructor arguments. You can easily do this with online tool https://abi.hashex.org. Just paste in abi to automatically parse constructor parameter types or add them manually.


3

You need parentheses around new B (see below): contract A { function createAndPassAmount(uint arg, uint amount) payable { B newD = (new B).value(amount)(arg); } }


3

The detail of contract creation are in Yellow Paper section 7, but it is too technical to quote here. But in section 7.1. Subtleties is more quotable Note that while the initialisation code is executing, the newly created address exists but with no intrinsic body code. Constructor is executed in the context of the new address, so the address can be used ...


3

The second version will only work with hardcoded values. How would you instantiate a new object with different paramters, dynamically? You can't do this: Crowdsale c = new Crowdsale(10);


3

In creator = TokenCreator(msg.sender); creator has the address of msg.sender. msg.sender is the address of the account that creates OwnedToken. There are 2 cases: an external account that creates OwnedToken OR a contract, like TokenCreator, that creates an OwnedToken (via new OwnedToken(...)). When deployed as a single source file, only the last ...


3

Now it is very easy with ABI-encoded Online Converter Step 1: Add parameters that you used to construct your contract Step 2: Click Generate ABI Step 3: Copy ABI-Encoded Step 4: Back to Etherscan and pasted it to ABI-Encoded Textbox


3

Agree with Guiseppe. The code isn't "initializing" new ob, just referencing existing. Here's a way to have many mappings that don't collide like that. Possibly will give you some ideas. pragma solidity ^0.4.0; contract TestcaseResetObject { uint public objectCount; struct MyObjectStruct { mapping(uint => uint) map; } ...


3

Remix will see the constructor needs an argument so it will put a field next to the Create button, in this case labeled _tokenAddress. Put the token contract address there, in quotes. In case it isn't clear, deploy the token contract first and copy the deployed address to the clipboard and then paste into Crowdsale's Create (constructor) function. Hope ...


3

I believe this exact scenario is covered by the Solidity: Inheritance > Arguments for Base Constructors Here is the example they give: pragma solidity ^0.4.22; contract Base { uint x; constructor(uint _x) public { x = _x; } } contract Derived2 is Base { constructor(uint _y) Base(_y * _y) public {} } So for you, it would look something like ...


3

In Solidity version 0.4.24 declaring a function with the contracts name as as constructor has been depreciated in favor for constructor() If you are using an older version of Solidity than 0.4.24, you declare the function as function contractName(). Similar to this answer constructors are called once, and only once. They are initiated once a contract ...


3

I have done similar work , what i found different was --> you have to pass data in you contract constructor as shown below :- var data = require('../contractABI/data'); var contract_abi = abiData.abi; var demo_Contract = web3.eth.contract(contract_abi); //getting public keys var owner_add = <address of owner , you can use your address like sender/rec ...


2

Mapping is a really particular type and it's different from a simple array. From the official documentation: Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is ...


2

I suspect you are talking about the case when the arguments for the constructor are known in advance before deployment. In this case you are right it's more efficient to get rid of the constructor parameters and add them as constants in the code thus potentially eliminating a few state variables and saving on gas. There are cases, however, when it's not ...


2

You cannot construct A if you do not give it a parameter. This will give you a binary for B: contract A { uint public a1; function A (address _a) { } } contract B is A { uint8 public b1; function B(address _a) A(_a) { } } This too: contract A { uint public a1; function A (address _a) { } } contract B is A { uint8 public b1; ...


2

Yes, your understanding is correct. As pointed out in the docs you linked to, Solidity follows C3 Linearization in which the order of execution is determined by the order of the declaration at the contract level and has nothing to do with the order in which you call/pass arguments up from the constructor signature. Solidity follows the path of Python and ...


2

From https://solidity.readthedocs.io/en/v0.4.21/types.html#array-literals-inline-arrays: Note that currently, fixed size memory arrays cannot be assigned to dynamically-sized memory arrays, i.e. the following is not possible: // This will not compile. pragma solidity ^0.4.0; contract C { function f() public { // The next line creates a type ...


2

When you deploy partOne its constructor will be called once. When you deploy partTwo the partOne's constructor will be invoked before the partTwo's one both only once.


Only top voted, non community-wiki answers of a minimum length are eligible