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.

Solidity is roughly speaking, an object-oriented language designed for writing contracts in Ethereum. Contracts are (typically) small programs which govern the behaviour of accounts within the Ethereum state. These programs operate within the context of the Ethereum environment. Such accounts are able to pass messages between themselves as well as doing practically Turing complete computation.

Solidity is perhaps the first example of a contract-oriented programming language; a slight tweak on the notion of object-orientation. While closely related to object-oriented languages, this is a language designed specifically to help express agreements that must encode ideas and relationships relevant to Real Life, or some formal model thereof. As such we see notions such as ownership, identity, protections and restrictions forming a core part of the vocabulary and idiomatic grammar.

We see language grammar actually tieing in with many of the aspects of this: the event primitives along with the indexed keyword explicitly address the logging environment which Ethereum provides. The variadic return values mimic the fact that output data of Ethereum's calling mechanism is, like the input data, an arbitrary byte array.

Hello, World!

No language would be complete without a Hello World program. Operating within the Ethereum environment, Solidity has no obvious way of "outputting" a string. The closest we can do is to use a log event to place a string into the blockchain:

contract HelloWorld {
  event Print(string out);
  function() external payable{ emit Print("Hello, World!"); }
}

Once contract is placed on the blockchain and sending some ether direct to its contract address would create a log entry on the blockchain of type Print with a parameter "Hello, World!".

Edit: Solidity is a high level programming language which looks like JavaScript and object oriented programming languages. It is the go to language used to write smart contracts on Ethereum. This is a simple definition of Solidity for non-techies who want to get on with coding for smart contracts on the Ethereum platform.

17676 questions
62
votes
5 answers

What is the difference between an internal/external and public/private function in solidity?

Currently reading solidity documentation: https://solidity.readthedocs.io/en/develop/types.html#function-types By default, function types are internal, so the internal keyword can be omitted. In contrast, contract functions themselves are public…
monty_lennie
  • 763
  • 1
  • 5
  • 9
36
votes
2 answers

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 you could always create a normal public variable…
Raine Revere
  • 3,600
  • 2
  • 23
  • 34
33
votes
3 answers

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) { return this.balance; // balance is "inherited"…
Satoshi Nakanishi
  • 5,749
  • 7
  • 38
  • 57
32
votes
4 answers

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() public payable returns (uint) { // Use…
idoor
  • 421
  • 1
  • 4
  • 4
31
votes
7 answers

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) { if (username == "deepak" && password ==…
Deepak Baberwal
  • 389
  • 1
  • 5
  • 5
30
votes
5 answers

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
  • 491
  • 1
  • 5
  • 7
28
votes
4 answers

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() function. I believe the return value of the push…
Kuelf Deez
  • 576
  • 2
  • 7
  • 15
27
votes
2 answers

What does _ (underscore) do?

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
  • 598
  • 1
  • 5
  • 11
24
votes
3 answers

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
  • 719
  • 1
  • 6
  • 17
23
votes
2 answers

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
  • 1,473
  • 5
  • 17
  • 30
22
votes
9 answers

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 have around five function arguments and a few…
Jeffrey Liang
  • 271
  • 1
  • 2
  • 4
19
votes
2 answers

Whats the difference between .call.value() and .call.value()()

contract Test { function Test(){ msg.sender.call.value(0); msg.sender.call.value(0)(); } } Whats the difference here between the first and second call? My hunch is the 1st one doesn't actually do anything, however this…
Akhil F
  • 1,928
  • 3
  • 19
  • 29
19
votes
1 answer

What are the attributes of the `msg` object and how can I list them?

I know msg.sender exists, it gives the address of the sender. What other attributes are there and how can I list them / where are they defined?
TMOTTM
  • 1,953
  • 2
  • 14
  • 23
18
votes
3 answers

How to do Solidity percentage calculation?

I'm trying to calculate a percentage of a number in solidity with the following code. The percentage is technically basis points (10000ths, instead of 100ths) because I want decimal percentage values. uint128 is enough for expressing the normal…
Svante
  • 450
  • 1
  • 3
  • 10
18
votes
4 answers

Delete all elements from an array?

I am writing a smart contract to be the backend of a game. Instead of using a mapping, I have decided to use an array to store the addresses of accounts who have paid my contract. (I know it is common practice to use a mapping for this but I find…
Bryan Campbell
  • 421
  • 2
  • 5
  • 10
1
2 3
99 100