mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #2895 from ethereum/docs-creation
Overhaul contract creation section in the documentation
This commit is contained in:
commit
1eb49fb144
@ -16,51 +16,23 @@ inaccessible.
|
|||||||
Creating Contracts
|
Creating Contracts
|
||||||
******************
|
******************
|
||||||
|
|
||||||
Contracts can be created "from outside" or from Solidity contracts.
|
Contracts can be created "from outside" via Ethereum transactions or from within Solidity contracts.
|
||||||
|
|
||||||
|
IDEs, such as `Remix <https://remix.ethereum.org/>`_, make the creation process seamless using UI elements.
|
||||||
|
|
||||||
|
Creating contracts programatically on Ethereum is best done via using the JavaScript API `web3.js <https://github.com/etherem/web3.js>`_.
|
||||||
|
As of today it has a method called `web3.eth.Contract <https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract>`_
|
||||||
|
to facilitate contract creation.
|
||||||
|
|
||||||
When a contract is created, its constructor (a function with the same
|
When a contract is created, its constructor (a function with the same
|
||||||
name as the contract) is executed once.
|
name as the contract) is executed once.
|
||||||
|
|
||||||
A constructor is optional. Only one constructor is allowed, and this means
|
A constructor is optional. Only one constructor is allowed, and this means
|
||||||
overloading is not supported.
|
overloading is not supported.
|
||||||
|
|
||||||
From ``web3.js``, i.e. the JavaScript
|
|
||||||
API, this is done as follows::
|
|
||||||
|
|
||||||
// Need to specify some source including contract name for the data param below
|
|
||||||
var source = "contract CONTRACT_NAME { function CONTRACT_NAME(uint a, uint b) {} }";
|
|
||||||
|
|
||||||
// The json abi array generated by the compiler
|
|
||||||
var abiArray = [
|
|
||||||
{
|
|
||||||
"inputs":[
|
|
||||||
{"name":"x","type":"uint256"},
|
|
||||||
{"name":"y","type":"uint256"}
|
|
||||||
],
|
|
||||||
"type":"constructor"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"constant":true,
|
|
||||||
"inputs":[],
|
|
||||||
"name":"x",
|
|
||||||
"outputs":[{"name":"","type":"bytes32"}],
|
|
||||||
"type":"function"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
var MyContract_ = web3.eth.contract(source);
|
|
||||||
MyContract = web3.eth.contract(MyContract_.CONTRACT_NAME.info.abiDefinition);
|
|
||||||
// deploy new contract
|
|
||||||
var contractInstance = MyContract.new(
|
|
||||||
10,
|
|
||||||
11,
|
|
||||||
{from: myAccount, gas: 1000000}
|
|
||||||
);
|
|
||||||
|
|
||||||
.. index:: constructor;arguments
|
.. index:: constructor;arguments
|
||||||
|
|
||||||
Internally, constructor arguments are passed after the code of
|
Internally, constructor arguments are passed :ref:`ABI encoded <ABI>` after the code of
|
||||||
the contract itself, but you do not have to care about this
|
the contract itself, but you do not have to care about this if you use ``web3.js``.
|
||||||
if you use ``web3.js``.
|
|
||||||
|
|
||||||
If a contract wants to create another contract, the source code
|
If a contract wants to create another contract, the source code
|
||||||
(and the binary) of the created contract has to be known to the creator.
|
(and the binary) of the created contract has to be known to the creator.
|
||||||
|
Loading…
Reference in New Issue
Block a user