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 {
|
||||
address public richest;
|
||||
uint public mostSent;
|
||||
@ -68,6 +70,8 @@ This is as opposed to the more intuitive sending pattern.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract SendContract {
|
||||
address public richest;
|
||||
uint public mostSent;
|
||||
@ -131,6 +135,8 @@ restrictions highly readable.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract AccessRestriction {
|
||||
// These will be assigned at the construction
|
||||
// phase, where `msg.sender` is the account
|
||||
@ -270,6 +276,8 @@ function finishes.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract StateMachine {
|
||||
enum Stages {
|
||||
AcceptingBlindedBids,
|
||||
|
@ -65,6 +65,8 @@ This means that cyclic creation dependencies are impossible.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract OwnedToken {
|
||||
// TokenCreator is a contract type that is defined below.
|
||||
// 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 {
|
||||
function f(uint a) private returns (uint b) { return a + 1; }
|
||||
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 {
|
||||
uint private data;
|
||||
|
||||
@ -243,6 +249,8 @@ be done at declaration.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
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 {
|
||||
uint public data;
|
||||
function x() {
|
||||
@ -274,6 +284,8 @@ The next example is a bit more complex:
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract Complex {
|
||||
struct Data {
|
||||
uint a;
|
||||
@ -307,6 +319,8 @@ inheritable properties of contracts and may be overridden by derived contracts.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract owned {
|
||||
function owned() { owner = msg.sender; }
|
||||
address owner;
|
||||
@ -408,6 +422,8 @@ for array and struct types and not possible for mapping types).
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
uint constant x = 32**22 + 8;
|
||||
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 {
|
||||
function() { x = 1; }
|
||||
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 {
|
||||
event Deposit(
|
||||
address indexed _from,
|
||||
@ -791,6 +811,8 @@ error "Linearization of inheritance graph impossible".
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract X {}
|
||||
contract A is X {}
|
||||
contract C is A, X {}
|
||||
@ -861,6 +883,8 @@ more advanced example to implement a set).
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
library Set {
|
||||
// We define a new struct datatype that will be used to
|
||||
// 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 {
|
||||
struct bigint {
|
||||
uint[] limbs;
|
||||
|
@ -98,6 +98,8 @@ parameters from the function declaration, but can be in arbitrary order.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
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 {
|
||||
// omitted name for parameter
|
||||
function func(uint k, uint) returns(uint) {
|
||||
@ -136,6 +140,8 @@ creation-dependencies are now possible.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract D {
|
||||
uint x;
|
||||
function D(uint a) {
|
||||
@ -343,6 +349,8 @@ idea is that assembly libraries will be used to enhance the language in such way
|
||||
|
||||
.. code::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
library GetCode {
|
||||
function at(address _addr) returns (bytes o_code) {
|
||||
assembly {
|
||||
@ -368,6 +376,8 @@ you really know what you are doing.
|
||||
|
||||
.. code::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
library VectorSum {
|
||||
// This function is less efficient because the optimizer currently fails to
|
||||
// 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::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
uint b;
|
||||
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::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
function f(uint x) returns (uint b) {
|
||||
assembly {
|
||||
|
@ -16,6 +16,8 @@ Storage
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract SimpleStorage {
|
||||
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 {
|
||||
// The keyword "public" makes those variables
|
||||
// readable from outside.
|
||||
|
@ -192,6 +192,8 @@ for the two input parameters and two returned values.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
/** @title Shape calculator.*/
|
||||
contract shapeCalculator{
|
||||
/**@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
|
||||
contract Fund {
|
||||
/// Mapping of ether shares of the contract.
|
||||
@ -73,6 +75,8 @@ outlined further below:
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract Fund {
|
||||
/// Mapping of ether shares of the contract.
|
||||
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 {
|
||||
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 {
|
||||
address owner;
|
||||
|
||||
|
@ -36,6 +36,8 @@ of votes.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
/// @title Voting with delegation.
|
||||
contract Ballot {
|
||||
// This declares a new complex type which will
|
||||
@ -208,6 +210,8 @@ activate themselves.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract SimpleAuction {
|
||||
// Parameters of the auction. Times are either
|
||||
// absolute unix timestamps (seconds since 1970-01-01)
|
||||
@ -377,6 +381,8 @@ high or low invalid bids.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract BlindAuction {
|
||||
struct Bid {
|
||||
bytes32 blindedBid;
|
||||
@ -543,6 +549,8 @@ Safe Remote Purchase
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract Purchase {
|
||||
uint public value;
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
address public seller;
|
||||
|
||||
@ -80,6 +86,8 @@ Events are convenience interfaces with the EVM logging facilities.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract SimpleAuction {
|
||||
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 {
|
||||
struct Voter { // Struct
|
||||
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 {
|
||||
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 {
|
||||
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
|
||||
ActionChoices choice;
|
||||
@ -300,6 +302,8 @@ memory-stored reference type does not create a copy.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
uint[] x; // the data location of x is storage
|
||||
|
||||
@ -378,6 +382,8 @@ the ``.length`` member.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
function f(uint len) {
|
||||
uint[] memory a = new uint[](7);
|
||||
@ -397,6 +403,8 @@ assigned to a variable right away.
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
function f() {
|
||||
g([uint(1), 2, 3]);
|
||||
@ -416,6 +424,8 @@ possible:
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract C {
|
||||
function f() {
|
||||
// The next line creates a type error because uint[3] memory
|
||||
@ -452,6 +462,8 @@ Members
|
||||
|
||||
::
|
||||
|
||||
pragma solidity ^0.4.0;
|
||||
|
||||
contract ArrayContract {
|
||||
uint[2**20] m_aLotOfIntegers;
|
||||
// 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 {
|
||||
// Defines a new type with two fields.
|
||||
struct Funder {
|
||||
|
Loading…
Reference in New Issue
Block a user