Questions tagged [solidity]

Solidity is a contract-oriented, high-level language whose syntax is similar to that of JavaScript and it is designed for writing smart contracts in Ethereum to run on the EVM. Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.

150
votes
11answers
31k views

Upgradeable smart contracts

If the contract issuer wants to have a way to upgrade the contract code, so that account data and other things carry over, can Ethereum provide for this? Also can this be done without changing the ...
105
votes
2answers
21k views

Are there well-solved and simple storage patterns for Solidity?

Simple and appropriate data organization can challenge Solidity newcomers. It wants us to organize everything in ways many of us aren’t accustomed to. Are there well-solved general patterns for ...
95
votes
2answers
17k views

`external` vs `public` best practices

Apart from the public modifier Ethereum introduces the external one. Both may be called outside of the contract and inside (the later one by this.f() pattern). Moreover, according to the docs: ...
64
votes
3answers
22k views

Difference between require and assert and the difference between revert and throw

I was looking at the docs and Im looking for clarification on the difference between require and assert and throw and revert. assert(bool condition): abort execution and revert state changes if ...
56
votes
2answers
20k views

Solidity: What is the difference between `view` and `constant`?

Solidity 0.4.16 introduced the view and constant function modifiers. The documentation says: constant for functions: Same as view. Does this mean view is just an alias for constant? If so, why do ...
53
votes
2answers
16k views

What's the difference between 'msg.sender' and 'tx.origin'?

If either are used in the body of a Solidity function that may change the state of a contract (write call), what's the primary difference and/or benefit of using one over the other. For example: ...
51
votes
5answers
13k views

How can you decompile a smart contract?

On the blockchain I can inspect the code of a contract, and see the EVM opcodes. Is there a way to decompile this and convert it back to (Solidity) source code?
50
votes
2answers
14k views

What does the indexed keyword do?

What does the "indexed" keyword do in the below line of code? I'm guessing it just tells the event object that the following input should be logged? Can we use it other places ie outside of events? ...
45
votes
7answers
6k views

How can I verify that a contract on the blockchain matches the source code?

Given the (Solidity) source code of a smart contract, is there a way to deterministically compile the code and compare it with the code on the blockchain? I want to verify that the contract does what ...
43
votes
2answers
29k views

<address>.send vs <address>.transfer best practice usage?

I noticed that address's offer two member functions that perform the transfer of ether to the specified address. send returns false upon failure and transfer raises an exception. In the Solidity ...
41
votes
2answers
26k views

What is an Event?

What is an Event? Are they only used in Solidity? How are events defined? How are events initiated? Is there a standard term for an event's invocation (do you say an event "happened", "was called",...
40
votes
6answers
18k views

Truffle Migrate doesn't work: Error: No network specified. Cannot determine current network

When I try to run my migration with truffle migrate the following error message appears: Error: No network specified. Cannot determine current network. at Object.detect (/usr/local/lib/...
39
votes
3answers
16k views

What is the zero, empty or null value of a struct?

If I have a mapping of string => custom defined struct, how do I check if a value is defined in the mapping? The docs state that "every possible key exists and is mapped to a value whose byte-...
38
votes
3answers
20k views

How do you refer to a contract own address inside it?

I am attempting to use 2 contracts together, and I need my 2nd contract to pass his own address to the first contract. Is there an easy way to do this? Like this.address? Or should I update my ...
38
votes
1answer
9k views

Are underscores `_` in modifiers code or are they just meant to look cool?

I often see _ in modifiers modifier onlyOwner() { if (msg.sender != owner) throw; _ } Does it execute any code or is it meant to make the code easier to read ?
37
votes
3answers
7k views

Why does uint8 cost more gas than uint256?

contract A { uint8 a = 0; } costs 20150 + 2000 gas during creation. as compared to contract A { uint a = 0; // or uint256 } costing 5050 + 2000 gas during creation It is odd that a variable ...
36
votes
5answers
25k views

How to delete an element at a certain index in an array?

Anyone know how to delete an element in a array? Is there any built in method to do that? If not, does anyone know of how to implement such a method?
36
votes
6answers
32k views

How do you get a json file (ABI) from a known contract address?

I want to watch a Solidity contract using the Mist browser. I know how to do that and it needs the address and the json. I have the address, but I don't have the json. I do have the source code. Can ...
35
votes
8answers
19k views

How to quickly test a Solidity function?

There are snippets and functions of Solidity code provided on this Ethereum Stack Exchange and other sites. What are ways to quickly test them, possibly debug a little, and play around?
32
votes
2answers
24k views

Use string type or bytes32?

What are the benefits to using string datatypes over bytes32 to represent text data. It seems my contract runs into an out of gas exception when I switch the data structures from bytes32 to string.
32
votes
3answers
9k views

Where do contract event logs get stored in the Ethereum architecture?

Background on Events: https://github.com/ethereum/wiki/wiki/Solidity-Features Events allow the convenient usage of the EVM logging facilities. Events are inheritable members of contracts. When they ...
32
votes
2answers
5k views

What are the steps to compile and deploy a library in Solidity?

I have several pieces of documentation about libraries, but I am not yet able to infer what steps are needed to compile and deploy a library and then a contract that uses it. The behavior of the ...
30
votes
2answers
20k views

Solidity, create contract from contract

Based on documentation it should be possible to create new contract from contract contractAddress = new OwnedToken(name); When I invoke this method from contract, the address I receive seems to be ...
29
votes
1answer
15k views

What does the keyword “memory” do exactly?

I've been looking through the code of Etherdice and noticed that some variables are declared like ParserResult memory result; and I haven't found the keyword "memory" in any documentation. What I ...
29
votes
1answer
25k views

How to Initialize a Struct?

I have a struct like so : struct fooStruct { uint foo; uint figther; } I would like to initialize that struct but it won't be stored in a mapping but inside an array. Is there a way to ...
28
votes
5answers
20k views

How to concatenate strings in solidity?

I'm trying to concatenate two strings in solidity. I tried with + or . but neither option works. There is not much documentation on string concatenation yet. Is it even possible? If not, can I ...
28
votes
3answers
20k views

ERC20 vs ERC223. List of differences

Is there a list of improvements that we will have in ERC223? Are there examples of contracts? Is there a migration path for existing ERC20 tokens?
28
votes
3answers
12k views

How to write an optimized (gas-cost) smart contract?

As we all know, there are many factors that determine a good smart contract, such as: Security: it has minimal/zero vulnerability so they cannot be exploited by an adversary. Immune to Attacks. Cost:...
28
votes
2answers
22k views

Remix warnings - state mutability and public visibility

I noticed the new Remix gave me two new warnings: browser/Untitled.sol:6:5: Warning: No visibility specified. Defaulting to "public". function setFee(uint _fee){ ^ Spanning multiple lines. ...
28
votes
1answer
9k views

What are event topics?

I know that indexed arguments index the values for those arguments so that filtering will be faster. But what are topics? And what are they used for? I know that signature of an event is a topic. ...
28
votes
2answers
8k views

Why do Solidity examples use bytes32 type instead of string?

In many Solidity examples I read that use strings for parameters or return values, I see they are typed as bytes32 although there's a string type. What is the real reason for that? Thanks.
27
votes
6answers
14k views

ecrecover from Geth and web3.eth.sign

I have tried to use ecrecover() to verify the signature of a message. I have looked at lots of references here and elsewhere, like: https://lab-blog.ey.com/2016/12/19/ecrecover/ How does one ...
27
votes
4answers
23k views

How to convert a bytes32 to string

How can I convert a bytes32 to a string? Does anyone has a magic function or library which does it?
27
votes
3answers
13k views

What is the difference between an internal/external and public/private function in solidity?

Currently reading solidity documentation: https://solidity.readthedocs.io/en/develop/types.html#function-types By default, function types are internal, so the internal keyword can be omitted. In ...
26
votes
3answers
22k views

How to get a contract's balance in Solidity?

How do I get the balance of a contract with solidity? I know geth has web3.eth.getBalance(), but that's to get the contract balance after it has been deployed. Is there a standard way of doing this, ...
26
votes
3answers
21k views

How to convert a string to bytes32?

How can I convert a string to a bytes32? Does anyone has a magic function or library which does it? For example: This works since as input I gave 32-bits. But it won't work with "> 32-bit" chars. ...
26
votes
4answers
6k views

Who is msg.sender when calling a contract from a contract

When I call a function in my contract which calls another function in another contract. In the second called contract function who is msg.sender? the contract calling the second contract or my account?...
26
votes
2answers
21k views

How can you figure out if a certain key exists in a mapping, Struct defined inside Library (in Solidity)?

pragma solidity ^0.4.8; library Library { struct data { unit val; } } - contract Array{ using Library for Library.data; mapping(address => Library.data) clusterContract; ...
26
votes
4answers
6k views

Error while compiling: Stack too deep

When trying to compile, I am getting the following error: "Internal compiler error: Stack too deep, try removing local variables." Is there any way around this? I'm not sure if I'll be able to ...
25
votes
10answers
10k views

How can you handle an expected throw in a contract test using truffle and ethereum-testRPC?

Is it possible to write a test using truffle that attempts to confirm that a throw occurs in a contract? For example, if I had a contract with function... contract TestContract { function testThrow(...
25
votes
6answers
14k views

How to compile Solidity contracts with Geth v1.6?

I tried to compile the following contract within geth: pragma solidity ^0.4.8; contract Test { uint256 public value; function Test() { value = 123; } } using the following ...
25
votes
3answers
5k views

What is the difference between transaction cost and execution cost in remix?

What is the difference between transaction cost and execution cost as seen after contract instantiation in remix? I don't think it matters, but here is my contract: contract DepositCounter { ...
25
votes
2answers
21k views

How does Mapping in solidity work?

I went through the Crowdfunding example in solidity where i encountered the following: contract Crowdfunding { struct CampaignData { address recipient; uint contributed; uint goal; ...
25
votes
3answers
9k views

What are truffle migrations?

As written in Truffle docs: Migrations are Javascript files that help you deploy contracts to the Ethereum network. These files are responsible for staging your deployment tasks, and they're ...
24
votes
3answers
23k views

payable() function In solidity

I have read many examples of payable functions, but I didn't understand any of them. Also, they talked about modifier in payable. My questions are: What exactly does payable() function do? ...
24
votes
6answers
4k views

What are common pitfalls or limitations when coding in Solidity?

What would be on your list to warn other developers about? What works in most other languages that does not work, or behave as expected, in Solidity? For example only: what parameters can be passed ...
24
votes
1answer
10k views

Ways to see if address is empty

Is owner > 0x0 a good way to test if an address owner was set in Solidity? I am not sure if this could break something in the future and if there are better ways to see if the address was set. What ...
24
votes
2answers
2k views

Is it a good practice to log an event every time I throw in Solidity?

In Solidity (0.3.2) I am aware that there is no way to throw specific exceptions. (Everything is an invalid JUMP error.) It seems beneficial to have a way to log specific errors. Does it make sense to ...
24
votes
1answer
7k views

Is it possible to get the transaction hash from within a Solidity function call?

Within a Solidity function, is the transaction hash available as a global variable from within the contract? I would have thought it would be something like tx.hash in the same way you can do tx....
24
votes
1answer
12k views

How does the delegatecall method work to call to another contract's method?

I'm trying to figure out how to use the delegatecall method on a contract address in a contract to call a function in a separate contract. I know Libraries are the higher level interface to use but I ...