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
30 votes
5 answers
40k views

How can I perform float type division in solidity?

How can I achieve proper division of two integers in solidity? Suppose I perform : 3/2 the solution provided is 1 and not 1.5 please help.
Vaibhav Tiwari's user avatar
30 votes
1 answer
14k views

How can I verify a cryptographic signature that was produced by an Ethereum address key pair

If I have a cryptographic signature from an Ethereum address key pair, how can I verify that signature?
Piper Merriam's user avatar
30 votes
2 answers
9k views

Is there a maximum number of entries for a mapping?

In many contracts, notably voting, there is a mapping of the kind mapping(address => bool) voted; How many voters can be registered in that way? is there a size limit? will the contract need more ...
Micha Roon's user avatar
  • 2,213
29 votes
6 answers
6k views

What are common pitfalls or limitations when coding in Solidity?

What would be on your list to warn other developers about? What works in most other languages that does not work, or behave as expected, in Solidity? For example only: what parameters can be passed ...
eth's user avatar
  • 85.9k
29 votes
2 answers
12k views

Can Solidity function modifiers access function arguments?

I would like to have a modifier that looks like this: modifier restrictTargetType(uint contractType) { if (contractTypes[target] != contractType) { throw; } _ } Where target is an address ...
PeterBB's user avatar
  • 615
29 votes
3 answers
13k views

How does Solidity's sha3 / keccak256 hash uints?

This is not a question about the issue with Ethereum using a non-standard sha3. I have found the right JS hashing library, and am able to get matching hashes in JS and Solidity on byte strings. What I ...
Jehan's user avatar
  • 783
29 votes
2 answers
5k views

What is the merit of creating new smart contract languages like Solidity instead of using other languages?

What is the pros and cons of creating new languages like Solidity for smart contracts instead of using other computer languages like Golang or Python?
Satoshi Nakanishi's user avatar
29 votes
2 answers
40k views

How can I return multiple strings from a contract function?

In solidity it's obvious how to get a single string returned from a function. But, I'd like to have the smart contract return multiple string values. Is there a way to do this?
Ethan Wilding's user avatar
29 votes
1 answer
31k views

Stack too deep, try removing local variables [duplicate]

When I set more than eight values as function’s return , the following error occurred. ( When the num of values is less than seven, the error didn’t occur) Could you tell me how can I get more than ...
Toshi's user avatar
  • 2,357
28 votes
7 answers
43k views

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

I have created the contract below using the online IDE Remix. the smart contract was fine until late last night when i got the following error for all functions(targeting the string data types ...
Lonwabo Zaula's user avatar
28 votes
3 answers
12k views

How to check events in Truffle Tests?

What's the best way to check that events were fired in a Truffle test?
nickjm's user avatar
  • 865
28 votes
4 answers
50k views

What is the return of array.push() in Solidity?

I saw these code lines in cryptozombies.io: uint id = zombies.push(Zombie(_name, _dna)) - 1; zombieToOwner[id] = msg.sender; I wouldn't have had a problem had there not been "- 1" after the push() ...
Kuelf Deez's user avatar
28 votes
2 answers
13k views

What does _ (underscore) do? [duplicate]

Browsing through the Ethereum docs I came across this modifier: modifier onlyOwner { if (msg.sender != owner) throw; _; } I am not familiar with usage of _. What does it mean in this context?...
nipponese's user avatar
  • 608
28 votes
8 answers
9k views

Source code formatter for Solidity?

Most programming languages have tools to format source code according to convention: for example, Python has autopep8 and yapf, and Go has gofmt. Does a similar tool exist for Solidity source code, ...
Pi Delport's user avatar
28 votes
6 answers
17k views

having a problem with solc-x version "solc 0.6.0 has not been installed"

I m trying to print out the variable (compiled_sol) as you will see in the code below of the python file named deploy.py so I can deploy my smart contract, but I keep running at this error *****INFO: ...
Houssam DERFOUFI's user avatar
28 votes
2 answers
28k views

How to pass arbitrary bytes to a function in Remix (2017)

According to this question I could write a simple contract like: contract SimpleStorage { bytes input; function setInput(bytes enterBytes){ input = enterBytes; } } Using "0x1234" as a ...
floyd's user avatar
  • 599
28 votes
1 answer
26k views

Create a two-dimensional array in Solidity

How to create arrays addresses using solidity? And we can write two-dimensional array? bool[2][] m_pairsOfFlags; Here is an example of the array, but it does not work
M.Sheremain's user avatar
28 votes
1 answer
5k views

Child contract vs struct

Let`s say I need to store and manipulate collection of complex data in the smart contract. There maybe complex rules defining who and how can change an item. There maybe be several millions of items ...
Sergey Ilin's user avatar
28 votes
1 answer
11k views

What is the cheapest hash function available in solidity?

AFAIK there are 3 hash functions to choose from in solidity (sha3, sha256, and ripemd). sha3 is native while the others use precompiled contracts. Which one of these is cheapest (in terms of gas)?
Akhil F's user avatar
  • 1,928
28 votes
2 answers
3k views

Is it a good practice to log an event every time I throw in Solidity?

In Solidity (0.3.2) I am aware that there is no way to throw specific exceptions. (Everything is an invalid JUMP error.) It seems beneficial to have a way to log specific errors. Does it make sense to ...
Raine Revere's user avatar
  • 3,610
28 votes
1 answer
10k views

Cannot perform write functions in smart contract - invalid address?

I have the following simple smart contract... contract SimpleStorage { uint storedData; function set(uint x) { storedData = x; } function get() constant returns (uint retVal) ...
billett's user avatar
  • 1,255
28 votes
4 answers
35k views

Solidity: Can you return dynamic arrays in a function?

I know I can return dynamic byte arrays in Solidity, such as in the following code: function createMemoryArray(uint size) returns (bytes) { // Dynamic memory arrays are created using `new`: ...
user avatar
27 votes
6 answers
13k views

Can I square root in Solidity?

Is it possible to square root 50 or should I use 7.071067. I'm very new in programming and I also understand that it's possible in all other languages, but I'm not able to see where the limitations ...
Sileniced's user avatar
  • 537
27 votes
2 answers
13k views

Is block.timestamp safe for longer time periods?

I would like to set a timer like uint timeout = now + 1 year; Is it possible that a miner cheats the timestamp of the block that it's included more then let's say a month. So if I need roughly 1 ...
Jerry's user avatar
  • 734
27 votes
2 answers
10k views

Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce?

Solidity has a function named sha3 and I tested it with an empty string input. It is not matching what other sha3 libraries produce. I see contracts written by others using the same sha3 function ...
eth's user avatar
  • 85.9k
27 votes
6 answers
17k views

How to compile Solidity contracts with Geth v1.6?

I tried to compile the following contract within geth: pragma solidity ^0.4.8; contract Test { uint256 public value; function Test() { value = 123; } } using the following ...
BokkyPooBah's user avatar
  • 40.3k
27 votes
5 answers
20k views

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

Is uint the same as uint256 in Solidity? If this is the case, then why do a lot smart contracts make extensive use of uint256 and not just write uint instead? Is this merely to be explicit?
Elie Steinbock's user avatar
27 votes
4 answers
22k views

Can't do any integer division

I'm building a new smart contract but really can't figure out how to perform integer division. I know that fixed point numbers haven't been implemented yet but it should be possible to perform 100/3 = ...
Hugo Moreau's user avatar
27 votes
1 answer
4k views

Are events permanently stored and reliably available to contracts in the future?

I've seen this forum post about using Ethereum as an event store: Events in Solidity, using blockchain as eventstore. However, it doesn't seem conclusive. For instance it says: I confirmed with ...
Noah Thorp's user avatar
27 votes
3 answers
21k views

How to use address.call{}() in Solidity

I have seen here and several other places that the correct way to send ether from a contract to an address is to call the .call{}() method. Then here, I have seen the method being used with arguments, ...
YulePale's user avatar
  • 1,853
26 votes
6 answers
37k views

What is address(this) in solidity?

As per my understanding contract`s own address is the address which we assign to owner of contract using msg.sender. But I saw this question on SE where it was described that a contract can access its ...
Anam Nizami's user avatar
26 votes
1 answer
26k views

How can you check if a `string` is empty in Solidity [duplicate]

How can you check if a type string is empty in Solidity? For instance this would work when testing if a string was initialized to have no characters: string public emptyStringTest = ''; or when you ...
Karl Floersch's user avatar
26 votes
4 answers
27k views

How to fill dynamic in-memory array

For some reason solidity doesn't allow to push values into memory array Member "push" is not available in bytes32[] memory outside of storage. Here is sample contract code: pragma solidity ^0.4....
Alex Zhukovskiy's user avatar
25 votes
2 answers
22k views

How to cast address to address payable in solidity 0.5.0

Got address from 3rd-party library: address wallet = rlp[5].toAddress(); And wanna transfer some funds to it: wallet.transfer(amount); Got error: TypeError: Member "transfer" not found or not ...
k06a's user avatar
  • 3,046
25 votes
3 answers
14k views

What is the purpose of underscores in front of parameters?

I see contracts that use underscores in front of parameter names, like so: function Greeter(string _greeting) public { greeting = _greeting; } What's the purpose of these underscores? Do they ...
Joël's user avatar
  • 1,720
25 votes
5 answers
13k views

Pass a function as a parameter in Solidity

Can you pass a function as a parameter in Solidity? My guess: There is the concept of address in Solidity, but they represent contracts. Contracts can have fallback functions, but I don't think you ...
Karl Floersch's user avatar
25 votes
1 answer
30k views

Mapping with array as Key Or Value data type

Is it possible to do a mapping with an Array as key/value ? Where an array would exist for each value ? I'm actually looking to store structs in a mapping but can't seem to have it work. Struct Foo{...
jayD's user avatar
  • 2,472
25 votes
1 answer
12k views

Does it cost more gas to call another contract's functions in solidity?

I would like to break my code up into multiple contracts, but I'm afraid of this increasing the gas fees. Does an external function call cost more gas than an internal call to a function within the ...
Jonah's user avatar
  • 655
25 votes
1 answer
11k views

What is the exact meaning of a transaction's new receipt 'status' field?

In this answer (https://ethereum.stackexchange.com/a/6010/1529) it seems to say that the status field will always be '1' unless the transaction failed, in which case it will be '0'. In the summary ...
Thomas Jay Rush's user avatar
25 votes
1 answer
13k views

Is Solidity block.number more secure than timestamp?

In Solidity, some properties such as block.timestamp are attackable by miners and are not (strongly) protected by the protocol. How about block.number, could a miner introduce a randomly high number? ...
SCBuergel's user avatar
  • 8,774
24 votes
3 answers
23k views

Listening to events using ethers.js on a hardhat test network

I am currently migrating a truffle project to hardhat. I am facing a problem with listening to events. I have a simple ERC20 token and want to listen to the Transfer event. I am running the contract ...
Sebastian Dine's user avatar
24 votes
9 answers
28k views

How to Fix "Stack Too Deep" Error?

I know that this has been covered in other questions here but I'm not sure what's happening in my case. My code is returning a compiler error "Stack too deep, try removing local variables." But I only ...
Jeffrey Liang's user avatar
24 votes
3 answers
19k views

Power operations in solidity

Does solidity supports power operations like 2^3 = 8 or should I perform multiple multiplications? Should i include a math library? I don't find anything in the official doc. Thanks
Arslan Smal's user avatar
24 votes
2 answers
16k views

What is the immutable keyword in Solidity?

What is the immutable keyword in Solidity and how do I use it?
Shane Fontaine's user avatar
24 votes
1 answer
15k views

Initialising structs to storage variables

The following code fails to compile with error: Type struct SomeStruct memory is not implicitly convertible to expected type struct SomeStruct storage pointer. contract StructExample { struct ...
naddison's user avatar
  • 528
24 votes
2 answers
4k views

Why are local variables allocated to storage instead of memory?

Local variables (i.e. variables declared inside functions) are held in persistent storage by default instead of non-persistent memory (source). The value of a local variable can not persist in ...
Harry Wright's user avatar
  • 1,171
24 votes
2 answers
15k views

Throw vs. Return

We're trying to figure out whether to use return or throw in Solidity when a condition fails and we don't assume user maliciousness. Here's the pros and cons we've figured so far: Why use throw Any ...
ronme's user avatar
  • 1,035
23 votes
4 answers
34k views

Solidity - List contains

given address[] wallets. What is the correct method to check that the list contains a given address? Does solidity provide any native list contains function? If not, is the only way to do a full ...
monokh's user avatar
  • 467
23 votes
3 answers
38k views

This contract may be abstract, not implement an abstract parent's methods completely or not invoke an inherited contract's constructor correctly

I'm trying to test out Ampleforth on Rinkbey using Remix. Here are all the files that I copy into remix. https://github.com/cryptoghoulz/based-contracts/tree/master/contracts/v4 They compile fine, but ...
Richard Garfield's user avatar
23 votes
2 answers
22k views

Meaning of 'using SafeMath for uint256;'

Hi I just started to study solidity. Can you tell me what is using SafeMath for uint256;? https://github.com/decentraland/mana/blob/master/contracts/ContinuousSale.sol#L10
zono's user avatar
  • 1,483