-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
296 lines (242 loc) · 9.11 KB
/
main.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
294
295
296
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"os"
"strconv"
// "github.com/Prosp3r/smartdo/interact"
"github.com/Prosp3r/smartdo/deploy"
"github.com/Prosp3r/smartdo/interact"
"github.com/holiman/uint256"
// "github.com/Prosp3r/smartdo/interact"
"github.com/Prosp3r/smartdo/utility"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
var (
EvMOSNet = "http://192.168.8.105:8545"
EvMOSNet2 = "https://evmos-evm-rpc.tk"
MainNet = "https://mainnet.infura.io/v3/8c5b190b405041f4afb69b99b46c4070"
GanaChe = ""
RinkeByTestNet = "https://rinkeby.infura.io/v3/8c5b190b405041f4afb69b99b46c4070"
KovanTestNet = "https://kovan.infura.io/v3/8c5b190b405041f4afb69b99b46c4070"
RopstenTestNet = "https://ropsten.infura.io/v3/8c5b190b405041f4afb69b99b46c4070"
KeyStoreLocation = "./wallet"
TestPassword1 = "password"
TestUserName1 = "efemena"
TestPassword2 = "akomeno123,"
TestUserName2 = "omovie"
ActiveNet = EvMOSNet2 //RopstenTestNet //RinkeByTestNet //KovanTestNet
)
func FailOnError(err error, note string) bool {
if err != nil {
fmt.Printf("Error: %v - %v\n", note, err)
return true
}
return false
}
//END SampleData - temporary information for testing
var address1 common.Address //"0xd54dBb460e43463D9382E38d06aAf258a27D050a"
var address2 common.Address //"0x8be9a9FCA9861b39487C8513C0EfD2D4C697011d"
func main() {
//Run OS flags
// flagger()
// eClient, err := ethclient.DialContext(context.Background(), ActiveNet)
eClient, err := ethclient.Dial(ActiveNet)
_ = FailOnError(err, "Error creating ether client")
defer eClient.Close()
/*
Commands
1. Check current app balance
2. Query app
3. Send tokens to app
9. Create wallet
*/
arg := os.Args
if len(arg) < 1 {
fmt.Println("Missing options ./smartdo <command> username password \n e.g: ./smartdo adduser username password")
return
}
command := arg[1]
//Deploy smart contract to active network
if command == "deploy" {
//Deploys the pre written ERC20 Toke to the active Testnet or mainnet
// $ ./smartdo deploy username password <contractname>
username := arg[2]
password := arg[3]
var Dep utility.DeployedContracts
//Deploy smart Contract to chosen testnet
deployname := arg[4]
dResult, err := deploy.Deploy(eClient, username, password)
if FailOnError(err, "Error creating a deployment") == true {
fmt.Printf("%v\n", err)
return
}
Dep.ContractHex = dResult.AddressHex
Dep.TranxHex = dResult.TransactionHex
Dep.NetworkDeployed = ActiveNet
DepJ, err := json.MarshalIndent(Dep, "", " ")
//
fmt.Println(Dep.TranxHex)
fmt.Println(Dep.ContractHex)
err = ioutil.WriteFile("loadedcontracts/"+deployname, DepJ, 0644)
//write to file
}
//Create account
if command == "adduser" {
//Creates a new encrypted ethereum compatible wallet
// $ ./smartdo adduser username password
username := arg[2]
password := arg[3]
uWallet, err := utility.CreateCryptoWallet(username, password)
if utility.FailOnError(err, "utility.CreateCryptoWallet") {
fmt.Println("Cound not create account - ", err)
return
}
fmt.Printf("Your crypto wallet : %v \n Username: %v \n Password: %v \n", uWallet.Address, username, password)
}
//Print address hex
if command == "mywallet" {
// $ ./smartdo mywallet username password
username := arg[2]
password := arg[3]
uCryptoKey, err := utility.ReadCryptoKey(username, password)
if utility.FailOnError(err, "utility.ReadCryptoKey") == true {
fmt.Println("Cound not find account - ", err)
return
}
address := utility.GetWalletAddress(uCryptoKey)
fmt.Printf("Your crypto wallet : %v \n", address)
}
//Print address balance
if command == "balance" {
// $ ./smartdo balance username password
username := arg[2]
password := arg[3]
uCryptoKey, err := utility.ReadCryptoKey(username, password)
if utility.FailOnError(err, "utility.ReadCryptoKey") == true {
fmt.Println("Cound not find account - ", err)
return
}
address := utility.GetWalletAddress(uCryptoKey)
balance := utility.CheckCryptoBalance(*address, eClient)
ethbalance := utility.WeiToEther(balance)
// fmt.Printf("Your crypto wallet : %v \nBalance: %v\n", address, balance)
fmt.Printf("Your crypto wallet : %v \nBalance(wei): %v\n Balanace(eth):%f\n", address, balance, ethbalance)
}
if command == "sendwei" {
//Send wei to another address
// $ ./smartdo sendwei username password <recipient_address e.g 0x8be9a9FCA9861b39487C8513C0EfD2D4C697011d> <sendAmount e.g. 200>
username := arg[2]
password := arg[3]
if len(arg) > 5 {
recipeient := arg[4]
sendAmount := arg[5]
s, err := strconv.Atoi(sendAmount)
if utility.FailOnError(err, "strconv.Atoi") == true {
fmt.Println("Cound not read amount to send - ", err)
return
}
amount := new(big.Int).SetUint64(uint64(s))
receiver := common.HexToAddress(recipeient)
senderAddress, err := utility.GetUserAddress(username, password)
if utility.FailOnError(err, "utility.GetUserAddress") == true {
fmt.Println("Could not find sender address")
return
}
senderBalance, err := utility.GetWalletBalance(eClient, senderAddress.Hex())
fmt.Printf("Sender address: %v \nSender balance: %v \nSending...", senderAddress.Hex(), senderBalance)
senderKeys, err := utility.ReadCryptoKey(TestUserName1, TestPassword1)
if FailOnError(err, "ReadCryptoWallet") == true {
fmt.Printf("Could not fetch sender private key - pleae make sure the password is correct. error: %v\n", err)
return
}
var AppData []byte = nil
send, err := utility.CreateNewTransaction(*senderAddress, receiver, amount, eClient, AppData)
if utility.FailOnError(err, "utility.CreateNewTransaction") == true {
fmt.Println("Cound not send amount - ", err)
return
}
tranxHex, err := utility.BindAndSendTransaction(eClient, send, senderKeys)
if utility.FailOnError(err, "BindAndSendTransaction") == true {
fmt.Println("Cound not send amount - ", err)
return
}
fmt.Printf("Trasaction was successful Hex: %v \n\n", *tranxHex)
return
}
fmt.Println("Command sendwei: Missing recipient address")
return
}
if command == "contract-mint" {
// $ ./smartdo contract-mint username password <contractName e.g. logi> <recipient_address e.g 0x8be9a9FCA9861b39487C8513C0EfD2D4C697011d> <amountOfTokens e.g. 200000000000000>
//username must be the one that deployed the contract
username := arg[2] //admin username
password := arg[3] //admin user password
contractName := arg[4] //which contract should be used for this interraction
recipientAddress := arg[5] //which address will receive the minted tokens
var ii interact.InputQuery
//read contract information by name used to save it
contract, err := utility.ReadStoredContract(contractName)
if utility.FailOnError(err, "BindAndSendTransaction") == true {
fmt.Printf("Cound not find smart contract - names: %v\n encountered error %v\n", contractName, err)
return
}
//If required
sendAmount := arg[6] //amount of tokens to mint to recipient address
s, err := strconv.Atoi(sendAmount)
if utility.FailOnError(err, "strconv.Atoi") == true {
fmt.Println("Cound not read amount to send - ", err)
return
}
ii.Amount = *uint256.NewInt(uint64(s))
ii.CcontractHex = contract.ContractHex
ii.Username = username
ii.Password = password
ii.AddressRecipient = common.HexToAddress(recipientAddress)
interact, err := interact.Mint(eClient, &ii)
if utility.FailOnError(err, "interact.Mint") == true {
fmt.Println("Cound not mint token(s) - ", err)
return
}
fmt.Println(interact.Hash().Hex())
fmt.Printf("Transaction Hex : %v \n", interact.Hash().Hex())
return
}
if command == "contract-transfer" {
// $ ./smartdo contract-transfer username password <contractName e.g. logi> <recipient_address e.g 0x8be9a9FCA9861b39487C8513C0EfD2D4C697011d> <amountOfTokens e.g. 2000000000000000000>
//username must be the one that deployed the contract
username := arg[2] //admin username
password := arg[3] //admin user password
contractName := arg[4] //which contract should be used for this interraction
recipientAddress := arg[5] //which address will receive the minted tokens
var ii interact.InputQuery
//read contract information by name used to save it
contract, err := utility.ReadStoredContract(contractName)
if utility.FailOnError(err, "BindAndSendTransaction") == true {
fmt.Printf("Cound not find smart contract - names: %v\n encountered error %v\n", contractName, err)
return
}
//If required
sendAmount := arg[6] //amount of tokens to mint to recipient address
s, err := strconv.Atoi(sendAmount)
if utility.FailOnError(err, "strconv.Atoi") == true {
fmt.Println("Cound not read amount to send - ", err)
return
}
ii.Amount = *uint256.NewInt(uint64(s))
ii.CcontractHex = contract.ContractHex
ii.Username = username
ii.Password = password
ii.AddressRecipient = common.HexToAddress(recipientAddress)
interact, err := interact.Transfer(eClient, &ii)
if utility.FailOnError(err, "interact.Mint") == true {
fmt.Println("Cound not mint token(s) - ", err)
return
}
fmt.Printf("Transaction Hex : %v \n", interact.Hash().Hex())
return
}
}