Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
73 votes

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

Data storage and representation in the EVM The address and address payable types both store a 160-bit Ethereum address. The concept of payable and non-payable addresses only exists in the Solidity ...
Jesbus's user avatar
  • 10.5k
28 votes
Accepted

Can't do any integer division

Problem is 4 & 5 fit in less that 256 bits (each). You end up with tiny uints in the constant expression, and then those aren't easily converted to the uint256, so ... cast the type explicitly. ...
Rob Hitchens's user avatar
  • 55.3k
28 votes
Accepted

Why write uint256 instead of uint if they're the same thing?

Yes, they are exact aliases. Some developers (including me) prefer to use uint256 because it is consistent with other uint data types, which also specify their size, and also because making the size ...
Edmund Edgar's user avatar
  • 16.9k
20 votes

Convert bytes32 to bytes

Since [email protected], you can use abi.encodePacked() for this, which returns bytes. For example ; contract C { function toBytes(bytes32 _data) public pure returns (bytes) { return abi....
PhABC's user avatar
  • 522
19 votes

Max/min values of standard data types

I assume you mean the integer types, because those are really the only types in Solidity that have a maximum and a minimum. Solidity does not support floating point types, and most likely will never ...
Jesbus's user avatar
  • 10.5k
14 votes

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

Yes, they store the same data, that is a valid ethereum address. The difference is that the compiler (at compile time) when encounter an “address payable” is ready to allow (if required in the ...
Rick Park's user avatar
  • 3,194
13 votes
Accepted

Max/min values of standard data types

Updated 2020 Solidity 0.6.8 introduced min and max keywords that can now natively tell you the min and max of an expected type. From the release page: Implemented type(T).min and type(T).max for ...
Shane Fontaine's user avatar
10 votes

Solidity function to return a data struct

You currently cannot return structs in Soldity as the answer to the question that you linked correctly outlines. But you can take your struct apart and return the individual elements (Solidity does ...
SCBuergel's user avatar
  • 8,774
10 votes

Why write uint256 instead of uint if they're the same thing?

In addition to what @EdmundEdgar said, it's also better to be explicit when constructing method signature ID's. For example if doing bytes4(keccak('transfer(address, uint)')), you'll get a different ...
Miguel Mota's user avatar
  • 5,163
8 votes
Accepted

address payable type - store address and send later using Solidity 0.5.0

UPDATE: Solidity 0.6.x now have explicit conversions to payable: payable(userAddress) And for smart contracts: payable(address(smartContract)) Old answer for Solidity 0.5.x: Here is how to cast ...
k06a's user avatar
  • 3,036
7 votes
Accepted

Solidity function to return a data struct

Update: See here. We can return structs but only for internal calls. Returning structs in new version in Solidity In this snippet, function tryIt() returns true after a successful compile. It's ...
Rob Hitchens's user avatar
  • 55.3k
6 votes

Type casting msg.value to uint248

For casting, use value = uint248(msg.value); Casting to save 8 bits in this case is not worth it and will probably cost more gas due to unpacking: see Why does uint8 cost more gas than uint256? Even ...
eth's user avatar
  • 85.9k
6 votes
Accepted

Why is uint so popular

It's unusual to need negative numbers. If you don't need negative numbers, there is no reason to use an int, and it's (slightly) easier to write secure code for positive numbers if you don't have to ...
Edmund Edgar's user avatar
  • 16.9k
6 votes

Convert bytes32 to bytes

Here is a totally inefficient method of converting bytes32 to bytes (while removing extra zeros bytes to the right). function bytes32ToBytes(bytes32 data) internal pure returns (bytes) { uint i = ...
Ismael's user avatar
  • 30.5k
5 votes

How to receive a fractional number as parameter to my function?

For the transfer you can specify the amount in wei, which would be 1250000000000000000 for 1.25 ETH. For that you can use unsigned integer. No need for decimals. In most cases (not only for ETH) you ...
ivicaa's user avatar
  • 7,519
5 votes

Can't do any integer division

as an addition to @rob's answer you can use : function calcul(uint a, uint b, uint precision) view returns ( uint) { return a*(10**precision)/b; } If we divide using the function above 7/3 ...
Badr Bellaj's user avatar
  • 18.8k
5 votes

Type casting msg.value to uint248

Here's a simple example to demonstrate casting msg.value to a uint248 value using Browser Solidity with the following code: pragma solidity ^0.4.8; contract Test { uint248 public value; ...
BokkyPooBah's user avatar
  • 40.3k
5 votes

Convert bytes32 to bytes

Answer for Solidity v0.8.4 and above You can use bytes.concat instead of abi.encodePacked. function toBytes(bytes32 data) public pure returns (bytes memory) { return bytes.concat(data); } My ...
Paul Razvan Berg's user avatar
5 votes
Accepted

How to call function with struct parameter in ethers?

For struct, you use an object to insert argument: const data = { value: 100, } const tx = await test.double(data); console.log(tx); I tried and got the output: [ BigNumber { value: "200&...
Huynh Son's user avatar
4 votes

Return string (64 chars) to another contract in solidity

For now, it's not possible to pass strings between contracts. I would consider chopping the string up into two bytes32 chunks. It would be more gas-efficient. Web3 clients are better suited to ...
Rob Hitchens's user avatar
  • 55.3k
4 votes

Type casting msg.value to uint248

Try this: value = uint248(msg.value);
Victor Baranov's user avatar
4 votes

Solidity bool size in Structs

I tested this simple contract: pragma solidity 0.4.20; contract Test { struct Example { bool v1; bool v2; bool v3; bool v4; bool v5; bool v6; bool v7; bool v8; } Example ...
medvedev1088's user avatar
4 votes
Accepted

Recursive struct in solidity

I just wrote a blog post about doubly linked lists that should help: https://programtheblockchain.com/posts/2018/03/30/storage-patterns-doubly-linked-list/. You can't have recursive structs, and ...
user19510's user avatar
  • 28.1k
4 votes
Accepted

true and false not the same as 1 and 0 for web3 / solidity?

How do I pass true or false in general via web3.js to a smart contract, as a string String(true)? In web3 you can use booleans true and false to pass true or false values for arguments to a ...
Jaime's user avatar
  • 8,350
4 votes

Solidity Invalid implicit conversion from string memory to bytes memory requested

Should probably be return sha256(bytes (document));
Mikhail Vladimirov's user avatar
4 votes
Accepted

Why use anything but uint256/int256 outside of a struct?

Tight packing of storage variables is not unique to structs. From the Solidity documentation on the layout of state variables in storage: Statically-sized variables (everything except mapping and ...
iamdefinitelyahuman's user avatar
4 votes
Accepted

Difference between a byte in a bytes32 and a byte in a bytes structure

The basic unit of work of the EVM, usually called a word, is 32 bytes. This means that for the EVM, no matter whether we want to express 1 or 2^150, it will take 32 bytes, or 256 bits, to do so. ...
Daniel Perez's user avatar
4 votes

Is it worth to store Ether at the full precision in database?

As the commenters have correctly pointed out, it depends entirely on what you are trying to achieve. You can, and probably should store full-precision strings so you have all significant digits, i.e. ...
Rob Hitchens's user avatar
  • 55.3k
4 votes
Accepted

How do you handle the hex prefix without the value (only "0x")?

0x is technically an empty byte array. In Solidity, you can't do direct comparison of bytes (or other dynamic types), so an easy way to check for this is by checking the length of the bytes, e.g. foo()...
Morten's user avatar
  • 6,017
3 votes
Accepted

Solidity bool size in Structs

NOTE: This answer includes only experimental data Please read @medvedev1088's answer before, because my answer is only the addition and some data from tests. From my tests in remix I got this data: ...
Roman Frolov's user avatar
  • 3,177

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