Some cleaning up
This commit is contained in:
parent
e78072d0aa
commit
546a312e0a
21
xcat/cli.py
21
xcat/cli.py
|
@ -1,12 +1,9 @@
|
||||||
import argparse, textwrap
|
import argparse, textwrap
|
||||||
from xcat.utils import *
|
from xcat.utils import *
|
||||||
import xcat.db as db
|
import xcat.db as db
|
||||||
import xcat.bitcoinRPC
|
|
||||||
import xcat.zcashRPC
|
|
||||||
import xcat.userInput as userInput
|
import xcat.userInput as userInput
|
||||||
from xcat.trades import *
|
from xcat.trades import *
|
||||||
from xcat.protocol import *
|
from xcat.protocol import *
|
||||||
import ast
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
def save_state(trade, tradeid):
|
def save_state(trade, tradeid):
|
||||||
|
@ -17,7 +14,6 @@ def checkSellStatus(tradeid):
|
||||||
trade = db.get(tradeid)
|
trade = db.get(tradeid)
|
||||||
status = seller_check_status(trade)
|
status = seller_check_status(trade)
|
||||||
print("In checkSellStatus", status)
|
print("In checkSellStatus", status)
|
||||||
# if trade.buy.get_status() == 'funded':
|
|
||||||
if status == 'init':
|
if status == 'init':
|
||||||
userInput.authorize_fund_sell(trade)
|
userInput.authorize_fund_sell(trade)
|
||||||
fund_tx = fund_sell_contract(trade)
|
fund_tx = fund_sell_contract(trade)
|
||||||
|
@ -32,7 +28,8 @@ def checkSellStatus(tradeid):
|
||||||
trade.buy.redeem_tx = txs['redeem_tx']
|
trade.buy.redeem_tx = txs['redeem_tx']
|
||||||
print("TRADE SUCCESSFULLY REDEEMED", trade)
|
print("TRADE SUCCESSFULLY REDEEMED", trade)
|
||||||
save_state(trade, tradeid)
|
save_state(trade, tradeid)
|
||||||
# elif trade.buy.get_status() == 'empty':
|
# Remove from db? Or just from temporary file storage
|
||||||
|
cleanup(tradeid)
|
||||||
elif status == 'sellerFunded':
|
elif status == 'sellerFunded':
|
||||||
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))
|
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':
|
# elif trade.buy.get_status() == 'redeemed':
|
||||||
|
@ -71,7 +68,6 @@ def seller_check_status(trade):
|
||||||
else:
|
else:
|
||||||
return 'init' # step0
|
return 'init' # step0
|
||||||
|
|
||||||
# TODO: function to calculate appropriate locktimes between chains
|
|
||||||
def checkBuyStatus(tradeid):
|
def checkBuyStatus(tradeid):
|
||||||
trade = db.get(tradeid)
|
trade = db.get(tradeid)
|
||||||
status = buyer_check_status(trade)
|
status = buyer_check_status(trade)
|
||||||
|
@ -80,31 +76,22 @@ def checkBuyStatus(tradeid):
|
||||||
print("Trade has not yet started, waiting for seller to fund the sell p2sh.")
|
print("Trade has not yet started, waiting for seller to fund the sell p2sh.")
|
||||||
elif status == 'buyerRedeemed':
|
elif status == 'buyerRedeemed':
|
||||||
print("This trade is complete, both sides redeemed.")
|
print("This trade is complete, both sides redeemed.")
|
||||||
# elif trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed':
|
|
||||||
elif status == 'sellerFunded':
|
elif status == 'sellerFunded':
|
||||||
print("One active trade available, fulfilling buyer contract...")
|
print("One active trade available, fulfilling buyer contract...")
|
||||||
print("Trade commitment", trade.commitment)
|
print("Trade commitment", trade.commitment)
|
||||||
# htlc = create_htlc(trade.buy.currency, trade.buy.fulfiller, trade.buy.initiator, trade.commitment, trade.buy.locktime)
|
# if verify_p2sh(trade):
|
||||||
# buyer_p2sh = htlc['p2sh']
|
|
||||||
# print("Buyer p2sh:", buyer_p2sh)
|
|
||||||
# If the two p2sh match...
|
|
||||||
# if buyer_p2sh == trade.buy.p2sh:
|
|
||||||
fund_tx = fund_contract(trade.buy)
|
fund_tx = fund_contract(trade.buy)
|
||||||
print("Fund tx coming back in cli", fund_tx)
|
print("\nBuyer's funding tx: ", fund_tx)
|
||||||
trade.buy.fund_tx = fund_tx
|
trade.buy.fund_tx = fund_tx
|
||||||
save_state(trade, tradeid)
|
save_state(trade, tradeid)
|
||||||
# else:
|
|
||||||
# print("Compiled p2sh for htlc does not match what seller sent.")
|
|
||||||
elif status == 'sellerRedeemed':
|
elif status == 'sellerRedeemed':
|
||||||
print("FUND TX CLI", trade.buy.fund_tx)
|
print("FUND TX CLI", trade.buy.fund_tx)
|
||||||
secret = find_secret_from_fundtx(trade.buy.currency, trade.buy.p2sh, trade.buy.fund_tx)
|
secret = find_secret_from_fundtx(trade.buy.currency, trade.buy.p2sh, trade.buy.fund_tx)
|
||||||
print("Secret in cli", secret)
|
print("Secret in cli", secret)
|
||||||
# secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
|
|
||||||
if secret != None:
|
if secret != None:
|
||||||
print("Found secret", secret)
|
print("Found secret", secret)
|
||||||
txs = redeem_p2sh(trade.sell, secret)
|
txs = redeem_p2sh(trade.sell, secret)
|
||||||
print("TXS IN SELLER REDEEMED", txs)
|
print("TXS IN SELLER REDEEMED", txs)
|
||||||
# trade.sell.fund_tx = txs['fund_tx']
|
|
||||||
trade.sell.redeem_tx = txs['redeem_tx']
|
trade.sell.redeem_tx = txs['redeem_tx']
|
||||||
print("TXID after buyer redeem", trade.sell.redeem_tx)
|
print("TXID after buyer redeem", trade.sell.redeem_tx)
|
||||||
save_state(trade, tradeid)
|
save_state(trade, tradeid)
|
||||||
|
|
|
@ -7,6 +7,7 @@ from xcat.utils import *
|
||||||
from xcat.trades import Contract, Trade
|
from xcat.trades import Contract, Trade
|
||||||
import xcat.userInput as userInput
|
import xcat.userInput as userInput
|
||||||
import xcat.db as db
|
import xcat.db as db
|
||||||
|
from xcatconf import *
|
||||||
|
|
||||||
def find_secret_from_fundtx(currency, p2sh, fundtx):
|
def find_secret_from_fundtx(currency, p2sh, fundtx):
|
||||||
print("Fund tx in protocol.py", fundtx)
|
print("Fund tx in protocol.py", fundtx)
|
||||||
|
@ -36,6 +37,16 @@ def check_fund_status(currency, address):
|
||||||
print("Checking funds in Zcash p2sh")
|
print("Checking funds in Zcash p2sh")
|
||||||
return zcashRPC.get_fund_status(address)
|
return zcashRPC.get_fund_status(address)
|
||||||
|
|
||||||
|
# TODO: function to calculate appropriate locktimes between chains
|
||||||
|
# def verify_p2sh(trade):
|
||||||
|
# htlc = create_htlc(trade.buy.currency, trade.buy.fulfiller, trade.buy.initiator, trade.commitment, trade.buy.locktime)
|
||||||
|
# buyer_p2sh = htlc['p2sh']
|
||||||
|
# print("Buyer p2sh:", buyer_p2sh)
|
||||||
|
# If the two p2sh match...
|
||||||
|
# if buyer_p2sh == trade.buy.p2sh:
|
||||||
|
# else:
|
||||||
|
# print("Compiled p2sh for htlc does not match what seller sent.")
|
||||||
|
|
||||||
def create_htlc(currency, funder, redeemer, commitment, locktime):
|
def create_htlc(currency, funder, redeemer, commitment, locktime):
|
||||||
print("Commitment in create_htlc", commitment)
|
print("Commitment in create_htlc", commitment)
|
||||||
if currency == 'bitcoin':
|
if currency == 'bitcoin':
|
||||||
|
@ -49,15 +60,8 @@ def fund_htlc(currency, p2sh, amount):
|
||||||
txid = bitcoinRPC.fund_htlc(p2sh, amount)
|
txid = bitcoinRPC.fund_htlc(p2sh, amount)
|
||||||
else:
|
else:
|
||||||
txid = zcashRPC.fund_htlc(p2sh, amount)
|
txid = zcashRPC.fund_htlc(p2sh, amount)
|
||||||
print("fund_htlc txid", txid )
|
print("Fund_htlc txid", txid )
|
||||||
return txid
|
return txid
|
||||||
#
|
|
||||||
# def fund_buy_contract(trade):
|
|
||||||
# buy = trade.buy
|
|
||||||
# txid = fund_htlc(buy.currency, buy.p2sh, buy.amount)
|
|
||||||
# setattr(trade.buy, 'fund_tx', txid)
|
|
||||||
# save(trade)
|
|
||||||
# return txid
|
|
||||||
|
|
||||||
def fund_contract(contract):
|
def fund_contract(contract):
|
||||||
txid = fund_htlc(contract.currency, contract.p2sh, contract.amount)
|
txid = fund_htlc(contract.currency, contract.p2sh, contract.amount)
|
||||||
|
|
|
@ -108,3 +108,10 @@ def save(trade):
|
||||||
'commitment': trade.commitment
|
'commitment': trade.commitment
|
||||||
}
|
}
|
||||||
save_trade(trade)
|
save_trade(trade)
|
||||||
|
|
||||||
|
# Remove tmp files when trade is complete
|
||||||
|
def cleanup(tradeid):
|
||||||
|
try:
|
||||||
|
os.remove(os.path.join(ROOT_DIR, '.tmp/{0}'.format(tradeid)))
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
Loading…
Reference in New Issue