Solidity v1
Solidity v1
A smart contract is a A smart contract in A smart contract not only defines the rules and penalties around an agreement in the same
computer program that Ethereum is written in way that a traditional contract does, but it can also automatically enforce those obligations.
directly controls the transfer Solidity – a contract-
of digital currencies or oriented, high-level
assets between parties language whose syntax is Contracts in Solidity are similar to classes in object-oriented languages. They contain
under certain conditions. similar to that of JavaScript. persistent data in state variables and functions that can modify these variables .
State Variables
contract Coin {
Contracts permanently stored in /* State variable*/
contract storage string public name = 'Cool Coin Token';
The contract is the basic structure of Solidity. It is a prototype // ...
of an object which lives on the blockchain. uint public totalSupply = 0;
address public authority;
// ...
contract Simple { }
uint public x = 2;
Executable units of
Functions
function() { function checkGoalReached() returns (bool) {
var z = x + 2; code within a contract if (now >= deadline ||
// var is “uint” here closeOnGoalReached) {
} if (amountRaised >= fundingGoal){
} fundingGoalReached = true;
GoalReached(beneficiary,
Types }
amountRaised);
contract SimpleAuction {
EVM logging facilities event HighestBidIncreased(address bidder,
• External functions consist of • Internal functions can only uint amount); // Event
an address and a function be used inside the current
signature and they can be contract because they function bid() payable {
passed via and returned cannot be executed outside // ...
from external function calls. of the context of the current HighestBidIncreased(msg.sender,
contract. Calling an internal msg.value); // Triggering event
function is realized by }
jumping to its entry label, }
just like when calling a
Custom defined types
Structs Types
contract Purchase {
Visibility and Getters used to create custom
types with a finite set
enum State { Created, Locked, Inactive }
// Enum
Solidity knows two kinds of function calls (internal ones that do of values }
not create an actual EVM call (also called a “message call”)
and external ones that do), consequently there are four types of
visibilities for functions and state variables.
• external. Part of the contract • public. Part of the contract interface, • internal. These functions and state • private. These functions and state
interface, thatcan be called from the functions can be either called variables can only be accessed, variables are only visible for the
other contracts and via transactions. internally or via messages. For without using this. contract they are defined in and not
An external function f cannot be public state variables, an automatic in derived contracts.
called internally. getter function is generated.
More information