mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-17 13:54:20 +01:00
rename python_snippets folder to python
.
This commit is contained in:
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