Contract creation

The second option is to deploy a new instance of a given contract. To achieve that, Solidity provides the new keyword, which can be used as follows:

contract ContractA {
uint256 x;

function ContractA (uint256 y) payable {
x = y;
}
}

contract ContractB {
ContractA CAinstance = new ContractA(10);
}

As you can see, we can pass arguments when we create a contract, since the created contract has a constructor that accepts arguments. The new ContractA(arg) line will execute the created contract's constructor and return its address. Note that an instance of each smart contract is implicitly convertible to its address, and in Solidity we are not dealing with real objects as in OOP to represent contracts as we do for classes.

Moreover, you can send ether from your source contract to the newly created contract using the .value() option, as follows:

 ContractA CAinstance = (new ContractA).value(amount)(arg);

Once the instance is created, the caller contract can pass messages to call remote methods or to read or edit remote states. Let’s see how to perform such calls.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset