From 0e3979aa9d99cec7224a30da6467c1652d0c39fd Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:10:03 +0200 Subject: [PATCH] fixes --- nostr_dvm/dvm.py | 4 ++-- nostr_dvm/interfaces/dvmtaskinterface.py | 11 ++++++--- nostr_dvm/utils/nut_wallet_utils.py | 7 ++++-- setup.py | 2 +- tests/dalle.py | 18 +++++++-------- tests/discovery.py | 2 +- tests/test_dvm_client.py | 29 +++++++++++++++++++----- 7 files changed, 48 insertions(+), 25 deletions(-) diff --git a/nostr_dvm/dvm.py b/nostr_dvm/dvm.py index 65f51468..3201938c 100644 --- a/nostr_dvm/dvm.py +++ b/nostr_dvm/dvm.py @@ -33,8 +33,8 @@ class DVM: job_list: list jobs_on_hold_list: list - #def __init__(self, dvm_config, admin_config=None): - # asyncio.run(self.run_dvm(dvm_config, admin_config)) + def __init__(self, dvm_config, admin_config=None): + asyncio.run(self.run_dvm(dvm_config, admin_config)) async def run_dvm(self, dvm_config, admin_config): diff --git a/nostr_dvm/interfaces/dvmtaskinterface.py b/nostr_dvm/interfaces/dvmtaskinterface.py index 3820a9d5..78849716 100644 --- a/nostr_dvm/interfaces/dvmtaskinterface.py +++ b/nostr_dvm/interfaces/dvmtaskinterface.py @@ -11,6 +11,7 @@ from nostr_sdk import Keys, Kind, LogLevel from nostr_dvm.dvm import DVM from nostr_dvm.utils.admin_utils import AdminConfig +from nostr_dvm.utils.backend_utils import keep_alive from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config from nostr_dvm.utils.nip88_utils import NIP88Config from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag @@ -107,9 +108,13 @@ async def run_dvm(self, dvm_config, admin_config): print("Implement the run dvm function") pass - def run(self): - dvm = DVM() - asyncio.run(dvm.run_dvm(self.dvm_config, self.admin_config)) + def run(self, join=False): + #dvm = DVM(self.dvm_config, self.admin_config) + #asyncio.run(dvm.run_dvm(self.dvm_config, self.admin_config)) + nostr_dvm_thread = Thread(target=self.DVM, args=[self.dvm_config, self.admin_config], daemon=False) + nostr_dvm_thread.start() + if join: + nostr_dvm_thread.join() async def schedule(self, dvm_config): """schedule something, e.g. define some time to update or to post, does nothing by default""" diff --git a/nostr_dvm/utils/nut_wallet_utils.py b/nostr_dvm/utils/nut_wallet_utils.py index 870d2bab..ef77153f 100644 --- a/nostr_dvm/utils/nut_wallet_utils.py +++ b/nostr_dvm/utils/nut_wallet_utils.py @@ -518,7 +518,7 @@ async def add_proofs_to_wallet(self, nut_wallet, mint_url, new_proofs, marker, s async def handle_low_balance_on_mint(self, nut_wallet, mint_url, mint, amount, client, keys): mint_amount = amount - mint.available_balance() - reserved_fees = 3 + reserved_fees = 0 await self.mint_cashu(nut_wallet, mint_url, client, keys, mint_amount+reserved_fees) @@ -594,10 +594,13 @@ async def send_nut_zap(self, amount, comment, nut_wallet: NutWallet, zapped_even try: proofs, fees = await cashu_wallet.select_to_send(mint.proofs, amount) - _, send_proofs = await cashu_wallet.swap_to_send( + print(fees) + keep_proofs, send_proofs = await cashu_wallet.swap_to_send( proofs, amount, secret_lock=secret_lock, set_reserved=True ) + print(keep_proofs) + for proof in send_proofs: nut_proof = { 'id': proof.id, diff --git a/setup.py b/setup.py index 29b621d2..e9d9e176 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = '0.8.0' +VERSION = '0.8.1' DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines' LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information') diff --git a/tests/dalle.py b/tests/dalle.py index 0399e1c0..c5c6ba17 100644 --- a/tests/dalle.py +++ b/tests/dalle.py @@ -1,3 +1,4 @@ +import asyncio import json import os from pathlib import Path @@ -16,18 +17,15 @@ from nostr_dvm.utils.nostr_utils import check_and_set_private_key from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys, get_price_per_sat -rebroadcast_NIP89 = False # Announce NIP89 on startup Only do this if you know what you're doing. +rebroadcast_NIP89 = False # Announce NIP89 on startup Only do this if you know what you're doing. rebroadcast_NIP65_Relay_List = False update_profile = False -#use_logger = True +use_logger = True log_level = LogLevel.ERROR - -#if use_logger: -# init_logger(log_level) - - +if use_logger: + init_logger(log_level) def build_dalle(name, identifier): @@ -56,15 +54,15 @@ def build_dalle(name, identifier): nip89info["image"]) nip89config.CONTENT = json.dumps(nip89info) aconfig = AdminConfig() - aconfig.REBROADCAST_NIP89 = False # We add an optional AdminConfig for this one, and tell the dvm to rebroadcast its NIP89 + aconfig.REBROADCAST_NIP89 = False # We add an optional AdminConfig for this one, and tell the dvm to rebroadcast its NIP89 aconfig.LUD16 = dvm_config.LN_ADDRESS return ImageGenerationDALLE(name=name, dvm_config=dvm_config, nip89config=nip89config, admin_config=aconfig) + def playground(): if os.getenv("OPENAI_API_KEY") is not None and os.getenv("OPENAI_API_KEY") != "": dalle = build_dalle("Dall-E 3", "dalle3") - dalle.run() - + dalle.run(True) if __name__ == '__main__': diff --git a/tests/discovery.py b/tests/discovery.py index 8b11a2de..be397f1d 100644 --- a/tests/discovery.py +++ b/tests/discovery.py @@ -42,7 +42,7 @@ RECONCILE_DB_RELAY_LIST = [ "wss://relay.nostr.net", "wss://relay.nostr.bg", "wss://relay.damus.io", "wss://nostr.oxtr.dev"] RELAY_LIST = ["wss://relay.primal.net", - "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg", + "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.net" ] diff --git a/tests/test_dvm_client.py b/tests/test_dvm_client.py index 0f8fda7b..318440d1 100644 --- a/tests/test_dvm_client.py +++ b/tests/test_dvm_client.py @@ -4,6 +4,7 @@ from pathlib import Path from threading import Thread +from nostr_dvm.utils.nip65_utils import nip65_announce_relays from nostr_dvm.utils.nut_wallet_utils import NutZapWallet from nostr_dvm.utils.print import bcolors @@ -74,14 +75,12 @@ async def nostr_client_test_image(prompt): iTag = Tag.parse(["i", prompt, "text"]) outTag = Tag.parse(["output", "image/png;format=url"]) paramTag1 = Tag.parse(["param", "size", "1024x1024"]) - tTag = Tag.parse(["t", "bitcoin"]) bidTag = Tag.parse(['bid', str(1000 * 1000), str(1000 * 1000)]) - relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", - "wss://nostr-pub.wellorder.net"]) + relaysTag = Tag.parse(['relays', "wss://relay.primal.net", "wss://nostr.oxtr.dev"]) alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to generate an Image from a given Input"]) event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_IMAGE, str("Generate an Image."), - [iTag, outTag, tTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys) + [iTag, outTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys) signer = NostrSigner.keys(keys) client = Client(signer) @@ -89,6 +88,8 @@ async def nostr_client_test_image(prompt): await client.add_relay(relay) await client.connect() config = DVMConfig + #config.NIP89.PK = keys.secret_key().to_hex() + #await nip65_announce_relays(config, client=client) await send_event(event, client=client, dvm_config=config) return event.as_json() @@ -340,6 +341,17 @@ async def nostr_client(): nutzap_wallet = NutZapWallet() nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys) + #dangerous, dont use this, except your wallet is messed up. + delete = False + if delete: + for mint in nut_wallet.nutmints: + await nutzap_wallet.update_spend_mint_proof_event(nut_wallet, mint.proofs, mint.mint_url, "", None, + None, client, keys) + + nut_wallet.balance = 0 + await nutzap_wallet.update_nut_wallet(nut_wallet, [], client, keys) + nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys) + class NotificationHandler(HandleNotification): async def handle(self, relay_url, subscription_id, event: Event): @@ -348,11 +360,15 @@ async def handle(self, relay_url, subscription_id, event: Event): if event.kind().as_u64() == 7000: print("[Nostr Client]: " + event.as_json()) amount_sats = 0 + status = "" for tag in event.tags(): if tag.as_vec()[0] == "amount": amount_sats = int(int(tag.as_vec()[1]) / 1000) # millisats - # THIS IS FO TESTING - if event.author().to_hex() == "89669b03bb25232f33192fdda77b8e36e3d3886e9b55b3c74b95091e916c8f98": + if tag.as_vec()[0] == "status": + status = tag.as_vec()[1] + # THIS IS FOR TESTING + if event.author().to_hex() == "89669b03bb25232f33192fdda77b8e36e3d3886e9b55b3c74b95091e916c8f98" and status == "payment-required": + nut_wallet = await nutzap_wallet.get_nut_wallet(client, keys) if nut_wallet is None: await nutzap_wallet.create_new_nut_wallet(dvmconfig.NUZAP_MINTS, dvmconfig.NUTZAP_RELAYS, client, keys, "Test", "My Nutsack") @@ -366,6 +382,7 @@ async def handle(self, relay_url, subscription_id, event: Event): event.author().to_hex(), client, keys) + elif 6000 < event.kind().as_u64() < 6999: print("[Nostr Client]: " + event.as_json()) print("[Nostr Client]: " + event.content())