2017-07-03 09:48:05 -07:00
|
|
|
import argparse, textwrap
|
|
|
|
from utils import *
|
|
|
|
import database as db
|
2017-07-12 19:45:45 -07:00
|
|
|
import bXcat, zXcat
|
|
|
|
from trades import *
|
|
|
|
from xcat import *
|
2017-07-12 20:33:56 -07:00
|
|
|
import ast
|
2017-07-12 19:45:45 -07:00
|
|
|
|
|
|
|
def find_role(contract):
|
|
|
|
# Obviously when regtest created both addrs on same machine, role is both.
|
|
|
|
if parse_addrs(contract.initiator):
|
|
|
|
return 'initiator'
|
|
|
|
else:
|
|
|
|
return 'fulfiller'
|
|
|
|
|
|
|
|
def parse_addrs(address):
|
|
|
|
if address[:1] == 'm':
|
|
|
|
status = bXcat.validateaddress(address)
|
|
|
|
else:
|
|
|
|
status = zXcat.validateaddress(address)
|
|
|
|
status = status['ismine']
|
|
|
|
print("Address {0} is mine: {1}".format(address, status))
|
|
|
|
return status
|
|
|
|
|
|
|
|
def checkSellActions(trade):
|
2017-07-24 13:23:40 -07:00
|
|
|
if trade.buy.get_status() == 'funded':
|
2017-07-12 19:45:45 -07:00
|
|
|
seller_redeem(trade)
|
2017-07-24 13:23:40 -07:00
|
|
|
elif trade.buy.get_status() == 'empty':
|
|
|
|
print("Buyer has not yet funded the contract where you offered to buy {0}, please wait for them to complete their part.".format(trade.buy.currency))
|
2017-07-12 19:45:45 -07:00
|
|
|
|
|
|
|
def checkBuyActions(trade):
|
2017-07-24 13:23:40 -07:00
|
|
|
if trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed':
|
2017-07-12 19:45:45 -07:00
|
|
|
print("One active trade available, fulfilling buyer contract...")
|
|
|
|
buyer_fulfill(trade)
|
2017-07-24 13:23:40 -07:00
|
|
|
elif trade.buy.get_status() == 'redeemed':
|
2017-07-12 19:45:45 -07:00
|
|
|
buyer_redeem(trade)
|
|
|
|
print("XCAT trade complete!")
|
2017-07-03 09:48:05 -07:00
|
|
|
|
2017-07-12 20:33:56 -07:00
|
|
|
def instantiateTrade(trade):
|
2017-07-24 13:23:40 -07:00
|
|
|
return Trade(buy=Contract(trade['buy']), sell=Contract(trade['sell']))
|
2017-07-12 20:33:56 -07:00
|
|
|
|
2017-07-03 09:48:05 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
|
|
|
|
description=textwrap.dedent('''\
|
2017-07-24 13:23:40 -07:00
|
|
|
== Trades ==
|
2017-07-12 19:45:45 -07:00
|
|
|
newtrade - create a new trade
|
|
|
|
checktrades - check for actions to be taken on existing trades
|
2017-07-24 13:23:40 -07:00
|
|
|
importtrade "hexstr" - import an existing trade from a hex string
|
|
|
|
exporttrade - export the data of an existing xcat trade as a hex string
|
2017-07-03 09:48:05 -07:00
|
|
|
|
|
|
|
'''))
|
2017-07-12 19:45:45 -07:00
|
|
|
parser.add_argument("command", action="store", help="list commands")
|
2017-07-12 20:33:56 -07:00
|
|
|
parser.add_argument("argument", action="store", nargs="*", help="add an argument")
|
2017-07-24 13:23:40 -07:00
|
|
|
# parser.add_argument("--daemon", "-d", action="store_true", help="Run as daemon process")
|
2017-07-03 09:48:05 -07:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-07-12 19:45:45 -07:00
|
|
|
# how to hold state of role
|
|
|
|
command = args.command
|
2017-07-24 13:23:40 -07:00
|
|
|
if command == 'importtrade':
|
2017-07-12 20:33:56 -07:00
|
|
|
hexstr = args.argument[0]
|
|
|
|
trade = x2s(hexstr)
|
|
|
|
trade = instantiateTrade(ast.literal_eval(trade))
|
2017-07-24 13:23:40 -07:00
|
|
|
db.create(trade)
|
|
|
|
# print(trade.toJ)
|
|
|
|
elif command == 'exporttrade':
|
2017-07-12 20:33:56 -07:00
|
|
|
trade = get_trade()
|
|
|
|
hexstr = s2x(str(trade))
|
|
|
|
print(trade)
|
|
|
|
print(hexstr)
|
2017-07-12 19:45:45 -07:00
|
|
|
elif command == 'checktrades':
|
|
|
|
trade = get_trade()
|
2017-07-12 20:33:56 -07:00
|
|
|
trade = instantiateTrade(trade)
|
2017-07-24 13:23:40 -07:00
|
|
|
if find_role(trade.sell) == 'initiator':
|
2017-07-12 19:45:45 -07:00
|
|
|
role = 'seller'
|
|
|
|
checkSellActions(trade)
|
|
|
|
else:
|
|
|
|
role = 'buyer'
|
|
|
|
checkBuyActions(trade)
|
|
|
|
elif command == 'newtrade':
|
2017-07-12 20:33:56 -07:00
|
|
|
erase_trade()
|
|
|
|
role = 'seller'
|
2017-07-24 07:41:03 -07:00
|
|
|
print("Creating new XCAT trade...")
|
2017-07-24 13:23:40 -07:00
|
|
|
trade = seller_initiate(Trade())
|
|
|
|
# Save it to leveldb
|
|
|
|
db.create(trade)
|
|
|
|
elif command == "daemon":
|
|
|
|
#TODO: implement
|
|
|
|
print("Run as daemon process")
|