-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.go
73 lines (59 loc) · 2.44 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
package main
import (
"context"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
"log"
)
func main() {
client := liteclient.NewConnectionPool()
cfg, err := liteclient.GetConfigFromUrl(context.Background(), "https://ton.org/global.config.json")
if err != nil {
log.Fatalln("get config err: ", err.Error())
return
}
// connect to mainnet lite servers
err = client.AddConnectionsFromConfig(context.Background(), cfg)
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
// initialize ton api lite connection wrapper with full proof checks
api := ton.NewAPIClient(client, ton.ProofCheckPolicySecure).WithRetry()
api.SetTrustedBlockFromConfig(cfg)
log.Println("fetching and checking proofs since config init block, it may take near a minute...")
master, err := api.CurrentMasterchainInfo(context.Background()) // we fetch block just to trigger chain proof check
if err != nil {
log.Fatalln("get masterchain info err: ", err.Error())
return
}
log.Println("master proof checks are completed successfully, now communication is 100% safe!")
// address on which we are accepting payments
treasuryAddress := address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N")
acc, err := api.GetAccount(context.Background(), master, treasuryAddress)
if err != nil {
log.Fatalln("get masterchain info err: ", err.Error())
return
}
// Cursor of processed transaction, save it to your db
// We start from last transaction, will not process transactions older than we started from.
// After each processed transaction, save lt to your db, to continue after restart
lastProcessedLT := acc.LastTxLT
// channel with new transactions
transactions := make(chan *tlb.Transaction)
// it is a blocking call, so we start it asynchronously
go api.SubscribeOnTransactions(context.Background(), treasuryAddress, lastProcessedLT, transactions)
log.Println("waiting for transfers...")
// listen for new transactions from channel
for tx := range transactions {
// process transaction here
log.Println(tx.String())
// update last processed lt and save it in db
lastProcessedLT = tx.LT
}
// it can happen due to none of available liteservers know old enough state for our address
// (when our unprocessed transactions are too old)
log.Println("something went wrong, transaction listening unexpectedly finished")
}