From 7328be0dc259d1cac6124c7ff040e52615f89822 Mon Sep 17 00:00:00 2001 From: Jay Graber Date: Mon, 31 Jul 2017 15:15:22 -0700 Subject: [PATCH] Lookup by tradeid --- xcat/cli.py | 22 +++++++++++----------- xcat/db.py | 21 +++++++++++++++++---- xcat/userInput.py | 8 ++++---- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/xcat/cli.py b/xcat/cli.py index 41878e6..48c908c 100644 --- a/xcat/cli.py +++ b/xcat/cli.py @@ -12,20 +12,22 @@ def save_state(trade, tradeid): save(trade) db.create(trade, tradeid) -def checkSellStatus(trade): +def checkSellStatus(tradeid): + trade = db.get(tradeid) if trade.buy.get_status() == 'funded': secret = get_secret() print("SECRET found in checksellactions", secret) trade = seller_redeem_p2sh(trade, secret) print("TRADE SUCCESSFULLY REDEEMED", trade) - save_state(trade) + save_state(trade, tradeid) 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)) elif trade.buy.get_status() == 'redeemed': print("You have already redeemed the p2sh on the second chain of this trade.") # TODO: function to calculate appropriate locktimes between chains -def checkBuyStatus(trade): +def checkBuyStatus(tradeid): + trade = db.get(tradeid) if trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed': print("One active trade available, fulfilling buyer contract...") # they should calculate redeemScript for themselves @@ -39,7 +41,7 @@ def checkBuyStatus(trade): fund_tx = fund_contract(trade.buy) trade.buy.fund_tx = fund_tx print("trade buy with redeemscript?", trade.buy.__dict__) - save_state(trade) + save_state(trade, tradeid) else: print("Compiled p2sh for htlc does not match what seller sent.") elif trade.buy.get_status() == 'redeemed': @@ -141,12 +143,10 @@ def main(): elif command == "step2": # trade = get_trade() tradeid = args.argument[0] - trade = db.get(tradeid) - print(trade) - checkBuyStatus(trade) + checkBuyStatus(tradeid) elif command == "step3": - trade = get_trade() - checkSellStatus(trade) + tradeid = args.argument[0] + checkSellStatus(tradeid) elif command == "step4": - trade = get_trade() - checkBuyStatus(trade) + tradeid = args.argument[0] + checkBuyStatus(tradeid) diff --git a/xcat/db.py b/xcat/db.py index 7543b20..71bd57c 100644 --- a/xcat/db.py +++ b/xcat/db.py @@ -3,6 +3,8 @@ from xcat.utils import * import binascii import sys import json +import ast +from xcat.trades import * db = plyvel.DB('/tmp/testdb', create_if_missing=True) @@ -24,14 +26,25 @@ def createByFundtx(trade): def get(txid): rawtrade = db.get(b(txid)) - tradestr = x2s(b2x(rawtrade)) + tradestr = str(rawtrade, 'utf-8') trade = instantiate(tradestr) return trade +def instantiate(trade): + if type(trade) == str: + print(type(trade)) + tradestr = json.loads(trade) + print(tradestr) + trade = Trade(buy=Contract(tradestr['buy']), sell=Contract(tradestr['sell']), commitment=tradestr['commitment']) + print(trade) + return trade + + + # db.delete(b'hello') -# testtrade = get('test') -# testtrade = instantiate(testtrade) -# print(testtrade) +testtrade = get('test') +testtrade = instantiate(testtrade) +print(testtrade) # hexstr = get(txid) # print(x2s(hexstr)) diff --git a/xcat/userInput.py b/xcat/userInput.py index dc6a8d8..7a648ef 100644 --- a/xcat/userInput.py +++ b/xcat/userInput.py @@ -50,11 +50,11 @@ def authorize_fund_sell(htlcTrade): def get_initiator_addresses(): btc_addr = input("Enter your bitcoin address: ") # btc_addr = bXcat.new_bitcoin_addr() - btc_addr = 'myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp' + btc_addr = 'mihDbm4QGzDUqYtjAaq71RSjXeA1ptDpkY' print(btc_addr) zec_addr = input("Enter your zcash address: ") # zec_addr = zXcat.new_zcash_addr() - zec_addr = 'tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ' + zec_addr = 'tmCDNXibkr2QoG7QxtwqeyfYcXBBZoqUwQm' print(zec_addr) addresses = {'bitcoin': btc_addr, 'zcash': zec_addr} return addresses @@ -62,11 +62,11 @@ def get_initiator_addresses(): def get_fulfiller_addresses(): btc_addr = input("Enter the bitcoin address of the party you want to trade with: ") # btc_addr = bXcat.new_bitcoin_addr() - btc_addr = 'mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z' + btc_addr = 'mk8k2dpYEGpffbr6JtDg1797prTM9UTTaw' print(btc_addr) zec_addr = input("Enter the zcash address of the party you want to trade with: ") # zec_addr = zXcat.new_zcash_addr() - zec_addr = 'tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY' + zec_addr = 'tmAz1aYqBzqvRnhCdp38e8q45rkJZTERGh9' print(zec_addr) addresses = {'bitcoin': btc_addr, 'zcash': zec_addr} return addresses