Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not use address indexers of mempool #9

Merged
merged 8 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 82 additions & 12 deletions boltz_client/boltz.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
""" boltz_client main module """

import asyncio
from dataclasses import dataclass
from typing import Optional

import httpx

from .helpers import req_wrap
from .mempool import MempoolClient
from .onchain import create_claim_tx, create_key_pair, create_preimage, create_refund_tx
from .onchain import (
create_claim_tx,
create_key_pair,
create_preimage,
create_refund_tx,
get_txid,
validate_address,
)


class BoltzLimitException(Exception):
Expand All @@ -18,6 +26,10 @@ class BoltzApiException(Exception):
pass


class BoltzAddressValidationException(Exception):
pass


class BoltzNotFoundException(Exception):
pass

Expand All @@ -28,12 +40,24 @@ def __init__(self, message: str, status: str):
self.status = status


class BoltzSwapTransactionException(Exception):
def __init__(self, message: str):
self.message = message


@dataclass
class BoltzSwapTransactionResponse:
transactionHex: Optional[str] = None
timeoutBlockHeight: Optional[str] = None
failureReason: Optional[str] = None


@dataclass
class BoltzSwapStatusResponse:
status: str
failureReason: Optional[str] = None
zeroConfRejected: Optional[str] = None
transaction: Optional[str] = None
transaction: Optional[dict] = None


@dataclass
Expand Down Expand Up @@ -61,7 +85,7 @@ class BoltzReverseSwapResponse:
class BoltzConfig:
network: str = "main"
api_url: str = "https://boltz.exchange/api"
mempool_url: str = "https://mempool.space/api"
mempool_url: str = "https://mempool.space/api/v1"
mempool_ws_url: str = "wss://mempool.space/api/v1/ws"
referral_id: str = "dni"

Expand Down Expand Up @@ -95,8 +119,8 @@ def check_version(self):
)

def get_fee_estimation(self, feerate: Optional[int]) -> int:
# TODO: hardcoded maximum tx size, in the future we try to get the size of the tx via embit
# we need a function like Transaction.vsize()
# TODO: hardcoded maximum tx size, in the future we try to get the size of the
# tx via embit we need a function like Transaction.vsize()
tx_size_vbyte = 200
mempool_fees = feerate if feerate else self.mempool.get_fees()
return mempool_fees * tx_size_vbyte
Expand All @@ -117,8 +141,10 @@ def set_limits(self) -> None:
def check_limits(self, amount: int) -> None:
valid = self.limit_minimal <= amount <= self.limit_maximal
if not valid:
msg = f"Boltz - swap not in boltz limits, amount: {amount}, min: {self.limit_minimal}, max: {self.limit_maximal}"
raise BoltzLimitException(msg)
raise BoltzLimitException(
f"Boltz - swap not in boltz limits, amount: {amount}, "
f"min: {self.limit_minimal}, max: {self.limit_maximal}"
)

def swap_status(self, boltz_id: str) -> BoltzSwapStatusResponse:
data = self.request(
Expand All @@ -134,8 +160,50 @@ def swap_status(self, boltz_id: str) -> BoltzSwapStatusResponse:

return status

def swap_transaction(self, boltz_id: str) -> BoltzSwapTransactionResponse:
data = self.request(
"post",
f"{self._cfg.api_url}/getswaptransaction",
json={"id": boltz_id},
headers={"Content-Type": "application/json"},
)
res = BoltzSwapTransactionResponse(**data)

if res.failureReason:
raise BoltzSwapTransactionException(res.failureReason)

return res

async def wait_for_txid(self, boltz_id: str) -> str:
while True:
try:
swap_transaction = self.swap_transaction(boltz_id)
if swap_transaction.transactionHex:
return get_txid(swap_transaction.transactionHex)
raise ValueError("transactionHex is empty")
except (ValueError, BoltzApiException, BoltzSwapTransactionException):
await asyncio.sleep(5)

async def wait_for_txid_on_status(self, boltz_id: str) -> str:
while True:
try:
status = self.swap_status(boltz_id)
assert status.transaction
txid = status.transaction.get("id")
assert txid
return txid
except (BoltzApiException, BoltzSwapStatusException, AssertionError):
await asyncio.sleep(5)

def validate_address(self, address: str):
try:
validate_address(address, self._cfg.network)
except ValueError as exc:
raise BoltzAddressValidationException(exc) from exc

async def claim_reverse_swap(
self,
boltz_id: str,
lockup_address: str,
receive_address: str,
privkey_wif: str,
Expand All @@ -144,11 +212,11 @@ async def claim_reverse_swap(
zeroconf: bool = False,
feerate: Optional[int] = None,
):
lockup_tx = await self.mempool.get_tx_from_address(lockup_address)

self.validate_address(receive_address)
lockup_txid = await self.wait_for_txid_on_status(boltz_id)
lockup_tx = await self.mempool.get_tx_from_txid(lockup_txid, lockup_address)
if not zeroconf and lockup_tx.status != "confirmed":
await self.mempool.wait_for_tx_confirmed(lockup_tx.txid)

txid, transaction = create_claim_tx(
lockup_tx=lockup_tx,
receive_address=receive_address,
Expand All @@ -157,21 +225,23 @@ async def claim_reverse_swap(
preimage_hex=preimage_hex,
fees=self.get_fee_estimation(feerate),
)

self.mempool.send_onchain_tx(transaction)
return txid

async def refund_swap(
self,
boltz_id: str,
privkey_wif: str,
lockup_address: str,
receive_address: str,
redeem_script_hex: str,
timeout_block_height: int,
feerate: Optional[int] = None,
) -> str:
self.validate_address(receive_address)
self.mempool.check_block_height(timeout_block_height)
lockup_tx = await self.mempool.get_tx_from_address(lockup_address)
lockup_txid = await self.wait_for_txid(boltz_id)
lockup_tx = await self.mempool.get_tx_from_txid(lockup_txid, lockup_address)
txid, transaction = create_refund_tx(
lockup_tx=lockup_tx,
privkey_wif=privkey_wif,
Expand Down
61 changes: 36 additions & 25 deletions boltz_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

config = BoltzConfig()

# use for manual testing
# config = BoltzConfig(
# network="regtest",
# api_url="http://localhost:9001",
# mempool_url="http://localhost:8999/api/v1",
# mempool_ws_url="ws://localhost:8999/api/v1/ws",
# )


@click.group()
def command_group():
Expand All @@ -36,9 +44,7 @@ def create_swap(payment_request):
click.echo()
click.echo(f"boltz_id: {swap.id}")
click.echo()
click.echo(
f"mempool.space url: {config.mempool_url.replace('/api', '')}/address/{swap.address}"
)
click.echo(f"mempool.space url: {config.mempool_url}/address/{swap.address}")
click.echo()
click.echo(f"refund privkey in wif: {refund_privkey_wif}")
click.echo(f"redeem_script_hex: {swap.redeemScript}")
Expand All @@ -52,18 +58,20 @@ def create_swap(payment_request):
click.echo("run this command if you need to refund:")
click.echo("CHANGE YOUR_RECEIVEADDRESS to your onchain address!!!")
click.echo(
f"boltz refund-swap {refund_privkey_wif} {swap.address} YOUR_RECEIVEADDRESS "
f"boltz refund-swap {swap.id} {refund_privkey_wif} {swap.address} YOUR_RECEIVEADDRESS "
f"{swap.redeemScript} {swap.timeoutBlockHeight}"
)


@click.command()
@click.argument("boltz_id", type=str)
@click.argument("privkey_wif", type=str)
@click.argument("lockup_address", type=str)
@click.argument("receive_address", type=str)
@click.argument("redeem_script_hex", type=str)
@click.argument("timeout_block_height", type=int)
def refund_swap(
boltz_id: str,
privkey_wif: str,
lockup_address: str,
receive_address: str,
Expand All @@ -76,11 +84,12 @@ def refund_swap(
client = BoltzClient(config)
txid = asyncio.run(
client.refund_swap(
privkey_wif,
lockup_address,
receive_address,
redeem_script_hex,
timeout_block_height,
boltz_id=boltz_id,
privkey_wif=privkey_wif,
lockup_address=lockup_address,
receive_address=receive_address,
redeem_script_hex=redeem_script_hex,
timeout_block_height=timeout_block_height,
)
)
click.echo("swap refunded!")
Expand All @@ -104,9 +113,7 @@ def create_reverse_swap(sats: int):
click.echo(f"redeem_script_hex: {swap.redeemScript}")
click.echo()
click.echo(f"boltz_id: {swap.id}")
click.echo(
f"mempool.space url: {config.mempool_url.replace('/api', '')}/address/{swap.lockupAddress}"
)
click.echo(f"mempool.space url: {config.mempool_url}/address/{swap.lockupAddress}")
click.echo()
click.echo("invoice:")
click.echo(swap.invoice)
Expand All @@ -115,7 +122,7 @@ def create_reverse_swap(sats: int):
click.echo("run this command after you see the lockup transaction:")
click.echo("CHANGE YOUR_RECEIVEADDRESS to your onchain address!!!")
click.echo(
f"boltz claim-reverse-swap {swap.lockupAddress} YOUR_RECEIVEADDRESS "
f"boltz claim-reverse-swap {swap.id} {swap.lockupAddress} YOUR_RECEIVEADDRESS "
f"{claim_privkey_wif} {preimage_hex} {swap.redeemScript}"
)

Expand Down Expand Up @@ -153,12 +160,13 @@ def create_reverse_swap_and_claim(

txid = asyncio.run(
client.claim_reverse_swap(
swap.lockupAddress,
receive_address,
claim_privkey_wif,
preimage_hex,
swap.redeemScript,
zeroconf,
boltz_id=swap.id,
lockup_address=swap.lockupAddress,
receive_address=receive_address,
privkey_wif=claim_privkey_wif,
preimage_hex=preimage_hex,
redeem_script_hex=swap.redeemScript,
zeroconf=zeroconf,
)
)

Expand All @@ -167,13 +175,15 @@ def create_reverse_swap_and_claim(


@click.command()
@click.argument("boltz_id", type=str)
@click.argument("lockup_address", type=str)
@click.argument("receive_address", type=str)
@click.argument("privkey_wif", type=str)
@click.argument("preimage_hex", type=str)
@click.argument("redeem_script_hex", type=str)
@click.argument("zeroconf", type=bool, default=False)
def claim_reverse_swap(
boltz_id: str,
lockup_address: str,
receive_address: str,
privkey_wif: str,
Expand All @@ -188,12 +198,13 @@ def claim_reverse_swap(

txid = asyncio.run(
client.claim_reverse_swap(
lockup_address,
receive_address,
privkey_wif,
preimage_hex,
redeem_script_hex,
zeroconf,
boltz_id=boltz_id,
lockup_address=lockup_address,
receive_address=receive_address,
privkey_wif=privkey_wif,
preimage_hex=preimage_hex,
redeem_script_hex=redeem_script_hex,
zeroconf=zeroconf,
)
)

Expand Down
Loading