Questions tagged [data-types]
The data-types tag has no usage guidance.
119
questions
2
votes
1
answer
20
views
Has Solidity Introduced Storage Optimization for Smaller Integer Types in Structs?
I am working on a project where storage efficiency is of utmost importance. Specifically, I have a scenario where I want to store an ID of type uint16 and a code of type uint8. The current plan is to ...
1
vote
1
answer
21
views
How to create a hash based on struct array types?
I have the following arguments to call a function in a smart contract:
ERC20Reward[] calldata _erc20Rewards
ERC721Reward[] calldata _erc721Rewards
ERC1155Reward[] calldata _erc1155Rewards
...
0
votes
1
answer
53
views
TypeError: Type is not callable in my code
I started learning solidity, and I try to make a basic contract that add stocks to specific addresses. Here it is:
pragma solidity 0.8.8;
contract faya{
holders[] public investors;
...
0
votes
0
answers
27
views
How to fix Integer Overflow warning in oyente?
I was working with smart contracts and was testing my contracts using oyente. I am getting integer overflow warning in my contract and i dont know how to fix it. Can anyone help ? Thanks in advance. ...
1
vote
1
answer
167
views
What's the best way to verify an address is correct in C#?
Is there an easy way to validate an Ethereum address in C#?
Basically, I am looking for the method that correspond to these ones in Javascript but in C#.
1
vote
2
answers
733
views
What is calldata offset for dynamic calldata arrays?
The Solidity docs say this about dynamic calldata arrays:
For dynamic calldata arrays, you can access their calldata offset (in bytes) and length (number of elements) using x.offset and x.length
But ...
1
vote
1
answer
125
views
How to silence warnings in return variables when they are not used?
I have the following function in solidity:
function validateAndPayForPaymasterTransaction(
bytes32,
bytes32,
Transaction calldata _transaction
) external payable override onlyBootloader ...
0
votes
1
answer
120
views
Comparison of gas cost in different types of data storage
I want to compare gas fee of different types of data includes: String, bytes, uint, bool, int, address and etc... does anyone knows how much they costs? or share a link about this?
Thank you
1
vote
1
answer
193
views
Return type of `type` function in solidity
In solidity, you can call the type function to get information about a type.
But what is the return type of the type function?
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
contract TypeTest ...
4
votes
1
answer
34
views
Is it possible to use interface functions as types?
I would like to use a function defined in an interface as a function type, like this:
pragma solidity >=0.8.17;
interface Foo {
function myFunction() external;
}
contract Contract {
...
0
votes
2
answers
95
views
bytes5 vs string
I have an smart contract with some defined fixed length variables from 3 to 5 characters long.
Defining these variables as string is more intuitive for those that come from another languages. Also ...
3
votes
2
answers
387
views
Do the tight variable packing rules apply to arrays in structs?
Tight variable packing is a known coding pattern in Solidity for saving gas by placing variables that are lower than 256 bits closer to one another in structs.
For instance, the following will cost ...
0
votes
1
answer
431
views
21 bytes Ethereum address with "0x" in 20 bytes address data type
I am learning solidity. Public address is dervied from last 20 bytes of public key. I have read that address data type has 20 bytes size and in many programme I have seen that they are equated to an ...
0
votes
1
answer
206
views
correct solidity type for a cid?
Im writing a function in solidity where the user enters a CID from an IPFS link, which looks like:
QmcQvCyLAncpEzvmYtKfvVPVayGqFvTQrYpUpPAJeoXGke
So far im using string type, and it works.
Which is ...
6
votes
2
answers
3k
views
How to call function with struct parameter in ethers?
My solidity:
contract Test {
struct Num {
uint value;
}
function double(Num calldata num) external pure returns(Num memory) {
return Num({ value: num.value * 2 });
}
}...
0
votes
2
answers
32
views
Is Ethereum capable of having dynamicly typed languages?
I am wondering if it is at least theoretically possible for Ethereum to have a language that is loose/weak typing.
From what I've seen all the languages on Ethereum are static typed.
My understanding ...
2
votes
2
answers
290
views
What is standard practice to cast msg.sender to "payable"?
Introduction
I am working on a bounty contract. Here is the function for somebody answering the bounty. I am trying to figure out when it is better to cast to payable, it seems intuitively that always ...
0
votes
1
answer
222
views
having trouble declaring a fixed size byte array constant
I was able to declare this constant as a bytes object, but am trying to compress it for efficiency, and I'm getting this error message:
Only constants of value type and byte array type are implemented....
0
votes
1
answer
839
views
Ethers-EIP712 wont work with strings
I tried to use eip-712 signature with my smart contract but I found out that it only work with fixed-size type. Does anyone know how I can use dynamically-Sized types like string, arrays and bytes
...
1
vote
0
answers
499
views
Copying strings and bytes from memory to storage via storage references
What's the proper way to copy a string (or bytes) from memory to storage? When copied directly into a state variable lvalue (or into a mapping), there's no problem, but when assigning a string memory ...
0
votes
1
answer
340
views
Passing different data types to uint parameter
I found out, that I can pass different data types (as argument), even if the Solidity method takes only uint data type, why is that possible?
Example:
function test (uint _param) public {
//do smth
...
1
vote
1
answer
72
views
Why cannot insert elements dynamically into 2D memory array
I have some issues with understanding exactly how the arrays and memory works in solidity, but I found this nice question that kind of help me to understand some parts - Solidity 2D in-memory array ...
2
votes
2
answers
112
views
What type is "MyInterface.MyCustomError" in Solidity?
I'm toying with the recently introduced custom errors. Here's a mini replica of my set up:
interface MyInterface {
error MyCustomError();
}
I'm writing my tests in Solidity and I'd like to use ...
0
votes
1
answer
78
views
UniswapV3 Pool Liquidity
function liquidity() external view returns (uint128)
Which side of the asset pair is referenced here, or is that value in ETH?
How do I scale it properly?
1
vote
2
answers
76
views
What is the Order of Execution of data types?
I have created and deployed(Remix) a simple contract to understand better data types. When I deploy contract, I see that data types are not in the order inside the contract. Here is my contract:
...
1
vote
1
answer
74
views
Memory to memory assignment doesn't point to the same reference
Hey I am trying to understand data location more it is stated in the docs that "Assignments from memory to memory only create references. This means that changes to one memory variable are also ...
0
votes
1
answer
278
views
Using a dynamic array for an index vs a fixed array
I'm going to be using a smart contract to add data to an array, so I'll be using array.push (fixed gas). The data added is an address and the purpose of the array is just to keep a list of all ...
0
votes
1
answer
28
views
how and where to use data areas
from https://docs.soliditylang.org/en/v0.8.11/types.html:
" If you use a reference type, you always have to explicitly provide the data area where the type is stored"
But later they give ...
0
votes
1
answer
697
views
How to convert between uint256[] and uint256[x]
Basically, I want to call a function myfunct(uint256[] memory) but I have a uint256[13]
I have the uint256[13] because I want to temporarily store uint256s in a function, and save memory--not do ...
0
votes
1
answer
604
views
How to convert uint8 to uint256 bitmap in solidity?
Say I have a uint8 with binary value 0000_1001 i.e. 8+1=9 in decimal. How do I convert this uint8 in a gas efficient way to a uint256 which represents a bitmap?, such that in this example the 9th bit(...
1
vote
3
answers
1k
views
Problem compiling subgraph with graph-ts new version
I'm getting this error: ERROR TS2322: Type '~lib/@graphprotocol/graph-ts/common/value/Value | null' is not assignable to type '~lib/@graphprotocol/graph-ts/common/value/Value'.
I know it is for this ...
1
vote
1
answer
593
views
Why do you have to specify calldata or memory for complex types?
Why do you have to specify calldata or memory for complex types like (string, array, struct etc)?
1
vote
1
answer
1k
views
Solidity 2D in-memory array with size defined by a variable
I want to define an in-memory 2-dimensional array without fully hard-coding its shape. Let's say I want a 3xN matrix (and to explain ill also set its 0th element to [1, 2, 3]):
function makeArr (int n)...
1
vote
1
answer
554
views
Subgraph (Graph Protocol) not building out of schema correctly
Doing a quick start tutorial to get back in the subgraph mode and my mapping (even though very simple) is not building. The types will compile, but I cannot deploy it due to it being unable to build.
...
0
votes
1
answer
82
views
Storing struct with mapping inside of mapping
I have the following structures defined in my smart contract:
mapping(bytes4 => Question) private questions;
struct Question {
bytes8[] answers;
mapping(bytes32 => bool) answerers;
}
...
0
votes
1
answer
330
views
what does "int x = type(int).min;" mean in Solidity? [duplicate]
In the Solidity docs,
"if you have int x = type(int).min;, then -x does not fit the positive range.This means that unchecked { assert(-x == x); } works, and the expression -x when used in checked ...
1
vote
1
answer
494
views
Where are value-type function arguments stored in Solidity?
When we define a function, say test(uint256 val). I would presume val would be implicitly defined in memory. But what if we change the funtion visibility modifier, does that change the location of ...
1
vote
2
answers
1k
views
Address to String solidity
I am using this code in converting address to string in solidity
function toAsciiString(address x) public returns (string memory) {
bytes memory s = new bytes(40);
for (uint i = 0; i < 20; ...
2
votes
1
answer
445
views
Best way to store percentages in a smart contract
I need to store the IRR (internal rate of return) of a project in my smart contract.
Since Solidity can't store decimals it must be saved as an integer.
Which of these options would be better and why:
...
0
votes
2
answers
843
views
How to implement a Set in solidity?
How to implement a Set in solidity that fulfills the following criteria?
Add and remove items
Check if an item is present
Iterate the Set
1
vote
1
answer
218
views
What is the difference between contract type and address?
I have seen here that:
Contract TokenCreator {
function createToken(bytes32 name)
public
returns (OwnedToken tokenAddress)
{
// Create a new `Token` contract and return ...
0
votes
1
answer
97
views
Data Stream Processing on Ethereum
I developed a system that receives continual streams of requests via a REST API. For each bit of data contained in the body of the request, the sender receives an according bit of data in the response....
0
votes
1
answer
481
views
Mapping in solidity
Mapping do not have a length, nor do they have the concept of key or a values being set.
Is this statement True or False?
Please provide the explanation also.
The solidity docs say “Mappings can be ...
2
votes
0
answers
378
views
Alias for external library type?
Say you're importing a library, and that library contains a struct:
pragma solidity >=0.8.0;
library SomeLibrary {
struct FooType {
uint256 value;
}
}
contract SomeContract {
....
4
votes
1
answer
950
views
How do you handle the hex prefix without the value (only "0x")?
First, I'm caling function foo() (from another contract) that returns me a uint in the form of bytes.
Then, I'm using BytesLib.toUint(foo(), 0) to retrieve the uint.
When the value is supposed to be 1,...
1
vote
1
answer
2k
views
error comparing two bytes memory
I am testing a smart contract in Remix and I get the following error when trying to compare two bytes memory using "==":
TypeError: Operator == not compatible with types bytes memory and ...
2
votes
1
answer
754
views
"generic object" data type in solidity
Is there a data type similar to "object" in C# that any solidity data type could rely on?
I currently have a struct with multiple fields and I would need one of them to be either a string, a ...
0
votes
0
answers
19
views
Mapping with different datatypes showing on one struct
is it possible that in I can use a uint and a address that showing on the same struct(Submission) as mapping ? I need something what makes it easy to read out the mapping with a uint (number) and with ...
1
vote
1
answer
297
views
What happens when the result of the shift operation does not fit in the type of the left operand?
For instance, take the following code:
pragma solidity ^0.8.0;
function foo() external pure returns (uint256, uint256, uint256) {
uint256 i1 = uint256(2) << 255;
uint256 i2 = uint256(4) ...
1
vote
2
answers
373
views
uint16 conversion issue
So I want to test this Solidity code using Typescript but faced with a type conversion issue.
uint16 base;
function addNumberToBase(uint16 number_) {
// This number must be between 1 ~ 5
...