├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 lexDAO 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solidity Style Guide 2 | solidity style tips for legal engineering drafting 3 | 4 | ## Events 5 | 6 | Events should be ordered in logical flow or chronology, as they often can paint a picture of how deals progress: 7 | 8 | event RegisterLocker(address indexed client, address[] indexed provider, address indexed resolver, address token, uint256[] amount, uint256 cap, uint256 index, uint256 termination, bytes32 details); 9 | event DepositLocker(uint256 indexed index, uint256 indexed sum); 10 | event Release(uint256 indexed index, uint256[] indexed milestone); 11 | event Withdraw(uint256 indexed index, uint256 indexed remainder); 12 | event Lock(address indexed sender, uint256 indexed index, bytes32 indexed details); 13 | event Resolve(address indexed resolver, uint256 indexed clientAward, uint256 indexed providerAward, uint256 index, uint256 resolutionFee, bytes32 details); 14 | 15 | Grouping the objects within an event in a logical order also makes a big difference and can reduce gas costs. Deciding what objects get emitted as part of an event is also an important consideration for providing the legal engineer with the right information and also constructing a full-stack deal application where the front-end can easily grab event objects either through listeners or a subgraph. 16 | 17 | When emiting an event from a function, it's preferable if the event goes at the end of the function, or right before the return, if applicable. Sometimes the event will need to go higher up in the function to avoid stack to deep issues. 18 | 19 | function submitWhitelistProposal(address tokenToWhitelist, bytes32 details) external returns (uint256 proposalId) { 20 | require(tokenToWhitelist != address(0), "need token"); 21 | require(!tokenWhitelist[tokenToWhitelist], "already whitelisted"); 22 | require(approvedTokens.length < MAX_TOKEN_WHITELIST_COUNT, "whitelist maxed"); 23 | 24 | uint8[7] memory flags; // [sponsored, processed, didPass, cancelled, whitelist, guildkick, action] 25 | flags[4] = 1; // whitelist 26 | 27 | _submitProposal(address(0), 0, 0, 0, tokenToWhitelist, 0, address(0), details, flags); 28 | 29 | return proposalCount - 1; 30 | } 31 | 32 | ## Function Variables 33 | 34 | Function variables should be stacked together per type, to prep gas-saving habit of "[variable packing](https://mudit.blog/solidity-gas-optimization-tips/)." Logical, and if possible, alphabetic order is preferred for consistency. Header comment to explain purpose and expected outcome of function is also recommended: 35 | 36 | function registerLocker( // register locker for token deposit and client deal confirmation 37 | address client, 38 | address[] calldata provider, 39 | address resolver, 40 | address token, 41 | uint256[] calldata amount, 42 | uint256 cap, 43 | uint256 milestones, 44 | uint256 termination, 45 | bytes32 details) 46 | ## Structs 47 | 48 | The objects included in structs should also be grouped logically and by type in order to save on gas costs. We recommend starting with addresses, then uints, then bytes and bools. 49 | 50 | struct Action { 51 | address proposer; 52 | address to; 53 | uint256 value; 54 | bytes data; 55 | } 56 | --------------------------------------------------------------------------------