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 df9ae19
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 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 @@ -30,9 +30,9 @@ async def send_order_event(self, order):

robot_name = await self.get_robot_name(order)

event = EventBuilder(38383, "", self.generate_tags(order, robot_name)).to_event(
keys
)
event = EventBuilder(
Kind(38383), "", Tag.parse(self.generate_tags(order, robot_name))
).to_event(keys)
event.custom_created_at(order.created_at.timestamp())
output = await client.send_event(event)
print(f"Nostr event sent: {output}")
Expand All @@ -41,6 +41,10 @@ async def send_order_event(self, order):
def get_robot_name(self, order):
return order.maker.username

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

def generate_tags(self, order, robot_name):
hashed_id = hashlib.md5(
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8")
Expand All @@ -49,22 +53,22 @@ def generate_tags(self, order, robot_name):
tags = [
["d", 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", self.get_robot_currency(order)],
["s", self.get_status_tag(order)],
["amt", "0"],
["fa", order.amount],
["pm", order.payment_method.split(" ")],
["premium", order.premium_percentile * 100],
["premium", order.premium * 100],
[
"source",
f"{config("HOST_NAME")}/{config("COORDINATOR_ALIAS")}/order/{order.id}",
],
["expiration", order.expires_at.timestamp()],
["y", "robosats", config("COORDINATOR_ALIAS", cast=str)],
["n", order.network],
["n", str(config("NETWORK"))],
["layer", self.get_layer_tag(order)],
["bond", order.bond],
["bond", order.bond_size],
["z", "order"],
]

Expand Down

0 comments on commit df9ae19

Please sign in to comment.