Modify checktrade for tests
This commit is contained in:
parent
6bb6a9c756
commit
9bb7b7c0f5
|
@ -74,6 +74,8 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
|
||||||
def fund_htlc(p2sh, amount):
|
def fund_htlc(p2sh, amount):
|
||||||
send_amount = float(amount) * COIN
|
send_amount = float(amount) * COIN
|
||||||
fund_txid = bitcoind.sendtoaddress(p2sh, send_amount)
|
fund_txid = bitcoind.sendtoaddress(p2sh, send_amount)
|
||||||
|
# Import address at same time that you fund it
|
||||||
|
bitcoind.importaddress(p2sh, "", False)
|
||||||
txid = b2x(lx(b2x(fund_txid)))
|
txid = b2x(lx(b2x(fund_txid)))
|
||||||
return txid
|
return txid
|
||||||
|
|
||||||
|
|
63
xcat/cli.py
63
xcat/cli.py
|
@ -15,6 +15,7 @@ def save_state(trade, tradeid):
|
||||||
def checkSellStatus(tradeid):
|
def checkSellStatus(tradeid):
|
||||||
trade = db.get(tradeid)
|
trade = db.get(tradeid)
|
||||||
if trade.buy.get_status() == 'funded':
|
if trade.buy.get_status() == 'funded':
|
||||||
|
input("Authorize retrieve secret:")
|
||||||
secret = get_secret()
|
secret = get_secret()
|
||||||
print("SECRET found in checksellactions", secret)
|
print("SECRET found in checksellactions", secret)
|
||||||
trade = seller_redeem_p2sh(trade, secret)
|
trade = seller_redeem_p2sh(trade, secret)
|
||||||
|
@ -28,22 +29,24 @@ def checkSellStatus(tradeid):
|
||||||
# TODO: function to calculate appropriate locktimes between chains
|
# TODO: function to calculate appropriate locktimes between chains
|
||||||
def checkBuyStatus(tradeid):
|
def checkBuyStatus(tradeid):
|
||||||
trade = db.get(tradeid)
|
trade = db.get(tradeid)
|
||||||
if trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed':
|
if trade.sell.get_status() == 'redeemed' and trade.buy.get_status() == 'redeemed':
|
||||||
|
print("This trade is complete, both sides redeemed.")
|
||||||
|
elif trade.sell.get_status() == 'funded' and trade.buy.get_status() != 'redeemed':
|
||||||
print("One active trade available, fulfilling buyer contract...")
|
print("One active trade available, fulfilling buyer contract...")
|
||||||
# they should calculate redeemScript for themselves
|
# they should calculate redeemScript for themselves
|
||||||
print("Trade commitment", trade.commitment)
|
print("Trade commitment", trade.commitment)
|
||||||
# TODO: which block to start computation from?
|
# TODO: which block to start computation from?
|
||||||
htlc = create_htlc(trade.buy.currency, trade.buy.fulfiller, trade.buy.initiator, trade.commitment, trade.buy.locktime)
|
# htlc = create_htlc(trade.buy.currency, trade.buy.fulfiller, trade.buy.initiator, trade.commitment, trade.buy.locktime)
|
||||||
buyer_p2sh = htlc['p2sh']
|
# buyer_p2sh = htlc['p2sh']
|
||||||
print("Buyer p2sh:", buyer_p2sh)
|
# print("Buyer p2sh:", buyer_p2sh)
|
||||||
# If the two p2sh match...
|
# If the two p2sh match...
|
||||||
if buyer_p2sh == trade.buy.p2sh:
|
# if buyer_p2sh == trade.buy.p2sh:
|
||||||
fund_tx = fund_contract(trade.buy)
|
fund_tx = fund_contract(trade.buy)
|
||||||
trade.buy.fund_tx = fund_tx
|
trade.buy.fund_tx = fund_tx
|
||||||
print("trade buy with redeemscript?", trade.buy.__dict__)
|
print("trade buy with redeemscript?", trade.buy.__dict__)
|
||||||
save_state(trade, tradeid)
|
save_state(trade, tradeid)
|
||||||
else:
|
# else:
|
||||||
print("Compiled p2sh for htlc does not match what seller sent.")
|
# print("Compiled p2sh for htlc does not match what seller sent.")
|
||||||
elif trade.buy.get_status() == 'redeemed':
|
elif trade.buy.get_status() == 'redeemed':
|
||||||
# TODO: secret parsing
|
# TODO: secret parsing
|
||||||
# secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
|
# secret = parse_secret(trade.buy.currency, trade.buy.redeem_tx)
|
||||||
|
@ -56,7 +59,7 @@ def checkBuyStatus(tradeid):
|
||||||
# Import a trade in hex, and save to db
|
# Import a trade in hex, and save to db
|
||||||
def importtrade(hexstr):
|
def importtrade(hexstr):
|
||||||
trade = x2s(hexstr)
|
trade = x2s(hexstr)
|
||||||
trade = instantiateTrade(ast.literal_eval(trade))
|
trade = db.instantiate(trade)
|
||||||
save_state(trade)
|
save_state(trade)
|
||||||
|
|
||||||
# Export a trade by its tradeid
|
# Export a trade by its tradeid
|
||||||
|
@ -67,10 +70,27 @@ def exporttrade(tradeid):
|
||||||
print(trade)
|
print(trade)
|
||||||
print(hexstr)
|
print(hexstr)
|
||||||
|
|
||||||
def findtrade(key):
|
def findtrade(tradeid):
|
||||||
trade = db.get(key)
|
trade = db.get(tradeid)
|
||||||
print(trade)
|
print(trade)
|
||||||
|
|
||||||
|
def checktrade(tradeid):
|
||||||
|
print("In checktrade")
|
||||||
|
trade = db.get(tradeid)
|
||||||
|
if find_role(trade.sell) == 'test':
|
||||||
|
input("Is this a test? Both buyer and seller addresses are yours, press 'enter' to test.")
|
||||||
|
checkBuyStatus(tradeid)
|
||||||
|
checkSellStatus(tradeid)
|
||||||
|
checkBuyStatus(tradeid)
|
||||||
|
elif find_role(trade.sell) == 'initiator':
|
||||||
|
print("You are the seller in this trade.")
|
||||||
|
role = 'seller'
|
||||||
|
checkSellStatus(tradeid)
|
||||||
|
else:
|
||||||
|
print("You are the buyer in this trade.")
|
||||||
|
role = 'buyer'
|
||||||
|
checkBuyStatus(tradeid)
|
||||||
|
|
||||||
def newtrade(tradeid):
|
def newtrade(tradeid):
|
||||||
erase_trade()
|
erase_trade()
|
||||||
role = 'seller'
|
role = 'seller'
|
||||||
|
@ -80,18 +100,15 @@ def newtrade(tradeid):
|
||||||
# db.create(trade)
|
# db.create(trade)
|
||||||
save_state(trade, tradeid)
|
save_state(trade, tradeid)
|
||||||
|
|
||||||
def instantiateTrade(trade):
|
|
||||||
return Trade(buy=Contract(trade['buy']), sell=Contract(trade['sell']))
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
|
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
|
||||||
description=textwrap.dedent('''\
|
description=textwrap.dedent('''\
|
||||||
== Trades ==
|
== Trades ==
|
||||||
newtrade - create a new trade
|
newtrade "tradeid" - create a new trade
|
||||||
checktrades - check for actions to be taken on existing trades
|
checktrade "tradeid"- check for actions to be taken on an existing trade
|
||||||
importtrade "hexstr" - import an existing trade from a hex string
|
importtrade "hexstr" - import an existing trade from a hex string
|
||||||
exporttrade - export the data of an existing trade as a hex string. Takes the tradeid as an argument
|
exporttrade "tradeid" - export the data of an existing trade as a hex string. Takes the tradeid as an argument
|
||||||
findtrade - find a trade by the txid of the currency being traded out of
|
findtrade "tradeid" - find a trade by the tradeid
|
||||||
|
|
||||||
'''))
|
'''))
|
||||||
parser.add_argument("command", action="store", help="list commands")
|
parser.add_argument("command", action="store", help="list commands")
|
||||||
|
@ -113,15 +130,9 @@ def main():
|
||||||
print("Finding trade")
|
print("Finding trade")
|
||||||
key = args.argument[0]
|
key = args.argument[0]
|
||||||
findtrade(key)
|
findtrade(key)
|
||||||
elif command == 'checktrades':
|
elif command == 'checktrade':
|
||||||
trade = get_trade()
|
tradeid = args.argument[0]
|
||||||
trade = instantiateTrade(trade)
|
checktrade(tradeid)
|
||||||
if find_role(trade.sell) == 'initiator':
|
|
||||||
role = 'seller'
|
|
||||||
checkSellStatus(trade)
|
|
||||||
else:
|
|
||||||
role = 'buyer'
|
|
||||||
checkBuyStatus(trade)
|
|
||||||
elif command == 'newtrade':
|
elif command == 'newtrade':
|
||||||
print("in new trade")
|
print("in new trade")
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -32,19 +32,16 @@ def get(txid):
|
||||||
|
|
||||||
def instantiate(trade):
|
def instantiate(trade):
|
||||||
if type(trade) == str:
|
if type(trade) == str:
|
||||||
print(type(trade))
|
|
||||||
tradestr = json.loads(trade)
|
tradestr = json.loads(trade)
|
||||||
print(tradestr)
|
|
||||||
trade = Trade(buy=Contract(tradestr['buy']), sell=Contract(tradestr['sell']), commitment=tradestr['commitment'])
|
trade = Trade(buy=Contract(tradestr['buy']), sell=Contract(tradestr['sell']), commitment=tradestr['commitment'])
|
||||||
print(trade)
|
|
||||||
return trade
|
return trade
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# db.delete(b'hello')
|
# db.delete(b'hello')
|
||||||
testtrade = get('test')
|
# testtrade = get('test')
|
||||||
testtrade = instantiate(testtrade)
|
# testtrade = instantiate(testtrade)
|
||||||
print(testtrade)
|
# print(testtrade)
|
||||||
|
|
||||||
# hexstr = get(txid)
|
# hexstr = get(txid)
|
||||||
# print(x2s(hexstr))
|
# print(x2s(hexstr))
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import hashlib, json, random, binascii
|
import hashlib, json, random, binascii
|
||||||
import xcat.trades as trades
|
import xcat.trades as trades
|
||||||
|
import xcat.bitcoinRPC as bitcoinRPC
|
||||||
|
import xcat.zcashRPC as zcashRPC
|
||||||
|
|
||||||
############################################
|
############################################
|
||||||
########### Data conversion utils ##########
|
########### Data conversion utils ##########
|
||||||
|
@ -41,16 +43,18 @@ def jsonformat(trade):
|
||||||
############################################
|
############################################
|
||||||
def find_role(contract):
|
def find_role(contract):
|
||||||
# Obviously when regtest created both addrs on same machine, role is both.
|
# Obviously when regtest created both addrs on same machine, role is both.
|
||||||
if parse_addrs(contract.initiator):
|
if is_myaddr(contract.initiator) and is_myaddr(contract.fulfiller):
|
||||||
|
return 'test'
|
||||||
|
elif is_myaddr(contract.initiator):
|
||||||
return 'initiator'
|
return 'initiator'
|
||||||
else:
|
else:
|
||||||
return 'fulfiller'
|
return 'fulfiller'
|
||||||
|
|
||||||
def parse_addrs(address):
|
def is_myaddr(address):
|
||||||
if address[:1] == 'm':
|
if address[:1] == 'm':
|
||||||
status = bXcat.validateaddress(address)
|
status = bitcoinRPC.validateaddress(address)
|
||||||
else:
|
else:
|
||||||
status = zXcat.validateaddress(address)
|
status = zcashRPC.validateaddress(address)
|
||||||
status = status['ismine']
|
status = status['ismine']
|
||||||
print("Address {0} is mine: {1}".format(address, status))
|
print("Address {0} is mine: {1}".format(address, status))
|
||||||
return status
|
return status
|
||||||
|
|
|
@ -64,6 +64,8 @@ def hashtimelockcontract(funder, redeemer, commitment, locktime):
|
||||||
def fund_htlc(p2sh, amount):
|
def fund_htlc(p2sh, amount):
|
||||||
send_amount = float(amount)*COIN
|
send_amount = float(amount)*COIN
|
||||||
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
|
fund_txid = zcashd.sendtoaddress(p2sh, send_amount)
|
||||||
|
# Import addr at same time as you fund
|
||||||
|
zcashd.importaddress(p2sh, "", False)
|
||||||
txid = b2x(lx(b2x(fund_txid)))
|
txid = b2x(lx(b2x(fund_txid)))
|
||||||
return txid
|
return txid
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue