python snippets

This commit is contained in:
ruben beck
2023-10-25 17:17:08 +02:00
committed by ok300
parent f16fd23a21
commit 1f6c87c774
26 changed files with 388 additions and 175 deletions

View File

@@ -0,0 +1,15 @@
from breez_sdk import BlockingBreezServices
import logging
import breez_sdk
sdk_services = BlockingBreezServices
try:
# ANCHOR: buy-btc
req = breez_sdk.BuyBitcoinRequest(breez_sdk.BuyBitcoinProvider.MOONPAY)
buy_bitcoin_resp = sdk_services.buy_bitcoin(req)
# ANCHOR_END: buy-btc
except Exception as error:
logging.error(error)
raise

View File

@@ -0,0 +1,24 @@
import logging
from breez_sdk import BlockingBreezServices
sdk_services = BlockingBreezServices
def get_lsp_info():
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:
logging.log(error)
raise
def connect_lsp(lsp_id):
try:
# ANCHOR: connect-lsp
sdk_services.connect_lsp(lsp_id)
# ANCHOR_END: connect-lsp
except Exception as error:
logging.log(error)
raise

View File

@@ -0,0 +1,25 @@
import breez_sdk
from breez_sdk import BlockingBreezServices
import logging
sdk_services = BlockingBreezServices
def list_supported_fiat_currencies():
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:
logging.error(error)
raise
def get_current_rates():
try:
# ANCHOR: fetch-fiat-rates
fiat_rates = sdk_services.fetch_fiat_rates
# ANCHOR_END: fetch-fiat-rates
return fiat_rates
except Exception as error:
logging.error(error)
raise

View File

@@ -0,0 +1,46 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
# ANCHOR: init-sdk
class SDKListener(breez_sdk.EventListener):
def on_event(self, event):
logging.info(event)
def getting_started():
seed = breez_sdk.mnemonic_to_seed("<mnemonic words>")
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 = "path to an existing directory"
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():
try:
# ANCHOR: fetch-balance
node_info = node_info()
ln_balance = node_info.channels_balance_msat
onchain_balance = node_info.onchain_balance_msat
# ANCHOR_END: fetch-balance
except Exception as error:
logging.error(error)
raise

View 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

View File

@@ -0,0 +1,24 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def auth():
# 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():
logging.info("Successfully authenticated")
else:
logging.error("Failed to authenticate")
except Exception as error:
logging.error(error)
raise
# ANCHOR_END: lnurl-auth

View File

@@ -0,0 +1,22 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def pay():
# 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:
logging.error(error)
raise

View File

@@ -0,0 +1,21 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def withdraw():
# 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:
logging.error(error)
raise

View File

@@ -0,0 +1,62 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def generate_receive_onchain_address():
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:
logging.error(error)
raise
def get_in_progress_swap():
try:
# ANCHOR: in-progress-swap
swap_info = sdk_services.in_progress_swap()
# ANCHOR_END: in-progress-swap
except Exception as error:
logging.error(error)
raise
def list_refundables():
try:
# ANCHOR: list-refundables
refundables = sdk_services.list_refundables()
# ANCHOR_END: list-refundables
except Exception as error:
logging.error(error)
raise
def execute_refund(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:
logging.error(error)
raise
def get_channel_opening_fees(amount_msat,):
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:
logging.error(error)
raise

View File

@@ -0,0 +1,17 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def receive_payment():
try:
# ANCHOR: receive-payment
req = breez_sdk.ReceivePaymentRequest(
amount_msat=300000,
description="Invoice for 300 000 sats")
result = sdk_services.receive_payment(req)
# ANCHOR_END: receive-payment
return result
except Exception as error:
logging.error(error)
raise

View File

@@ -0,0 +1,47 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def get_current_fees():
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)
logging.info("Total estimated fees for reverseswap: ", current_fees.total_estimated_fees)
# ANCHOR_END: estimate-current-reverse-swap-total-fees
return current_fees
except Exception as error:
logging.error(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(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.fee_hash, sat_per_vbyte)
sdk_services.send_onchain(req)
# ANCHOR_END: start-reverse-swap
except Exception as error:
logging.error(error)
raise
def check_reverse_swap_status():
try:
# ANCHOR: check-reverse-swaps-status
reverse_swaps = sdk_services.in_progress_reverse_swaps()
for rs in reverse_swaps:
logging.info("Reverse swap ",rs.id , " in progress, status is ", rs.status)
# ANCHOR_END: check-reverse-swaps-status
except Exception as error:
logging.error(error)
raise

View File

@@ -0,0 +1,17 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def send_spontaneous_payment():
# 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)
return result
# ANCHOR: send-spontaneous-payment
except Exception as error:
logging.error(error)
raise

View File

@@ -0,0 +1,16 @@
import breez_sdk
import logging
sdk_services = breez_sdk.BlockingBreezServices
def retrieve_backup_files():
try:
# ANCHOR: static-channel-backup
req = breez_sdk.StaticBackupRequest(working_dir="<working directory>")
backup_data = breez_sdk.static_backup(req)
# ANCHOR_END: static-channel-backup
return backup_data
except Exception as error:
logging.error(error)
raise

View File

@@ -65,13 +65,8 @@ try {
<div slot="title">Python</div> <div slot="title">Python</div>
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/buy_btc.py:buy-btc}}
buy_bitcoin_resp = sdk_services.buy_bitcoin(
breez_sdk.BuyBitcoinRequest(
breez_sdk.BuyBitcoinProvider.MOONPAY))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -60,13 +60,8 @@ try {
<div slot="title">Python</div> <div slot="title">Python</div>
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/connecting_lsp.py:get-lsp-info}}
lsp_id = sdk_services.lsp_id()
lsp_info = sdk_services.lsp_info()
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -142,10 +137,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/connecting_lsp.py:connect-lsp}}
sdk_services.connect_lsp(lsp_id)
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -43,11 +43,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/fiat_currencies.py:list-fiat-currencies}}
fiat_currencies = sdk_services.list_fiat_currencies()
except Exception as error:
#Handle error
``` ```
</section> </section>

View File

@@ -134,26 +134,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
# SDK events listener {{#include ../../snippets/python_snippets/getting_started.py:init-sdk}}
class SDKListener(breez_sdk.EventListener):
def on_event(self, event):
print(event)
# Create the default config
seed = mnemonic_to_seed("<mnemonic words>")
invite_code = "<invite code>"
api_key = "<api key>"
config = breez_sdk.default_config(breez_sdk.EnvironmentType.PRODUCTION, apiKey,
breez_sdk.NodeConfig.GREENLIGHT(breez_sdk.GreenlightNodeConfig(None, invite_code)))
# Customize the config object according to your needs
config.working_dir = "path to an existing directory"
try:
# Connect to the Breez SDK make it ready for use
sdk_services = breez_sdk.connect(config, seed, SDKListener())
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -233,12 +214,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/getting_started.py:fetch-balance}}
node_info = node_info()
ln_balance = node_info.channels_balance_msat
onchain_balance = node_info.onchain_balance_msat
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -55,10 +55,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/list_payments.py:list-payments}}
sdk_services.list_payments(breez_sdk.ListPaymentsRequest(breez_sdk.PaymentTypeFilter.All))
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -140,14 +137,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/list_payments.py:list-payments-filtered}}
sdk_services.list_payments(
breez_sdk.ListPaymentsRequest(
breez_sdk.PaymentTypeFilter.Sent,
from_timestamp = 1696880000,
include_failures = True))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -77,20 +77,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
# Endpoint can also be of the form: {{#include ../../snippets/python_snippets/lnurl_auth.py:lnurl-auth}}
# 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):
result = sdk_services.lnurl_auth(parsed_input.data)
if result.is_ok():
print("Successfully authenticated")
else:
print("Failed to authenticate")
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -75,22 +75,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
# Endpoint can also be of the form: {{#include ../../snippets/python_snippets/lnurl_pay.py:lnurl-pay}}
# 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
result = sdk_service.pay_lnurl(
breez_sdk.LnUrlPayRequest(
data=parsed_input.data,
amount_msat=amount_msat,
comment="comment"))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -79,17 +79,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
# Endpoint can also be of the form: {{#include ../../snippets/python_snippets/lnurl_withdraw.py:lnurl-withdraw}}
# 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(breez_sdk.LnUrlWithdrawRequest(data=parsed_input.data, amount_msat=amount_msat, description="comment"))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -63,13 +63,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/receive_onchain.py: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
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -145,10 +139,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/receive_onchain.py:in-progress-swap}}
swap_info = sdk_services.in_progress_swap()
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -230,10 +221,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/receive_onchain.py:list-refundables}}
refundables = sdk_services.list_refundables()
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -318,15 +306,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
destination_address = "..." {{#include ../../snippets/python_snippets/receive_onchain.py:execute-refund}}
sat_per_vbyte = 5
try:
result = sdk_services.refund(swap_address=refundable.bitcoin_address,
to_address=destination_address,
sat_per_vbyte=sat_per_vbyte)
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -404,13 +384,7 @@ do {
<section> <section>
```python,ignore ```python,ignore
amount_msat = <amount msats> {{#include ../../snippets/python_snippets/receive_onchain.py:get-channel-opening-fees}}
try:
channel_fees = sdk_services.open_channel_fee(
breez_sdk.OpenChannelFeeRequest(
amount_msat=amount_msat))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -62,13 +62,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/receive_payment.py:receive-payment}}
receive_payment_response = sdk_services.receive_payment(
breez_sdk.ReceivePaymentRequest(
amount_msat=3_000_000,
description="Invoice for 3000 sats"))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -61,12 +61,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/send_onchain.py:estimate-current-reverse-swap-total-fees}}
current_fees = sdk_services.fetch_reverse_swap_fees(
breez_sdk.ReverseSwapFeesRequest(send_amount_sat=50000))
print("Total estimated fees for reverseswap: ", current_fees.total_estimated_fees)
except Exception as error:
# Handle error
``` ```
</section> </section>
@@ -140,8 +135,7 @@ Log.v("Breez", "Maximum amount, in sats: ${fees.max}")
<section> <section>
```python,ignore ```python,ignore
print("Minimum amount, in sats: ", current_fees.min) {{#include ../../snippets/python_snippets/send_onchain.py:get-current-reverse-swap-min-max}}
print("Maximum amount, in sats: ", current_fees.max)
``` ```
</section> </section>
@@ -227,16 +221,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
destination_address = "bc1.." {{#include ../../snippets/python_snippets/send_onchain.py:start-reverse-swap}}
amount_sat = 50000
sat_per_vbyte = 5
try:
sdk_services.send_onchain(amount_sat=amount_sat,
onchain_recipient_address=destination_address,
pair_hash=current_fees.fee_hash,
sat_per_vbyte=sat_per_vbyte)
except Exception as error:
# Handle erorr
``` ```
</section> </section>
@@ -312,12 +297,7 @@ for (rs in sdk.inProgressReverseSwaps()) {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/send_onchain.py: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)
except Exception as error:
# Handle erorr
``` ```
</section> </section>

View File

@@ -62,13 +62,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/send_spontaneous_payment.py:send-spontaneous-payment}}
sdk_services.send_spontaneous_payment(
breez_sdk.SendSpontaneousPaymentRequest(
node_id="...",
amount_msat=3000000))
except Exception as error:
# Handle error
``` ```
</section> </section>

View File

@@ -64,10 +64,7 @@ try {
<section> <section>
```python,ignore ```python,ignore
try: {{#include ../../snippets/python_snippets/static_channel_backup.py:static-channel-backup}}
backup_data = breez_sdk.static_backup(breez_sdk.StaticBackupRequest(working_dir="<working directory>"))
except Exception as error:
# Handle error
``` ```
</section> </section>