forked from tonft-app/highload-wallet-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (136 loc) · 4.17 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
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"
)
var client *liteclient.ConnectionPool
var api *ton.APIClient
var w *wallet.Wallet
// get responseWriter and
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(),
})
}
func sendTransactions(responseWriter http.ResponseWriter, req *http.Request) {
sendModeString := req.URL.Query().Get("send_mode")
log.Println("Send mode:", sendModeString)
sendMode, err := strconv.ParseUint(sendModeString, 10, 8)
if err != nil {
respondError(responseWriter, err)
return
}
commentText := req.URL.Query().Get("comment")
log.Println("comment:", commentText)
var txs map[string]string
err = json.NewDecoder(req.Body).Decode(&txs)
log.Println("Transactions:", txs)
if err != nil {
log.Println("Json decode err:", err.Error())
respondError(responseWriter, err)
return
}
block, err := api.CurrentMasterchainInfo(context.Background())
if err != nil {
respondError(responseWriter, err)
return
}
balance, err := w.GetBalance(context.Background(), block)
if err != nil {
respondError(responseWriter, err)
return
}
var totalAmount float64
for _, amtStr := range txs {
amtFloat, err := strconv.ParseFloat(amtStr, 64)
if err != nil {
respondError(responseWriter, err)
return
}
totalAmount += amtFloat
}
totalAmountUint64 := uint64(totalAmount * 1e9)
if balance.NanoTON().Uint64() >= totalAmountUint64 {
comment, err := wallet.CreateCommentCell(commentText)
if err != nil {
log.Fatalln("CreateComment err:", err.Error())
return
}
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,
},
})
}
log.Println("Sending transaction and waiting for confirmation...")
txHash, err := w.SendManyWaitTxHash(context.Background(), messages)
if err != nil {
respondError(responseWriter, err)
return
}
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)
json.NewEncoder(responseWriter).Encode(map[string]string{
"txHash": base64.StdEncoding.EncodeToString(txHash),
"link": "https://tonscan.org/tx/" + base64.URLEncoding.EncodeToString(txHash),
})
return
}
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusBadRequest)
json.NewEncoder(responseWriter).Encode(map[string]string{
"error": "Not enough balance",
})
}
func main() {
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())
http.HandleFunc("/sendTransactions", sendTransactions)
http.ListenAndServe(":8888", nil)
}