mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
add "pragma solidity ^0.4.0;" to code examples
This commit is contained in:
parent
341c9436a8
commit
183cd70c47
@ -28,6 +28,8 @@ become the new richest.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract WithdrawalContract {
|
contract WithdrawalContract {
|
||||||
address public richest;
|
address public richest;
|
||||||
uint public mostSent;
|
uint public mostSent;
|
||||||
@ -68,6 +70,8 @@ This is as opposed to the more intuitive sending pattern.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SendContract {
|
contract SendContract {
|
||||||
address public richest;
|
address public richest;
|
||||||
uint public mostSent;
|
uint public mostSent;
|
||||||
@ -131,6 +135,8 @@ restrictions highly readable.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract AccessRestriction {
|
contract AccessRestriction {
|
||||||
// These will be assigned at the construction
|
// These will be assigned at the construction
|
||||||
// phase, where `msg.sender` is the account
|
// phase, where `msg.sender` is the account
|
||||||
@ -270,6 +276,8 @@ function finishes.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract StateMachine {
|
contract StateMachine {
|
||||||
enum Stages {
|
enum Stages {
|
||||||
AcceptingBlindedBids,
|
AcceptingBlindedBids,
|
||||||
|
@ -65,6 +65,8 @@ This means that cyclic creation dependencies are impossible.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract OwnedToken {
|
contract OwnedToken {
|
||||||
// TokenCreator is a contract type that is defined below.
|
// TokenCreator is a contract type that is defined below.
|
||||||
// It is fine to reference it as long as it is not used
|
// It is fine to reference it as long as it is not used
|
||||||
@ -189,6 +191,8 @@ return parameter list for functions.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f(uint a) private returns (uint b) { return a + 1; }
|
function f(uint a) private returns (uint b) { return a + 1; }
|
||||||
function setData(uint a) internal { data = a; }
|
function setData(uint a) internal { data = a; }
|
||||||
@ -201,6 +205,8 @@ In the following example, ``D``, can call ``c.getData()`` to retrieve the value
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint private data;
|
uint private data;
|
||||||
|
|
||||||
@ -243,6 +249,8 @@ be done at declaration.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint public data = 42;
|
uint public data = 42;
|
||||||
}
|
}
|
||||||
@ -262,6 +270,8 @@ it is evaluated as a state variable and if it is accessed externally
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint public data;
|
uint public data;
|
||||||
function x() {
|
function x() {
|
||||||
@ -274,6 +284,8 @@ The next example is a bit more complex:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Complex {
|
contract Complex {
|
||||||
struct Data {
|
struct Data {
|
||||||
uint a;
|
uint a;
|
||||||
@ -307,6 +319,8 @@ inheritable properties of contracts and may be overridden by derived contracts.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract owned {
|
contract owned {
|
||||||
function owned() { owner = msg.sender; }
|
function owned() { owner = msg.sender; }
|
||||||
address owner;
|
address owner;
|
||||||
@ -408,6 +422,8 @@ for array and struct types and not possible for mapping types).
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint constant x = 32**22 + 8;
|
uint constant x = 32**22 + 8;
|
||||||
string constant text = "abc";
|
string constant text = "abc";
|
||||||
@ -455,6 +471,8 @@ Please ensure you test your fallback function thoroughly to ensure the execution
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Test {
|
contract Test {
|
||||||
function() { x = 1; }
|
function() { x = 1; }
|
||||||
uint x;
|
uint x;
|
||||||
@ -523,6 +541,8 @@ All non-indexed arguments will be stored in the data part of the log.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract ClientReceipt {
|
contract ClientReceipt {
|
||||||
event Deposit(
|
event Deposit(
|
||||||
address indexed _from,
|
address indexed _from,
|
||||||
@ -791,6 +811,8 @@ error "Linearization of inheritance graph impossible".
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract X {}
|
contract X {}
|
||||||
contract A is X {}
|
contract A is X {}
|
||||||
contract C is A, X {}
|
contract C is A, X {}
|
||||||
@ -861,6 +883,8 @@ more advanced example to implement a set).
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
library Set {
|
library Set {
|
||||||
// We define a new struct datatype that will be used to
|
// We define a new struct datatype that will be used to
|
||||||
// hold its data in the calling contract.
|
// hold its data in the calling contract.
|
||||||
@ -931,6 +955,8 @@ custom types without the overhead of external function calls:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
library BigInt {
|
library BigInt {
|
||||||
struct bigint {
|
struct bigint {
|
||||||
uint[] limbs;
|
uint[] limbs;
|
||||||
|
@ -98,6 +98,8 @@ parameters from the function declaration, but can be in arbitrary order.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f(uint key, uint value) { ... }
|
function f(uint key, uint value) { ... }
|
||||||
|
|
||||||
@ -115,6 +117,8 @@ Those names will still be present on the stack, but they are inaccessible.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
// omitted name for parameter
|
// omitted name for parameter
|
||||||
function func(uint k, uint) returns(uint) {
|
function func(uint k, uint) returns(uint) {
|
||||||
@ -136,6 +140,8 @@ creation-dependencies are now possible.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract D {
|
contract D {
|
||||||
uint x;
|
uint x;
|
||||||
function D(uint a) {
|
function D(uint a) {
|
||||||
@ -343,6 +349,8 @@ idea is that assembly libraries will be used to enhance the language in such way
|
|||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
library GetCode {
|
library GetCode {
|
||||||
function at(address _addr) returns (bytes o_code) {
|
function at(address _addr) returns (bytes o_code) {
|
||||||
assembly {
|
assembly {
|
||||||
@ -368,6 +376,8 @@ you really know what you are doing.
|
|||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
library VectorSum {
|
library VectorSum {
|
||||||
// This function is less efficient because the optimizer currently fails to
|
// This function is less efficient because the optimizer currently fails to
|
||||||
// remove the bounds checks in array access.
|
// remove the bounds checks in array access.
|
||||||
@ -622,6 +632,8 @@ It is planned that the stack height changes can be specified in inline assembly.
|
|||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint b;
|
uint b;
|
||||||
function f(uint x) returns (uint r) {
|
function f(uint x) returns (uint r) {
|
||||||
@ -696,6 +708,8 @@ be just ``0``, but it can also be a complex functional-style expression.
|
|||||||
|
|
||||||
.. code::
|
.. code::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f(uint x) returns (uint b) {
|
function f(uint x) returns (uint b) {
|
||||||
assembly {
|
assembly {
|
||||||
|
@ -16,6 +16,8 @@ Storage
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SimpleStorage {
|
contract SimpleStorage {
|
||||||
uint storedData;
|
uint storedData;
|
||||||
|
|
||||||
@ -63,6 +65,8 @@ registering with username and password - all you need is an Ethereum keypair.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Coin {
|
contract Coin {
|
||||||
// The keyword "public" makes those variables
|
// The keyword "public" makes those variables
|
||||||
// readable from outside.
|
// readable from outside.
|
||||||
|
@ -192,6 +192,8 @@ for the two input parameters and two returned values.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
/** @title Shape calculator.*/
|
/** @title Shape calculator.*/
|
||||||
contract shapeCalculator{
|
contract shapeCalculator{
|
||||||
/**@dev Calculates a rectangle's surface and perimeter.
|
/**@dev Calculates a rectangle's surface and perimeter.
|
||||||
|
@ -51,6 +51,8 @@ complete contract):
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
// THIS CONTRACT CONTAINS A BUG - DO NOT USE
|
// THIS CONTRACT CONTAINS A BUG - DO NOT USE
|
||||||
contract Fund {
|
contract Fund {
|
||||||
/// Mapping of ether shares of the contract.
|
/// Mapping of ether shares of the contract.
|
||||||
@ -73,6 +75,8 @@ outlined further below:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Fund {
|
contract Fund {
|
||||||
/// Mapping of ether shares of the contract.
|
/// Mapping of ether shares of the contract.
|
||||||
mapping(address => uint) shares;
|
mapping(address => uint) shares;
|
||||||
@ -149,6 +153,8 @@ Never use tx.origin for authorization. Let's say you have a wallet contract like
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract TxUserWallet {
|
contract TxUserWallet {
|
||||||
address owner;
|
address owner;
|
||||||
|
|
||||||
@ -166,6 +172,8 @@ Now someone tricks you into sending ether to the address of this attack wallet:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract TxAttackWallet {
|
contract TxAttackWallet {
|
||||||
address owner;
|
address owner;
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ of votes.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
/// @title Voting with delegation.
|
/// @title Voting with delegation.
|
||||||
contract Ballot {
|
contract Ballot {
|
||||||
// This declares a new complex type which will
|
// This declares a new complex type which will
|
||||||
@ -208,6 +210,8 @@ activate themselves.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SimpleAuction {
|
contract SimpleAuction {
|
||||||
// Parameters of the auction. Times are either
|
// Parameters of the auction. Times are either
|
||||||
// absolute unix timestamps (seconds since 1970-01-01)
|
// absolute unix timestamps (seconds since 1970-01-01)
|
||||||
@ -377,6 +381,8 @@ high or low invalid bids.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract BlindAuction {
|
contract BlindAuction {
|
||||||
struct Bid {
|
struct Bid {
|
||||||
bytes32 blindedBid;
|
bytes32 blindedBid;
|
||||||
@ -543,6 +549,8 @@ Safe Remote Purchase
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Purchase {
|
contract Purchase {
|
||||||
uint public value;
|
uint public value;
|
||||||
address public seller;
|
address public seller;
|
||||||
|
@ -20,6 +20,8 @@ State variables are values which are permanently stored in contract storage.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SimpleStorage {
|
contract SimpleStorage {
|
||||||
uint storedData; // State variable
|
uint storedData; // State variable
|
||||||
// ...
|
// ...
|
||||||
@ -38,6 +40,8 @@ Functions are the executable units of code within a contract.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SimpleAuction {
|
contract SimpleAuction {
|
||||||
function bid() { // Function
|
function bid() { // Function
|
||||||
// ...
|
// ...
|
||||||
@ -58,6 +62,8 @@ Function modifiers can be used to amend the semantics of functions in a declarat
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Purchase {
|
contract Purchase {
|
||||||
address public seller;
|
address public seller;
|
||||||
|
|
||||||
@ -80,6 +86,8 @@ Events are convenience interfaces with the EVM logging facilities.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract SimpleAuction {
|
contract SimpleAuction {
|
||||||
event HighestBidIncreased(address bidder, uint amount); // Event
|
event HighestBidIncreased(address bidder, uint amount); // Event
|
||||||
|
|
||||||
@ -102,6 +110,8 @@ Structs are custom defined types that can group several variables (see
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Ballot {
|
contract Ballot {
|
||||||
struct Voter { // Struct
|
struct Voter { // Struct
|
||||||
uint weight;
|
uint weight;
|
||||||
@ -121,6 +131,8 @@ Enums can be used to create custom types with a finite set of values (see
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract Purchase {
|
contract Purchase {
|
||||||
enum State { Created, Locked, Inactive } // Enum
|
enum State { Created, Locked, Inactive } // Enum
|
||||||
}
|
}
|
||||||
|
@ -241,6 +241,8 @@ to and from all integer types but implicit conversion is not allowed.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract test {
|
contract test {
|
||||||
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
|
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
|
||||||
ActionChoices choice;
|
ActionChoices choice;
|
||||||
@ -300,6 +302,8 @@ memory-stored reference type does not create a copy.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
uint[] x; // the data location of x is storage
|
uint[] x; // the data location of x is storage
|
||||||
|
|
||||||
@ -378,6 +382,8 @@ the ``.length`` member.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f(uint len) {
|
function f(uint len) {
|
||||||
uint[] memory a = new uint[](7);
|
uint[] memory a = new uint[](7);
|
||||||
@ -397,6 +403,8 @@ assigned to a variable right away.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f() {
|
function f() {
|
||||||
g([uint(1), 2, 3]);
|
g([uint(1), 2, 3]);
|
||||||
@ -416,6 +424,8 @@ possible:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract C {
|
contract C {
|
||||||
function f() {
|
function f() {
|
||||||
// The next line creates a type error because uint[3] memory
|
// The next line creates a type error because uint[3] memory
|
||||||
@ -452,6 +462,8 @@ Members
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract ArrayContract {
|
contract ArrayContract {
|
||||||
uint[2**20] m_aLotOfIntegers;
|
uint[2**20] m_aLotOfIntegers;
|
||||||
// Note that the following is not a pair of arrays but an array of pairs.
|
// Note that the following is not a pair of arrays but an array of pairs.
|
||||||
@ -521,6 +533,8 @@ shown in the following example:
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
|
pragma solidity ^0.4.0;
|
||||||
|
|
||||||
contract CrowdFunding {
|
contract CrowdFunding {
|
||||||
// Defines a new type with two fields.
|
// Defines a new type with two fields.
|
||||||
struct Funder {
|
struct Funder {
|
||||||
|
Loading…
Reference in New Issue
Block a user