-
Notifications
You must be signed in to change notification settings - Fork 117
/
main.go
85 lines (71 loc) · 2.11 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
package main
import (
"context"
"fmt"
"log"
"sort"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/ton"
)
func main() {
client := liteclient.NewConnectionPool()
// connect to mainnet lite servers
err := client.AddConnectionsFromConfigUrl(context.Background(), "https://ton.org/global.config.json")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
// initialize ton api lite connection wrapper
api := ton.NewAPIClient(client, ton.ProofCheckPolicyFast).WithRetry()
// if we want to route all requests to the same node, we can use it
ctx := client.StickyContext(context.Background())
// we need fresh block info to run get methods
b, err := api.CurrentMasterchainInfo(ctx)
if err != nil {
log.Fatalln("get block err:", err.Error())
return
}
addr := address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N")
// we use WaitForBlock to make sure block is ready,
// it is optional but escapes us from liteserver block not ready errors
res, err := api.WaitForBlock(b.SeqNo).GetAccount(ctx, b, addr)
if err != nil {
log.Fatalln("get account err:", err.Error())
return
}
fmt.Printf("Is active: %v\n", res.IsActive)
if res.IsActive {
fmt.Printf("Status: %s\n", res.State.Status)
fmt.Printf("Balance: %s TON\n", res.State.Balance.String())
if res.Data != nil {
fmt.Printf("Data: %s\n", res.Data.Dump())
}
}
// take last tx info from account info
lastHash := res.LastTxHash
lastLt := res.LastTxLT
fmt.Printf("\nTransactions:\n")
for {
// last transaction has 0 prev lt
if lastLt == 0 {
break
}
// load transactions in batches with size 15
list, err := api.ListTransactions(ctx, addr, 15, lastLt, lastHash)
if err != nil {
log.Printf("send err: %s", err.Error())
return
}
// set previous info from the oldest transaction in list
lastHash = list[0].PrevTxHash
lastLt = list[0].PrevTxLT
// reverse list to show the newest first
sort.Slice(list, func(i, j int) bool {
return list[i].LT > list[j].LT
})
for _, t := range list {
fmt.Println(t.String())
}
}
}