Skip to content

Commit

Permalink
integrate deezy.io to send to onchain addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Aug 22, 2022
1 parent a16becd commit 54908d6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
75 changes: 75 additions & 0 deletions onchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

decodepay "github.com/fiatjaf/ln-decodepay"
"github.com/fiatjaf/lntxbot/t"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

// powered by deezy.io

func handleSendToAddress(ctx context.Context, address string, msats int64) {
u := ctx.Value("initiator").(*User)

params, _ := json.Marshal(struct {
AmountSats int64 `json:"amount_sats"`
OnChainAddress string `json:"on_chain_address"`
OnChainSatsPerVByte int `json:"on_chain_sats_per_vbyte"`
}{msats / 1000, address, 2})

resp, err := http.Post("https://api.deezy.io/v1/swap", "application/json", bytes.NewBuffer(params))
if err != nil {
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("failed to call deezy.io API: %s", err.Error()),
})
return
}
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
text := string(b)
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("deezy.io API returned an error (%d): %s", resp.StatusCode, text),
})
return
}

var val struct {
Bolt11Invoice string `json:"bolt11_invoice"`
}
if err := json.NewDecoder(resp.Body).Decode(&val); err != nil || val.Bolt11Invoice == "" {
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("deezy.io API returned a broken response"),
})
return
}

processingMessageId := send(ctx, u, val.Bolt11Invoice+"\n\n"+translate(ctx, t.PROCESSING))

if inv, err := decodepay.Decodepay(val.Bolt11Invoice); err != nil {
send(ctx, u, t.ERROR,
t.T{"Err": fmt.Errorf("error parsing invoice: %w", err)},
processingMessageId)
return
} else if inv.MSatoshi > (msats + 800) {
send(ctx, u, t.ERROR,
t.T{"Err": "invoice is too expensive, so we're stopping here just in case, let us know if this is wrong"},
processingMessageId)
return
}

if _, err := u.payInvoice(ctx, val.Bolt11Invoice, 0); err != nil {
send(ctx, u, t.ERROR, t.T{"Err": err.Error()}, processingMessageId)
} else {
deleteMessage(&tgbotapi.Message{
Chat: &tgbotapi.Chat{ID: u.TelegramChatId},
MessageID: processingMessageId.(int),
})
}
}
7 changes: 7 additions & 0 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ func handleSend(ctx context.Context, opts docopt.Opts) {
return
}

// maybe this is an onchain address like bc1...?
if strings.HasPrefix(username, "bc1") {
handleSendToAddress(ctx, username, msats)
// end here since the flow will proceed on handleSendToAddress
return
}

message := ctx.Value("message").(*tgbotapi.Message)
receiver, err = examineTelegramUsername(username)
if receiver != nil {
Expand Down

0 comments on commit 54908d6

Please sign in to comment.