Skip to content

Commit

Permalink
Async in nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Aug 7, 2024
1 parent dfe0f26 commit 7cba270
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions api/nostr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid

from asgiref.sync import sync_to_async
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner, Kind, Tag
from api.models import Order
from decouple import config

Expand All @@ -29,45 +29,55 @@ async def send_order_event(self, order):
await client.connect()

robot_name = await self.get_robot_name(order)

event = EventBuilder(38383, "", self.generate_tags(order, robot_name)).to_event(
keys
currency = await self.get_robot_currency(order)

event = (
EventBuilder(
Kind(38383),
"",
Tag.parse(self.generate_tags(order, robot_name, currency)),
)
.custom_created_at(order.created_at.timestamp())
.to_event(keys)
)
event.custom_created_at(order.created_at.timestamp())
output = await client.send_event(event)
print(f"Nostr event sent: {output}")

@sync_to_async
def get_robot_name(self, order):
return order.maker.username

def generate_tags(self, order, robot_name):
@sync_to_async
def get_robot_currency(self, order):
return order.currency

def generate_tags(self, order, robot_name, currency):
hashed_id = hashlib.md5(
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8")
).hexdigest()

tags = [
["d", uuid.UUID(hashed_id)],
["d", str(uuid.UUID(hashed_id))],
["name", robot_name],
["k", order.type.lower()],
["f", order.currency],
["k", "sell" if order.type == Order.Types.SELL else "buy"],
["f", currency],
["s", self.get_status_tag(order)],
["amt", "0"],
["fa", order.amount],
["pm", order.payment_method.split(" ")],
["premium", order.premium_percentile * 100],
["fa", str(order.amount)],
["pm"] + order.payment_method.split(" "),
["premium", str(order.premium)],
[
"source",
f"{config("HOST_NAME")}/{config("COORDINATOR_ALIAS")}/order/{order.id}",
f"http://{config("HOST_NAME")}/{config("COORDINATOR_ALIAS")}/order/{order.id}",
],
["expiration", order.expires_at.timestamp()],
["y", "robosats", config("COORDINATOR_ALIAS", cast=str)],
["n", order.network],
["layer", self.get_layer_tag(order)],
["bond", order.bond],
["n", str(config("NETWORK"))],
self.get_layer_tag(order),
["bond", str(order.bond_size)],
["z", "order"],
]

print(tags)
if order.latitude and order.longitude:
tags.extend([["g", pygeohash.encode(order.latitude, order.longitude)]])

Expand All @@ -81,6 +91,6 @@ def get_status_tag(self, order):

def get_layer_tag(self, order):
if order.type == Order.Types.SELL:
return ["onchain", "lightning"]
return ["layer", "onchain", "lightning"]
else:
return ["lightning"]
return ["layer", "lightning"]

0 comments on commit 7cba270

Please sign in to comment.