-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
executable file
·82 lines (66 loc) · 2.39 KB
/
main.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
const Configuration = require('./nodejs-sdk/packages/api').Configuration;
const Web3jService = require('./nodejs-sdk/packages/api').Web3jService;
const createContractClass = require('./nodejs-sdk/packages/api/compile/contractClass').createContractClass;
let config = new Configuration('config.json');
let web3j = new Web3jService(config);
const readline = require('readline');
const fs = require('fs');
async function input(query) {
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve, reject) => {
rl.question(query, (res) => {
rl.close();
resolve(res);
});
});
}
async function main() {
let name = await input('which contract are you going to use? ');
console.log('Loading contract from compiled file ...');
let compiled = JSON.parse(fs.readFileSync(`compiled/${name}.json`))
let contract = createContractClass(
compiled.name, compiled.abi, compiled.bin, config.encryptType
).newInstance();
console.log('Loading deployed contract address from deployed file ...');
let contractAddr = JSON.parse(fs.readFileSync(`deployed/${name}.json`))['contractAddress'];
contract.$load(web3j, contractAddr);
console.log('Done.');
let account = 'ca'; // 默认使用CA账户
while (1) {
let instruction = (await input('> ')).split(/\s+/);
let method = instruction[0];
let parameters = instruction.splice(1);
if (method == '.exit') { // 退出
break;
}
if (method == '.switch') {
if (parameters.length != 1) {
console.log(`it seems that parameters are not correct. parameters: ${parameters}`);
continue;
}
account = parameters[0];
console.log(`ok. set account to '${account}'`);
continue;
}
if (method == '.who') {
console.log(`current account '${account}'`);
continue;
}
if (!contract[method]) {
console.log(`undefined method '${method}'`);
continue;
}
try {
contract.$by(account);
let res = await contract[method](...parameters);
console.log(res);
} catch(err) {
console.log('error occurred.');
console.log(err);
}
}
}
main();