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 06822c0 commit cc83cfc
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions api/nostr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pygeohash
import hashlib
import uuid

from django.utils.decorators import sync_to_async
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner
from api.models import Order
from decouple import config
Expand All @@ -26,19 +28,27 @@ async def send_order_event(self, order):
await client.add_relays(["ws://localhost:7777"])
await client.connect()

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

event = EventBuilder(38383, "", 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}")

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

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

tags = [
["d", uuid.UUID(hashed_id)],
["name", order.maker.robot_name],
["name", robot_name],
["k", order.type.lower()],
["f", order.currency],
["s", self.get_status_tag(order)],
Expand Down

0 comments on commit cc83cfc

Please sign in to comment.