-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (145 loc) · 4.39 KB
/
index.js
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
const JSON5 = require('json5')
module.exports = function(embark) {
let contract;
let accounts;
embark.registerConsoleCommand(function(cmd, options) {
return {
match: () => {
const cmdName = cmd.match(/".*?"|\S+/g);
return (Array.isArray(cmdName) &&
(cmdName[0] === 'send' || cmdName[0] === 'call') &&
cmdName[1] === 'init')
},
process: async (callback) => {
const cmdName = cmd.match(/".*?"|\S+/g);
if(!cmdName.length === 3) {
help();
} else {
accounts = await web3.eth.getAccounts()
embark.events.request('contracts:contract', cmdName[2], async (cntrct) => {
if (!cntrct || !cntrct.deployedAddress) {
return callback("Could not find contract " + cmdName[2] + ".");
}
contract = new web3.eth.Contract(cntrct.abiDefinition, cntrct.address)
console.log("ECCÌ set to interact with contract at " + contract._address + ".")
});
}
return callback(null, "");
}
};
})
embark.registerConsoleCommand(function(cmd, options) {
return {
match: () => {
const cmdName = cmd.match(/".*?"|\S+/g);
return (Array.isArray(cmdName) &&
(cmdName[0] === 'send' || cmdName[0] === 'call') &&
cmdName[1] === 'help')
},
process: async (callback) => {
help();
return callback(null, "");
}
};
})
embark.registerConsoleCommand(function(cmd, options) {
return {
match: () => {
const cmdName = cmd.match(/{.*?}|\S+/g);
return (Array.isArray(cmdName) &&
cmdName[0] === 'call')
},
process: async (callback) => {
const cmdName = cmd.match(/{.*?}|\S+/g);
if(!(cmdName.length >= 2 && cmdName.length <= 4)) {
help();
return callback(null, "")
} else {
let func = contract.methods[cmdName[1]]
if(!func) {
console.log("'" + cmdName[1] + "' is not a function of the contract at address " + contract._address + ".")
return
}
let params = parseParams(cmdName[2]);
let options = parseOptions(cmdName[3]);
if(!params || params === "{}") {
return callback(null, await func().call(options))
} else {
return callback(null, await func(...params).call(options))
}
}
}
};
})
embark.registerConsoleCommand(function(cmd, options) {
return {
match: () => {
const cmdName = cmd.match(/{.*?}|\S+/g);
return (Array.isArray(cmdName) &&
cmdName[0] === 'send')
},
process: async (callback) => {
const cmdName = cmd.match(/{.*?}|\S+/g);
if(!(cmdName.length >= 2 && cmdName.length <= 4)) {
help();
return callback(null, "")
} else {
let params = parseParams(cmdName[2]);
let func = contract.methods[cmdName[1]]
if(!func) {
console.log("'" + cmdName[1] + "' is not a function of the contract at address " + contract._address + ".")
return
}
let options = parseOptions(cmdName[3]);
if(!params) {
return callback(null, await func().send(options))
} else {
return callback(null, await func(...params).send(options))
}
}
}
};
})
function help() {
console.log("Usage: [send|call] (init | <function> [params] [options])")
console.log("")
console.log(" where [params] are the function parameters")
console.log(" and [options] are the call/send parameters ('from', 'gas', etc.).")
console.log("")
console.log("Commands:")
console.log("\tinit <name>\tDefine contract to interact with by <name>.")
console.log("\tcall\t\tCall a constant contract function.")
console.log("\tsend\t\tCall a non-constant contract function.")
console.log("\thelp\t\tThis help.")
}
function parseParams(paramString) {
if(!paramString || paramString === "{}")
return;
params = paramString.split(",")
for(var i = 0; i < params.length; i++) {
console.log(i, params[i])
params[i] = params[i].replace(/["'{}]/g, '').trim()
console.log(i, params[i])
}
params = params.map(obj => {
let evaled = eval(obj)
if(typeof evaled == 'number')
return obj
else return evaled
});
return params;
}
function parseOptions(optionsString) {
if(!optionsString || optionsString === "{}") {
let defaultAddress = eval(accounts[0])
return JSON5.parse("{from: \"" + accounts[0] + "\"}");
}
options = JSON5.parse(optionsString, (key, value) => {
let evaled = eval(value)
if(typeof evaled == 'number')
return value
else return evaled
})
return options;
}
}