220
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 ...
- 53.9k
162
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 ...
- 16.7k
143
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 ...
- 35.9k
126
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 ...
- 3,171
124
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♦
- 82.2k
120
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 ...
- 32.2k
95
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)...
- 2,577
95
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 ...
- 35.9k
84
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 ...
- 1,048
83
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];
...
- 35.9k
64
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♦
- 82.2k
62
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 ...
- 773
62
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 ...
- 5,571
61
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 == ...
- 1,892
57
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♦
- 82.2k
53
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 ...
52
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 ...
- 2,946
51
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 ...
- 39.8k
50
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♦
- 82.2k
50
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 ...
- 4,250
45
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 ...
- 1,095
43
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 ...
- 16.6k
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);
...
- 7,926
43
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♦
- 82.2k
42
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 ...
- 7,964
41
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♦
- 82.2k
41
votes
Accepted
39
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♦
- 82.2k
38
votes
Accepted
How to update a deployed smart contract?
Contract code is permanent. There is no way to alter the code of a deployed contract except by destroying it altogether by the SELFDESTRUCT opcode (selfdestruct() in solidity.)
There's four ways, ...
- 7,190
37
votes
How to delete an element at a certain index in an array?
This constant operation works without preserving order:
uint[] internal array;
// Move the last element to the deleted spot.
// Remove the last element.
function _burn(uint index) internal {
...
- 471
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
contract-development × 4626solidity × 2555
contract-design × 1084
remix × 490
contract-deployment × 405
tokens × 372
contract-invocation × 364
go-ethereum × 361
web3js × 356
contract-debugging × 282
blockchain × 260
erc-20 × 244
truffle × 236
transactions × 231
ether × 139
gas × 139
dapp-development × 113
dapps × 88
security × 88
arrays × 83
evm × 82
nft × 79
storage × 77
metamask × 76
events × 74