2015-12-07 20:16:25 +00:00
##################################
Expressions and Control Structures
##################################
2016-11-23 13:47:58 +00:00
.. index :: ! parameter, parameter;input, parameter;output
Input Parameters and Output Parameters
======================================
2016-11-24 17:32:45 +00:00
As in Javascript, functions may take parameters as input;
2016-11-23 13:47:58 +00:00
unlike in Javascript and C, they may also return arbitrary number of
parameters as output.
Input Parameters
----------------
2018-09-20 12:50:26 +00:00
The input parameters are declared the same way as variables are.
The name of unused parameters can be omitted.
2016-11-23 13:47:58 +00:00
For example, suppose we want our contract to
accept one kind of external calls with two integers, we would write
something like::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2017-07-10 21:58:23 +00:00
2016-11-23 13:47:58 +00:00
contract Simple {
2018-08-09 13:36:00 +00:00
uint sum;
function taker(uint _a, uint _b) public {
sum = _a + _b;
2016-11-23 13:47:58 +00:00
}
}
2018-09-20 12:50:26 +00:00
Input parameters can be used just as any other local variable
can be used, they can also be assigned to.
2016-11-23 13:47:58 +00:00
Output Parameters
-----------------
The output parameters can be declared with the same syntax after the
`` returns `` keyword. For example, suppose we wished to return two results:
the sum and the product of the two given integers, then we would
write::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2017-07-10 21:58:23 +00:00
2016-11-23 13:47:58 +00:00
contract Simple {
2018-07-10 07:17:33 +00:00
function arithmetic(uint _a, uint _b)
2017-12-12 18:47:30 +00:00
public
pure
returns (uint o_sum, uint o_product)
{
2016-11-23 13:47:58 +00:00
o_sum = _a + _b;
o_product = _a * _b;
}
}
The names of output parameters can be omitted.
2018-09-20 12:50:26 +00:00
The output values can also be specified using `` return `` statements,
which are also capable of :ref: `returning multiple values<multi-return>` .
Return parameters can be used as any other local variable and they
are zero-initialized; if they are not explicitly
set, they stay zero.
2016-11-23 13:47:58 +00:00
2016-07-30 07:13:05 +00:00
.. index :: if, else, while, do/while, for, break, continue, return, switch, goto
2015-12-07 20:16:25 +00:00
Control Structures
===================
2018-09-20 12:50:26 +00:00
Most of the control structures known from curly-braces languages are available in Solidity:
There is: `` if `` , `` else `` , `` while `` , `` do `` , `` for `` , `` break `` , `` continue `` , `` return `` , with
2016-08-16 13:06:08 +00:00
the usual semantics known from C or JavaScript.
2015-12-07 20:16:25 +00:00
Parentheses can *not* be omitted for conditionals, but curly brances can be omitted
around single-statement bodies.
Note that there is no type conversion from non-boolean to boolean types as
2016-11-23 13:47:58 +00:00
there is in C and JavaScript, so `` if (1) { ... } `` is *not* valid
Solidity.
2016-11-24 11:22:57 +00:00
.. _multi-return:
2016-11-23 13:47:58 +00:00
Returning Multiple Values
-------------------------
When a function has multiple output parameters, `` return (v0, v1, ...,
vn)`` can return multiple values. The number of components must be
the same as the number of output parameters.
2015-12-07 20:16:25 +00:00
.. index :: ! function;call, function;internal, function;external
2016-02-19 10:30:00 +00:00
.. _function-calls:
2015-12-07 20:16:25 +00:00
Function Calls
==============
Internal Function Calls
-----------------------
Functions of the current contract can be called directly ("internally"), also recursively, as seen in
this nonsensical example::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2017-07-10 21:58:23 +00:00
2016-05-18 15:11:39 +00:00
contract C {
2018-08-09 13:36:00 +00:00
function g(uint a) public pure returns (uint ret) { return a + f(); }
2017-12-12 18:47:30 +00:00
function f() internal pure returns (uint ret) { return g(7) + f(); }
2015-12-07 20:16:25 +00:00
}
These function calls are translated into simple jumps inside the EVM. This has
the effect that the current memory is not cleared, i.e. passing memory references
to internally-called functions is very efficient. Only functions of the same
contract can be called internally.
2018-09-20 12:50:26 +00:00
You should still avoid excessive recursion, as every internal function call
uses up at least one stack slot and there are at most 1024 slots available.
2015-12-07 20:16:25 +00:00
External Function Calls
-----------------------
2017-01-20 20:52:36 +00:00
The expressions `` this.g(8); `` and `` c.g(2); `` (where `` c `` is a contract
2016-09-05 14:29:08 +00:00
instance) are also valid function calls, but this time, the function
2015-12-07 20:16:25 +00:00
will be called "externally", via a message call and not directly via jumps.
2016-09-05 14:29:08 +00:00
Please note that function calls on `` this `` cannot be used in the constructor, as the
actual contract has not been created yet.
2015-12-07 20:16:25 +00:00
Functions of other contracts have to be called externally. For an external call,
all function arguments have to be copied to memory.
2017-03-15 23:50:58 +00:00
When calling functions of other contracts, the amount of Wei sent with the call and
the gas can be specified with special options `` .value() `` and `` .gas() `` , respectively::
2016-05-11 19:48:59 +00:00
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.0 <0.6.0;
2017-07-10 21:58:23 +00:00
2015-12-07 20:16:25 +00:00
contract InfoFeed {
2017-12-12 18:47:30 +00:00
function info() public payable returns (uint ret) { return 42; }
2015-12-07 20:16:25 +00:00
}
2016-05-18 15:05:28 +00:00
2015-12-07 20:16:25 +00:00
contract Consumer {
2016-05-11 19:53:45 +00:00
InfoFeed feed;
2018-08-22 12:28:43 +00:00
function setFeed(InfoFeed addr) public { feed = addr; }
2017-12-13 07:55:46 +00:00
function callFeed() public { feed.info.value(10).gas(800)(); }
2015-12-07 20:16:25 +00:00
}
2018-08-22 12:28:43 +00:00
You need to use the modifier `` payable `` with the `` info `` function because
otherwise, the `` .value() `` option would not be available.
2016-09-05 14:29:08 +00:00
2018-08-22 12:28:43 +00:00
.. warning ::
2018-09-20 12:50:26 +00:00
Be careful that `` feed.info.value(10).gas(800) `` only locally sets the `` value `` and amount of `` gas `` sent with the function call, and the parentheses at the end perform the actual call. So in this case, the function is not called.
2015-12-07 20:16:25 +00:00
2016-08-07 17:46:11 +00:00
Function calls cause exceptions if the called contract does not exist (in the
sense that the account does not contain code) or if the called contract itself
throws an exception or goes out of gas.
2016-06-28 15:29:08 +00:00
.. warning ::
2016-06-29 18:12:07 +00:00
Any interaction with another contract imposes a potential danger, especially
2018-08-09 19:10:53 +00:00
if the source code of the contract is not known in advance. The
current contract hands over control to the called contract and that may potentially
2016-07-07 20:34:05 +00:00
do just about anything. Even if the called contract inherits from a known parent contract,
the inheriting contract is only required to have a correct interface. The
implementation of the contract, however, can be completely arbitrary and thus,
pose a danger. In addition, be prepared in case it calls into other contracts of
2016-06-29 18:12:07 +00:00
your system or even back into the calling contract before the first
2016-06-28 15:29:08 +00:00
call returns. This means
2016-07-07 17:53:57 +00:00
that the called contract can change state variables of the calling contract
via its functions. Write your functions in a way that, for example, calls to
2016-06-29 18:12:07 +00:00
external functions happen after any changes to state variables in your contract
2016-08-16 09:22:24 +00:00
so your contract is not vulnerable to a reentrancy exploit.
2016-06-28 15:29:08 +00:00
2015-12-07 20:16:25 +00:00
Named Calls and Anonymous Function Parameters
---------------------------------------------
2018-09-20 12:50:26 +00:00
Function call arguments can be given by name, in any order,
2016-08-17 09:04:40 +00:00
if they are enclosed in `` { } `` as can be seen in the following
example. The argument list has to coincide by name with the list of
parameters from the function declaration, but can be in arbitrary order.
2015-12-07 20:16:25 +00:00
::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.0 <0.6.0;
2016-09-05 11:54:54 +00:00
2016-05-18 15:11:39 +00:00
contract C {
2018-08-09 13:36:00 +00:00
mapping(uint => uint) data;
function f() public {
set({value: 2, key: 3});
2017-07-10 22:07:27 +00:00
}
2016-05-18 15:05:28 +00:00
2018-08-09 13:36:00 +00:00
function set(uint key, uint value) public {
data[key] = value;
2016-05-11 19:53:45 +00:00
}
2018-08-09 13:36:00 +00:00
2016-08-16 13:30:44 +00:00
}
2016-05-18 15:05:28 +00:00
2016-08-17 09:04:40 +00:00
Omitted Function Parameter Names
--------------------------------
The names of unused parameters (especially return parameters) can be omitted.
2017-11-04 01:19:59 +00:00
Those parameters will still be present on the stack, but they are inaccessible.
2016-05-18 15:05:28 +00:00
2016-08-16 13:30:44 +00:00
::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2016-09-05 11:54:54 +00:00
2016-08-16 13:30:44 +00:00
contract C {
2016-08-16 07:41:50 +00:00
// omitted name for parameter
2017-12-12 18:47:30 +00:00
function func(uint k, uint) public pure returns(uint) {
2016-05-11 19:53:45 +00:00
return k;
}
2015-12-07 20:16:25 +00:00
}
2017-09-03 09:48:48 +00:00
2015-12-07 20:16:25 +00:00
2016-07-11 13:04:33 +00:00
.. index :: ! new, contracts;creating
.. _creating-contracts:
2016-08-18 17:03:41 +00:00
Creating Contracts via `` new ``
==============================
2016-07-11 13:04:33 +00:00
2018-09-20 12:50:26 +00:00
A contract can create other contracts using the `` new `` keyword. The full
code of the contract being created has to be known when the creating contract
is compiled so recursive creation-dependencies are not possible.
2016-07-11 13:04:33 +00:00
::
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2016-09-05 11:54:54 +00:00
2016-07-11 13:04:33 +00:00
contract D {
2018-08-09 13:36:00 +00:00
uint public x;
2018-07-02 14:25:54 +00:00
constructor(uint a) public payable {
2016-07-11 13:04:33 +00:00
x = a;
}
}
2016-08-18 17:03:41 +00:00
2016-07-11 13:04:33 +00:00
contract C {
D d = new D(4); // will be executed as part of C's constructor
2017-12-12 18:47:30 +00:00
function createD(uint arg) public {
2016-07-11 13:04:33 +00:00
D newD = new D(arg);
2018-08-09 13:36:00 +00:00
newD.x();
2016-07-11 13:04:33 +00:00
}
2017-12-12 18:47:30 +00:00
function createAndEndowD(uint arg, uint amount) public payable {
2016-07-11 13:04:33 +00:00
// Send ether along with the creation
D newD = (new D).value(amount)(arg);
2018-08-09 13:36:00 +00:00
newD.x();
2016-07-11 13:04:33 +00:00
}
}
2018-09-20 12:50:26 +00:00
As seen in the example, it is possible to send Ether while creating
2017-09-03 09:48:48 +00:00
an instance of `` D `` using the `` .value() `` option, but it is not possible
to limit the amount of gas.
If the creation fails (due to out-of-stack, not enough balance or other problems),
an exception is thrown.
2016-07-11 13:04:33 +00:00
2015-12-07 20:16:25 +00:00
Order of Evaluation of Expressions
==================================
The evaluation order of expressions is not specified (more formally, the order
in which the children of one node in the expression tree are evaluated is not
specified, but they are of course evaluated before the node itself). It is only
guaranteed that statements are executed in order and short-circuiting for
2016-07-28 19:16:52 +00:00
boolean expressions is done. See :ref: `order` for more information.
2015-12-07 20:16:25 +00:00
.. index :: ! assignment
Assignment
==========
.. index :: ! assignment;destructuring
Destructuring Assignments and Returning Multiple Values
-------------------------------------------------------
2018-09-20 12:50:26 +00:00
Solidity internally allows tuple types, i.e. a list of objects of potentially different types whose number is a constant at compile-time. Those tuples can be used to return multiple values at the same time.
These can then either be assigned to newly declared variables or to pre-existing variables (or LValues in general).
Tuples are not proper types in Solidity, they can only be used to form syntactic
groupings of expressions.
2015-12-07 20:16:25 +00:00
2018-04-30 11:30:09 +00:00
::
2018-10-08 21:01:52 +00:00
pragma solidity >0.4.23 <0.6.0;
2017-07-10 21:58:23 +00:00
2015-12-07 20:16:25 +00:00
contract C {
2016-05-11 19:53:45 +00:00
uint[] data;
2017-12-12 18:47:30 +00:00
function f() public pure returns (uint, bool, uint) {
2016-05-11 19:53:45 +00:00
return (7, true, 2);
}
2017-12-12 18:47:30 +00:00
function g() public {
2018-08-09 13:36:00 +00:00
// Variables declared with type and assigned from the returned tuple,
2018-08-09 19:10:53 +00:00
// not all elements have to be specified (but the number must match).
2018-08-09 13:36:00 +00:00
(uint x, , uint y) = f();
2016-05-11 19:53:45 +00:00
// Common trick to swap values -- does not work for non-value storage types.
(x, y) = (y, x);
// Components can be left out (also for variable declarations).
2018-08-09 13:36:00 +00:00
(data.length, , ) = f(); // Sets the length to 7
2016-05-11 19:53:45 +00:00
}
2015-12-07 20:16:25 +00:00
}
2018-09-20 12:50:26 +00:00
It is not possible to mix variable declarations and non-declaration assignments,
i.e. the following is not valid: `` (x, uint y) = (1, 2); ``
2018-05-04 14:27:21 +00:00
.. note ::
2018-07-05 11:58:33 +00:00
Prior to version 0.5.0 it was possible to assign to tuples of smaller size, either
2018-05-04 14:27:21 +00:00
filling up on the left or on the right side (which ever was empty). This is
2018-07-05 11:58:33 +00:00
now disallowed, so both sides have to have the same number of components.
2018-05-04 14:27:21 +00:00
2018-09-20 12:50:26 +00:00
.. warning ::
Be careful when assigning to multiple variables at the same time when
reference types are involved, because it could lead to unexpected
copying behaviour.
2015-12-07 20:16:25 +00:00
Complications for Arrays and Structs
------------------------------------
2018-09-20 12:50:26 +00:00
The semantics of assignments are a bit more complicated for non-value types like arrays and structs.
2016-05-24 17:57:36 +00:00
Assigning *to* a state variable always creates an independent copy. On the other hand, assigning to a local variable creates an independent copy only for elementary types, i.e. static types that fit into 32 bytes. If structs or arrays (including `` bytes `` and `` string `` ) are assigned from a state variable to a local variable, the local variable holds a reference to the original state variable. A second assignment to the local variable does not modify the state but only changes the reference. Assignments to members (or elements) of the local variable *do* change the state.
2015-12-07 20:16:25 +00:00
2016-07-04 14:51:02 +00:00
.. index :: ! scoping, declarations, default value
.. _default-value:
2015-12-07 20:16:25 +00:00
2016-05-11 21:02:28 +00:00
Scoping and Declarations
========================
2016-05-11 19:47:05 +00:00
2016-07-05 15:44:39 +00:00
A variable which is declared will have an initial default value whose byte-representation is all zeros.
2016-06-22 17:25:26 +00:00
The "default values" of variables are the typical "zero-state" of whatever the type is. For example, the default value for a `` bool ``
2016-07-05 15:44:39 +00:00
is `` false `` . The default value for the `` uint `` or `` int `` types is `` 0 `` . For statically-sized arrays and `` bytes1 `` to `` bytes32 `` , each individual
2016-06-22 17:25:26 +00:00
element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, `` bytes ``
2016-07-08 18:07:58 +00:00
and `` string `` , the default value is an empty array or string.
2016-05-11 19:47:05 +00:00
2018-06-15 10:30:28 +00:00
Scoping in Solidity follows the widespread scoping rules of C99
2018-02-15 10:43:53 +00:00
(and many other languages): Variables are visible from the point right after their declaration
2018-09-20 12:50:26 +00:00
until the end of the smallest `` { } `` -block that contains the declaration. As an exception to this rule, variables declared in the
2018-02-15 10:43:53 +00:00
initialization part of a for-loop are only visible until the end of the for-loop.
Variables and other items declared outside of a code block, for example functions, contracts,
2018-09-20 12:50:26 +00:00
user-defined types, etc., are visible even before they were declared. This means you can
2018-02-15 10:43:53 +00:00
use state variables before they are declared and call functions recursively.
As a consequence, the following examples will compile without warnings, since
2018-06-15 10:30:28 +00:00
the two variables have the same name but disjoint scopes.
2018-02-15 10:43:53 +00:00
::
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2018-02-15 10:43:53 +00:00
contract C {
function minimalScoping() pure public {
{
2018-08-09 13:36:00 +00:00
uint same;
same = 1;
2018-02-15 10:43:53 +00:00
}
{
2018-08-09 13:36:00 +00:00
uint same;
same = 3;
2018-02-15 10:43:53 +00:00
}
}
}
As a special example of the C99 scoping rules, note that in the following,
the first assignment to `` x `` will actually assign the outer and not the inner variable.
In any case, you will get a warning about the outer variable being shadowed.
::
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2018-08-09 13:36:00 +00:00
// This will report a warning
2018-02-15 10:43:53 +00:00
contract C {
function f() pure public returns (uint) {
uint x = 1;
{
x = 2; // this will assign to the outer variable
uint x;
}
return x; // x has value 2
}
}
2018-06-15 10:30:28 +00:00
.. warning ::
Before version 0.5.0 Solidity followed the same scoping rules as JavaScript, that is, a variable declared anywhere within a function would be in scope
2018-09-20 12:50:26 +00:00
for the entire function, regardless where it was declared. The following example shows a code snippet that used
2018-06-15 10:30:28 +00:00
to compile but leads to an error starting from version 0.5.0.
::
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2018-08-09 13:36:00 +00:00
// This will not compile
2018-06-15 10:30:28 +00:00
contract C {
function f() pure public returns (uint) {
x = 2;
uint x;
return x;
}
}
2017-06-15 16:36:16 +00:00
.. index :: ! exception, ! throw, ! assert, ! require, ! revert
2016-07-04 14:51:02 +00:00
2018-09-20 12:27:02 +00:00
.. _assert-and-require:
2017-06-15 16:36:16 +00:00
Error handling: Assert, Require, Revert and Exceptions
======================================================
Solidity uses state-reverting exceptions to handle errors. Such an exception will undo all changes made to the
state in the current call (and all its sub-calls) and also flag an error to the caller.
The convenience functions `` assert `` and `` require `` can be used to check for conditions and throw an exception
2017-07-25 23:48:48 +00:00
if the condition is not met. The `` assert `` function should only be used to test for internal errors, and to check invariants.
2017-09-03 09:48:48 +00:00
The `` require `` function should be used to ensure valid conditions, such as inputs, or contract state variables are met, or to validate return values from calls to external contracts.
2017-07-26 15:02:06 +00:00
If used properly, analysis tools can evaluate your contract to identify the conditions and function calls which will reach a failing `` assert `` . Properly functioning code should never reach a failing assert statement; if this happens there is a bug in your contract which you should fix.
2015-12-07 20:16:25 +00:00
2017-07-25 23:48:48 +00:00
There are two other ways to trigger exceptions: The `` revert `` function can be used to flag an error and
2017-12-30 12:46:02 +00:00
revert the current call. It is possible to provide a string message containing details about the error
that will be passed back to the caller.
2017-06-15 16:36:16 +00:00
2017-08-28 11:03:39 +00:00
.. note ::
2018-09-20 12:55:33 +00:00
There used to be a keyword called `` throw `` with the same semantics as `` revert() `` which
whas deprecated in version 0.4.13 and removed in version 0.5.0.
2017-08-28 11:03:39 +00:00
2017-09-03 09:48:48 +00:00
When exceptions happen in a sub-call, they "bubble up" (i.e. exceptions are rethrown) automatically. Exceptions to this rule are `` send ``
2018-09-20 12:55:33 +00:00
and the low-level functions `` call `` , `` delegatecall `` and `` staticcall `` -- those return `` false `` as their first return value in case
2017-06-16 17:14:21 +00:00
of an exception instead of "bubbling up".
2015-12-07 20:16:25 +00:00
2017-08-01 11:54:59 +00:00
.. warning ::
2018-09-20 12:55:33 +00:00
The low-level functions `` call `` , `` delegatecall `` and `` staticcall `` return `` true `` as their first return value if the called account is non-existent, as part of the design of EVM. Existence must be checked prior to calling if desired.
2017-08-01 11:54:59 +00:00
2015-12-07 20:16:25 +00:00
Catching exceptions is not yet possible.
2017-06-15 16:36:16 +00:00
In the following example, you can see how `` require `` can be used to easily check conditions on inputs
2018-01-03 14:30:01 +00:00
and how `` assert `` can be used for internal error checking. Note that you can optionally provide
2018-04-06 14:11:52 +00:00
a message string for `` require `` , but not for `` assert `` .
2018-01-03 14:30:01 +00:00
::
2015-12-07 20:16:25 +00:00
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2016-09-05 14:29:08 +00:00
2015-12-07 20:16:25 +00:00
contract Sharer {
2018-09-05 15:59:55 +00:00
function sendHalf(address payable addr) public payable returns (uint balance) {
2018-01-03 14:30:01 +00:00
require(msg.value % 2 == 0, "Even value required.");
2018-07-05 12:32:32 +00:00
uint balanceBeforeTransfer = address(this).balance;
2017-06-15 16:36:16 +00:00
addr.transfer(msg.value / 2);
// Since transfer throws an exception on failure and
// cannot call back here, there should be no way for us to
// still have half of the money.
2018-07-05 12:32:32 +00:00
assert(address(this).balance == balanceBeforeTransfer - msg.value / 2);
return address(this).balance;
2015-12-07 20:16:25 +00:00
}
}
2017-06-16 17:14:21 +00:00
An `` assert `` -style exception is generated in the following situations:
2015-12-07 20:16:25 +00:00
2017-01-20 21:53:45 +00:00
#. If you access an array at a too large or negative index (i.e. `` x[i] `` where `` i >= x.length `` or `` i < 0 `` ).
#. If you access a fixed-length `` bytesN `` at a too large or negative index.
#. If you divide or modulo by zero (e.g. `` 5 / 0 `` or `` 23 % 0 `` ).
#. If you shift by a negative amount.
#. If you convert a value too big or negative into an enum type.
2017-01-26 15:28:44 +00:00
#. If you call a zero-initialized variable of internal function type.
2017-03-09 16:03:53 +00:00
#. If you call `` assert `` with an argument that evaluates to false.
2015-12-07 20:16:25 +00:00
2017-06-15 16:36:16 +00:00
A `` require `` -style exception is generated in the following situations:
2017-03-16 00:43:25 +00:00
2017-03-09 16:03:53 +00:00
#. Calling `` require `` with an argument that evaluates to `` false `` .
2018-08-15 15:06:42 +00:00
#. If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation `` call `` , `` send `` , `` delegatecall `` , `` callcode `` or `` staticcall `` is used. The low level operations never throw exceptions but indicate failures by returning `` false `` .
2017-06-15 16:36:16 +00:00
#. If you create a contract using the `` new `` keyword but the contract creation does not finish properly (see above for the definition of "not finish properly").
#. If you perform an external function call targeting a contract that contains no code.
#. If your contract receives Ether via a public function without `` payable `` modifier (including the constructor and the fallback function).
#. If your contract receives Ether via a public getter function.
#. If a `` .transfer() `` fails.
2017-02-10 13:31:40 +00:00
2017-06-15 16:36:16 +00:00
Internally, Solidity performs a revert operation (instruction `` 0xfd `` ) for a `` require `` -style exception and executes an invalid operation
(instruction `` 0xfe `` ) to throw an `` assert `` -style exception. In both cases, this causes
the EVM to revert all changes made to the state. The reason for reverting is that there is no safe way to continue execution, because an expected effect
2017-01-26 15:28:44 +00:00
did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction
2017-06-15 16:36:16 +00:00
(or at least call) without effect. Note that `` assert `` -style exceptions consume all gas available to the call, while
2017-07-18 12:25:17 +00:00
`` require `` -style exceptions will not consume any gas starting from the Metropolis release.
2017-12-30 12:46:02 +00:00
2018-01-03 14:30:01 +00:00
The following example shows how an error string can be used together with revert and require:
2017-12-30 12:46:02 +00:00
::
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2017-12-30 12:46:02 +00:00
contract VendingMachine {
2018-07-04 16:04:44 +00:00
function buy(uint amount) public payable {
2017-12-30 12:46:02 +00:00
if (amount > msg.value / 2 ether)
revert("Not enough Ether provided.");
2018-01-03 14:30:01 +00:00
// Alternative way to do it:
require(
amount <= msg.value / 2 ether,
"Not enough Ether provided."
);
2017-12-30 12:46:02 +00:00
// Perform the purchase.
}
}
2018-04-06 14:11:52 +00:00
The provided string will be :ref: `abi-encoded <ABI>` as if it were a call to a function `` Error(string) `` .
In the above example, `` revert("Not enough Ether provided."); `` will cause the following hexadecimal data be
set as error return data:
.. code ::
0x08c379a0 // Function selector for Error(string)
0x0000000000000000000000000000000000000000000000000000000000000020 // Data offset
0x000000000000000000000000000000000000000000000000000000000000001a // String length
0x4e6f7420656e6f7567682045746865722070726f76696465642e000000000000 // String data