Some changes to creation, events and modifiers.

This commit is contained in:
chriseth 2019-12-05 15:23:02 +01:00
parent 1890d17744
commit 5816c43df9
3 changed files with 25 additions and 17 deletions

View File

@ -8,16 +8,17 @@ Contracts can be created "from outside" via Ethereum transactions or from within
IDEs, such as `Remix <https://remix.ethereum.org/>`_, make the creation process seamless using UI elements. IDEs, such as `Remix <https://remix.ethereum.org/>`_, make the creation process seamless using UI elements.
Creating contracts programmatically on Ethereum is best done via using the JavaScript API `web3.js <https://github.com/ethereum/web3.js>`_. One way to create contracts programmatically on Ethereum is via the JavaScript API `web3.js <https://github.com/ethereum/web3.js>`_.
It has a function called `web3.eth.Contract <https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract>`_ It has a function called `web3.eth.Contract <https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#new-contract>`_
to facilitate contract creation. to facilitate contract creation.
When a contract is created, its :ref:`constructor <constructor>` (a function declared with the ``constructor`` keyword) is executed once. When a contract is created, its :ref:`constructor <constructor>` (a function declared with
the ``constructor`` keyword) is executed once.
A constructor is optional. Only one constructor is allowed, which means A constructor is optional. Only one constructor is allowed, which means
overloading is not supported. overloading is not supported.
After the constructor has executed, the final code of the contract is deployed to the After the constructor has executed, the final code of the contract is stored on the
blockchain. This code includes all public and external functions and all functions blockchain. This code includes all public and external functions and all functions
that are reachable from there through function calls. The deployed code does not that are reachable from there through function calls. The deployed code does not
include the constructor code or internal functions only called from the constructor. include the constructor code or internal functions only called from the constructor.
@ -57,18 +58,20 @@ This means that cyclic creation dependencies are impossible.
// See the next section for details. // See the next section for details.
owner = msg.sender; owner = msg.sender;
// We do an explicit type conversion from `address` // We perform an explicit type conversion from `address`
// to `TokenCreator` and assume that the type of // to `TokenCreator` and assume that the type of
// the calling contract is `TokenCreator`, there is // the calling contract is `TokenCreator`, there is
// no real way to check that. // no real way to verify that.
// This does not create a new contract.
creator = TokenCreator(msg.sender); creator = TokenCreator(msg.sender);
name = _name; name = _name;
} }
function changeName(bytes32 newName) public { function changeName(bytes32 newName) public {
// Only the creator can alter the name -- // Only the creator can alter the name.
// the comparison is possible since contracts // We compare the contract based on its
// are explicitly convertible to addresses. // address which can be retrieved by
// explicit conversion to address.
if (msg.sender == address(creator)) if (msg.sender == address(creator))
name = newName; name = newName;
} }
@ -94,9 +97,9 @@ This means that cyclic creation dependencies are impossible.
returns (OwnedToken tokenAddress) returns (OwnedToken tokenAddress)
{ {
// Create a new `Token` contract and return its address. // Create a new `Token` contract and return its address.
// From the JavaScript side, the return type is // From the JavaScript side, the return type
// `address`, as this is the closest type available in // of this function is `address`, as this is
// the ABI. // the closest type available in the ABI.
return new OwnedToken(name); return new OwnedToken(name);
} }

View File

@ -13,12 +13,12 @@ Events are inheritable members of contracts. When you call them, they cause the
arguments to be stored in the transaction's log - a special data structure arguments to be stored in the transaction's log - a special data structure
in the blockchain. These logs are associated with the address of the contract, in the blockchain. These logs are associated with the address of the contract,
are incorporated into the blockchain, and stay there as long as a block is are incorporated into the blockchain, and stay there as long as a block is
accessible (forever as of the Frontier and Homestead releases, but this might accessible (forever as of now, but this might
change with Serenity). The Log and its event data is not accessible from within change with Serenity). The Log and its event data is not accessible from within
contracts (not even from the contract that created them). contracts (not even from the contract that created them).
It is possible to request a simple payment verification (SPV) for logs, so if It is possible to request a Merkle proof for logs, so if
an external entity supplies a contract with such a verification, it can check an external entity supplies a contract with such a proof, it can check
that the log actually exists inside the blockchain. You have to supply block headers that the log actually exists inside the blockchain. You have to supply block headers
because the contract can only see the last 256 block hashes. because the contract can only see the last 256 block hashes.

View File

@ -6,9 +6,14 @@
Function Modifiers Function Modifiers
****************** ******************
Modifiers can be used to easily change the behaviour of functions. For example, Modifiers can be used to change the behaviour of functions in a declarative way.
they can automatically check a condition prior to executing the function. Modifiers are For example,
inheritable properties of contracts and may be overridden by derived contracts. you can use a modifier to automatically check a condition prior to executing the function.
Modifiers are
inheritable properties of contracts and may be overridden by derived contracts, but only
if they are marked ``virtual``. For details, please see
:ref:`Modifier Overriding <modifier-overriding>`.
:: ::