This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·115 lines (98 loc) · 3.26 KB
/
cli.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
#!/usr/bin/env node
// Lotionroom - Tendermint / Cosmos proof of concept contract made with Zenroom
// Copyright (C) 2020 Dyne.org foundation
// designed, written and maintained by
// Denis Roio and Puria Nafisi Azizi
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
let connect = require('lotion-connect')
let YAML = require('yaml')
var fs = require('fs');
const read_file = filename => {
try {
return fs.readFileSync(filename, 'utf8')
} catch (e) {
console.log(`File '${filename}' NOT FOUND!`)
}
}
const read_json = filename => {
try {
return JSON.parse(read_file(filename));
} catch (e) {
console.log('malformed JSON. try like this:')
console.log('{ "foo": "bar" }\n\n\n')
console.error(e.message);
}
}
const read_gci = () => {
try {
return read_file("genesis.gci")
} catch (e) {
console.log('genesis.gci file not found!\n\nIs the node runnig?\n\nAre you in the right folder?')
}
}
let cx
async function main() {
switch(process.argv[2]) {
case 'context':
cx = await connect(read_gci())
console.log(YAML.stringify(await cx.state))
process.exit()
break;
case 'version':
cx = await connect(read_gci())
console.log(YAML.stringify(await cx.state.zenroom))
process.exit()
break;
case 'api':
cx = await connect(read_gci())
// const api = await cx.state.contracts
console.log(YAML.stringify(await cx.state.contracts))
process.exit()
break;
case 'state':
cx = await connect(read_gci())
// const api = await cx.state.contracts
console.log(YAML.stringify(await cx.state.current))
process.exit()
break;
case 'send':
const gci = read_gci()
let { send } = await connect(gci)
const contract = process.argv[3]
try {
const data = process.argv[4]?read_json(process.argv[4]):null
const keys = process.argv[5]?read_json(process.argv[5]):null
let tx = {
"contract": contract,
"keys": keys,
"data": data }
console.log(`To ${gci}:\n${YAML.stringify(tx)}`)
const res = await send(tx)
console.log(`Reply:\n${YAML.stringify(res)}\n`)
process.exit()
} catch (e) {
console.log('malformed JSON. try like this:')
console.log('$ lotion send <gci> \'{ "foo": "bar" }\'')
}
process.exit()
break;
default:
console.log(
`
Usage:
$ lotion state Get the latest state of an app
$ lotion send <data-filename.json> <keys-filename.json> <zencode-filename.zen> Send a zencode transaction to a running app
`
)
}
}
main()