mirror of
https://github.com/RoboSats/robosats.git
synced 2025-01-18 12:11:35 +00:00
Async to sync
This commit is contained in:
parent
19fc1f08a8
commit
af3a03c891
54
api/nostr.py
54
api/nostr.py
@ -1,7 +1,9 @@
|
|||||||
import pygeohash
|
import pygeohash
|
||||||
import hashlib
|
import hashlib
|
||||||
import uuid
|
import uuid
|
||||||
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner
|
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner, Kind, Tag
|
||||||
from api.models import Order
|
from api.models import Order
|
||||||
from decouple import config
|
from decouple import config
|
||||||
|
|
||||||
@ -26,38 +28,56 @@ class Nostr:
|
|||||||
await client.add_relays(["ws://localhost:7777"])
|
await client.add_relays(["ws://localhost:7777"])
|
||||||
await client.connect()
|
await client.connect()
|
||||||
|
|
||||||
event = EventBuilder(38383, "", self.generate_tags(order)).to_event(keys)
|
robot_name = await self.get_robot_name(order)
|
||||||
event.custom_created_at(order.created_at.timestamp())
|
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)
|
||||||
|
)
|
||||||
output = await client.send_event(event)
|
output = await client.send_event(event)
|
||||||
print(f"Nostr event sent: {output}")
|
print(f"Nostr event sent: {output}")
|
||||||
|
|
||||||
def generate_tags(self, order):
|
@sync_to_async
|
||||||
|
def get_robot_name(self, order):
|
||||||
|
return order.maker.username
|
||||||
|
|
||||||
|
@sync_to_async
|
||||||
|
def get_robot_currency(self, order):
|
||||||
|
return str(order.currency)
|
||||||
|
|
||||||
|
def generate_tags(self, order, robot_name, currency):
|
||||||
hashed_id = hashlib.md5(
|
hashed_id = hashlib.md5(
|
||||||
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8")
|
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8")
|
||||||
).hexdigest()
|
).hexdigest()
|
||||||
|
|
||||||
tags = [
|
tags = [
|
||||||
["d", uuid.UUID(hashed_id)],
|
["d", str(uuid.UUID(hashed_id))],
|
||||||
["name", order.maker.robot_name],
|
["name", robot_name],
|
||||||
["k", order.type.lower()],
|
["k", "sell" if order.type == Order.Types.SELL else "buy"],
|
||||||
["f", order.currency],
|
["f", currency],
|
||||||
["s", self.get_status_tag(order)],
|
["s", self.get_status_tag(order)],
|
||||||
["amt", "0"],
|
["amt", "0"],
|
||||||
["fa", order.amount],
|
["fa", str(order.amount)],
|
||||||
["pm", order.payment_method.split(" ")],
|
["pm"] + order.payment_method.split(" "),
|
||||||
["premium", order.premium_percentile * 100],
|
["premium", str(order.premium)],
|
||||||
[
|
[
|
||||||
"source",
|
"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()],
|
["expiration", int(order.expires_at.timestamp())],
|
||||||
["y", "robosats", config("COORDINATOR_ALIAS", cast=str)],
|
["y", "robosats", config("COORDINATOR_ALIAS", cast=str)],
|
||||||
["n", order.network],
|
["n", str(config("NETWORK"))],
|
||||||
["layer", self.get_layer_tag(order)],
|
["layer"] + self.get_layer_tag(order),
|
||||||
["bond", order.bond],
|
["bond", str(order.bond_size)],
|
||||||
["z", "order"],
|
["z", "order"],
|
||||||
]
|
]
|
||||||
|
print(tags)
|
||||||
if order.latitude and order.longitude:
|
if order.latitude and order.longitude:
|
||||||
tags.extend([["g", pygeohash.encode(order.latitude, order.longitude)]])
|
tags.extend([["g", pygeohash.encode(order.latitude, order.longitude)]])
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import asyncio
|
from asgiref.sync import async_to_sync
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
from celery.exceptions import SoftTimeLimitExceeded
|
from celery.exceptions import SoftTimeLimitExceeded
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ def nostr_send_order_event(order_id=None):
|
|||||||
order = Order.objects.get(id=order_id)
|
order = Order.objects.get(id=order_id)
|
||||||
|
|
||||||
nostr = Nostr()
|
nostr = Nostr()
|
||||||
asyncio.run(nostr.send_order_event(order))
|
async_to_sync(nostr.send_order_event)(order)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -30,3 +30,4 @@ django-cors-headers==4.4.0
|
|||||||
base91==1.0.1
|
base91==1.0.1
|
||||||
nostr-sdk==0.32.2
|
nostr-sdk==0.32.2
|
||||||
pygeohash==1.2.0
|
pygeohash==1.2.0
|
||||||
|
asgiref == 3.8.1
|
||||||
|
@ -5,7 +5,7 @@ from django.urls import reverse
|
|||||||
from api.management.commands.clean_orders import Command as CleanOrders
|
from api.management.commands.clean_orders import Command as CleanOrders
|
||||||
from api.management.commands.follow_invoices import Command as FollowInvoices
|
from api.management.commands.follow_invoices import Command as FollowInvoices
|
||||||
from api.models import Order
|
from api.models import Order
|
||||||
from api.tasks import follow_send_payment, send_notification, nostr_send_order_event
|
from api.tasks import follow_send_payment, send_notification
|
||||||
from tests.utils.node import (
|
from tests.utils.node import (
|
||||||
add_invoice,
|
add_invoice,
|
||||||
create_address,
|
create_address,
|
||||||
@ -156,7 +156,6 @@ class Trade:
|
|||||||
wait_nodes_sync()
|
wait_nodes_sync()
|
||||||
|
|
||||||
@patch("api.tasks.send_notification.delay", send_notification)
|
@patch("api.tasks.send_notification.delay", send_notification)
|
||||||
@patch("api.tasks.nostr_send_order_event.delay", nostr_send_order_event)
|
|
||||||
def publish_order(self):
|
def publish_order(self):
|
||||||
# Maker's first order fetch. Should trigger maker bond hold invoice generation.
|
# Maker's first order fetch. Should trigger maker bond hold invoice generation.
|
||||||
self.get_order()
|
self.get_order()
|
||||||
|
Loading…
Reference in New Issue
Block a user