-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1362 from RoboSats/use-nostr-as-cache-system
Use nostr as cache system
- Loading branch information
Showing
19 changed files
with
435 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pygeohash | ||
import hashlib | ||
import uuid | ||
|
||
from asgiref.sync import sync_to_async | ||
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner, Kind, Tag | ||
from api.models import Order | ||
from decouple import config | ||
|
||
|
||
class Nostr: | ||
"""Simple nostr events manager to be used as a cache system for clients""" | ||
|
||
async def send_order_event(self, order): | ||
"""Creates the event and sends it to the coordinator relay""" | ||
|
||
if config("NOSTR_NSEC", cast=str, default="") == "": | ||
return | ||
|
||
print("Sending nostr event") | ||
|
||
# Initialize with coordinator Keys | ||
keys = Keys.parse(config("NOSTR_NSEC", cast=str)) | ||
signer = NostrSigner.keys(keys) | ||
client = Client(signer) | ||
|
||
# Add relays and connect | ||
await client.add_relays(["ws://localhost:7777"]) | ||
await client.connect() | ||
|
||
robot_name = await self.get_robot_name(order) | ||
currency = await self.get_robot_currency(order) | ||
|
||
event = EventBuilder( | ||
Kind(38383), "", self.generate_tags(order, robot_name, currency) | ||
).to_event(keys) | ||
await client.send_event(event) | ||
print(f"Nostr event sent: {event.as_json()}") | ||
|
||
@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( | ||
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8") | ||
).hexdigest() | ||
|
||
tags = [ | ||
Tag.parse(["d", str(uuid.UUID(hashed_id))]), | ||
Tag.parse(["name", robot_name]), | ||
Tag.parse(["k", "sell" if order.type == Order.Types.SELL else "buy"]), | ||
Tag.parse(["f", currency]), | ||
Tag.parse(["s", self.get_status_tag(order)]), | ||
Tag.parse(["amt", "0"]), | ||
Tag.parse( | ||
["fa"] + [str(order.amount)] | ||
if not order.has_range | ||
else [str(order.min_amount), str(order.max_amount)] | ||
), | ||
Tag.parse(["pm"] + order.payment_method.split(" ")), | ||
Tag.parse(["premium", str(order.premium)]), | ||
Tag.parse( | ||
[ | ||
"source", | ||
f"http://{config("HOST_NAME")}/order/{config("COORDINATOR_ALIAS", cast=str).lower()}/{order.id}", | ||
] | ||
), | ||
Tag.parse(["expiration", str(int(order.expires_at.timestamp()))]), | ||
Tag.parse(["y", "robosats", config("COORDINATOR_ALIAS", cast=str).lower()]), | ||
Tag.parse(["n", str(config("NETWORK"))]), | ||
Tag.parse(["layer"] + self.get_layer_tag(order)), | ||
Tag.parse(["bond", str(order.bond_size)]), | ||
Tag.parse(["z", "order"]), | ||
] | ||
|
||
if order.latitude and order.longitude: | ||
tags.extend( | ||
[Tag.parse(["g", pygeohash.encode(order.latitude, order.longitude)])] | ||
) | ||
|
||
return tags | ||
|
||
def get_status_tag(self, order): | ||
if order.status == Order.Status.PUB: | ||
return "pending" | ||
else: | ||
return "success" | ||
|
||
def get_layer_tag(self, order): | ||
if order.type == Order.Types.SELL: | ||
return ["onchain", "lightning"] | ||
else: | ||
return ["lightning"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
FROM ubuntu:jammy | ||
ENV TZ=Europe/London | ||
|
||
RUN apt update && apt install -y --no-install-recommends \ | ||
git g++ make pkg-config libtool ca-certificates \ | ||
libssl-dev zlib1g-dev liblmdb-dev libflatbuffers-dev \ | ||
libsecp256k1-dev libzstd-dev | ||
|
||
# setup app | ||
RUN git clone https://github.com/KoalaSat/strfry /app | ||
|
||
WORKDIR /app | ||
|
||
RUN git submodule update --init | ||
RUN make setup-golpe | ||
RUN make clean | ||
RUN make -j4 | ||
|
||
RUN apt update && apt install -y --no-install-recommends \ | ||
liblmdb0 libflatbuffers1 libsecp256k1-0 libb2-1 libzstd1 torsocks cron\ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
RUN echo "TorAddress 127.0.0.1" >> /etc/tor/torsocks.conf | ||
RUN echo "TorPort 9050" >> /etc/tor/torsocks.conf | ||
|
||
# Setting up crontab | ||
COPY crontab /etc/cron.d/crontab | ||
RUN chmod 0644 /etc/cron.d/crontab | ||
RUN crontab /etc/cron.d/crontab | ||
|
||
# Setting up entrypoints | ||
COPY sync.sh /etc/strfry/sync.sh | ||
COPY entrypoint.sh /etc/strfry/entrypoint.sh | ||
|
||
RUN chmod +x /etc/strfry/entrypoint.sh | ||
RUN chmod +x /etc/strfry/sync.sh | ||
|
||
#Setting up logs | ||
RUN touch /var/log/cron.log && chmod 0644 /var/log/cron.log | ||
|
||
ENTRYPOINT ["/etc/strfry/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Edit this file to introduce tasks to be run by cron. | ||
# | ||
# Each task to run has to be defined through a single line | ||
# indicating with different fields when the task will be run | ||
# and what command to run for the task | ||
# | ||
# To define the time you can provide concrete values for | ||
# minute (m), hour (h), day of month (dom), month (mon), | ||
# and day of week (dow) or use '*' in these fields (for 'any'). | ||
# | ||
# Notice that tasks will be started based on the cron's system | ||
# daemon's notion of time and timezones. | ||
# | ||
# Output of the crontab jobs (including errors) is sent through | ||
# email to the user the crontab file belongs to (unless redirected). | ||
# | ||
# For example, you can run a backup of all your user accounts | ||
# at 5 a.m every week with: | ||
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ | ||
# | ||
# For more information see the manual pages of crontab(5) and cron(8) | ||
# | ||
# m h dom mon dow command | ||
*/1 * * * * torsocks /etc/strfry/sync.sh >> /var/log/cron.log 2>&1 |
Oops, something went wrong.