-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
210 lines (170 loc) · 4.98 KB
/
main.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
package main
import (
"context"
"encoding/base64"
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/ton/wallet"
"github.com/xssnick/tonutils-go/tvm/cell"
)
var (
client *liteclient.ConnectionPool
api *ton.APIClient
w *wallet.Wallet
)
func main() {
initializeApp()
http.HandleFunc("/sendTransactions", sendTransactionsHandler)
http.ListenAndServe(":8888", nil)
}
func initializeApp() {
var err error
client = liteclient.NewConnectionPool()
err = client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
api = ton.NewAPIClient(client)
err = godotenv.Load()
if err != nil {
log.Fatalln("Error loading .env file", err.Error())
}
seedWords := os.Getenv("SEED_PHRASE")
var words []string
if seedWords == "" {
log.Fatalln("SEED_PHRASE env is empty")
words = wallet.NewSeed()
log.Println("Generated seed words:", strings.Join(words, " "))
} else {
words = strings.Split(seedWords, " ")
}
w, err = wallet.FromSeed(api, words, wallet.HighloadV2R2)
if err != nil {
log.Fatalln("FromSeed err:", err.Error())
return
}
log.Println("Wallet address:", w.Address())
}
func sendTransactionsHandler(responseWriter http.ResponseWriter, req *http.Request) {
sendMode, err := getSendMode(req)
if err != nil {
respondError(responseWriter, err)
return
}
commentText := req.URL.Query().Get("comment")
log.Println("comment:", commentText)
txs, err := getTransactions(req)
if err != nil {
respondError(responseWriter, err)
return
}
err = processTransactions(responseWriter, sendMode, commentText, txs)
if err != nil {
respondError(responseWriter, err)
}
}
func getSendMode(req *http.Request) (uint64, error) {
sendModeString := req.URL.Query().Get("send_mode")
log.Println("Send mode:", sendModeString)
return strconv.ParseUint(sendModeString, 10, 8)
}
func getTransactions(req *http.Request) (map[string]string, error) {
var txs map[string]string
err := json.NewDecoder(req.Body).Decode(&txs)
if err != nil {
log.Println("Json decode err:", err.Error())
return nil, err
}
log.Println("Transactions:", txs)
return txs, nil
}
func processTransactions(responseWriter http.ResponseWriter, sendMode uint64, commentText string, txs map[string]string) error {
block, err := api.CurrentMasterchainInfo(context.Background())
if err != nil {
return err
}
balance, err := w.GetBalance(context.Background(), block)
if err != nil {
return err
}
totalAmount, err := calculateTotalAmount(txs)
if err != nil {
return err
}
if balance.NanoTON().Uint64() >= totalAmount {
comment, err := wallet.CreateCommentCell(commentText)
if err != nil {
return err
}
messages := createMessages(sendMode, comment, txs)
log.Println("Sending transaction and waiting for confirmation...")
txHash, err := w.SendManyWaitTxHash(context.Background(), messages)
if err != nil {
return err
}
return sendSuccessResponse(responseWriter, txHash)
}
return notEnoughBalanceError()
}
func calculateTotalAmount(txs map[string]string) (uint64, error) {
var totalAmount float64
for _, amtStr := range txs {
amtFloat, err := strconv.ParseFloat(amtStr, 64)
if err != nil {
return 0, err
}
totalAmount += amtFloat
}
return uint64(totalAmount * 1e9), nil
}
func createMessages(sendMode uint64, comment *cell.Cell, txs map[string]string) []*wallet.Message {
var messages []*wallet.Message
for addrStr, amtStr := range txs {
messages = append(messages, &wallet.Message{
Mode: uint8(sendMode),
InternalMessage: &tlb.InternalMessage{
Bounce: false,
DstAddr: address.MustParseAddr(addrStr),
Amount: tlb.MustFromTON(amtStr),
Body: comment,
},
})
}
return messages
}
func sendSuccessResponse(responseWriter http.ResponseWriter, txHash []byte) error {
log.Println("Transaction sent, hash:", base64.StdEncoding.EncodeToString(txHash))
log.Println("Explorer link: https://tonscan.org/tx/" + base64.URLEncoding.EncodeToString(txHash))
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusOK)
return json.NewEncoder(responseWriter).Encode(map[string]string{
"txHash": base64.StdEncoding.EncodeToString(txHash),
"link": "https://tonscan.org/tx/" + base64.URLEncoding.EncodeToString(txHash),
})
}
func notEnoughBalanceError() error {
return &customError{"Not enough balance"}
}
type customError struct {
msg string
}
func (e *customError) Error() string {
return e.msg
}
func respondError(responseWriter http.ResponseWriter, err error) {
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusBadRequest)
json.NewEncoder(responseWriter).Encode(map[string]string{
"error": err.Error(),
})
}