2015-12-07 20:16:25 +00:00
###########################
Frequently Asked Questions
###########################
2016-05-18 15:18:39 +00:00
This list was originally compiled by `fivedogit <mailto:fivedogit@gmail.com> `_ .
2015-12-07 20:16:25 +00:00
***** ***** *****
Basic Questions
***** ***** *****
What is the transaction "payload"?
==================================
This is just the bytecode "data" sent along with the request.
2018-07-12 12:52:12 +00:00
2015-12-07 20:16:25 +00:00
Create a contract that can be killed and return funds
=====================================================
First, a word of warning: Killing contracts sounds like a good idea, because "cleaning up"
is always good, but as seen above, it does not really clean up. Furthermore,
if Ether is sent to removed contracts, the Ether will be forever lost.
2016-05-05 19:06:04 +00:00
If you want to deactivate your contracts, it is preferable to **disable** them by changing some
2015-12-07 20:16:25 +00:00
internal state which causes all functions to throw. This will make it impossible
to use the contract and ether sent to the contract will be returned automatically.
2016-05-24 17:57:36 +00:00
Now to answering the question: Inside a constructor, `` msg.sender `` is the
creator. Save it. Then `` selfdestruct(creator); `` to kill and return funds.
2015-12-07 20:16:25 +00:00
`example <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/05_greeter.sol> `_
2016-05-24 17:57:36 +00:00
Note that if you `` import "mortal" `` at the top of your contracts and declare
`` contract SomeContract is mortal { ... `` and compile with a compiler that already
2017-03-15 22:58:14 +00:00
has it (which includes `Remix <https://remix.ethereum.org/> `_ ), then
2016-05-24 17:57:36 +00:00
`` kill() `` is taken care of for you. Once a contract is "mortal", then you can
`` contractname.kill.sendTransaction({from:eth.coinbase}) `` , just the same as my
2015-12-07 20:16:25 +00:00
examples.
2016-05-24 17:57:36 +00:00
Is it possible to in-line initialize an array like so: `` string[] myarray = ["a", "b"]; ``
2016-05-25 13:54:34 +00:00
=========================================================================================
2015-12-07 20:16:25 +00:00
2016-02-10 16:45:46 +00:00
Yes. However it should be noted that this currently only works with statically sized memory arrays. You can even create an inline memory
2018-09-25 17:32:03 +00:00
array in the return statement.
2016-02-10 16:45:46 +00:00
Example::
2016-02-10 16:53:08 +00:00
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2017-07-17 09:24:18 +00:00
2016-02-10 16:45:46 +00:00
contract C {
2018-07-14 21:42:01 +00:00
function f() public pure returns (uint8[5] memory) {
2016-05-18 15:11:39 +00:00
string[4] memory adaArr = ["This", "is", "an", "array"];
2018-08-09 13:36:00 +00:00
adaArr[0] = "That";
return [1, 2, 3, 4, 5];
2016-02-10 16:45:46 +00:00
}
}
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
If I return an `` enum `` , I only get integer values in web3.js. How to get the named values?
2016-05-25 13:54:34 +00:00
===========================================================================================
2015-12-07 20:16:25 +00:00
Enums are not supported by the ABI, they are just supported by Solidity.
You have to do the mapping yourself for now, we might provide some help
later.
Can state variables be initialized in-line?
===========================================
2016-05-13 14:32:35 +00:00
Yes, this is possible for all types (even for structs). However, for arrays it
2016-01-12 05:41:20 +00:00
should be noted that you must declare them as static memory arrays.
2015-12-07 20:16:25 +00:00
Examples::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.0 <0.6.0;
2017-07-17 09:24:18 +00:00
2015-12-07 20:16:25 +00:00
contract C {
2016-05-05 19:10:32 +00:00
struct S {
uint a;
uint b;
}
2015-12-07 20:16:25 +00:00
S public x = S(1, 2);
string name = "Ada";
2017-07-17 09:24:18 +00:00
string[4] adaArr = ["This", "is", "an", "array"];
2015-12-07 20:16:25 +00:00
}
2016-05-05 19:10:32 +00:00
2015-12-07 20:16:25 +00:00
contract D {
C c = new C();
}
2016-05-25 13:56:24 +00:00
How do structs work?
====================
2015-12-07 20:16:25 +00:00
See `struct_and_for_loop_tester.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/65_struct_and_for_loop_tester.sol> `_ .
2016-05-24 17:57:36 +00:00
What are some examples of basic string manipulation (`` substring `` , `` indexOf `` , `` charAt `` , etc)?
2016-05-25 13:54:34 +00:00
==================================================================================================
2015-12-07 20:16:25 +00:00
There are some string utility functions at `stringUtils.sol <https://github.com/ethereum/dapp-bin/blob/master/library/stringUtils.sol> `_
2016-05-25 14:01:26 +00:00
which will be extended in the future. In addition, Arachnid has written `solidity-stringutils <https://github.com/Arachnid/solidity-stringutils> `_ .
2015-12-07 20:16:25 +00:00
For now, if you want to modify a string (even when you only want to know its length),
2016-05-24 17:57:36 +00:00
you should always convert it to a `` bytes `` first::
2015-12-07 20:16:25 +00:00
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.0 <0.6.0;
2017-07-17 09:24:18 +00:00
2015-12-07 20:16:25 +00:00
contract C {
string s;
2016-05-05 19:10:32 +00:00
2017-12-12 18:47:30 +00:00
function append(byte c) public {
2015-12-07 20:16:25 +00:00
bytes(s).push(c);
}
2016-05-05 19:10:32 +00:00
2017-12-12 18:47:30 +00:00
function set(uint i, byte c) public {
2015-12-07 20:16:25 +00:00
bytes(s)[i] = c;
}
}
Can I concatenate two strings?
==============================
2018-09-25 17:32:03 +00:00
Yes, you can use `` abi.encodePacked `` ::
pragma solidity >=0.4.0 <0.6.0;
library ConcatHelper {
function concat(bytes memory a, bytes memory b)
internal pure returns (bytes memory) {
return abi.encodePacked(a, b);
}
}
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
Why is the low-level function `` .call() `` less favorable than instantiating a contract with a variable (`` ContractB b; `` ) and executing its functions (`` b.doSomething(); `` )?
2016-05-25 13:54:34 +00:00
=============================================================================================================================================================================
2015-12-07 20:16:25 +00:00
If you use actual functions, the compiler will tell you if the types
or your arguments do not match, if the function does not exist
or is not visible and it will do the packing of the
arguments for you.
See `ping.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_ping.sol> `_ and
`pong.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_pong.sol> `_ .
2016-05-24 17:57:36 +00:00
When returning a value of say `` uint `` type, is it possible to return an `` undefined `` or "null"-like value?
2016-05-25 13:54:34 +00:00
============================================================================================================
2015-12-07 20:16:25 +00:00
This is not possible, because all types use up the full value range.
2016-05-24 17:57:36 +00:00
You have the option to `` throw `` on error, which will also revert the whole
2015-12-07 20:16:25 +00:00
transaction, which might be a good idea if you ran into an unexpected
situation.
If you do not want to throw, you can return a pair::
2018-10-08 21:01:52 +00:00
pragma solidity >0.4.23 <0.6.0;
2017-07-17 09:24:18 +00:00
2015-12-07 20:16:25 +00:00
contract C {
uint[] counters;
2016-05-05 19:10:32 +00:00
2015-12-07 20:16:25 +00:00
function getCounter(uint index)
2017-12-12 18:47:30 +00:00
public
view
2015-12-07 20:16:25 +00:00
returns (uint counter, bool error) {
2016-08-24 19:28:07 +00:00
if (index >= counters.length)
return (0, true);
else
return (counters[index], false);
2016-05-05 19:10:32 +00:00
}
2017-12-12 18:47:30 +00:00
function checkCounter(uint index) public view {
2018-04-30 11:30:09 +00:00
(uint counter, bool error) = getCounter(index);
2016-05-05 19:10:32 +00:00
if (error) {
2018-08-09 13:36:00 +00:00
// Handle the error
2016-08-24 19:28:07 +00:00
} else {
2018-08-09 13:36:00 +00:00
// Do something with counter.
require(counter > 7, "Invalid counter value");
2016-05-05 19:10:32 +00:00
}
2015-12-07 20:16:25 +00:00
}
}
Are comments included with deployed contracts and do they increase deployment gas?
==================================================================================
No, everything that is not needed for execution is removed during compilation.
This includes, among others, comments, variable names and type names.
What happens if you send ether along with a function call to a contract?
========================================================================
It gets added to the total balance of the contract, just like when you send ether when creating a contract.
2016-09-05 14:29:08 +00:00
You can only send ether along to a function that has the `` payable `` modifier,
otherwise an exception is thrown.
2015-12-07 20:16:25 +00:00
***** ***** ***** ***
Advanced Questions
***** ***** ***** ***
How do you get a random number in a contract? (Implement a self-returning gambling contract.)
=============================================================================================
Getting randomness right is often the crucial part in a crypto project and
most failures result from bad random number generators.
If you do not want it to be safe, you build something similar to the `coin flipper <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/35_coin_flipper.sol> `_
but otherwise, rather use a contract that supplies randomness, like the `RANDAO <https://github.com/randao/randao> `_ .
Get return value from non-constant function from another contract
=================================================================
The key point is that the calling contract needs to know about the function it intends to call.
See `ping.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_ping.sol> `_
and `pong.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/45_pong.sol> `_ .
How do you create 2-dimensional arrays?
=======================================
See `2D_array.sol <https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/55_2D_array.sol> `_ .
2016-05-24 17:57:36 +00:00
Note that filling a 10x10 square of `` uint8 `` + contract creation took more than `` 800,000 ``
gas at the time of this writing. 17x17 took `` 2,000,000 `` gas. With the limit at
2015-12-07 20:16:25 +00:00
3.14 million... well, there’ s a pretty low ceiling for what you can create right
now.
Note that merely "creating" the array is free, the costs are in filling it.
Note2: Optimizing storage access can pull the gas costs down considerably, because
2016-05-24 17:57:36 +00:00
32 `` uint8 `` values can be stored in a single slot. The problem is that these optimizations
2015-12-07 20:16:25 +00:00
currently do not work across loops and also have a problem with bounds checking.
You might get much better results in the future, though.
2016-03-23 21:42:01 +00:00
How do I initialize a contract with only a specific amount of wei?
==================================================================
Currently the approach is a little ugly, but there is little that can be done to improve it.
2016-05-24 17:57:36 +00:00
In the case of a `` contract A `` calling a new instance of `` contract B `` , parentheses have to be used around
`` new B `` because `` B.value `` would refer to a member of `` B `` called `` value `` .
2016-10-17 17:14:21 +00:00
You will need to make sure that you have both contracts aware of each other's presence and that `` contract B `` has a `` payable `` constructor.
2016-03-23 21:42:01 +00:00
In this example::
2016-05-12 17:16:01 +00:00
2018-09-25 16:40:45 +00:00
pragma solidity >0.4.99 <0.6.0;
2017-07-17 09:24:18 +00:00
2016-10-17 17:14:21 +00:00
contract B {
2018-07-02 14:25:54 +00:00
constructor() public payable {}
2016-10-17 17:14:21 +00:00
}
2016-05-18 15:05:28 +00:00
2016-03-23 21:42:01 +00:00
contract A {
2018-07-12 18:07:16 +00:00
B child;
2016-05-05 19:10:32 +00:00
2017-12-12 18:47:30 +00:00
function test() public {
2016-03-23 21:42:01 +00:00
child = (new B).value(10)(); //construct a new B with 10 wei
}
}
2016-05-24 17:57:36 +00:00
What is the relationship between `` bytes32 `` and `` string `` ? Why is it that `` bytes32 somevar = "stringliteral"; `` works and what does the saved 32-byte hex value mean?
2016-05-25 13:54:34 +00:00
========================================================================================================================================================================
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
The type `` bytes32 `` can hold 32 (raw) bytes. In the assignment `` bytes32 samevar = "stringliteral"; `` ,
the string literal is interpreted in its raw byte form and if you inspect `` somevar `` and
see a 32-byte hex value, this is just `` "stringliteral" `` in hex.
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
The type `` bytes `` is similar, only that it can change its length.
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
Finally, `` string `` is basically identical to `` bytes `` only that it is assumed
2016-08-10 18:52:11 +00:00
to hold the UTF-8 encoding of a real string. Since `` string `` stores the
data in UTF-8 encoding it is quite expensive to compute the number of
2015-12-07 20:16:25 +00:00
characters in the string (the encoding of some characters takes more
2016-05-24 17:57:36 +00:00
than a single byte). Because of that, `` string s; s.length `` is not yet
supported and not even index access `` s[2] `` . But if you want to access
2015-12-07 20:16:25 +00:00
the low-level byte encoding of the string, you can use
2016-05-24 17:57:36 +00:00
`` bytes(s).length `` and `` bytes(s)[2] `` which will result in the number
2016-08-10 18:52:11 +00:00
of bytes in the UTF-8 encoding of the string (not the number of
characters) and the second byte (not character) of the UTF-8 encoded
2015-12-07 20:16:25 +00:00
string, respectively.
2016-05-24 17:57:36 +00:00
Can a contract pass an array (static size) or string or `` bytes `` (dynamic size) to another contract?
2016-05-25 13:54:34 +00:00
=====================================================================================================
2015-12-07 20:16:25 +00:00
Sure. Take care that if you cross the memory / storage boundary,
independent copies will be created::
2018-09-24 12:46:23 +00:00
pragma solidity >=0.4.16 <0.6.0;
2017-07-17 09:24:18 +00:00
2015-12-07 20:16:25 +00:00
contract C {
2016-05-18 15:05:28 +00:00
uint[20] x;
2016-05-05 19:10:32 +00:00
2017-12-12 18:47:30 +00:00
function f() public {
2016-05-18 15:05:28 +00:00
g(x);
h(x);
}
2016-05-05 19:10:32 +00:00
2018-07-14 21:42:01 +00:00
function g(uint[20] memory y) internal pure {
2016-05-18 15:05:28 +00:00
y[2] = 3;
}
2016-05-05 19:10:32 +00:00
2017-07-17 09:24:18 +00:00
function h(uint[20] storage y) internal {
2016-05-18 15:05:28 +00:00
y[3] = 4;
}
}
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
The call to `` g(x) `` will not have an effect on `` x `` because it needs
2018-07-14 21:42:01 +00:00
to create an independent copy of the storage value in memory.
On the other hand, `` h(x) `` successfully modifies `` x `` because only
a reference and not a copy is passed.
2015-12-07 20:16:25 +00:00
2016-05-24 17:57:36 +00:00
Is it possible to return an array of strings (`` string[] `` ) from a Solidity function?
2016-05-25 13:54:34 +00:00
=====================================================================================
2015-12-07 20:16:25 +00:00
2018-09-25 17:32:03 +00:00
Only when `` pragma experimental "ABIEncoderV2"; `` is used.
2015-12-07 20:16:25 +00:00
2015-12-10 11:34:46 +00:00
What does the following strange check do in the Custom Token contract?
======================================================================
::
2017-05-02 15:33:46 +00:00
require((balanceOf[_to] + _value) >= balanceOf[_to]);
2015-12-10 11:34:46 +00:00
Integers in Solidity (and most other machine-related programming languages) are restricted to a certain range.
2016-05-24 17:57:36 +00:00
For `` uint256 `` , this is `` 0 `` up to `` 2**256 - 1 `` . If the result of some operation on those numbers
2015-12-10 11:34:46 +00:00
does not fit inside this range, it is truncated. These truncations can have
`serious consequences <https://en.bitcoin.it/wiki/Value_overflow_incident> `_ , so code like the one
above is necessary to avoid certain attacks.
2018-08-14 15:39:18 +00:00
Why are explicit conversions between fixed-size bytes types and integer types failing?
======================================================================================
Since version 0.5.0 explicit conversions between fixed-size byte arrays and integers are only allowed,
if both types have the same size. This prevents unexpected behaviour when truncating or padding.
Such conversions are still possible, but intermediate casts are required that make the desired
truncation and padding convention explicit. See :ref: `types-conversion-elementary-types` for a full
explanation and examples.
Why can number literals not be converted to fixed-size bytes types?
===================================================================
Since version 0.5.0 only hexadecimal number literals can be converted to fixed-size bytes
types and only if the number of hex digits matches the size of the type. See :ref: `types-conversion-literals`
for a full explanation and examples.
2015-12-07 20:16:25 +00:00
More Questions?
===============
If you have more questions or your question is not answered here, please talk to us on
`gitter <https://gitter.im/ethereum/solidity> `_ or file an `issue <https://github.com/ethereum/solidity/issues> `_ .