├── .gitignore ├── .version ├── LICENSE.md ├── README.md ├── behavioral ├── command.sol ├── queue.sol └── state_machine.sol ├── concurrency └── .gitkeep ├── creational └── .gitkeep └── structural └── .gitkeep /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | *.iws 4 | /out/ 5 | .idea_modules/ 6 | atlassian-ide-plugin.xml 7 | com_crashlytics_export_strings.xml 8 | crashlytics.properties 9 | crashlytics-build.properties 10 | fabric.properties 11 | -------------------------------------------------------------------------------- /.version: -------------------------------------------------------------------------------- 1 | 0.1 -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2017 Todd Baur 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | solidity-patterns 2 | ================= 3 | **solidity-patterns** is a repo for software design patterns for the [solidity](http://solidity.readthedocs.io) programming language. 4 | The intent is to create as many applicable examples of common patterns, in hopes of promoting the language and improving it. 5 | It is not the intent these contracts have any really useful applications on the [Ethererum](http://ethereum.org) network, but they should work if deployed. 6 | 7 | 8 | ## Download 9 | * [Grab the latest](https://github.com/toadkicker/solidity-patterns/archive/master.zip) 10 | 11 | ## Usage 12 | ``` 13 | $ git clone https://github.com/toadkicker/solidity-patterns.git 14 | ``` 15 | 16 | ## Contributors 17 | 18 | ### Contributors on GitHub 19 | * [Contributors](https://github.com/toadkicker/solidity-patterns/graphs/contributors) 20 | 21 | ## License 22 | * see [LICENSE](https://github.com/toadkicker/solidity-patterns/blob/master/LICENSE.md) file 23 | 24 | ## Version 25 | See [.version](https://github.com/toadkicker/solidity-patterns/blob/master/.version) 26 | 27 | ## How-to use this code 28 | * see [deploying a solidity contract with geth](https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial) 29 | 30 | ## Contact 31 | #### Developer/Company 32 | * Homepage: toadkicker.com 33 | * e-mail: todd@toadkicker.com 34 | * Twitter: [@toadkicker](https://twitter.com/toadkicker "toadkicker on twitter") 35 | * I'm usually hanging out in the [Embark gitter.im](https://gitter.im/iurimatias/embark-framework) rooms. 36 | 37 | [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=toadkicker&url=https://github.com/toadkicker/solidity-patterns&title=sw-name&language=&tags=github&category=software) 38 | -------------------------------------------------------------------------------- /behavioral/command.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.0; 2 | 3 | contract Command { 4 | struct Light { 5 | bool state; 6 | } 7 | 8 | string[] public history; 9 | 10 | Light[] public bulbs; 11 | 12 | /* The Command interface */ 13 | function Execute(string command) { 14 | history.push(command); 15 | } 16 | 17 | /* The Invoker class */ 18 | function Switch(bytes32 command) { 19 | if(command == "on") { 20 | Execute("on"); 21 | } else { 22 | Execute("off"); 23 | } 24 | } 25 | /* The Receiver class */ 26 | function Lamp() { 27 | Light memory light; 28 | light.state != light.state; 29 | } 30 | 31 | /* The Command for turning on the light - ConcreteCommand #1 */ 32 | function On () { 33 | Execute("on"); 34 | } 35 | /* The Command for turning off the light - ConcreteCommand #2 */ 36 | function Off() { 37 | Execute("off"); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /behavioral/queue.sol: -------------------------------------------------------------------------------- 1 | contract queue 2 | { 3 | uint/*Value*/[2**64/*Capacity*/] q; 4 | uint front = 0; 5 | uint back = 0; 6 | /// TODO: the number of elements stored in the queue. 7 | function length() constant returns (uint) { return back - front; } 8 | /// TODO: the number of elements this queue can hold 9 | /// @invariant capacity() < length() 10 | function capacity() constant returns (uint) { return q.length - 1; } 11 | /// TODO: push a new element to the back of the queue 12 | /// @precondition length() < capacity() - 1 13 | function push(uint /*Value*/ data) 14 | { 15 | if ((back + 1) % q.length == front) 16 | return; // throw; 17 | q[back] = data; 18 | back = (back + 1) % q.length; 19 | } 20 | /// TODO: remove and return the element at the front of the queue 21 | /// @precondition length() > 0 22 | function pop() returns (uint /* Value */ r) 23 | { 24 | if (back == front) 25 | return; // throw; 26 | r = q[front]; 27 | delete q[front]; 28 | front = (front + 1) % q.length; 29 | } 30 | } -------------------------------------------------------------------------------- /behavioral/state_machine.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.0; 2 | 3 | contract StateMachine { 4 | enum Stages { 5 | AcceptingBlindedBids, 6 | RevealBids, 7 | AnotherStage, 8 | AreWeDoneYet, 9 | Finished 10 | } 11 | 12 | // This is the current stage. 13 | Stages public stage = Stages.AcceptingBlindedBids; 14 | 15 | uint public creationTime = now; 16 | 17 | modifier atStage(Stages _stage) { 18 | if (stage != _stage) throw; 19 | _; 20 | } 21 | 22 | function nextStage() internal { 23 | stage = Stages(uint(stage) + 1); 24 | } 25 | 26 | // Perform timed transitions. Be sure to mention 27 | // this modifier first, otherwise the guards 28 | // will not take the new stage into account. 29 | modifier timedTransitions() { 30 | if (stage == Stages.AcceptingBlindedBids && 31 | now >= creationTime + 10 days) 32 | nextStage(); 33 | if (stage == Stages.RevealBids && 34 | now >= creationTime + 12 days) 35 | nextStage(); 36 | // The other stages transition by transaction 37 | _; 38 | } 39 | 40 | // Order of the modifiers matters here! 41 | function bid() 42 | payable 43 | timedTransitions 44 | atStage(Stages.AcceptingBlindedBids) 45 | { 46 | // We will not implement that here 47 | } 48 | 49 | function reveal() 50 | timedTransitions 51 | atStage(Stages.RevealBids) 52 | { 53 | } 54 | 55 | // This modifier goes to the next stage 56 | // after the function is done. 57 | modifier transitionNext() 58 | { 59 | _; 60 | nextStage(); 61 | } 62 | 63 | function g() 64 | timedTransitions 65 | atStage(Stages.AnotherStage) 66 | transitionNext 67 | { 68 | } 69 | 70 | function h() 71 | timedTransitions 72 | atStage(Stages.AreWeDoneYet) 73 | transitionNext 74 | { 75 | } 76 | 77 | function i() 78 | timedTransitions 79 | atStage(Stages.Finished) 80 | { 81 | } 82 | } -------------------------------------------------------------------------------- /concurrency/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toadkicker/solidity-patterns/f3d00f4c482432eb9acb5826822337c588de242a/concurrency/.gitkeep -------------------------------------------------------------------------------- /creational/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toadkicker/solidity-patterns/f3d00f4c482432eb9acb5826822337c588de242a/creational/.gitkeep -------------------------------------------------------------------------------- /structural/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/toadkicker/solidity-patterns/f3d00f4c482432eb9acb5826822337c588de242a/structural/.gitkeep --------------------------------------------------------------------------------