├── .gitattributes ├── .gitignore ├── LICENCE ├── README.md ├── SmartSavingContract.sln └── SmartSavingContract ├── Neo.ConvertTask.dll ├── Properties └── AssemblyInfo.cs ├── SmartSavingContract.cs ├── SmartSavingContract.csproj ├── build.tasks └── packages.config /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc 262 | 263 | *.avm -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 BlockSaver 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 | # BlockSaver SmartSavingContract 2 | 3 | ## Introduction 4 | There is a massive trend on holding of cryptocurrencies (popularly called “hodling”). 5 | NEO is not an exception to that trend. Our application aims to reward faithful investors 6 | and enthusiasts and punish those eager on quick profit. Or maybe just those technology 7 | believers that tend to give away part of their paycheck every month to buy some NEO. 8 | 9 | By opening savings at BlockSaver you can define for how long you wish to lock your assets. 10 | We accept both NEO and GAS assets. Based on how long have you locked your assets away, 11 | we will define your interest rate. On expiration of that duration, 12 | we will transfer assets from smart contract to your designated wallet. 13 | Withdrawing beforehand will get you your assets but we will keep certain 14 | amount based on our provision(penalty). 15 | This app will also enable you to set up periodically billings on your 16 | credit card that will be traded on best exchange rates and added 17 | to your existing savings funds. If you notice on some occasion some 18 | favorable NEO or GAS assets you can ask for withdrawal and trigger 19 | payment to existing savings. 20 | 21 | ## Development 22 | 23 | ### Requirements 24 | * Windows 7 or Windows 10 PC 25 | * Visual Studio 2017 26 | * Following Visual Studio extensions: 27 | * .NET Core cross-platform development 28 | * Following Visual Studio plugins: 29 | * NeoContractPlugin 30 | * [neon - c# compiler](https://github.com/neo-project/neo-compiler) - [installation instructions](http://docs.neo.org/en-us/sc/getting-started-csharp.html) 31 | 32 | ### Instructions 33 | * Open `SmartSavingContract.sln` in Visual Studio 34 | * Build SmartSavingContract 35 | * `bin/Debug` should contain `SmartSavingContract.avm file` 36 | 37 | ### Deployment 38 | * Install [neo-gui-developer](https://github.com/CityOfZion/neo-gui-developer) 39 | * Run neo-gui-developer 40 | * Open wallet 41 | * Advanced->Deploy Contract 42 | * input params: 0710 43 | * output params: 05 44 | * check "Need Storage" 45 | * Load your avm file 46 | * Copy contract hash 47 | * Test 48 | * Invoke 49 | 50 | ## Testing 51 | 52 | Neo-python commands for contract invoking: 53 | 54 | //create 55 | `testInvoke 20dd61645c511e8e1d5c12a722a65db7420ed798 "637265617465536176696e6773" [�6c6f6f70�,1510488386]` 56 | 57 | //all savings 58 | `testInvoke 20dd61645c511e8e1d5c12a722a65db7420ed798 "676574416c6c536176696e6773" []` 59 | 60 | //savings details 61 | `testInvoke 20dd61645c511e8e1d5c12a722a65db7420ed798 "676574536176696e677342794e616d65" [�6c6f6f70�]` 62 | 63 | //transfer 64 | `testInvoke 20dd61645c511e8e1d5c12a722a65db7420ed798 "7472616e73666572" 74657374c 1510488386 --attach-neo=1 --attach-gas=1` 65 | 66 | 67 | ### Private Network 68 | ```json 69 | { 70 | "ProtocolConfiguration": { 71 | "Magic": 56753, 72 | "AddressVersion": 23, 73 | "StandbyValidators": [ 74 | "02b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc2", 75 | "02103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e", 76 | "03d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee699", 77 | "02a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd62" 78 | 79 | ], 80 | "SeedList": [ 81 | "188.226.138.245:20333", 82 | "188.226.138.245:20334", 83 | "188.226.138.245:20335", 84 | "188.226.138.245:20336" 85 | ], 86 | "SystemFee": { 87 | "EnrollmentTransaction": 1000, 88 | "IssueTransaction": 500, 89 | "PublishTransaction": 500, 90 | "RegisterTransaction": 10000 91 | } 92 | 93 | } 94 | } 95 | ``` 96 | Versions: 97 | 98 | | Version | Contract Hash | 99 | |---------|---------------| 100 | | v0.0.1 | 20dd61645c511e8e1d5c12a722a65db7420ed798 | 101 | | v0.0.2 | 3d428cf6137bb0e09b82125a27dfc61a29723394 | 102 | 103 | ### Test Network 104 | Versions: 105 | 106 | | Version | Contract Hash | 107 | |---------|---------------| 108 | | v1.0.0 | 3d428cf6137bb0e09b82125a27dfc61a29723394 | 109 | -------------------------------------------------------------------------------- /SmartSavingContract.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27004.2009 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartSavingContract", "SmartSavingContract\SmartSavingContract.csproj", "{A024E040-51F7-482C-B79C-89FC4B368B2C}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8870485B-5C0E-4472-A11D-4985215F6FA4}" 9 | ProjectSection(SolutionItems) = preProject 10 | README.md = README.md 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|Any CPU = Debug|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {A024E040-51F7-482C-B79C-89FC4B368B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {A024E040-51F7-482C-B79C-89FC4B368B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {A024E040-51F7-482C-B79C-89FC4B368B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {A024E040-51F7-482C-B79C-89FC4B368B2C}.Release|Any CPU.Build.0 = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | GlobalSection(ExtensibilityGlobals) = postSolution 28 | SolutionGuid = {26BD7F77-2FAA-424E-973E-9D8CC557B7C2} 29 | EndGlobalSection 30 | EndGlobal 31 | -------------------------------------------------------------------------------- /SmartSavingContract/Neo.ConvertTask.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BlockSaver/smart-saving-contract/f39d3527d953f6b531b2eced83e00bd4a9222488/SmartSavingContract/Neo.ConvertTask.dll -------------------------------------------------------------------------------- /SmartSavingContract/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SmartSavingContract")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("BlockSaver")] 12 | [assembly: AssemblyProduct("SmartSavingContract")] 13 | [assembly: AssemblyCopyright("Copyright © 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("a024e040-51f7-482c-b79c-89fc4b368b2c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SmartSavingContract/SmartSavingContract.cs: -------------------------------------------------------------------------------- 1 | using Neo.SmartContract.Framework; 2 | using Neo.SmartContract.Framework.Services.Neo; 3 | using Neo.SmartContract.Framework.Services.System; 4 | using System; 5 | using System.Numerics; 6 | using System.Text; 7 | 8 | namespace SmartSavingContract 9 | { 10 | public class SmartSavingContract : SmartContract 11 | { 12 | 13 | //neo asset id 14 | private static readonly byte[] NEO = { 155, 124, 255, 218, 166, 116, 190, 174, 15, 147, 14, 190, 96, 133, 175, 144, 147, 229, 254, 86, 179, 74, 92, 34, 12, 205, 207, 110, 252, 51, 111, 197 }; 15 | 16 | //neo gas id 17 | private static readonly byte[] NEO_GAS = { 231, 45, 40, 105, 121, 238, 108, 177, 183, 230, 93, 253, 223, 178, 227, 132, 16, 11, 141, 20, 142, 119, 88, 222, 66, 228, 22, 139, 113, 121, 44, 96 }; 18 | 19 | //contract owner public key 20 | private static readonly byte[] CONTRACT_OWNER = { 108, 134, 156, 63, 118, 253, 80, 65, 30, 70, 152, 182, 20, 222, 126, 201, 17, 14, 146, 101, 170, 211, 90, 227, 16, 54, 140, 24, 115, 6, 78, 148, 2 }; 21 | 22 | //savings duration id 23 | private static readonly byte[] DURATION = { 0x03 }; 24 | 25 | public static object Main(string operation, params object[] args) 26 | { 27 | Runtime.Log("operation: "+operation); 28 | switch (operation) 29 | { 30 | //This operation serves for creating new savings with zero balances. 31 | //It should receive name(string) as arg0 and duration(integer) as arg1. 32 | //Duration should be unix timestamp after which can savings be closed freely 33 | //It will return true is savings is created successfully, false otherwise 34 | case "createSavings": 35 | { 36 | Runtime.Log("createSavings command!"); 37 | return CreateSavings((byte[]) args[0], (string) args[1],((byte[])args[2]).AsBigInteger()); 38 | } 39 | //Returns json array of savings ids of all savings binded to caller address 40 | case "getAllSavings": 41 | { 42 | Runtime.Log("getAllSavings command!"); 43 | return GetAllSavings((byte[])args[0]); 44 | } 45 | //Returns json object with savings details. 46 | //Arg0 should be savingsId 47 | case "getSavingsByName": 48 | { 49 | Runtime.Log("getSavingsByName command!"); 50 | return GetSavingsByName((byte[])args[0], (string)args[1]); 51 | } 52 | //This method requires attached neo or gas which will be recorded os savings id 53 | //Both server(for reocurring payments) and client(for single non-planned payments) 54 | //will use this method 55 | //arg0 should be savingsId 56 | case "transfer": 57 | { 58 | Runtime.Log("transfer command!"); 59 | return Transfer((byte[])args[0], (string) args[1]); 60 | } 61 | //This method only deletes savings data! 62 | //If current timestamp is greater than savings duration all assets will be transfered to 63 | //clients address. If savings is closed prematurely, some percentage will be kept on owner wallet. 64 | //Only server with contract owner address can close savings 65 | case "closeSavings": 66 | { 67 | Runtime.Log("closeSavings command!"); 68 | return CloseSavings((byte[])args[0], (string)args[1], (byte[]) args[2]); 69 | } 70 | } 71 | return "unknown command"; 72 | } 73 | 74 | /** 75 | * Operations 76 | * */ 77 | 78 | public static bool CreateSavings(byte[] owner, string name, BigInteger duration) 79 | { 80 | Runtime.Log("Creating new savings..."); 81 | BigInteger zero = 0; 82 | Runtime.Log("sender obtained"); 83 | if (GetSavingsByName(owner, name) != null) { 84 | Runtime.Log("Savings with that name and awner already exists!"); 85 | return false; 86 | } 87 | Runtime.Log("Savings with that name does not exists"); 88 | string savings = GetAllSavings(owner); 89 | if (savings != null) 90 | { 91 | Runtime.Log("all savings:"); 92 | Runtime.Log(savings); 93 | savings += ","; 94 | savings += name; 95 | } 96 | else { 97 | savings = name; 98 | } 99 | Storage.Put(Storage.CurrentContext, owner, savings); 100 | StoreNeoBalance(owner, name, zero); 101 | StoreNeoGasBalance(owner, name, zero); 102 | StoreDuration(owner, name, duration); 103 | return true; 104 | } 105 | 106 | public static string GetAllSavings(byte[] owner) 107 | { 108 | byte[] content = Storage.Get(Storage.CurrentContext, owner); 109 | if (content != null) 110 | { 111 | string savings = Helper.AsString(content); 112 | Runtime.Log("Savings: " + savings); 113 | return savings; 114 | } 115 | return null; 116 | } 117 | 118 | public static string GetSavingsByName(byte[] owner, string name) 119 | { 120 | Runtime.Log("getting savings in json"); 121 | BigInteger duration = GetDuration(owner, name); 122 | if (duration == 0) return null; 123 | string savings = "{"; 124 | savings += "\"name\":\""; 125 | savings += name; 126 | savings+= "\","; 127 | savings += "\"neo\":"; 128 | Runtime.Log("fetching neo balance"); 129 | BigInteger neoBalance = GetNeoBalance(owner, name); 130 | Runtime.Notify(neoBalance + 1); 131 | savings += neoBalance; 132 | savings += ","; 133 | savings += "\"gas\":"; 134 | Runtime.Log("fetching gas balance"); 135 | BigInteger neoGasBalance = GetNeoGasBalance(owner, name); 136 | Runtime.Notify(neoGasBalance + 1); 137 | Runtime.Log("gas: "+Helper.AsString(neoGasBalance.ToByteArray())); 138 | savings += neoGasBalance; 139 | savings += ","; 140 | savings += "\"duration\":"; 141 | Runtime.Log("fetching duration"); 142 | savings += duration; 143 | savings += ","; 144 | savings += "\"name\":\""; 145 | savings += Helper.AsString(owner); 146 | savings += "\"}"; 147 | return savings; 148 | } 149 | 150 | public static bool Transfer(byte[] owner, string name) 151 | { 152 | BigInteger neoBalance = GetNeoBalance(owner, name); 153 | BigInteger neoContribution = GetNeoContributionValue(); 154 | neoBalance = neoBalance + neoContribution; 155 | StoreNeoBalance(owner, name, neoBalance); 156 | BigInteger neoGasBalance = GetNeoBalance(owner, name); 157 | BigInteger neoGasContribution = GetNeoContributionValue(); 158 | neoGasBalance = neoGasBalance + neoGasContribution; 159 | StoreNeoGasBalance(owner, name, neoGasBalance); 160 | return true; 161 | } 162 | 163 | public static bool CloseSavings(byte[] savingsOwner, string name, byte[] signature) 164 | { 165 | //this is commented out because we aren't really sure how to generate signature in neo-gui 166 | //we also realize that currently this is security flaw 167 | //if (!VerifySignature(signature, CONTRACT_OWNER)) return false; 168 | Runtime.Log("Closing savings ->" + name + "..."); 169 | string savings = GetAllSavings(savingsOwner); 170 | Runtime.Log("Current savings" + savings); 171 | byte[] newSavings = RemoveFromArray(savings.AsByteArray(), name); 172 | Runtime.Log("Removed from savings list"); 173 | Storage.Put(Storage.CurrentContext, savingsOwner, newSavings); 174 | Runtime.Log("Stored new savings list"); 175 | StoreNeoBalance(savingsOwner, name, 0); 176 | Runtime.Log("Stored new neo balance"); 177 | StoreNeoGasBalance(savingsOwner, name, 0); 178 | Runtime.Log("Stored new neo gas balance"); 179 | StoreDuration(savingsOwner, name, 0); 180 | Runtime.Log("Stored new duration"); 181 | return true; 182 | } 183 | 184 | /** 185 | * Utility methods 186 | * */ 187 | private static void StoreNeoBalance(byte[] sender, string name, BigInteger value) 188 | { 189 | StoreData(sender, name, NEO, value.AsByteArray()); 190 | } 191 | 192 | private static void StoreNeoGasBalance(byte[] sender, string name, BigInteger value) 193 | { 194 | StoreData(sender, name, NEO_GAS, value.AsByteArray()); 195 | } 196 | 197 | private static void StoreDuration(byte[] sender, string name, BigInteger value) 198 | { 199 | StoreData(sender, name, DURATION, value.AsByteArray()); 200 | } 201 | 202 | private static BigInteger GetNeoBalance(byte[] sender, string name) 203 | { 204 | byte[] data = GetData(sender, name, NEO); 205 | if (data == null) { 206 | return 0; 207 | } 208 | return data.AsBigInteger(); 209 | } 210 | 211 | private static BigInteger GetNeoGasBalance(byte[] sender, string name) 212 | { 213 | byte[] data = GetData(sender, name, NEO_GAS); 214 | if (data == null) 215 | { 216 | return 0; 217 | } 218 | return data.AsBigInteger(); 219 | } 220 | 221 | private static BigInteger GetDuration(byte[] sender, string name) 222 | { 223 | byte[] data = GetData(sender, name, DURATION); 224 | if (data == null) 225 | { 226 | return 0; 227 | } 228 | return data.AsBigInteger(); 229 | } 230 | 231 | private static void StoreData(byte[] sender, string name, byte[] assetId, byte[] data) 232 | { 233 | Storage.Put( 234 | Storage.CurrentContext, 235 | Helper.Concat( 236 | Helper.Concat(sender, Helper.AsByteArray(name)), 237 | assetId 238 | ), 239 | data 240 | ); 241 | } 242 | 243 | private static byte[] GetData(byte[] sender, string name, byte[] assetId) 244 | { 245 | return Storage.Get( 246 | Storage.CurrentContext, 247 | Helper.Concat( 248 | Helper.Concat(sender, Helper.AsByteArray(name)), 249 | assetId 250 | ) 251 | ); 252 | } 253 | 254 | private static ulong GetNeoContributionValue() 255 | { 256 | return GetAssetContribution(NEO); 257 | } 258 | 259 | private static ulong GetAssetContribution(byte[] assetId) 260 | { 261 | Transaction tx = (Transaction)ExecutionEngine.ScriptContainer; 262 | TransactionOutput[] outputs = tx.GetOutputs(); 263 | ulong value = 0; 264 | foreach (TransactionOutput output in outputs) 265 | { 266 | if (output.ScriptHash == GetReceiver() && output.AssetId == assetId) 267 | { 268 | value += (ulong)output.Value; 269 | } 270 | else 271 | { 272 | Runtime.Log("Asset id: "); 273 | Runtime.Log(Helper.AsString(output.AssetId)); 274 | } 275 | } 276 | return value; 277 | } 278 | 279 | // get smart contract script hash 280 | private static byte[] GetReceiver() 281 | { 282 | return ExecutionEngine.ExecutingScriptHash; 283 | } 284 | 285 | private static byte[] RemoveFromArray(byte[] source, string needle) { 286 | if (source.Equals(needle.AsByteArray())) { 287 | return null; 288 | } 289 | string needle2 = needle + ","; 290 | byte[] pattern = needle.AsByteArray(); 291 | byte[] pattern2 = needle2.AsByteArray(); 292 | byte[] result = new byte[0]; 293 | for (int i = 0; i < source.Length; i++) { 294 | byte[] part = Helper.Range(source, i, pattern2.Length); 295 | if (part.Equals(pattern2)) { 296 | result = Helper.Concat(result, takeTillEnd(source, i + pattern2.Length)); 297 | return result; 298 | } 299 | part = Helper.Range(source, i, pattern.Length); 300 | if (part.Equals(pattern)) 301 | { 302 | result = Helper.Concat(result, takeTillEnd(source, i + pattern.Length)); 303 | return result; 304 | } 305 | result = Helper.Concat(result, new byte[] { source[i] }); 306 | } 307 | return result; 308 | } 309 | 310 | private static byte[] takeTillEnd(byte[] source, int index) { 311 | return Helper.Range(source, index, source.Length - index); 312 | } 313 | 314 | private static bool ArrayContains(string[] array, string needle) 315 | { 316 | foreach (string i in array) { 317 | if (i.Equals(needle)) { 318 | return true; 319 | } 320 | } 321 | return false; 322 | } 323 | } 324 | } 325 | -------------------------------------------------------------------------------- /SmartSavingContract/SmartSavingContract.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {A024E040-51F7-482C-B79C-89FC4B368B2C} 9 | Library 10 | Properties 11 | SmartSavingContract 12 | SmartSavingContract 13 | v4.0 14 | 512 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | none 27 | true 28 | bin\Release\ 29 | 30 | 31 | prompt 32 | 4 33 | 34 | 35 | 36 | ..\packages\Neo.SmartContract.Framework.2.3.0\lib\net40\Neo.SmartContract.Framework.dll 37 | True 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /SmartSavingContract/build.tasks: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /SmartSavingContract/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | --------------------------------------------------------------------------------