74
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 ...
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.
...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
8
votes
Accepted
Can solidity have negative numbers?
The answer to your question is Yes:
Solidity supports int types, which are treated arithmetically as signed values (i.e., can be positive, negative and zero).
The answer to your concern is No:
...
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 ...
7
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 ...
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♦
- 86.3k
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 ...
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 = ...
6
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&...
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 ...
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 ...
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;
...
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 ...
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 ...
4
votes
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 ...
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 ...
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 ...
4
votes
Solidity Invalid implicit conversion from string memory to bytes memory requested
Should probably be
return sha256(bytes (document));
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 ...
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.
...
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. ...
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()...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
data-types × 128solidity × 91
contract-development × 12
storage × 12
arrays × 12
struct × 12
type-casting × 10
bytes × 9
remix × 8
addresses × 8
memory × 8
contract-design × 7
string × 7
calldata × 7
gas × 6
mapping × 5
uint256 × 5
go-ethereum × 4
abi × 4
error × 4
database × 4
integers × 4
web3js × 3
evm × 3
exchanges × 3