├── README.md ├── density.sol ├── functions └── bitwise.sol ├── interfaces └── token.sol └── modifiers ├── deactivate.sol ├── mortal.sol └── owneronly.sol /README.md: -------------------------------------------------------------------------------- 1 | # Density 2 | 3 | [![Join the chat at https://gitter.im/axic/density](https://badges.gitter.im/axic/density.svg)](https://gitter.im/axic/density?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 4 | 5 | Density is a library or collection of useful modifiers and methods for [Solidity](https://ethereum.github.io/solidity/), one of the high-level languages for the Ethereum virtual machine (EVM). 6 | 7 | ## Usage 8 | 9 | Either import the whole library: 10 | ```js 11 | import "density.sol" 12 | contract Test is owneronly, mortal { 13 | function Test() { 14 | } 15 | 16 | function method(string arg) owneronly { 17 | return strconcat("Hello ", arg); 18 | } 19 | } 20 | ``` 21 | 22 | Or just the parts you need: 23 | ```js 24 | import "interfaces/token.sol" 25 | contract Test { 26 | function Test() { 27 | } 28 | 29 | function method(address _address) { 30 | IToken token = IToken(_address); 31 | if (token.balanceOf(this) > 1 ether) 32 | // We are happy 33 | } 34 | } 35 | ``` 36 | 37 | ## Background 38 | 39 | Inspiration came from different sources: 40 | * https://github.com/ethereum/wiki/wiki/Solidity-standard-library 41 | * https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs 42 | * https://github.com/ethereum/wiki/wiki/Solidity-Features 43 | 44 | 45 | ## Contributing 46 | 47 | Please add yourself as the author using the doxygen command *@author* in the appropriate source file. 48 | 49 | ## License (MIT) 50 | 51 | Permission is hereby granted, free of charge, to any person obtaining a copy of 52 | this software and associated documentation files (the "Software"), to deal in 53 | the Software without restriction, including without limitation the rights to 54 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 55 | the Software, and to permit persons to whom the Software is furnished to do so, 56 | subject to the following conditions: 57 | 58 | The above copyright notice and this permission notice shall be included in all 59 | copies or substantial portions of the Software. 60 | 61 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 62 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 63 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 64 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 65 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 66 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 67 | -------------------------------------------------------------------------------- /density.sol: -------------------------------------------------------------------------------- 1 | import "functions/bitwise.sol"; 2 | 3 | import "interfaces/token.sol"; 4 | 5 | import "modifiers/deactivate.sol"; 6 | import "modifiers/mortal.sol"; 7 | import "modifiers/owneronly.sol"; 8 | -------------------------------------------------------------------------------- /functions/bitwise.sol: -------------------------------------------------------------------------------- 1 | // 2 | // The EVM is missing two very useful bitwise operators, the left and right shifts. 3 | // 4 | // See more at: 5 | // - https://github.com/ethereum/solidity/issues/33 6 | // - https://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift 7 | // 8 | // @author Alex Beregszaszi 9 | // 10 | 11 | contract bitwise { 12 | function shl(uint input, uint8 bits) internal returns (uint ret) { 13 | return input * (2 ** bits); 14 | } 15 | 16 | function shr(uint input, uint8 bits) internal returns (uint ret) { 17 | return input / (2 ** bits); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /interfaces/token.sol: -------------------------------------------------------------------------------- 1 | // 2 | // Based on https://github.com/ethereum/wiki/wiki/Standardized_Contract_APIs 3 | // 4 | 5 | contract IToken { 6 | function transfer(address _to, uint256 _value) returns (bool success); 7 | function transferFrom(address _from, address _to, uint256 _value) returns (bool success); 8 | function balanceOf(address _address) constant returns (uint256 balance); 9 | function approve(address _address) returns (bool _success); 10 | function unapprove(address _address) returns (bool _success); 11 | function isApprovedFor(address _target, address _proxy) constant returns (bool _r); 12 | function approveOnce(address _address, uint256 _maxValue) returns (bool _success); 13 | function isApprovedOnceFor(address _target, address _proxy) returns (uint256 _maxValue); 14 | } 15 | -------------------------------------------------------------------------------- /modifiers/deactivate.sol: -------------------------------------------------------------------------------- 1 | // 2 | // Deactivate your contract, instead of killing it. 3 | // 4 | // As mentioned on https://ethereum.github.io/solidity//docs/faq/basics/. 5 | // 6 | 7 | contract usingDeactivate { 8 | bool _deactivated = false; 9 | 10 | modifier untilDeactivate { 11 | if (_deactivated) 12 | throw; 13 | _ 14 | } 15 | 16 | function deactivate() internal { 17 | _deactivated = true; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /modifiers/mortal.sol: -------------------------------------------------------------------------------- 1 | // 2 | // Basic design pattern suggested by the Solidity tutorials 3 | // 4 | 5 | import "owneronly"; 6 | 7 | contract mortal is owneronly { 8 | function kill() owneronly { 9 | suicide(_owner); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /modifiers/owneronly.sol: -------------------------------------------------------------------------------- 1 | // 2 | // Basic design pattern suggested by the Solidity tutorials 3 | // 4 | 5 | contract owneronly { 6 | address _owner; 7 | 8 | modifier setowner() { 9 | _owner = msg.sender; 10 | _ 11 | } 12 | 13 | modifier owneronly() { 14 | if(msg.sender == _owner) 15 | _ 16 | } 17 | } 18 | --------------------------------------------------------------------------------