Add option to pass trade data in as json on cli

This commit is contained in:
Jay Graber 2017-09-22 18:37:00 -07:00
parent 76c1729031
commit 1f3a65cd42
3 changed files with 58 additions and 63 deletions

View File

@ -228,19 +228,13 @@ def newtrade(tradeid, **kwargs):
protocol = Protocol()
print("Creating new XCAT trade...")
utils.erase_trade()
conf = kwargs['conf'] if 'conf' in kwargs else 'regtest'
network = kwargs['network'] if 'network' in kwargs else 'regtest'
tradeid, trade = protocol.initialize_trade(
tradeid,
conf=conf,
network=network)
tradeid, trade = protocol.initialize_trade(tradeid, conf=conf, network=network)
print("New trade created: {0}".format(trade))
trade = protocol.seller_init(tradeid, trade, network=network)
print("\nUse 'xcat exporttrade [tradeid]' to export the trade and sent "
"to the buyer.\n")
print("\nUse 'xcat exporttrade [tradeid]' to export the trade and send to the buyer.\n")
save_state(trade, tradeid)
return trade
@ -266,26 +260,15 @@ def main():
findtrade "tradeid" - find a trade by the tradeid
'''))
parser.add_argument(
"command", action="store", help="list commands")
parser.add_argument(
"arguments", action="store", nargs="*", help="add arguments")
parser.add_argument(
"-w", "--wormhole", action="store_true",
help="Transfer trade data through magic-wormhole")
parser.add_argument(
"-c", "--conf", action="store",
help="Use default trade data in conf file.")
parser.add_argument(
"-n", "--network", action="store",
help=("Set network to regtest or mainnet. "
"Defaults to testnet while in alpha."))
# parser.add_argument(
# "--daemon", "-d", action="store_true",
# help="Run as daemon process")
parser.add_argument("command", action="store", help="list commands")
parser.add_argument("arguments", action="store", nargs="*", help="add arguments")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debug mode. Defaults to false")
parser.add_argument("-w", "--wormhole", action="store_true", help="Transfer trade data through magic-wormhole")
parser.add_argument("-c", "--conf", action="store", help="Use trade data in conf file ('testnet' or 'regtest'), or pass trade data in on cli as json.")
parser.add_argument("-n", "--network", action="store", help="Set network to regtest or mainnet. Defaults to testnet while in alpha.")
# parser.add_argument("--daemon", "-d", action="store_true", help="Run as daemon process")
args = parser.parse_args()
print(args)
if hasattr(args, 'debug'):
numeric_level = getattr(logging, 'DEBUG', None)
@ -341,31 +324,28 @@ def main():
utils.throw("Usage: newtrade [tradeid]")
tradeid = args.arguments[0]
if args.conf is None:
newtrade(tradeid, network=NETWORK, conf='cli')
conf = 'cli'
else:
newtrade(tradeid, network=NETWORK, conf=args.conf)
conf = args.conf
newtrade(tradeid, network=NETWORK, conf=conf)
elif command == "daemon":
# TODO: not implemented
print("Run as daemon process")
# Ad hoc testing of workflow starts here
elif command == "step1":
tradeid = args.arguments[0]
checkSellStatus(tradeid)
elif command == "step2":
tradeid = args.arguments[0]
checkBuyStatus(tradeid)
elif command == "step3":
# protocol = Protocol()
# protocol.generate(31)
tradeid = args.arguments[0]
checkSellStatus(tradeid)
elif command == "step4":
# generate(1)
generate(1)
tradeid = args.arguments[0]
checkBuyStatus(tradeid)

View File

@ -5,6 +5,9 @@ from xcat.xcatconf import ADDRS
from xcat.trades import Contract, Trade
from xcat.bitcoinRPC import bitcoinProxy
from xcat.zcashRPC import zcashProxy
import logging
import json
<<<<<<< HEAD
from xcat.db import DB
@ -186,35 +189,6 @@ class Protocol():
"{0} {1}!".format(buy.amount, buy.currency))
return txs
def initialize_trade(self, tradeid, **kwargs):
trade = Trade()
conf = kwargs['conf']
if conf == 'cli':
init_addrs = userInput.get_initiator_addresses()
fulfill_addrs = userInput.get_fulfiller_addresses()
amounts = userInput.get_trade_amounts()
print("AMOUNTS", amounts)
else:
init_addrs = ADDRS[conf]['initiator']
fulfill_addrs = ADDRS[conf]['fulfiller']
amounts = ADDRS[conf]['amounts']
sell = amounts['sell']
buy = amounts['buy']
sell_currency = sell['currency']
buy_currency = buy['currency']
sell['initiator'] = init_addrs[sell_currency]
buy['initiator'] = init_addrs[buy_currency]
sell['fulfiller'] = fulfill_addrs[sell_currency]
buy['fulfiller'] = fulfill_addrs[buy_currency]
# initializing contract classes with addresses, currencies, and amounts
trade.sell = Contract(sell)
trade.buy = Contract(buy)
print(trade.sell.__dict__)
print(trade.buy.__dict__)
return tradeid, trade
def seller_init(self, tradeid, trade, network):
db = DB()
secret = utils.generate_password()
@ -235,3 +209,39 @@ class Protocol():
trade.commitment = utils.b2x(hash_of_secret)
print("TRADE after seller init {0}".format(trade.toJSON()))
return trade
def initialize_trade(self, tradeid, **kwargs):
trade = Trade()
conf = kwargs['conf']
if conf == 'cli':
init_addrs = userInput.get_initiator_addresses()
fulfill_addrs = userInput.get_fulfiller_addresses()
amounts = userInput.get_trade_amounts()
print("AMOUNTS", amounts)
else:
print("Conf in init trade", conf)
if conf == 'testnet' or conf == 'regtest':
# If json is not passed on cli, use ADDR obj from xcatconf.py
conf = ADDRS[conf]
else:
# Allow for passing in multiple trades at a time
conf = json.loads(conf)[0]
init_addrs = conf['initiator']
fulfill_addrs = conf['fulfiller']
amounts = conf['amounts']
sell = amounts['sell']
buy = amounts['buy']
sell_currency = sell['currency']
buy_currency = buy['currency']
sell['initiator'] = init_addrs[sell_currency]
buy['initiator'] = init_addrs[buy_currency]
sell['fulfiller'] = fulfill_addrs[sell_currency]
buy['fulfiller'] = fulfill_addrs[buy_currency]
# initializing contract classes with addresses, currencies, and amounts
trade.sell = Contract(sell)
trade.buy = Contract(buy)
print(trade.sell.__dict__)
print(trade.buy.__dict__)
return tradeid, trade

View File

@ -23,3 +23,8 @@ ADDRS = {
"amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}}
}
}
NETWORK = 'testnet'
# Pass regtest trade data in on command line
# '[{"initiator": {"bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b", "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc"}, "fulfiller": {"bitcoin": "moRt56gJQGDNK46Y6fYy2HbooKnQXrTGDN", "zcash": "tmK3rGzHDqa78MCwEicx9VcY9ZWX9gCF2nd"}, "amounts": {"buy": {"currency": "zcash", "amount": 0.02}, "sell": {"currency": "bitcoin", "amount": 0.01}}}]'