├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2019, LiquidApps 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eos-contracts-best-practices 2 | EOSIO contracts references, examples and best-practices 3 | 4 | # Examples 5 | ## Deleting table rows without declaring tables 6 | ```cpp 7 | void clear_secondary_idx256(name TableName, uint8_t index_num, uint64_t scope){ 8 | uint64_t primary = 0; 9 | 10 | auto key = eosio::key256(); 11 | auto it2 = eosio::_multi_index_detail::secondary_index_db_functions::db_idx_lowerbound( 12 | _self.value, 13 | scope, 14 | (static_cast(TableName.value) & 0xFFFFFFFFFFFFFFF0ULL) 15 | | (index_num & 0x000000000000000FULL), 16 | key, 17 | primary); 18 | while (it2 >= 0) { 19 | auto del = it2; 20 | uint64_t dummy; 21 | it2 = db_idx256_next(it2, &dummy); 22 | db_idx256_remove(del); 23 | } 24 | } 25 | void clear_primary(name TableName, uint64_t scope){ 26 | auto it = db_lowerbound_i64(_self.value, scope, TableName.value, 0); 27 | while (it >= 0) { 28 | auto del = it; 29 | uint64_t dummy; 30 | it = db_next_i64(it, &dummy); 31 | db_remove_i64(del); 32 | } 33 | } 34 | 35 | ACTION clearpkgs(){ 36 | require_auth(_self); 37 | clear_primary("package"_n, _self.value); 38 | clear_secondary_idx256("package"_n, 0, _self.value); 39 | clear_secondary_idx256("package"_n, 1, _self.value); 40 | } 41 | ``` 42 | 43 | # References 44 | ## Security best-practices 45 | https://github.com/slowmist/eos-smart-contract-security-best-practices/blob/master/README_EN.md 46 | 47 | https://github.com/NoneAge/EOS_dApp_Security_Incident_Analysis 48 | 49 | ## Performance best-practices: 50 | https://medium.com/@bytemaster/developing-efficient-contracts-8a8e62011c6d 51 | 52 | ## More References: 53 | 54 | * Transfer/Action intercepting contract - https://github.com/GetScatter/RIDL-Contracts/blob/master/scatterfunds/scatterfunds.cpp#L167 55 | * Protecting against intecepting unstaked/refunded tokens by mistake - https://github.com/GetScatter/RIDL-Contracts/commit/1b9480983579de5b5d55837364bb374ef87e5c83 56 | * Random numbers - https://github.com/bada-studio/knights_contract/blob/master/knights/contract/player_control.hpp#L187 - ? 57 | * Events - https://github.com/bancorprotocol/contracts_eos/blob/master/contracts/eos/BancorConverter/BancorConverter.cpp#L98 58 | * Deferred transaction - https://github.com/eosdac/dacservice/blob/master/dacservice.cpp#L53 59 | * Transfer memo as parameter - https://github.com/eosdac/dacservice/blob/master/dacservice.cpp#L25 60 | * Migrate tables - https://github.com/nsjames/EOSIO-Migration-Tutorial/blob/master/migrate.cpp 61 | * Customized token contract example and best practices. - *TODO* 62 | * Permissions/Authorizations examples - *TODO* 63 | * Protecting from RAM theft - *TODO* 64 | * Contract examples using the security best practices from above - *TODO* 65 | * Submarine Operations (commit/reveal) - *TODO* 66 | * Atomically erasing an entire table (by replacing pointers) - *TODO* 67 | * Using asserts efficiently - *TODO* 68 | * Always-false asserts - *TODO* 69 | * Context-free actions - *TODO* 70 | * Ricardian contracts - *TODO* 71 | * Construct bounds and query secondary index from eosjs - *TODO* - https://github.com/greymass/eos-voter/blob/c003a04a6bd8937c416e61ad005824fe83c9f517/app/shared/actions/governance/proposals.js#L94 72 | 73 | # Reviewed by: 74 | --------------------------------------------------------------------------------