Last active
January 9, 2023 09:16
-
-
Save Stadicus/1f141004b35b5a49cb62a01c1dc58af4 to your computer and use it in GitHub Desktop.
deezy-swap.sh
This file contains hidden or 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
#!/bin/bash | |
set -eu | |
# Deezy Swap: push Bitcoin Lightning liquidity to LND on-chain address | |
# -------------------------------------------------------------------------- | |
# This script automates swapping Bitcoin Lightning funds to Bitcoin on-chain | |
# through deezy.io, an external service. | |
# | |
# Use at your own risk. | |
# | |
# ------------------------------------------------------------------------------ | |
# | |
# Set your own API access token | |
# (see https://docs.deezy.io/#tag/Swap-(LN-to-BTC)/operation/createSwap) | |
API_TOKEN="00000000000000000000000000000000" | |
# ------------------------------------------------------------------------------ | |
# print usage information for script | |
usage() { | |
echo "Deezy Swap: push Bitcoin Lightning liquidity to LND on-chain address | |
usage: deezy-swap.sh [--version] [--help] amount onchain_fees | |
Parameters: | |
amount sats to swap | |
onchain_fees onchain fees in sats/vbyte | |
" | |
} | |
# error handling | |
errorExit() { | |
exit 1 | |
} | |
# ------------------------------------------------------------------------------ | |
# check script arguments | |
if [[ "${1-undefined}" == "-v" ]] || [[ "${1-undefined}" == "--version" ]]; then | |
echo "deezy-swap version 0.1" | |
exit 0 | |
elif [[ ${#} -lt 2 ]] || [[ "${1}" == "-h" ]] || [[ "${1}" == "--help" ]]; then | |
usage | |
exit 0 | |
fi | |
# ------------------------------------------------------------------------------ | |
AMOUNT="${1:-}" | |
ONCHAIN_FEES="${2:-}" | |
# obtain on-chain address from LND wallet | |
ONCHAIN_ADDRESS=$( lncli newaddress p2tr | jq -r '.address' ) | |
if [ "${ONCHAIN_ADDRESS:0:3}" == "bc1" ]; then | |
echo "OK: Obtained on-chain address from LND: ${ONCHAIN_ADDRESS}" | |
else | |
echo "ERR: ERR: Could not obtain valid on-chain address from LND." | |
errorExit | |
fi | |
# create swap and get LN invoice from Deezy.io | |
LN_INVOICE=$( curl -s -X POST -H 'Content-type: application/json' -H 'x-api-token:'${API_TOKEN}'' --data '{"amount_sats":'${AMOUNT}',"on_chain_address":"'"${ONCHAIN_ADDRESS}"'","on_chain_sats_per_vbyte": '"${ONCHAIN_FEES}"'}' https://api.deezy.io/v1/swap | jq -r '.bolt11_invoice') | |
LN_INVOICE_DESC=$( lncli decodepayreq "${LN_INVOICE}" | jq -r '.description' ) | |
LN_INVOICE_SATS=$( lncli decodepayreq "${LN_INVOICE}" | jq -r '.num_satoshis' ) | |
if [ "${LN_INVOICE:0:4}" == "lnbc" ]; then | |
printf "OK: Obtained LN invoice from Deezy.io: \n\n%s \n\n%s \n\n" "${LN_INVOICE}" "${LN_INVOICE_DESC}" | |
else | |
echo "ERR: ERR: Could not obtain valid on-chain address from LND." | |
errorExit | |
fi | |
while true; do | |
read -r -p "Do you want to proceed? (y/n) " yn | |
case $yn in | |
[yY] ) | |
echo; | |
break;; | |
[nN] ) | |
echo "Aborted by user"; | |
errorExit;; | |
* ) | |
echo "invalid response";; | |
esac | |
done | |
# pay LN invoice to swap liquidity | |
# -- if payment fails, the script terminates immediately | |
lncli payinvoice --force "${LN_INVOICE}" | |
# log swap in CSV file for accounting | |
LOGFILENAME="$HOME/deezy-swaps.csv" | |
if [ ! -f "$LOGFILENAME" ]; then | |
echo "date; sats_out; ln_invoice; sats_in; onchain_address" > "${LOGFILENAME}" | |
fi | |
TIMESTAMP=$( date +"%F %T" ) | |
echo "${TIMESTAMP}; ${LN_INVOICE_SATS}; ${LN_INVOICE}; ${AMOUNT}; ${ONCHAIN_ADDRESS}" >> "${LOGFILENAME}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment