225 votes
Accepted

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

Here are some simple and useful patterns in increasing order of utility. Event logs are omitted for brevity. In practice, it's desirable to emit events for every important state change. Simple List ...
Rob Hitchens's user avatar
175 votes
Accepted

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

memory and calldata (as well as storage) are keywords that define the data area where a variable is stored. To answer your question directly, memory should be used when declaring variables (both ...
Shane Fontaine's user avatar
144 votes

Upgradeable smart contracts

Once a contract is in the blockchain, it is final and cannot be changed. Certain parameters, of course, can be changed if they are allowed to change via the original code. One method of updating ...
Tjaden Hess's user avatar
  • 36.9k
128 votes

What is an ABI and why is it needed to interact with contracts?

The ABI, Application Binary Interface, is basically how you call functions in a contract and get data back. An ABI determines such details as how functions are called and in which binary format ...
eth's user avatar
  • 85.4k
127 votes
Accepted

What is an ABI and why is it needed to interact with contracts?

ABI stands for application binary interface. In general, an ABI is the interface between two program modules, one of which is often at the level of machine code. The interface is the de facto method ...
q9f's user avatar
  • 32.9k
127 votes
Accepted

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

edit 2022-12-08: As of Solidity 0.8.0 this answer is no longer accurate with respect to opcode and gas usage. Furthermore, internal errors like division by zero, assertion failure and others do not ...
maurelian's user avatar
  • 3,211
98 votes
Accepted

How to Initialize a Struct?

Yes, just use fooStruct myStruct = fooStruct(1,2); Or fooStruct myStruct = fooStruct({foo:1, fighter:2}); Or fooStruct memory myStruct; // for temporary data myStruct.figther = 2; // will only ...
Tjaden Hess's user avatar
  • 36.9k
97 votes
Accepted

How can an Ethereum contract get data from a website?

You can use an Oracle. An oracle is any device or entity that connects real-world data with the blockchain. There are several examples of oracle technologies. Chainlink and Provable (formerly Oraclize)...
Thomas Bertani's user avatar
87 votes

How to concatenate strings in solidity?

Update: As of Solidity v0.8.12 the string.concat(a,b) function is the preferred method, as mentioned here. Optimized for low gas-consumption The ethereum community has implemented some ABI encoding ...
Lucas Aschenbach's user avatar
86 votes
Accepted

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

Use the delete operator to delete the element: delete array[index]; If you don't want to leave a gap, you need to move each element manually: contract test{ uint[] array = [1,2,3,4,5]; ...
Tjaden Hess's user avatar
  • 36.9k
67 votes
Accepted

Use string type or bytes32?

Why string instead of bytes32? Use string for arbitrary-length string (UTF-8) data that's longer than 32 bytes. Frontends can decode a long string easier using methods like web3.toAscii or UTF-8 (...
eth's user avatar
  • 85.4k
65 votes

operator == not compatible with type string storage ref and literal_string

If you don't want to load the StringUtils contract, I found it's also possible to simply: keccak256(portcheck) == keccak256("signed"). Can anyone confirm that this would also cost less gas than a ...
Greg Mikeska's user avatar
65 votes
Accepted

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

In Ethereum, transactions cost gas and hence ether. The gas consumption of a transaction depends on the opcodes that the EVM has to execute. The gas cost for each Opcode can be found as explained in ...
Achala Dissanayake's user avatar
63 votes
Accepted

Ways to see if address is empty

Though this works, I personally think it is a good idea to have both operand types equal on boolean operations , as you mentioned for future compatibility. Instead of using > operator use equality == ...
Abhiram mishra's user avatar
59 votes
Accepted

How can you call a payable function in another contract with arguments and send funds?

Update per @Girish comment, in Solidity 0.6+ the syntax has changed to: address.function{value:msg.value}(arg1, arg2, arg3) Original The general syntax for calling a function in another contract ...
eth's user avatar
  • 85.4k
57 votes
Accepted

Contract should be marked as abstract

Overview of the issue You get this error whenever there are functions you need to implement in your contract from an inherited contract. For example, if you have a contract that inherits an interface ...
Markus - soliditydeveloper.com's user avatar
53 votes
Accepted

Returning a struct and reading via Web3

Summary Return the fields of the struct as separate return variables. Edit: As of 2021, it's possible to return the struct directly. See this answer. Example I'm running this code in my local dev ...
BokkyPooBah's user avatar
  • 40.3k
53 votes
Accepted

How to store IPFS hash using bytes32?

Your example shows storing an IPFS identity using it's alphanumeric encoding (Qm...), which is the same Base58 encoding that Bitcoin uses. However, what it's representing at its core is a number (the ...
MidnightLightning's user avatar
52 votes
Accepted

How to quickly test a Solidity function?

One way is to use Remix, the browser-based Solidity realtime compiler and runtime Solidity (formerly named browser-solidity). Paste the code in Remix. Wrap the function inside a contract if needed. (...
eth's user avatar
  • 85.4k
52 votes
Accepted

Can we get all elements stored in a mapping in the contract

Mappings do not store their keys, only the value which is stored at the state memory address calculated by a sha3 hash of the the key itself. Any lookup into a mapping has to provide that original key ...
o0ragman0o's user avatar
  • 4,320
48 votes
Accepted

Solidity, create contract from contract

Is this even possible (I remember I read somewhere that only normal accounts can send contract creating transactions)? This is definitely possible. When I invoke this method from contract, the ...
Hcharlanes's user avatar
  • 1,135
48 votes
Accepted

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

The JSON is called an ABI. You do need the source code, as you have, and one way to get the ABI is to paste it in Remix IDE, compile it, and then click the 'ABI' button to copy the ABI into your ...
eth's user avatar
  • 85.4k
46 votes

How can I represent decimal values in Solidity?

Math in Solidity is done entirely using fixed-point. For ether, there's no need to use fractional values - all values are represented in wei, which is the smallest unit of ether. If you want to send ...
Nick Johnson's user avatar
  • 8,144
46 votes
Accepted

How to delete an element from a mapping?

Yes, delete user[someAddress]; will work with structs that do not contain a mapping. For this question, because name and nickname are not mappings, they will be deleted (set to zero) automatically: ...
eth's user avatar
  • 85.4k
44 votes

How can an Ethereum contract get data from a website?

You can't do this directly; Ethereum contracts can't hit URLs, because Ethereum needs everyone to be able to independently validate the outcome of running any given contract, and you can't guarantee ...
Edmund Edgar's user avatar
  • 16.9k
43 votes
Accepted

Convert address to string

Use abi.encodePacked(x) where x is the address. (Thanks @k06a)
eth's user avatar
  • 85.4k
43 votes
Accepted

How to call my contract's function using sendTransaction

Posting solution to my own question for anyone who needs it. To call a contract function from geth: const contractAbi = eth.contract(AbiOfContract); const myContract = contractAbi.at(contractAddress); ...
Prashant Prabhakar Singh's user avatar
40 votes
Accepted

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

You check that a value is defined in the mapping by checking it is not zero. If an explicit setting of zero has meaning for your application, you need auxiliary data (or structure) to track when a ...
eth's user avatar
  • 85.4k
39 votes
Accepted

How to concatenate strings in solidity?

As of Feb 2022, in Solidity v0.8.12 you can now concatenate strings in a simpler fashion! string.concat(s1, s2) Taken directly from the solidity docs on strings and bytes: Solidity does not have ...
Freddie von Stange's user avatar
39 votes
Accepted

Why do Solidity examples use bytes32 type instead of string?

2 main reasons: Contracts currently cannot read a string that's returned by another contract. The EVM has a word-size of 32 bytes, so it is "optimized" for dealing with data in chunks of 32 bytes. (...
eth's user avatar
  • 85.4k

Only top scored, non community-wiki answers of a minimum length are eligible