Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Add subscribe_to_payment_paths #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions xrpl_trading_bot/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from xrpl_trading_bot.clients.main import xrp_request_async
from xrpl_trading_bot.clients.methods import (
build_path_finds,
get_gateway_fees,
subscribe_to_account_balances,
subscribe_to_order_books,
subscribe_to_payment_paths,
)
from xrpl_trading_bot.clients.websocket_uri import FullHistoryNodes, NonFullHistoryNodes

__all__ = [
"build_path_finds",
"get_gateway_fees",
"subscribe_to_account_balances",
"subscribe_to_order_books",
"subscribe_to_payment_paths",
"xrp_request_async",
"FullHistoryNodes",
"NonFullHistoryNodes",
Expand Down
1 change: 0 additions & 1 deletion xrpl_trading_bot/clients/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ async def xrp_request_async(
async with AsyncWebsocketClient(url=uri) as client:
requests_for_gather = [client.request(request) for request in requests]
responses = await gather(*requests_for_gather)

return list(responses)
75 changes: 72 additions & 3 deletions xrpl_trading_bot/clients/methods.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from asyncio import run
from decimal import Decimal
from typing import Dict, List, cast
from typing import Dict, Generator, List, cast

from websockets.exceptions import ConnectionClosedError
from xrpl.clients import WebsocketClient
from xrpl.models import AccountInfo, AccountLines, IssuedCurrency, Response, Subscribe
from xrpl.models.requests.subscribe import SubscribeBook
from xrpl.models import (
AccountInfo,
AccountLines,
IssuedCurrency,
IssuedCurrencyAmount,
PathFind,
PathFindSubcommand,
Response,
Subscribe,
SubscribeBook
)
from xrpl.utils import drops_to_xrp

from xrpl_trading_bot.clients.main import xrp_request_async
Expand Down Expand Up @@ -177,6 +186,66 @@ def subscribe_to_order_books(
return subscribe_books


def subscribe_to_payment_paths(path_finds: List[PathFind], all_payment_paths):
with WebsocketClient(FullHistoryNodes.XRPLF) as client:
for path_find in path_finds:
client.send(path_find)
for message in client:
result = message.get("result")
if result is not None:
destination_amount = result["destination_amount"]
alternatives = result["alternatives"]
else:
destination_amount = message["destination_amount"]
alternatives = message["alternatives"]
currency_id = (
f"{destination_amount['currency']}.{destination_amount['issuer']}"
) if isinstance(destination_amount, dict) else "XRP"
all_payment_paths[currency_id] = {
"value": destination_amount["value"] if (
isinstance(destination_amount, dict)
) else destination_amount,
"alternatives": alternatives,
}


def _chunk_path_finds(
path_finds: List[PathFind],
) -> Generator[List[PathFind], None, None]:
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(path_finds), 5):
yield path_finds[i : i + 5]


def build_path_finds(wallet: XRPWallet) -> List[List[PathFind]]:
path_finds = [
PathFind(
subcommand=PathFindSubcommand.CREATE,
source_account=wallet.classic_address,
destination_account=wallet.classic_address,
destination_amount="-1",
),
]
tokens = [
currency for currency in wallet.balances.keys() if currency != "XRP"
]
for token in tokens:
currency, issuer = token.split(".")
path_finds.append(
PathFind(
subcommand=PathFindSubcommand.CREATE,
source_account=wallet.classic_address,
destination_account=wallet.classic_address,
destination_amount=IssuedCurrencyAmount(
currency=currency,
issuer=issuer,
value=-1,
),
)
)
return [list(pf) for pf in _chunk_path_finds(path_finds)]


def get_gateway_fees(wallet: XRPWallet) -> Dict[str, Decimal]:
balances = wallet.balances
currencies = balances.keys()
Expand Down
7 changes: 6 additions & 1 deletion xrpl_trading_bot/globals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from xrpl_trading_bot.globals.constants import WALLET
from xrpl_trading_bot.globals.variables import all_order_books, gateway_fees
from xrpl_trading_bot.globals.variables import (
all_order_books,
gateway_fees,
all_payment_paths,
)

__all__ = [
"all_order_books",
"all_payment_paths",
"gateway_fees",
"WALLET",
]
1 change: 1 addition & 0 deletions xrpl_trading_bot/globals/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

all_order_books = OrderBooks()
gateway_fees: Dict[str, Decimal] = {}
all_payment_paths = {}
23 changes: 21 additions & 2 deletions xrpl_trading_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@
subscribe_to_account_balances,
subscribe_to_order_books,
)
from xrpl_trading_bot.clients.methods import get_gateway_fees
from xrpl_trading_bot.globals import WALLET, all_order_books, gateway_fees
from xrpl_trading_bot.clients.methods import (
build_path_finds,
get_gateway_fees,
subscribe_to_payment_paths,
)
from xrpl_trading_bot.globals import (
WALLET,
all_order_books,
gateway_fees,
all_payment_paths,
)
from xrpl_trading_bot.order_books import build_subscription_books

if __name__ == "__main__":
Expand All @@ -21,6 +30,14 @@
)
balances_subscribtion.start()
sleep(5)
path_finds = build_path_finds(wallet=WALLET)

paths_threads = [
Thread(target=subscribe_to_payment_paths, args=(chunk, all_payment_paths, ))
for chunk in path_finds
]
for thread in paths_threads:
thread.start()
gateway_fees.update(get_gateway_fees(wallet=WALLET))
subscribe_books = build_subscription_books(wallet=WALLET)
subscribe_book_threads: List[Thread] = [
Expand All @@ -38,5 +55,7 @@
if num % 5 == 0:
sleep(10)
balances_subscribtion.join()
for thread in paths_threads:
thread.join()
for thread in subscribe_book_threads:
thread.join()
32 changes: 32 additions & 0 deletions xrpl_trading_bot/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from threading import Thread
from xrpl.models import IssuedCurrencyAmount, PathFindSubcommand, PathFind
from xrpl.clients import WebsocketClient


def sub():
with WebsocketClient("wss://xrplcluster.com/") as client:
req = PathFind(
subcommand=PathFindSubcommand.CREATE,
source_account="rPu2feBaViWGmWJhvaF5yLocTVD8FUxd2A",
destination_account="rPu2feBaViWGmWJhvaF5yLocTVD8FUxd2A",
destination_amount="-1"
)
req2 = PathFind(
subcommand=PathFindSubcommand.CREATE,
source_account="rPu2feBaViWGmWJhvaF5yLocTVD8FUxd2A",
destination_account="rPu2feBaViWGmWJhvaF5yLocTVD8FUxd2A",
destination_amount=IssuedCurrencyAmount(
currency="USD",
issuer="rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
value=-1
)
)
client.send(req)
client.send(req2)
for message in client:
print(message)


t = Thread(target=sub)
t.start()
t.join()
4 changes: 2 additions & 2 deletions xrpl_trading_bot/txn_parser/utils/order_book_changes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ def _derive_quality(

quality = Decimal(taker_gets_value) / Decimal(taker_pays_value)
if possible_currency_pair != pair:
return _format_quality("{:.12}".format(1 / quality))
return _format_quality("{:.14}".format(1 / quality))

return _format_quality("{:.12}".format(quality))
return _format_quality("{:.14}".format(quality))


def _derive_unfunded_amounts(
Expand Down