-
Notifications
You must be signed in to change notification settings - Fork 43
/
amounts.go
225 lines (209 loc) · 8 KB
/
amounts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package telegram
import (
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
"github.com/LightningTipBot/LightningTipBot/internal/price"
"github.com/LightningTipBot/LightningTipBot/internal/runtime"
"github.com/LightningTipBot/LightningTipBot/internal/storage/transaction"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
func getArgumentFromCommand(input string, which int) (output string, err error) {
if len(strings.Split(input, " ")) < which+1 {
return "", errors.New("message doesn't contain enough arguments")
}
output = strings.Split(input, " ")[which]
return output, nil
}
func decodeAmountFromCommand(input string) (amount int, err error) {
if len(strings.Split(input, " ")) < 2 {
errmsg := "message doesn't contain any amount"
// log.Errorln(errmsg)
return 0, errors.New(errmsg)
}
amount, err = getAmount(strings.Split(input, " ")[1])
return amount, err
}
func getAmount(input string) (amount int, err error) {
// convert something like 1.2k into 1200
if strings.HasSuffix(strings.ToLower(input), "k") {
fmount, err := strconv.ParseFloat(strings.TrimSpace(input[:len(input)-1]), 64)
if err != nil {
return 0, err
}
amount = int(fmount * 1000)
return amount, err
}
// convert fiat currencies to satoshis
for currency, symbol := range price.P.Currencies {
if strings.HasPrefix(input, symbol) || strings.HasSuffix(input, symbol) || // for 1$ and $1
strings.HasPrefix(strings.ToLower(input), strings.ToLower(currency)) || // for USD1
strings.HasSuffix(strings.ToLower(input), strings.ToLower(currency)) { // for 1USD
numeric_string := ""
numeric_string = strings.Replace(input, symbol, "", 1) // for symbol like $
numeric_string = strings.Replace(strings.ToLower(numeric_string), strings.ToLower(currency), "", 1) // for 1USD
fmount, err := strconv.ParseFloat(numeric_string, 64)
if err != nil {
log.Errorln(err)
return 0, err
}
if !(price.Price[currency] > 0) {
return 0, errors.New("price is zero")
}
amount = int(fmount / price.Price[currency] * float64(100_000_000))
return amount, nil
}
}
// use plain integer as satoshis
amount, err = strconv.Atoi(input)
if err != nil {
return 0, err
}
if amount <= 0 {
return 0, errors.New("amount must be greater than 0")
}
return amount, err
}
type EnterAmountStateData struct {
ID string `json:"ID"` // holds the ID of the tx object in bunt db
Type string `json:"Type"` // holds type of the tx in bunt db (needed for type checking)
Amount int64 `json:"Amount"` // holds the amount entered by the user mSat
AmountMin int64 `json:"AmountMin"` // holds the minimum amount that needs to be entered mSat
AmountMax int64 `json:"AmountMax"` // holds the maximum amount that needs to be entered mSat
OiringalCommand string `json:"OiringalCommand"` // hold the originally entered command for evtl later use
}
func (bot *TipBot) askForAmount(ctx context.Context, id string, eventType string, amountMin int64, amountMax int64, originalCommand string) (enterAmountStateData *EnterAmountStateData, err error) {
user := LoadUser(ctx)
if user.Wallet == nil {
return // errors.New("user has no wallet"), 0
}
enterAmountStateData = &EnterAmountStateData{
ID: id,
Type: eventType,
AmountMin: amountMin,
AmountMax: amountMax,
OiringalCommand: originalCommand,
}
// set LNURLPayParams in the state of the user
stateDataJson, err := json.Marshal(enterAmountStateData)
if err != nil {
log.Errorln(err)
return
}
SetUserState(user, bot, lnbits.UserEnterAmount, string(stateDataJson))
askAmountText := Translate(ctx, "lnurlEnterAmountMessage")
if amountMin > 0 && amountMax >= amountMin {
askAmountText = fmt.Sprintf(Translate(ctx, "lnurlEnterAmountRangeMessage"), enterAmountStateData.AmountMin/1000, enterAmountStateData.AmountMax/1000)
}
// Let the user enter an amount and return
bot.trySendMessage(user.Telegram, askAmountText, tb.ForceReply)
return
}
// enterAmountHandler is invoked in anyTextHandler when the user needs to enter an amount
// the amount is then stored as an entry in the user's stateKey in the user database
// any other handler that relies on this, needs to load the resulting amount from the database
func (bot *TipBot) enterAmountHandler(ctx context.Context, m *tb.Message) {
user := LoadUser(ctx)
if user.Wallet == nil {
return // errors.New("user has no wallet"), 0
}
if !(user.StateKey == lnbits.UserEnterAmount) {
ResetUserState(user, bot)
return // errors.New("user state does not match"), 0
}
var EnterAmountStateData EnterAmountStateData
err := json.Unmarshal([]byte(user.StateData), &EnterAmountStateData)
if err != nil {
log.Errorf("[enterAmountHandler] %s", err.Error())
ResetUserState(user, bot)
return
}
amount, err := getAmount(m.Text)
if err != nil {
log.Warnf("[enterAmountHandler] %s", err.Error())
bot.trySendMessage(m.Sender, Translate(ctx, "lnurlInvalidAmountMessage"))
ResetUserState(user, bot)
return //err, 0
}
// amount not in allowed range from LNURL
if EnterAmountStateData.AmountMin > 0 && EnterAmountStateData.AmountMax >= EnterAmountStateData.AmountMin && // this line checks whether min_max is set at all
(amount > int(EnterAmountStateData.AmountMax/1000) || amount < int(EnterAmountStateData.AmountMin/1000)) { // this line then checks whether the amount is in the range
err = fmt.Errorf("amount not in range")
log.Warnf("[enterAmountHandler] %s", err.Error())
bot.trySendMessage(m.Sender, fmt.Sprintf(Translate(ctx, "lnurlInvalidAmountRangeMessage"), EnterAmountStateData.AmountMin/1000, EnterAmountStateData.AmountMax/1000))
ResetUserState(user, bot)
return
}
// find out which type the object in bunt has waiting for an amount
// we stored this in the EnterAmountStateData before
switch EnterAmountStateData.Type {
case "LnurlPayState":
tx := &LnurlPayState{Base: transaction.New(transaction.ID(EnterAmountStateData.ID))}
sn, err := tx.Get(tx, bot.Bunt)
if err != nil {
return
}
LnurlPayState := sn.(*LnurlPayState)
LnurlPayState.Amount = amount * 1000 // mSat
// add result to persistent struct
runtime.IgnoreError(LnurlPayState.Set(LnurlPayState, bot.Bunt))
EnterAmountStateData.Amount = int64(amount) * 1000 // mSat
StateDataJson, err := json.Marshal(EnterAmountStateData)
if err != nil {
log.Errorln(err)
return
}
SetUserState(user, bot, lnbits.UserHasEnteredAmount, string(StateDataJson))
bot.lnurlPayHandlerSend(ctx, m)
return
case "LnurlWithdrawState":
tx := &LnurlWithdrawState{Base: transaction.New(transaction.ID(EnterAmountStateData.ID))}
sn, err := tx.Get(tx, bot.Bunt)
if err != nil {
return
}
LnurlWithdrawState := sn.(*LnurlWithdrawState)
LnurlWithdrawState.Amount = amount * 1000 // mSat
// add result to persistent struct
runtime.IgnoreError(LnurlWithdrawState.Set(LnurlWithdrawState, bot.Bunt))
EnterAmountStateData.Amount = int64(amount) * 1000 // mSat
StateDataJson, err := json.Marshal(EnterAmountStateData)
if err != nil {
log.Errorln(err)
return
}
SetUserState(user, bot, lnbits.UserHasEnteredAmount, string(StateDataJson))
bot.lnurlWithdrawHandlerWithdraw(ctx, m)
return
case "CreateInvoiceState":
m.Text = fmt.Sprintf("/invoice %d", amount)
SetUserState(user, bot, lnbits.UserHasEnteredAmount, "")
bot.invoiceHandler(ctx, m)
return
case "CreateDonationState":
m.Text = fmt.Sprintf("/donate %d", amount)
SetUserState(user, bot, lnbits.UserHasEnteredAmount, "")
bot.donationHandler(ctx, m)
return
case "CreateSendState":
splits := strings.SplitAfterN(EnterAmountStateData.OiringalCommand, " ", 2)
if len(splits) > 1 {
m.Text = fmt.Sprintf("/send %d %s", amount, splits[1])
SetUserState(user, bot, lnbits.UserHasEnteredAmount, "")
bot.sendHandler(ctx, m)
return
}
return
default:
ResetUserState(user, bot)
return
}
// // reset database entry
// ResetUserState(user, bot)
// return
}