Skip to main content

Questions tagged [solidity]

Solidity is a contract-oriented, high-level language whose syntax is similar to that of JavaScript and it is designed for writing smart contracts in Ethereum to run on the EVM. Solidity is statically typed, supports inheritance, libraries and complex user-defined types among other features.

Filter by
Sorted by
Tagged with
42 votes
2 answers
33k views

What is the difference between bytes and bytes32?

I know bytes32 stores 32 bytes, but what does bytes store? And what is the difference between the two?
cryptorenaisance's user avatar
41 votes
5 answers
48k views

Convert address to string

In Solidity, how can I convert the sender address to a string? The comments in How to convert an address to bytes in Solidity? did not provide a working solution
nick carraway's user avatar
41 votes
2 answers
23k views

Deploy contract from contract in Solidity

It's a common pattern to use a hub/factory contract to create multiple instances of a standard contract. It's also common to require a way to enumerate the addresses of the created contracts. Is ...
Rob Hitchens's user avatar
  • 55.4k
40 votes
1 answer
38k views

How to delete an element from a mapping?

I have a mapping like this: struct data { string name; string nickname; } mapping(address => data) public user; What is the correct way to delete one element from the variable user? ...
Bumblebee's user avatar
  • 1,761
39 votes
7 answers
32k views

How to convert a uint to bytes in Solidity?

Is there an easy way to convert a uint to bytes in Solidity?
eth's user avatar
  • 86.1k
39 votes
5 answers
39k views

Returning a struct and reading via Web3

I am storing data in my contract using a mapping of structs. For examples sake, say I'm storing employee info (name/address/salary), mapped by their employee ID. Via a web front-end, I'd like to be ...
Amer Ameen's user avatar
39 votes
2 answers
14k views

Why do Solidity examples use bytes32 type instead of string?

In many Solidity examples I read that use strings for parameters or return values, I see they are typed as bytes32 although there's a string type. What is the real reason for that? Thanks.
Nicolas Massart's user avatar
38 votes
3 answers
26k views

'internal' keyword in a function definition in Solidity

Does the internal keyword in a function definition such as: function doSomeThing(uint _param) internal {...} work similar to designating a method as private in other languages and thus makes the ...
Terek Judi's user avatar
38 votes
2 answers
34k views

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

I have a contract named Call: contract Call { ... function () payable { // TODO: Call the call function in the main contract // and forward all funds (msg.value) sent to this contract ...
Pabi's user avatar
  • 1,129
38 votes
1 answer
28k views

Ways to see if address is empty

Is owner > 0x0 a good way to test if an address owner was set in Solidity? I am not sure if this could break something in the future and if there are better ways to see if the address was set. What ...
Roland Kofler's user avatar
38 votes
14 answers
93k views

Source File requires different compiler version

I am using solidity version "pragma solidity ^0.4.20;". When I checked JSON files of contract, compiler version shows : "compiler": { "name": "solc", "version": "0.4.21+commit.dfe3193c....
angelmaria's user avatar
  • 1,061
38 votes
4 answers
30k views

Why are there two methods encoding arguments? "abi.encode" and "abi.encodePacked"

When we call a function on an external contract, the EVM computes a bytes buffer which contains the function signature and arguments. There are two ways to serialize arguments: abi.encode() abi....
Bob5421's user avatar
  • 1,477
38 votes
3 answers
45k views

How can I view event logs for an ethereum contract?

After reading the this post, it seems like I should be able to access the logs of events that have been called on an Ethereum contract. The section I am interested in is Option 3 which is using event ...
user1911077's user avatar
37 votes
4 answers
34k views

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

Can someone help out with the stated error, I am trying to run code to check the status of a String variable. While compiling using the online solidity compiler I am getting two errors on customcheck ...
srbamb's user avatar
  • 463
37 votes
4 answers
21k views

Store data in mapping vs. array

I see there're some Solidity examples that are using mapping for storing state, and some are using arrays. For example this example is using mapping: struct Campaign { .... } uint numCampaigns; ...
Igor Artamonov's user avatar
36 votes
1 answer
26k views

What does the keyword `super` in Solidity do?

I came across the super keyword in Solidity in the context of overriding functions. What does it do?
SCBuergel's user avatar
  • 8,774
36 votes
2 answers
40k views

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

Can we get all elements stored in a mapping in the contract using web3.js ?
takampika's user avatar
  • 385
36 votes
1 answer
35k views

What is the receive keyword in solidity?

Solidity has a receive keyword. What is it and how do I use it?
Shane Fontaine's user avatar
36 votes
10 answers
18k views

How can you handle an expected throw in a contract test using truffle and ethereum-testRPC?

Is it possible to write a test using truffle that attempts to confirm that a throw occurs in a contract? For example, if I had a contract with function... contract TestContract { function testThrow(...
thedob's user avatar
  • 615
36 votes
3 answers
24k views

ERC20 vs ERC223. List of differences

Is there a list of improvements that we will have in ERC223? Are there examples of contracts? Is there a migration path for existing ERC20 tokens?
rstormsf's user avatar
  • 4,357
36 votes
2 answers
29k views

Can you create a constant (read-only) state variable in Solidity?

Can you declare a constant variable in Solidity? const uint answer = 42; Or is the only way to do it with a const function? function GetAnswer() constant returns(uint ret) { return 42; } I know ...
Raine Revere's user avatar
  • 3,610
36 votes
2 answers
18k views

Ethereum ecrecover signature verification and encryption

The question of signing and verifying signatures with Solidity has been asked in these questions, both of which I have tried to study: How can I sign a piece of data with the private key of an ...
arik's user avatar
  • 503
35 votes
10 answers
51k views

How can I return struct when function is called?

I'd like to return struct type User. However, when I tried the following getUser function, it returns an error. Is there any way that struct data would be returned from a function? Contract struct ...
Toshi's user avatar
  • 2,357
35 votes
3 answers
21k views

understanding mload assembly function

I am learning the inline assembly in solidity, I have found the function mload(0x40) I am confused with what this function does. Did it reserve 4 Slot/word in memory or did it load the data stored in ...
Sig Touri's user avatar
  • 1,090
35 votes
2 answers
27k views

What is the difference between `now` and `block.timestamp`?

My main goal is to obtain time spent between two transaction calls inside my smart-contract. I am not sure, should I depend on now or block.timestamp to obtain the most accurate time difference in ...
alper's user avatar
  • 8,394
35 votes
2 answers
7k views

Ethereum smart contract security checklist

Is there a tool, checklist of methodology to help reviewing smart contract security? E.g. for each of function you review Is there re-entry possibility and how it behaves on re-entry How it behaves ...
Mikko Ohtamaa's user avatar
34 votes
6 answers
27k views

ecrecover from Geth and web3.eth.sign

I have tried to use ecrecover() to verify the signature of a message. I have looked at lots of references here and elsewhere, like: https://lab-blog.ey.com/2016/12/19/ecrecover/ How does one ...
Daniel Mermelstein's user avatar
34 votes
4 answers
38k views

Contract should be marked as abstract

I'm getting the error: Contract "StarNotary" should be marked as abstract. pragma solidity >=0.4.24; import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract ...
Kevvv's user avatar
  • 797
34 votes
7 answers
16k views

How to store IPFS hash using bytes32?

Following Q/A (What datatype should I use for an IPFS address hash?) recommend us to use bytes to store IPFS hash. I was using following example(https://github.com/AdrianClv/ethereum-ipfs/blob/master/...
alper's user avatar
  • 8,394
34 votes
2 answers
27k views

Solidity inherited constructors and calling the parent constructor

I'm trying to inherit from a contract. I'm expecting the child contract to have the same constructor as the parent (or be overwritten) pragma solidity ^0.4.2; contract parent{ string public ...
user1938620's user avatar
34 votes
8 answers
48k views

Smart Contract Error: Returned values aren't valid, did it run Out of Gas?

Deployed a contract using Remix IDE to Rinkeby testnet. Etherscan contract address and code is here. contract SimpleCounter { int counter; constructor() public { counter = 0; } ...
RafiAlhamd's user avatar
34 votes
2 answers
25k views

Remix warnings - state mutability and public visibility

I noticed the new Remix gave me two new warnings: browser/Untitled.sol:6:5: Warning: No visibility specified. Defaulting to "public". function setFee(uint _fee){ ^ Spanning multiple lines. ...
thefett's user avatar
  • 3,910
34 votes
6 answers
30k views

How do I specify a different Solidity version in a Truffle contract?

I've got a contract where I want to use <address>.transfer(), which was released in the latest Solidity version 0.4.10. I have as the first line of my contract: pragma solidity 0.4.10; but ...
JohnAllen's user avatar
  • 1,318
34 votes
3 answers
41k views

TypeError: Data location must be "storage" or "memory" for parameter in function, but none was given

/// @dev Adds a new transaction to the transaction mapping, if transaction does not exist yet. /// @param destination Transaction target address. /// @param value Transaction ether value. /// @param ...
CryptoScroller's user avatar
33 votes
7 answers
49k views

how to compare strings in solidity?

pragma solidity ^0.4.11; contract test2 { address creater; string username; string password; function testusernamepassword(string username,string password) returns (bool) { ...
Deepak Baberwal's user avatar
33 votes
4 answers
26k views

What is the purpose of "unchecked" in Solidity?

According to Solidity documentation: Prior to Solidity 0.8.0, arithmetic operations would always wrap in case of under- or overflow leading to widespread use of libraries that introduce additional ...
Alexander Herranz's user avatar
33 votes
3 answers
24k views

Can I save structs in a Mapping?

Is it possible to store structs in a Mapping or any dynamic memory structure in my Solidity SmartContract?
arodriguezdonaire's user avatar
33 votes
3 answers
23k views

What is the 'this' keyword in Solidity?

What is the specification of the 'this' keyword in Solidity? How does it work? Sample code from Solidity Features · ethereum/wiki Wiki contract Helper { function getBalance() returns (uint bal) { ...
Satoshi Nakanishi's user avatar
33 votes
3 answers
30k views

How does Mapping in solidity work?

I went through the Crowdfunding example in solidity where i encountered the following: contract Crowdfunding { struct CampaignData { address recipient; uint contributed; uint goal; ...
Rahul Sharma's user avatar
  • 1,303
33 votes
1 answer
5k views

How does the solidity optimizer work?

The compiler for solidity supports the flag --optimize which performs some set of optimizations to the compiled solidity code, typically resulting in a reduction of gas costs. How does the optimizer ...
Piper Merriam's user avatar
32 votes
4 answers
33k views

Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient

What does this warning actually mean and how to resolve this ?
Lokesh Mishra's user avatar
32 votes
4 answers
61k views

What is msg.value?

Can somebody explain what is the magic msg.value? I came across this code: https://learnxinyminutes.com/docs/solidity/ /// @return The balance of the user after the deposit is made function deposit() ...
idoor's user avatar
  • 421
31 votes
2 answers
107k views

What is uint256?

With regard to Solidity, What is UINT256? From the token example at https://ethereum.org/token : /* This creates an array with all balances */ mapping (address => uint256) public balanceOf; ...
Bruce Seymour's user avatar
31 votes
3 answers
10k views

What is the difference between the constant and immutable keywords in Solidity?

What is the difference between the constant and immutable keywords in Solidity and how do I use them?
Shane Fontaine's user avatar
31 votes
1 answer
21k views

How does the delegatecall method work to call to another contract's method?

I'm trying to figure out how to use the delegatecall method on a contract address in a contract to call a function in a separate contract. I know Libraries are the higher level interface to use but I ...
aDude's user avatar
  • 313
30 votes
4 answers
36k views

How to get return values when a non view function is called?

I am writing below a simple contract which stores all results of questionnaires for each ID. contract answer{ mapping(address => mapping(string => bool)) voters; struct qList { uint ...
A. Take's user avatar
  • 424
30 votes
4 answers
33k views

Calling function from deployed contract

I want to know how to instantiate an existing contract deployed on the blockchain using its address. e.g: contract A { function f1() {} } A is deployed on the blockchain, and in contract B I ...
Sig Touri's user avatar
  • 1,090
30 votes
6 answers
32k views

Solidity: How can we write a error message in "require"?

Im learning Solidity. As far as I'm aware, it's recommended using "require" to handle errors. But, I'd like to inform the users about the kind of errors they make, e.g. an argument is invalid or out ...
Aydin's user avatar
  • 2,117
30 votes
1 answer
42k views

How to convert string to bytes32 in web3js?

I have a contract with a public variable that's bytes32. If I enter the value 20160528 then it auto converts to bytes32. Is there anyway to convert it back from the nodejs terminal? I've been ...
thefett's user avatar
  • 3,910
30 votes
3 answers
23k views

What is msg.data?

Is msg.data equal to {from: addr1, data:something}? How to handle the data 'something'? when execution this code: web3.eth.sendTransaction({from: ..., to: addressOfE, data: something}); how to ...
wakerch's user avatar
  • 451