-
Notifications
You must be signed in to change notification settings - Fork 0
/
chasmcli.py
321 lines (253 loc) · 12.3 KB
/
chasmcli.py
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""Client for running commands on node"""
import argparse
from chasm import rpc
from chasm.consensus import Side
from chasm.maintenance.config import Config, DEFAULT_CONFIG_FILE
from chasm.maintenance.logger import Logger
from chasm.rpc import list_token_names, TIMEOUT_FORMAT, ALL_ADDRESSES
from chasm.rpc.client import show_transaction, show_balance, generate_account, \
create_offer, accept_offer, unlock_deposit, transfer, \
show_keys, show_marketplace, show_matches, show_accepted_offers, show_all_funds, \
build_tx, xpeer_transfer, sign, send, show_dutxos, show_utxos, confirm
# pylint: disable=missing-docstring
def get_parser():
parser = argparse.ArgumentParser(description='Chasm client',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-n', '--node', required=False, help="hostname")
parser.add_argument('-p', "--port", required=False, help="port")
parser.add_argument('-d', '--datadir', required=False, help="datadir for chasm storage")
parser.add_argument('-c', '--config', required=False, help='config file')
create_subparsers(parser.add_subparsers())
parser.set_defaults(func=lambda x: parser.print_help())
return parser
def create_subparsers(subparsers):
create_balance_parser(subparsers)
create_confirm_parser(subparsers)
create_build_parser(subparsers)
create_dutxo_parser(subparsers)
create_funds_parser(subparsers)
create_gen_address_parser(subparsers)
create_keys_parser(subparsers)
create_marketplace_parser(subparsers)
create_match_parser(subparsers)
create_matches_parser(subparsers)
create_offer_parser(subparsers)
create_offers_parser(subparsers)
create_send_tx_parser(subparsers)
create_sign_parser(subparsers)
create_show_tx_parser(subparsers)
create_transfer_parser(subparsers)
create_unlock_parser(subparsers)
create_utxo_parser(subparsers)
create_xpeer_parser(subparsers)
def create_marketplace_parser(subparsers):
parser = subparsers.add_parser('marketplace',
description="show all current offers, \
possible tokens: {}".format(list_token_names()))
parser.add_argument('--token', default="all",
help="currency for sale")
parser.add_argument('--expected', default="all",
help="payment currency")
parser.set_defaults(func=show_marketplace)
def create_show_tx_parser(subparsers):
parser = subparsers.add_parser('show',
description="show transaction")
parser.add_argument('--tx', help="hash of the transaction",
required=True)
parser.set_defaults(func=show_transaction)
def create_send_tx_parser(subparsers):
parser = subparsers.add_parser('send',
description="show transaction")
parser.add_argument('--tx', help="hash of the transaction",
required=True)
parser.add_argument('--signatures', default=[], nargs='*',
help="list of signatures")
parser.set_defaults(func=send)
def create_confirm_parser(subparsers):
parser = subparsers.add_parser('confirm',
description="confirm exchange")
parser.add_argument('--address', help="address for income",
required=True)
parser.add_argument('--exchange', help="exchange (hash of the offer)",
required=True)
parser.add_argument('--proof_in', help="proof of offer maker's transfer(tx hash)",
required=True)
parser.add_argument('--proof_out', help="proof of offer taker's transfer(tx hash)",
required=True)
parser.set_defaults(func=confirm)
def create_sign_parser(subparsers):
parser = subparsers.add_parser('sign',
description="sign transaction")
parser.add_argument('--tx', help="hash of the transaction",
required=True)
parser.add_argument('--address', help="address of signer",
required=True)
parser.set_defaults(func=sign)
def create_balance_parser(subparsers):
parser = subparsers.add_parser('balance',
description="show balance of the account")
parser.add_argument('--address', required=True,
help="address")
parser.set_defaults(func=show_balance)
def create_dutxo_parser(subparsers):
parser = subparsers.add_parser('dutxo',
description="show DUTXOs of the account")
parser.add_argument('--address', required=True,
help="address")
parser.set_defaults(func=show_dutxos)
def create_utxo_parser(subparsers):
parser = subparsers.add_parser('utxo',
description="show UTXOs of the account")
parser.add_argument('--address', required=True,
help="address")
parser.set_defaults(func=show_utxos)
def create_build_parser(subparsers):
parser = subparsers.add_parser('build',
description="build any transaction from json file")
parser.add_argument('--file', required=True,
help="path to json file with transaction")
parser.set_defaults(func=build_tx)
def create_funds_parser(subparsers):
parser = subparsers.add_parser('funds',
description="show balance of all accounts")
parser.set_defaults(func=show_all_funds)
def create_gen_address_parser(subparsers):
parser = subparsers.add_parser('generate',
description="generate new address")
parser.set_defaults(func=generate_account)
def create_offer_parser(subparsers):
parser = subparsers.add_parser('offer',
description="create an exchange offer")
parser.add_argument('--address', required=True,
help='address of offer maker(xpc)')
parser.add_argument('--token', required=True,
help="Currency being sold, \
possible tokens: {}"
.format(list_token_names()))
parser.add_argument('--amount', required=True, type=int,
help="amount of token being sold(smallest denomination")
parser.add_argument('--expected', required=True,
help="expected incoming currency, \
possible tokens: {}"
.format(list_token_names()))
parser.add_argument('--price', required=True, type=int,
help='price of offered tokens')
parser.add_argument('--receive', required=True,
help='receive payment address')
parser.add_argument('--confirmation', required=True, type=int,
help='confirmation fee(bdzys)')
parser.add_argument('--deposit', required=True, type=int,
help='deposit fee(bdzys)')
parser.add_argument('--timeout', required=True,
help='offer timeout, format: {}'
.format(TIMEOUT_FORMAT.replace("%", "")))
parser.add_argument('--fee', required=True, type=int,
help="transaction fee(bdzys)")
parser.set_defaults(func=create_offer)
def create_offers_parser(subparsers):
parser = subparsers.add_parser('offers',
description="show accepted offers")
parser.add_argument('--address', default=ALL_ADDRESSES,
help="address for income")
parser.set_defaults(func=show_accepted_offers
)
def create_match_parser(subparsers):
parser = subparsers.add_parser('match',
description="accept the offer")
parser.add_argument('--offer', required=True,
help="hash of the offer")
parser.add_argument('--address', required=True,
help='address of offer taker(xpc)')
parser.add_argument('--receive', required=True,
help='receive payment address')
parser.add_argument('--confirmation', required=True, type=int,
help='confirmation fee(bdzys)')
parser.add_argument('--deposit', required=True, type=int,
help='deposit fee(bdzys)')
parser.add_argument('--fee', required=True, type=int,
help="transaction fee(bdzys)")
parser.set_defaults(func=accept_offer)
def create_matches_parser(subparsers):
parser = subparsers.add_parser('matches',
description="show offer matches")
parser.add_argument('--address', default=ALL_ADDRESSES,
help="address for income")
parser.set_defaults(func=show_matches)
def create_unlock_parser(subparsers):
parser = subparsers.add_parser('unlock',
description="unlock the deposit")
parser.add_argument('--offer', required=True,
help="hash of the offer")
parser.add_argument('--address', required=True,
help='address of requestor(xpc)')
parser.add_argument('--deposit', required=True, type=int,
help='deposit fee(bdzys)')
parser.add_argument('--fee', required=True, type=int,
help="transaction fee(bdzys)")
parser.add_argument('--side', required=True, type=int,
help="side of the exchange: maker({}), taker({})"
.format(Side.OFFER_MAKER.value,
Side.OFFER_TAKER.value))
parser.add_argument('--proof', required=True,
help='proof of honesty(hex)')
parser.set_defaults(func=unlock_deposit)
def create_transfer_parser(subparsers):
parser = subparsers.add_parser('transfer',
description="transfer funds")
parser.add_argument('--fee', required=True, type=int,
help="transaction fee(in bdzys denomination)")
parser.add_argument('--owner', required=True,
help="owner address")
parser.add_argument('--value', required=True, type=int,
help="amount to be transferred(bdzys)")
parser.add_argument('--receiver', required=True,
help="receiver address")
parser.set_defaults(func=transfer)
def create_xpeer_parser(subparsers):
parser = subparsers.add_parser('xpeer',
description="transfer xpc as a part of an exchange")
parser.add_argument('--fee', required=True, type=int,
help="transaction fee(in bdzys denomination)")
parser.add_argument('--owner', required=True,
help="owner address")
parser.add_argument('--value', required=True, type=int,
help="amount to be transferred(bdzys)")
parser.add_argument('--receiver', required=True,
help="receiver address")
parser.add_argument('--offer', required=True,
help="offer hash")
parser.set_defaults(func=xpeer_transfer)
def create_keys_parser(subparsers):
parser = subparsers.add_parser('keys',
description="show all addresses from datadir")
parser.set_defaults(func=show_keys)
def _prepare_config(args):
overridden = {}
if args.datadir:
overridden['datadir'] = args.datadir
if args.node:
overridden['node'] = args.node
if args.port:
overridden['rpc_port'] = args.port
return Config(args.config or DEFAULT_CONFIG_FILE, overridden=overridden)
def main():
"""
Main function, runs client side
"""
parser = get_parser()
args = parser.parse_args()
config = _prepare_config(args)
Logger.level = config.get('logger_level')
logger = Logger('chasm.cli.rpc')
rpc.client.logger = logger
args.node = config.get('node')
args.port = config.get('rpc_port')
args.datadir = config.get('datadir')
try:
args.func(args)
except RuntimeError:
logger.exception("An error occurred!")
except KeyboardInterrupt:
logger.info("Keyboard interrupt")
if __name__ == "__main__":
main()