mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Changed whitespace formatting
This commit is contained in:
parent
ff26ea6c08
commit
7c22a387f3
@ -179,6 +179,7 @@ function finishes.
|
|||||||
AreWeDoneYet,
|
AreWeDoneYet,
|
||||||
Finished
|
Finished
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is the current stage.
|
// This is the current stage.
|
||||||
Stages public stage = Stages.AcceptingBlindedBids;
|
Stages public stage = Stages.AcceptingBlindedBids;
|
||||||
|
|
||||||
@ -188,9 +189,11 @@ function finishes.
|
|||||||
if (stage != _stage) throw;
|
if (stage != _stage) throw;
|
||||||
_
|
_
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextStage() internal {
|
function nextStage() internal {
|
||||||
stage = Stages(uint(stage) + 1);
|
stage = Stages(uint(stage) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform timed transitions. Be sure to mention
|
// Perform timed transitions. Be sure to mention
|
||||||
// this modifier first, otherwise the guards
|
// this modifier first, otherwise the guards
|
||||||
// will not take the new stage into account.
|
// will not take the new stage into account.
|
||||||
@ -211,6 +214,7 @@ function finishes.
|
|||||||
{
|
{
|
||||||
// We will not implement that here
|
// We will not implement that here
|
||||||
}
|
}
|
||||||
|
|
||||||
function reveal()
|
function reveal()
|
||||||
timedTransitions
|
timedTransitions
|
||||||
atStage(Stages.RevealBids)
|
atStage(Stages.RevealBids)
|
||||||
@ -227,6 +231,7 @@ function finishes.
|
|||||||
_
|
_
|
||||||
nextStage();
|
nextStage();
|
||||||
}
|
}
|
||||||
|
|
||||||
function g()
|
function g()
|
||||||
timedTransitions
|
timedTransitions
|
||||||
atStage(Stages.AnotherStage)
|
atStage(Stages.AnotherStage)
|
||||||
@ -235,12 +240,14 @@ function finishes.
|
|||||||
// If you want to use `return` here,
|
// If you want to use `return` here,
|
||||||
// you have to call `nextStage()` manually.
|
// you have to call `nextStage()` manually.
|
||||||
}
|
}
|
||||||
|
|
||||||
function h()
|
function h()
|
||||||
timedTransitions
|
timedTransitions
|
||||||
atStage(Stages.AreWeDoneYet)
|
atStage(Stages.AreWeDoneYet)
|
||||||
transitionNext
|
transitionNext
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
function i()
|
function i()
|
||||||
timedTransitions
|
timedTransitions
|
||||||
atStage(Stages.Finished)
|
atStage(Stages.Finished)
|
||||||
|
@ -44,7 +44,8 @@ API, this is done as follows::
|
|||||||
var MyContract = web3.eth.contract(abiArray);
|
var MyContract = web3.eth.contract(abiArray);
|
||||||
// deploy new contract
|
// deploy new contract
|
||||||
var contractInstance = MyContract.new(
|
var contractInstance = MyContract.new(
|
||||||
10, 11,
|
10,
|
||||||
|
11,
|
||||||
{from: myAccount, gas: 1000000}
|
{from: myAccount, gas: 1000000}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -55,6 +55,8 @@ of other contracts, the amount of Wei sent with the call and the gas can be spec
|
|||||||
contract InfoFeed {
|
contract InfoFeed {
|
||||||
function info() returns (uint ret) { return 42; }
|
function info() returns (uint ret) { return 42; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract Consumer {
|
contract Consumer {
|
||||||
InfoFeed feed;
|
InfoFeed feed;
|
||||||
function setFeed(address addr) { feed = InfoFeed(addr); }
|
function setFeed(address addr) { feed = InfoFeed(addr); }
|
||||||
@ -77,10 +79,12 @@ of unused parameters (especially return parameters) can be omitted.
|
|||||||
|
|
||||||
contract c {
|
contract c {
|
||||||
function f(uint key, uint value) { ... }
|
function f(uint key, uint value) { ... }
|
||||||
|
|
||||||
function g() {
|
function g() {
|
||||||
// named arguments
|
// named arguments
|
||||||
f({value: 2, key: 3});
|
f({value: 2, key: 3});
|
||||||
}
|
}
|
||||||
|
|
||||||
// omitted parameters
|
// omitted parameters
|
||||||
function func(uint k, uint) returns(uint) {
|
function func(uint k, uint) returns(uint) {
|
||||||
return k;
|
return k;
|
||||||
@ -212,7 +216,7 @@ In the following example, we show how `throw` can be used to easily revert an Et
|
|||||||
|
|
||||||
contract Sharer {
|
contract Sharer {
|
||||||
function sendHalf(address addr) returns (uint balance) {
|
function sendHalf(address addr) returns (uint balance) {
|
||||||
if (!addr.send(msg.value/2))
|
if (!addr.send(msg.value / 2))
|
||||||
throw; // also reverts the transfer to Sharer
|
throw; // also reverts the transfer to Sharer
|
||||||
return this.balance;
|
return this.balance;
|
||||||
}
|
}
|
||||||
@ -290,6 +294,7 @@ you really know what you are doing.
|
|||||||
for (uint i = 0; i < _data.length; ++i)
|
for (uint i = 0; i < _data.length; ++i)
|
||||||
o_sum += _data[i];
|
o_sum += _data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// We know that we only access the array in bounds, so we can avoid the check.
|
// We know that we only access the array in bounds, so we can avoid the check.
|
||||||
// 0x20 needs to be added to an array because the first slot contains the
|
// 0x20 needs to be added to an array because the first slot contains the
|
||||||
// array length.
|
// array length.
|
||||||
|
@ -692,11 +692,11 @@ What happens to a struct's mapping when copying over a struct?
|
|||||||
|
|
||||||
This is a very interesting question. Suppose that we have a contract field set up like such::
|
This is a very interesting question. Suppose that we have a contract field set up like such::
|
||||||
|
|
||||||
struct user{
|
struct user {
|
||||||
mapping(string => address) usedContracts;
|
mapping(string => address) usedContracts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function somefunction{
|
function somefunction {
|
||||||
user user1;
|
user user1;
|
||||||
user1.usedContracts["Hello"] = "World";
|
user1.usedContracts["Hello"] = "World";
|
||||||
user user2 = user1;
|
user user2 = user1;
|
||||||
@ -715,6 +715,8 @@ You will need to make sure that you have both contracts aware of each other's pr
|
|||||||
In this example::
|
In this example::
|
||||||
|
|
||||||
contract B {}
|
contract B {}
|
||||||
|
|
||||||
|
|
||||||
contract A {
|
contract A {
|
||||||
address child;
|
address child;
|
||||||
|
|
||||||
|
@ -51,9 +51,11 @@ There are some types in Solidity's type system that have no counterpart in the s
|
|||||||
if (useB) f = b;
|
if (useB) f = b;
|
||||||
return f(x);
|
return f(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
function a(uint x) returns (uint z) {
|
function a(uint x) returns (uint z) {
|
||||||
return x * x;
|
return x * x;
|
||||||
}
|
}
|
||||||
|
|
||||||
function b(uint x) returns (uint z) {
|
function b(uint x) returns (uint z) {
|
||||||
return 2 * x;
|
return 2 * x;
|
||||||
}
|
}
|
||||||
|
@ -292,7 +292,7 @@ activate themselves.
|
|||||||
}
|
}
|
||||||
|
|
||||||
Blind Auction
|
Blind Auction
|
||||||
================
|
=============
|
||||||
|
|
||||||
The previous open auction is extended to a blind auction
|
The previous open auction is extended to a blind auction
|
||||||
in the following. The advantage of a blind auction is
|
in the following. The advantage of a blind auction is
|
||||||
|
@ -171,21 +171,21 @@ to and from all integer types but implicit conversion is not allowed.
|
|||||||
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
|
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
|
||||||
ActionChoices choice;
|
ActionChoices choice;
|
||||||
ActionChoices constant defaultChoice = ActionChoices.GoStraight;
|
ActionChoices constant defaultChoice = ActionChoices.GoStraight;
|
||||||
function setGoStraight()
|
|
||||||
{
|
function setGoStraight() {
|
||||||
choice = ActionChoices.GoStraight;
|
choice = ActionChoices.GoStraight;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since enum types are not part of the ABI, the signature of "getChoice"
|
// Since enum types are not part of the ABI, the signature of "getChoice"
|
||||||
// will automatically be changed to "getChoice() returns (uint8)"
|
// will automatically be changed to "getChoice() returns (uint8)"
|
||||||
// for all matters external to Solidity. The integer type used is just
|
// for all matters external to Solidity. The integer type used is just
|
||||||
// large enough to hold all enum values, i.e. if you have more values,
|
// large enough to hold all enum values, i.e. if you have more values,
|
||||||
// `uint16` will be used and so on.
|
// `uint16` will be used and so on.
|
||||||
function getChoice() returns (ActionChoices)
|
function getChoice() returns (ActionChoices) {
|
||||||
{
|
|
||||||
return choice;
|
return choice;
|
||||||
}
|
}
|
||||||
function getDefaultChoice() returns (uint)
|
|
||||||
{
|
function getDefaultChoice() returns (uint) {
|
||||||
return uint(defaultChoice);
|
return uint(defaultChoice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,8 +226,9 @@ memory-stored reference type does not create a copy.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
contract c {
|
contract C {
|
||||||
uint[] x; // the data location of x is storage
|
uint[] x; // the data location of x is storage
|
||||||
|
|
||||||
// the data location of memoryArray is memory
|
// the data location of memoryArray is memory
|
||||||
function f(uint[] memoryArray) {
|
function f(uint[] memoryArray) {
|
||||||
x = memoryArray; // works, copies the whole array to storage
|
x = memoryArray; // works, copies the whole array to storage
|
||||||
@ -244,6 +245,7 @@ memory-stored reference type does not create a copy.
|
|||||||
g(x); // calls g, handing over a reference to x
|
g(x); // calls g, handing over a reference to x
|
||||||
h(x); // calls h and creates an independent, temporary copy in memory
|
h(x); // calls h and creates an independent, temporary copy in memory
|
||||||
}
|
}
|
||||||
|
|
||||||
function g(uint[] storage storageArray) internal {}
|
function g(uint[] storage storageArray) internal {}
|
||||||
function h(uint[] memoryArray) {}
|
function h(uint[] memoryArray) {}
|
||||||
}
|
}
|
||||||
@ -343,19 +345,23 @@ Members
|
|||||||
// 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.
|
||||||
bool[2][] m_pairsOfFlags;
|
bool[2][] m_pairsOfFlags;
|
||||||
// newPairs is stored in memory - the default for function arguments
|
// newPairs is stored in memory - the default for function arguments
|
||||||
|
|
||||||
function setAllFlagPairs(bool[2][] newPairs) {
|
function setAllFlagPairs(bool[2][] newPairs) {
|
||||||
// assignment to a storage array replaces the complete array
|
// assignment to a storage array replaces the complete array
|
||||||
m_pairsOfFlags = newPairs;
|
m_pairsOfFlags = newPairs;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setFlagPair(uint index, bool flagA, bool flagB) {
|
function setFlagPair(uint index, bool flagA, bool flagB) {
|
||||||
// access to a non-existing index will throw an exception
|
// access to a non-existing index will throw an exception
|
||||||
m_pairsOfFlags[index][0] = flagA;
|
m_pairsOfFlags[index][0] = flagA;
|
||||||
m_pairsOfFlags[index][1] = flagB;
|
m_pairsOfFlags[index][1] = flagB;
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeFlagArraySize(uint newSize) {
|
function changeFlagArraySize(uint newSize) {
|
||||||
// if the new size is smaller, removed array elements will be cleared
|
// if the new size is smaller, removed array elements will be cleared
|
||||||
m_pairsOfFlags.length = newSize;
|
m_pairsOfFlags.length = newSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
function clear() {
|
function clear() {
|
||||||
// these clear the arrays completely
|
// these clear the arrays completely
|
||||||
delete m_pairsOfFlags;
|
delete m_pairsOfFlags;
|
||||||
@ -363,7 +369,9 @@ Members
|
|||||||
// identical effect here
|
// identical effect here
|
||||||
m_pairsOfFlags.length = 0;
|
m_pairsOfFlags.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes m_byteData;
|
bytes m_byteData;
|
||||||
|
|
||||||
function byteArrays(bytes data) {
|
function byteArrays(bytes data) {
|
||||||
// byte arrays ("bytes") are different as they are stored without padding,
|
// byte arrays ("bytes") are different as they are stored without padding,
|
||||||
// but can be treated identical to "uint8[]"
|
// but can be treated identical to "uint8[]"
|
||||||
@ -372,9 +380,11 @@ Members
|
|||||||
m_byteData[3] = 8;
|
m_byteData[3] = 8;
|
||||||
delete m_byteData[2];
|
delete m_byteData[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
function addFlag(bool[2] flag) returns (uint) {
|
function addFlag(bool[2] flag) returns (uint) {
|
||||||
return m_pairsOfFlags.push(flag);
|
return m_pairsOfFlags.push(flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMemoryArray(uint size) returns (bytes) {
|
function createMemoryArray(uint size) returns (bytes) {
|
||||||
// Dynamic memory arrays are created using `new`:
|
// Dynamic memory arrays are created using `new`:
|
||||||
uint[2][] memory arrayOfPairs = new uint[2][](size);
|
uint[2][] memory arrayOfPairs = new uint[2][](size);
|
||||||
@ -405,6 +415,7 @@ shown in the following example:
|
|||||||
address addr;
|
address addr;
|
||||||
uint amount;
|
uint amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Campaign {
|
struct Campaign {
|
||||||
address beneficiary;
|
address beneficiary;
|
||||||
uint fundingGoal;
|
uint fundingGoal;
|
||||||
@ -412,13 +423,16 @@ shown in the following example:
|
|||||||
uint amount;
|
uint amount;
|
||||||
mapping (uint => Funder) funders;
|
mapping (uint => Funder) funders;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint numCampaigns;
|
uint numCampaigns;
|
||||||
mapping (uint => Campaign) campaigns;
|
mapping (uint => Campaign) campaigns;
|
||||||
|
|
||||||
function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
|
function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
|
||||||
campaignID = numCampaigns++; // campaignID is return variable
|
campaignID = numCampaigns++; // campaignID is return variable
|
||||||
// Creates new struct and saves in storage. We leave out the mapping type.
|
// Creates new struct and saves in storage. We leave out the mapping type.
|
||||||
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
|
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function contribute(uint campaignID) {
|
function contribute(uint campaignID) {
|
||||||
Campaign c = campaigns[campaignID];
|
Campaign c = campaigns[campaignID];
|
||||||
// Creates a new temporary memory struct, initialised with the given values
|
// Creates a new temporary memory struct, initialised with the given values
|
||||||
@ -427,6 +441,7 @@ shown in the following example:
|
|||||||
c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
|
c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
|
||||||
c.amount += msg.value;
|
c.amount += msg.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkGoalReached(uint campaignID) returns (bool reached) {
|
function checkGoalReached(uint campaignID) returns (bool reached) {
|
||||||
Campaign c = campaigns[campaignID];
|
Campaign c = campaigns[campaignID];
|
||||||
if (c.amount < c.fundingGoal)
|
if (c.amount < c.fundingGoal)
|
||||||
@ -497,6 +512,7 @@ It is important to note that `delete a` really behaves like an assignment to `a`
|
|||||||
contract DeleteExample {
|
contract DeleteExample {
|
||||||
uint data;
|
uint data;
|
||||||
uint[] dataArray;
|
uint[] dataArray;
|
||||||
|
|
||||||
function f() {
|
function f() {
|
||||||
uint x = data;
|
uint x = data;
|
||||||
delete x; // sets x to 0, does not affect data
|
delete x; // sets x to 0, does not affect data
|
||||||
|
Loading…
Reference in New Issue
Block a user