Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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.

Filter by
Sorted by
Tagged with
324 votes
9 answers
114k 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: ...
Jakub Wojciechowski's user avatar
187 votes
13 answers
52k 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 this? Also can this be done without changing the ...
Mikko Ohtamaa's user avatar
174 votes
4 answers
46k 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 ...
Rob Hitchens's user avatar
  • 55.3k
147 votes
2 answers
66k views

When should I use calldata and when should I use memory?

I have seen people use both memory and calldata keywords when writing Solidity. Specifically, they are used when declaring function parameters that take dynamic types like 'structs' or 'arrays'. When ...
Shane Fontaine's user avatar
116 votes
2 answers
70k 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? ...
jyapx's user avatar
  • 1,315
108 votes
2 answers
41k 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: ...
dbryson's user avatar
  • 6,393
105 votes
5 answers
43k views

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

I was looking at the docs and I'm looking for clarification on the difference between require and assert and throw and revert. assert(bool condition): abort execution and revert state changes if ...
Daniel Kobe's user avatar
  • 1,591
95 votes
7 answers
92k 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 ...
q9f's user avatar
  • 32.9k
80 votes
2 answers
32k 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 ...
SCBuergel's user avatar
  • 8,774
79 votes
4 answers
66k 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 ...
Michael's user avatar
  • 1,065
79 votes
4 answers
103k views

Is the block.timestamp value in Solidity seconds or milliseconds?

I'm looking to use block.timestamp in one of my contracts and I want to know whether the block.timestamp is seconds like in Python or milliseconds like in Java or some other value? Also, is it safe ...
etherfaces's user avatar
  • 1,153
78 votes
9 answers
111k 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, ...
user avatar
77 votes
6 answers
95k views

Fallback function in Solidity

I have seen many examples of the fallback function in Solidity, but I didn't understand any of them. Also, I've seen references to the payable modifier. My questions are: What exactly does the ...
Gopal ojha's user avatar
  • 2,249
76 votes
1 answer
63k 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 ...
jayD's user avatar
  • 2,472
75 votes
7 answers
127k 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 ...
Thomas Jay Rush's user avatar
74 votes
5 answers
28k 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 ...
uzyn's user avatar
  • 1,100
73 votes
3 answers
69k 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 ...
user697's user avatar
  • 2,176
70 votes
7 answers
38k 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?
J-B's user avatar
  • 8,961
67 votes
9 answers
77k 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?
user697's user avatar
  • 2,176
66 votes
14 answers
63k views

Hardhat - How to interact with a deployed contract?

I can deploy a contract using ethers and Hardhat with the following: const myContract = await hre.ethers.getContractFactory("SomeContract"); const deployedContract = await myContract.deploy()...
JohnWLIV's user avatar
  • 763
65 votes
2 answers
78k views

How can I 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; ...
alper's user avatar
  • 8,395
64 votes
7 answers
17k 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 ...
J-B's user avatar
  • 8,961
63 votes
3 answers
39k views

What's the difference between 'address' and 'address payable'?

I saw a Solidity smart contract where some variables were declared with the type address and some were declared with the type address payable. What are the differences between the two? Do they store ...
Jesbus's user avatar
  • 10.5k
62 votes
4 answers
65k 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.
ethereal's user avatar
  • 1,805
62 votes
5 answers
52k 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 ...
monty_lennie's user avatar
58 votes
1 answer
18k 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 ?
dor's user avatar
  • 725
57 votes
5 answers
28k 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?...
Daniel Gretzke's user avatar
56 votes
2 answers
26k 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. ...
uberlaufer's user avatar
56 votes
3 answers
38k 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",...
Lee's user avatar
  • 8,568
54 votes
8 answers
32k 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?
eth's user avatar
  • 85.9k
54 votes
4 answers
29k 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-...
Thomas Clowes's user avatar
53 votes
7 answers
76k 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 will works since as input I gave bytes32 . But it does not work with the ...
alper's user avatar
  • 8,395
53 votes
1 answer
50k views

What are the virtual and override keywords in Solidity?

Solidity 0.6.0 introduced the keywords virtual and override. What do they do?
Shane Fontaine's user avatar
51 votes
4 answers
41k views

When to use "View" and "Pure" in place of "Constant" [duplicate]

According to the solidity 0.4.17 Release Notes this release finally checks the modifiers view (used to be named constant) and pure on functions. As a rule of thumb, use view if your function ...
Rob Hitchens's user avatar
  • 55.3k
50 votes
12 answers
71k 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?
arodriguezdonaire's user avatar
50 votes
3 answers
18k 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 ...
Alan Wunsche's user avatar
  • 1,655
50 votes
3 answers
21k 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 ...
takeshi's user avatar
  • 1,760
47 votes
1 answer
33k views

What are Solidity events and how they are related to topics and logs?

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. ...
Narayan Prusty's user avatar
46 votes
3 answers
22k 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:...
Aydin's user avatar
  • 2,117
46 votes
2 answers
87k views

How to initialize an empty array and push items into it?

Here's my function: function Test() constant returns (uint[]) { var myArray = uint[]; myArray.push(123); return myArray; } Here's an error I get in the solidity online compiler: ...
manidos's user avatar
  • 4,298
46 votes
6 answers
44k views

How can I represent decimal values in Solidity?

I just started writing some solidity and I noticed that there are no decimal values such as double or float. What do you guys do when you need to return send to a wallet let's say 1.57 ether? Or when ...
sfrj's user avatar
  • 782
45 votes
3 answers
15k 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 { ...
Raine Revere's user avatar
  • 3,610
45 votes
3 answers
81k views

What's the Solidity statement to print data to the console?

Sometimes it would be really helpful to be able to see string representations of variable values in the Geth console, like of argument values in functions. Can I somehow print them to the Geth console?...
TMOTTM's user avatar
  • 1,953
45 votes
3 answers
56k views

How to loop through mapping in solidity?

I have a mapping in my contract like below: mapping (address => uint) public voterCount; To calculate the total votes, I would like to loop through the mapping and sum the uint values. How do I do ...
maheshmurthy's user avatar
  • 1,237
44 votes
2 answers
31k 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 ...
Ales's user avatar
  • 915
44 votes
2 answers
9k 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 ...
Raine Revere's user avatar
  • 3,610
44 votes
1 answer
24k 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....
billett's user avatar
  • 1,255
43 votes
6 answers
25k 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/...
user1491307's user avatar
  • 1,221
43 votes
8 answers
16k 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 ...
Amer Ameen's user avatar
42 votes
7 answers
28k views

How does a contract find out if another address is a contract?

Is it possible, from within a contract written in Solidity, to check if a contract is placed on a specific address or if this address does not contain any code?
Thorkil Værge's user avatar

1
2 3 4 5
357