mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-17 05:44:20 +01:00
rename python_snippets folder to python
.
This commit is contained in:
34
snippets/python/README.md
Normal file
34
snippets/python/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
## Steps to run locally
|
||||
1. Build a python package
|
||||
- By running the publish-all-platforms CI in the breez-sdk repository (use dummy binaries)
|
||||
- or by downloading from Pypi
|
||||
2. Download the wheel artifact for your platform. For linux that is `python-wheel-3.8-manylinux_2_31_x86_64`
|
||||
3. Unzip the artifact in the `snippets/python/packages` folder.
|
||||
4. Run `pip install packages/{NAME_OF_.WHL_FILE}`
|
||||
4. happy coding!
|
||||
|
||||
To check the syntax:
|
||||
|
||||
```bash
|
||||
cd snippets/python
|
||||
python3 -m compileall src
|
||||
```
|
||||
|
||||
To check the snippet formatting:
|
||||
|
||||
```bash
|
||||
cd snippets/python/src
|
||||
ruff --ignore F841 --ignore F401 --output-format=github .
|
||||
```
|
||||
|
||||
To execute the snippets locally, in order to check for type correctness:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
export API_KEY="..."
|
||||
export MNEMONIC="..."
|
||||
|
||||
cd snippets/python
|
||||
python main.py $API_KEY $MNEMONIC
|
||||
```
|
||||
93
snippets/python/main.py
Executable file
93
snippets/python/main.py
Executable file
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
|
||||
import breez_sdk
|
||||
from src.getting_started import getting_started,getting_started_node_info
|
||||
from src.connecting_lsp import get_lsp_info, connect_lsp
|
||||
from src.buy_btc import buy
|
||||
from src.send_onchain import get_current_fees, list_current_fees, check_reverse_swap_status, start_reverse_swap
|
||||
from src.static_channel_backup import retrieve_backup_files
|
||||
from src.send_spontaneous_payment import send_spontaneous_payment
|
||||
from src.receive_payment import receive_payment
|
||||
from src.receive_onchain import generate_receive_onchain_address, get_in_progress_swap, list_refundables, execute_refund, get_channel_opening_fees
|
||||
from src.fiat_currencies import list_supported_fiat_currencies, get_current_rates
|
||||
from src.lnurl_auth import auth
|
||||
from src.lnurl_pay import pay
|
||||
from src.lnurl_withdraw import withdraw
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
|
||||
class SDKListener(breez_sdk.EventListener):
|
||||
def on_event(self, event):
|
||||
print(event)
|
||||
|
||||
|
||||
def main():
|
||||
temp_dir = tempfile.TemporaryDirectory()
|
||||
|
||||
|
||||
api_key = os.getenv("API_KEY")
|
||||
mnemonic = os.getenv("MNEMONIC")
|
||||
|
||||
# getting started
|
||||
sdk_services = getting_started(api_key, mnemonic, temp_dir.name)
|
||||
node_info = getting_started_node_info(sdk_services)
|
||||
print(type(node_info))
|
||||
|
||||
# lsp info
|
||||
get_lsp_info(sdk_services)
|
||||
|
||||
# connect lsp
|
||||
connect_lsp(sdk_services,sdk_services.lsp_id())
|
||||
|
||||
# buy btc
|
||||
buy(sdk_services)
|
||||
|
||||
# send onchain
|
||||
current_fees = get_current_fees(sdk_services)
|
||||
list_current_fees(current_fees)
|
||||
check_reverse_swap_status(sdk_services)
|
||||
start_reverse_swap(sdk_services,current_fees, 7)
|
||||
|
||||
# static backup
|
||||
temp_dir2 = tempfile.TemporaryDirectory()
|
||||
retrieve_backup_files(sdk_services,temp_dir2.name)
|
||||
|
||||
# receive payment
|
||||
receive_payment(sdk_services)
|
||||
|
||||
# receive onchain
|
||||
generate_receive_onchain_address(sdk_services)
|
||||
get_in_progress_swap(sdk_services)
|
||||
refundables = list_refundables(sdk_services)
|
||||
execute_refund(sdk_services,refundables)
|
||||
get_channel_opening_fees(sdk_services,3000000)
|
||||
|
||||
#send spontaneous payment
|
||||
send_spontaneous_payment(sdk_services)
|
||||
|
||||
# fiat currencies
|
||||
list_supported_fiat_currencies(sdk_services)
|
||||
get_current_rates(sdk_services)
|
||||
|
||||
# lnurl auth
|
||||
auth(sdk_services)
|
||||
|
||||
# lnurl pay
|
||||
pay(sdk_services)
|
||||
|
||||
# lnurl withdraw
|
||||
withdraw(sdk_services)
|
||||
|
||||
|
||||
|
||||
# use temp_dir, and remove when done:
|
||||
temp_dir.cleanup()
|
||||
temp_dir2.cleanup()
|
||||
|
||||
|
||||
|
||||
main()
|
||||
|
||||
|
||||
15
snippets/python/src/buy_btc.py
Normal file
15
snippets/python/src/buy_btc.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import logging
|
||||
import breez_sdk
|
||||
|
||||
|
||||
|
||||
|
||||
def buy(sdk_services):
|
||||
try:
|
||||
# ANCHOR: buy-btc
|
||||
req = breez_sdk.BuyBitcoinRequest(breez_sdk.BuyBitcoinProvider.MOONPAY)
|
||||
buy_bitcoin_resp = sdk_services.buy_bitcoin(req=req)
|
||||
# ANCHOR_END: buy-btc
|
||||
except Exception as error:
|
||||
logging.error(error)
|
||||
raise
|
||||
20
snippets/python/src/connecting_lsp.py
Normal file
20
snippets/python/src/connecting_lsp.py
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
def get_lsp_info(sdk_services):
|
||||
try:
|
||||
# ANCHOR: get-lsp-info
|
||||
lsp_id = sdk_services.lsp_id()
|
||||
lsp_info = sdk_services.lsp_info()
|
||||
# ANCHOR_END: get-lsp-info
|
||||
return lsp_info
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def connect_lsp(sdk_services,lsp_id):
|
||||
try:
|
||||
# ANCHOR: connect-lsp
|
||||
sdk_services.connect_lsp(lsp_id)
|
||||
# ANCHOR_END: connect-lsp
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
20
snippets/python/src/fiat_currencies.py
Normal file
20
snippets/python/src/fiat_currencies.py
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
def list_supported_fiat_currencies(sdk_services):
|
||||
try:
|
||||
# ANCHOR: list-fiat-currencies
|
||||
supported_fiat_currencies = sdk_services.list_fiat_currencies()
|
||||
# ANCHOR_END: list-fiat-currencies
|
||||
return supported_fiat_currencies
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def get_current_rates(sdk_services):
|
||||
try:
|
||||
# ANCHOR: fetch-fiat-rates
|
||||
fiat_rates = sdk_services.fetch_fiat_rates
|
||||
# ANCHOR_END: fetch-fiat-rates
|
||||
return fiat_rates
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
47
snippets/python/src/getting_started.py
Normal file
47
snippets/python/src/getting_started.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import breez_sdk
|
||||
|
||||
import logging
|
||||
|
||||
|
||||
# ANCHOR: init-sdk
|
||||
class SDKListener(breez_sdk.EventListener):
|
||||
def on_event(self, event):
|
||||
logging.info(event)
|
||||
|
||||
def getting_started(API_KEY,mnemonic,temp_dir):
|
||||
|
||||
seed = breez_sdk.mnemonic_to_seed(mnemonic)
|
||||
invite_code = "<invite code>"
|
||||
api_key = API_KEY
|
||||
config = breez_sdk.default_config(
|
||||
breez_sdk.EnvironmentType.PRODUCTION,
|
||||
api_key,
|
||||
breez_sdk.NodeConfig.GREENLIGHT(breez_sdk.GreenlightNodeConfig(None, invite_code)))
|
||||
|
||||
# Customize the config object according to your needs
|
||||
config.working_dir = temp_dir
|
||||
|
||||
try:
|
||||
# Connect to the Breez SDK make it ready for use
|
||||
sdk_services = breez_sdk.connect(config, seed, SDKListener())
|
||||
|
||||
return sdk_services
|
||||
except Exception as error:
|
||||
logging.error(error)
|
||||
raise
|
||||
# ANCHOR_END: init-sdk
|
||||
|
||||
def getting_started_node_info(sdk_services):
|
||||
|
||||
try:
|
||||
# ANCHOR: fetch-balance
|
||||
node_info = sdk_services.node_info()
|
||||
ln_balance = node_info.channels_balance_msat
|
||||
onchain_balance = node_info.onchain_balance_msat
|
||||
# ANCHOR_END: fetch-balance
|
||||
return node_info
|
||||
except Exception as error:
|
||||
logging.error(error)
|
||||
raise
|
||||
|
||||
|
||||
27
snippets/python/src/list_payments.py
Normal file
27
snippets/python/src/list_payments.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import breez_sdk
|
||||
import logging
|
||||
|
||||
sdk_services = breez_sdk.BlockingBreezServices
|
||||
|
||||
|
||||
def list_payments():
|
||||
try:
|
||||
# ANCHOR: list-payments
|
||||
sdk_services.list_payments(breez_sdk.ListPaymentsRequest(breez_sdk.PaymentTypeFilter.All))
|
||||
# ANCHOR_END: list-payments
|
||||
except Exception as error:
|
||||
logging.error(error)
|
||||
raise
|
||||
|
||||
def list_payments_filtered():
|
||||
try:
|
||||
# ANCHOR: list-payments-filtered
|
||||
req = breez_sdk.ListPaymentsRequest(
|
||||
breez_sdk.PaymentTypeFilter.Sent,
|
||||
from_timestamp = 1696880000,
|
||||
include_failures = True)
|
||||
sdk_services.list_payments(req)
|
||||
# ANCHOR_END: list-payments-filtered
|
||||
except Exception as error:
|
||||
logging.error(error)
|
||||
raise
|
||||
22
snippets/python/src/lnurl_auth.py
Normal file
22
snippets/python/src/lnurl_auth.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import breez_sdk
|
||||
|
||||
|
||||
def auth(sdk_services):
|
||||
# ANCHOR: lnurl-auth
|
||||
# Endpoint can also be of the form:
|
||||
# keyauth://domain.com/auth?key=val
|
||||
lnurl_auth_url = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttvdankjm3lw3skw0tvdankjm3xdvcn6vtp8q6n2dfsx5mrjwtrxdjnqvtzv56rzcnyv3jrxv3sxqmkyenrvv6kve3exv6nqdtyv43nqcmzvdsnvdrzx33rsenxx5unqc3cxgeqgntfgu"
|
||||
|
||||
try:
|
||||
parsed_input = breez_sdk.parse_input(lnurl_auth_url)
|
||||
if isinstance(parsed_input, breez_sdk.InputType.LN_URL_AUTH):
|
||||
req = breez_sdk
|
||||
result = sdk_services.lnurl_auth(parsed_input.data)
|
||||
if result.is_ok():
|
||||
print("Successfully authenticated")
|
||||
else:
|
||||
print("Failed to authenticate")
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
# ANCHOR_END: lnurl-auth
|
||||
20
snippets/python/src/lnurl_pay.py
Normal file
20
snippets/python/src/lnurl_pay.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import breez_sdk
|
||||
|
||||
|
||||
def pay(sdk_services):
|
||||
# ANCHOR: lnurl-pay
|
||||
# Endpoint can also be of the form:
|
||||
# lnurlp://domain.com/lnurl-pay?key=val
|
||||
# lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4excttsv9un7um9wdekjmmw84jxywf5x43rvv35xgmr2enrxanr2cfcvsmnwe3jxcukvde48qukgdec89snwde3vfjxvepjxpjnjvtpxd3kvdnxx5crxwpjvyunsephsz36jf
|
||||
lnurl_pay_url = "lightning@address.com"
|
||||
try:
|
||||
parsed_input = breez_sdk.parse_input(lnurl_pay_url)
|
||||
if isinstance(parsed_input, breez_sdk.InputType.LN_URL_PAY):
|
||||
amount_msat = parsed_input.data.min_sendable
|
||||
req = breez_sdk.LnUrlPayRequest(parsed_input.data,amount_msat,"comment")
|
||||
result = sdk_services.pay_lnurl(req)
|
||||
# ANCHOR_END: lnurl-pay
|
||||
return result
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
19
snippets/python/src/lnurl_withdraw.py
Normal file
19
snippets/python/src/lnurl_withdraw.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import breez_sdk
|
||||
|
||||
|
||||
def withdraw(sdk_services):
|
||||
# ANCHOR: lnurl-withdraw
|
||||
# Endpoint can also be of the form:
|
||||
# lnurlw://domain.com/lnurl-withdraw?key=val
|
||||
lnurl_withdraw_url = "lnurl1dp68gurn8ghj7mr0vdskc6r0wd6z7mrww4exctthd96xserjv9mn7um9wdekjmmw843xxwpexdnxzen9vgunsvfexq6rvdecx93rgdmyxcuxverrvcursenpxvukzv3c8qunsdecx33nzwpnvg6ryc3hv93nzvecxgcxgwp3h33lxk"
|
||||
|
||||
try:
|
||||
parsed_input = breez_sdk.parse_input(lnurl_withdraw_url)
|
||||
if isinstance(parsed_input, breez_sdk.InputType.LN_URL_WITHDRAW):
|
||||
amount_msat = parsed_input.data.min_withdrawable
|
||||
result = sdk_services.withdraw_lnurl(parsed_input.data, amount_msat, "comment")
|
||||
# ANCHOR_END: lnurl-withdraw
|
||||
return result
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
60
snippets/python/src/receive_onchain.py
Normal file
60
snippets/python/src/receive_onchain.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import breez_sdk
|
||||
|
||||
def generate_receive_onchain_address(sdk_services):
|
||||
try:
|
||||
# ANCHOR: generate-receive-onchain-address
|
||||
swap_info = sdk_services.receive_onchain(breez_sdk.ReceiveOnchainRequest())
|
||||
|
||||
# Send your funds to the below bitcoin address
|
||||
address = swap_info.bitcoin_address
|
||||
# ANCHOR_END: generate-receive-onchain-address
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def get_in_progress_swap(sdk_services):
|
||||
try:
|
||||
# ANCHOR: in-progress-swap
|
||||
swap_info = sdk_services.in_progress_swap()
|
||||
# ANCHOR_END: in-progress-swap
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
|
||||
def list_refundables(sdk_services):
|
||||
try:
|
||||
# ANCHOR: list-refundables
|
||||
refundables = sdk_services.list_refundables()
|
||||
# ANCHOR_END: list-refundables
|
||||
return refundables
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def execute_refund(sdk_services,refundable):
|
||||
if refundable is breez_sdk.SwapInfo:
|
||||
# ANCHOR: execute-refund
|
||||
destination_address = "..."
|
||||
|
||||
sat_per_vbyte = 5
|
||||
try:
|
||||
result = sdk_services.refund(
|
||||
swap_address=refundable.bitcoin_address,
|
||||
to_address=destination_address,
|
||||
sat_per_vbyte=sat_per_vbyte)
|
||||
# ANCHOR_END: execute-refund
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def get_channel_opening_fees(sdk_services ,amount_msat=None):
|
||||
try:
|
||||
# ANCHOR: get-channel-opening-fees
|
||||
req = breez_sdk.OpenChannelFeeRequest(amount_msat)
|
||||
channel_fees = sdk_services.open_channel_fee(req)
|
||||
# ANCHOR_END: get-channel-opening-fees
|
||||
return channel_fees
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
15
snippets/python/src/receive_payment.py
Normal file
15
snippets/python/src/receive_payment.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import breez_sdk
|
||||
|
||||
|
||||
def receive_payment(sdk_services):
|
||||
try:
|
||||
# ANCHOR: receive-payment
|
||||
req = breez_sdk.ReceivePaymentRequest(
|
||||
amount_msat=3000000,
|
||||
description="Invoice for 3 000 000 sats")
|
||||
result = sdk_services.receive_payment(req)
|
||||
# ANCHOR_END: receive-payment
|
||||
return result
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
44
snippets/python/src/send_onchain.py
Normal file
44
snippets/python/src/send_onchain.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import breez_sdk
|
||||
|
||||
def get_current_fees(sdk_services):
|
||||
try:
|
||||
# ANCHOR: estimate-current-reverse-swap-total-fees
|
||||
req = breez_sdk.ReverseSwapFeesRequest(send_amount_sat=50000)
|
||||
current_fees = sdk_services.fetch_reverse_swap_fees(req)
|
||||
print("Total estimated fees for reverseswap: ", current_fees)
|
||||
# ANCHOR_END: estimate-current-reverse-swap-total-fees
|
||||
return current_fees
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def list_current_fees(current_fees):
|
||||
# ANCHOR: get-current-reverse-swap-min-max
|
||||
print("Minimum amount, in sats: ", current_fees.min)
|
||||
print("Maximum amount, in sats: ", current_fees.max)
|
||||
# ANCHOR_END: get-current-reverse-swap-min-max
|
||||
|
||||
def start_reverse_swap(sdk_services, current_fees,fee_rate):
|
||||
# ANCHOR: start-reverse-swap
|
||||
destination_address = "bc1.."
|
||||
amount_sat = 50000
|
||||
sat_per_vbyte = fee_rate
|
||||
try:
|
||||
req = breez_sdk.SendOnchainRequest(amount_sat, destination_address, current_fees.fees_hash, sat_per_vbyte)
|
||||
sdk_services.send_onchain(req)
|
||||
# ANCHOR_END: start-reverse-swap
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
def check_reverse_swap_status(sdk_services):
|
||||
try:
|
||||
# ANCHOR: check-reverse-swaps-status
|
||||
reverse_swaps = sdk_services.in_progress_reverse_swaps()
|
||||
for rs in reverse_swaps:
|
||||
print("Reverse swap ",rs.id , " in progress, status is ", rs.status)
|
||||
# ANCHOR_END: check-reverse-swaps-status
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
|
||||
16
snippets/python/src/send_spontaneous_payment.py
Normal file
16
snippets/python/src/send_spontaneous_payment.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import breez_sdk
|
||||
|
||||
|
||||
|
||||
def send_spontaneous_payment(sdk_services):
|
||||
# ANCHOR: send-spontaneous-payment
|
||||
node_id = "..."
|
||||
amount_msat = 300000
|
||||
try:
|
||||
req = breez_sdk.SendSpontaneousPaymentRequest(node_id,amount_msat)
|
||||
result = sdk_services.send_spontaneous_payment(req)
|
||||
# ANCHOR: send-spontaneous-payment
|
||||
return result
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
12
snippets/python/src/static_channel_backup.py
Normal file
12
snippets/python/src/static_channel_backup.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import breez_sdk
|
||||
|
||||
def retrieve_backup_files(sdk_services, working_dir):
|
||||
try:
|
||||
# ANCHOR: static-channel-backup
|
||||
req = breez_sdk.StaticBackupRequest(working_dir=working_dir)
|
||||
backup_data = breez_sdk.static_backup(req)
|
||||
# ANCHOR_END: static-channel-backup
|
||||
return backup_data
|
||||
except Exception as error:
|
||||
print(error)
|
||||
raise
|
||||
Reference in New Issue
Block a user