This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
293 lines (275 loc) · 8.25 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"fmt"
"log"
database "github.com/YaleOpenLab/opensolar/database"
platform "github.com/YaleOpenLab/opensolar/platform"
scan "github.com/YaleOpenLab/opensolar/scan"
xlm "github.com/YaleOpenLab/opensolar/xlm"
)
func ValidateInputs() {
if (opts.RecYears != 0) && !(opts.RecYears == 3 || opts.RecYears == 5 || opts.RecYears == 7) {
log.Fatal(fmt.Errorf("Number of years not supported"))
}
}
func StartPlatform() (string, string, error) {
var publicKey string
var seed string
ValidateInputs()
database.CreateHomeDir()
allContracts, err := database.RetrieveAllProjects()
if err != nil {
log.Println("Error retrieving all projects from the database")
return publicKey, seed, err
}
if len(allContracts) == 0 {
log.Println("Populating database with test values")
err = InsertDummyData()
if err != nil {
return publicKey, seed, err
}
}
publicKey, seed, err = platform.InitializePlatform()
return publicKey, seed, err
}
func NewUserPrompt() (string, string, string, string, error) {
realName, err := scan.ScanForString()
if err != nil {
fmt.Println("Couldn't read user input")
return "", "", "", "", err
}
fmt.Printf("%s: ", "ENTER YOUR USERNAME")
loginUserName, err := scan.ScanForString()
if err != nil {
fmt.Println("Couldn't read user input")
return "", "", "", "", err
}
fmt.Printf("%s: ", "ENTER DESIRED PASSWORD, YOU WILL NOT BE ASKED TO CONFIRM THIS")
loginPassword, err := scan.ScanForPassword()
if err != nil {
fmt.Println("Couldn't read password")
return "", "", "", "", err
}
fmt.Printf("%s: ", "ENTER SEED PASSWORD, YOU WILL NOT BE ASKED TO CONFIRM THIS")
seedPassword, err := scan.ScanForPassword()
return realName, loginUserName, loginPassword, seedPassword, err
}
func NewInvestorPrompt() error {
log.Println("You have chosen to create a new investor account, welcome")
loginUserName, loginPassword, realName, seedpwd, err := NewUserPrompt()
if err != nil {
log.Println(err)
return err
}
_, err = database.NewInvestor(loginUserName, loginPassword, seedpwd, realName)
if err != nil {
log.Println("FAILED TO SETUP ACCOUNT, TRY AGAIN")
return err
}
return err
}
func NewRecipientPrompt() error {
log.Println("You have chosen to create a new recipient account, welcome")
loginUserName, loginPassword, realName, seedpwd, err := NewUserPrompt()
if err != nil {
log.Println(err)
return err
}
_, err = database.NewRecipient(loginUserName, loginPassword, seedpwd, realName)
if err != nil {
log.Println("FAILED TO SETUP ACCOUNT, TRY AGAIN")
return err
}
return err
}
func LoginPrompt() (database.Investor, database.Recipient, database.Entity, bool, bool, error) {
rbool := false
cbool := false
var investor database.Investor
var recipient database.Recipient
var contractor database.Entity
fmt.Println("---------SELECT YOUR ROLE---------")
fmt.Println(" i. INVESTOR")
fmt.Println(" r. RECIPIENT")
fmt.Println(" c. CONTRACTOR")
optS, err := scan.ScanForString()
if err != nil {
log.Println("Failed to read user input")
return investor, recipient, contractor, rbool, cbool, err
}
if optS == "I" || optS == "i" {
fmt.Println("WELCOME BACK INVESTOR")
} else if optS == "R" || optS == "r" {
fmt.Println("WELCOME BACK RECIPIENT")
rbool = true
} else if optS == "C" || optS == "c" {
cbool = true
fmt.Println("WELCOME BACK CONTRACTOR")
} else {
log.Println("INVALID INPUT, EXITING!")
return investor, recipient, contractor, rbool, cbool, fmt.Errorf("INVALID INPUT, EXITING!")
}
// ask for username and password combo here
fmt.Printf("%s: ", "ENTER YOUR USERNAME")
loginUserName, err := scan.ScanForString()
if err != nil {
fmt.Println("Couldn't read user input")
return investor, recipient, contractor, rbool, cbool, err
}
fmt.Printf("%s: ", "ENTER YOUR PASSWORD: ")
loginPassword, err := scan.ScanForPassword()
if err != nil {
fmt.Println("Couldn't read password")
return investor, recipient, contractor, rbool, cbool, err
}
user, err := database.ValidateUser(loginUserName, loginPassword)
if err != nil {
fmt.Println("Couldn't read password")
return investor, recipient, contractor, rbool, cbool, err
}
log.Println("WATCH USER INDEX: ", user.Index)
if rbool {
recipient, err = database.RetrieveRecipient(user.Index)
if err != nil {
return investor, recipient, contractor, rbool, cbool, err
}
} else if cbool {
contractor, err = database.RetrieveEntity(user.Index)
if err != nil {
return investor, recipient, contractor, rbool, cbool, err
}
} else {
investor, err = database.RetrieveInvestor(user.Index)
if err != nil {
return investor, recipient, contractor, rbool, cbool, err
}
}
return investor, recipient, contractor, rbool, cbool, nil
}
func OriginContractPrompt(contractor *database.Entity) error {
fmt.Println("YOU HAVE DECIDED TO PROPOSE A NEW CONTRACT")
fmt.Println("ENTER THE PANEL SIZE")
panelSize, err := scan.ScanForString()
if err != nil {
return err
}
fmt.Println("ENTER THE COST OF PROJECT")
totalValue, err := scan.ScanForInt()
if err != nil {
return err
}
fmt.Println("ENTER THE LOCATION OF PROJECT")
location, err := scan.ScanForString()
if err != nil {
return err
}
fmt.Println("ENTER THE ESTIMATED NUMBER OF YEARS FOR COMPLETION")
years, err := scan.ScanForInt()
if err != nil {
return err
}
fmt.Println("ENTER METADATA REGARDING THE PROJECT")
metadata, err := scan.ScanForString()
if err != nil {
return err
}
fmt.Println("ENTER THE RECIPIENT'S USER ID")
recIndex, err := scan.ScanForInt()
if err != nil {
return err
}
originContract, err := contractor.OriginContract(panelSize, totalValue, location, years, metadata, recIndex)
if err != nil {
return err
}
// project insertion is done by the above function, so we needn't call the database to do it again for us
PrintProject(originContract)
return nil
}
func ProposeContractPrompt(contractor *database.Entity) error {
fmt.Println("YOU HAVE DECIDED TO PROPOSE A NEW CONTRACT")
fmt.Println("ENTER THE PROJECT INDEX")
contractIndex, err := scan.ScanForInt()
if err != nil {
return err
}
// we need to check if this contract index exists and retrieve
rContract, err := database.RetrieveProject(contractIndex)
if err != nil {
return err
}
// TODO: PRint CONTRACTS HERE
log.Println("YOUR CONTRACT IS: ")
PrintProject(rContract)
if rContract.Params.Index == 0 || rContract.Stage != 1 {
// prevent people form porposing contracts for non originated contracts
return fmt.Errorf("Invalid contract index")
}
panelSize := rContract.Params.PanelSize
location := rContract.Params.Location
fmt.Println("ENTER THE COST OF PROJECT")
totalValue, err := scan.ScanForInt()
if err != nil {
return err
}
fmt.Println("ENTER THE ESTIMATED NUMBER OF YEARS FOR COMPLETION")
years, err := scan.ScanForInt()
if err != nil {
return err
}
fmt.Println("ENTER METADATA REGARDING THE PROJECT")
metadata, err := scan.ScanForString()
if err != nil {
return err
}
fmt.Println("ENTER THE RECIPIENT'S USER ID")
recIndex, err := scan.ScanForInt()
if err != nil {
return err
}
originContract, err := contractor.ProposeContract(panelSize, totalValue, location, years, metadata, recIndex, contractIndex)
if err != nil {
return err
}
// project insertion is done by the above function, so we needn't call the database to do it again for us
PrintProject(originContract)
return nil
}
func Stage3ProjectsDisplayPrompt() {
fmt.Println("------------LIST OF ALL AVAILABLE PROJECTS------------")
allProjects, err := database.RetrieveProjects(database.FinalizedProject)
if err != nil {
log.Println("Error retrieving all projects from the database")
} else {
PrintProjects(allProjects)
}
}
func DisplayOriginProjects() {
fmt.Println("PRINTING ALL ORIGINATED PROJECTS: ")
x, err := database.RetrieveProjects(database.OriginProject)
if err != nil {
log.Println(err)
} else {
PrintProjects(x)
}
}
func ExitPrompt() {
// check whether he wants to go back to the display all screen again
fmt.Println("DO YOU REALLY WANT TO EXIT? (PRESS Y TO CONFIRM)")
exitOpt, err := scan.ScanForString()
if err != nil {
log.Println(err)
}
if exitOpt == "Y" || exitOpt == "y" {
fmt.Println("YOU HAVE DECIDED TO EXIT")
log.Fatal("")
}
}
func BalanceDisplayPrompt(publicKey string) {
balances, err := xlm.GetAllBalances(publicKey)
if err != nil {
log.Println(err)
} else {
PrintBalances(balances)
}
}