-
Notifications
You must be signed in to change notification settings - Fork 117
/
main.go
100 lines (79 loc) · 2.96 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
package main
import (
"context"
"encoding/base64"
"log"
"strings"
"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"
)
func main() {
client := liteclient.NewConnectionPool()
// get config
cfg, err := liteclient.GetConfigFromUrl(context.Background(), "https://ton.org/testnet-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
}
// api client with full proof checks
api := ton.NewAPIClient(client, ton.ProofCheckPolicyFast).WithRetry()
api.SetTrustedBlockFromConfig(cfg)
// bound all requests to single ton node
ctx := client.StickyContext(context.Background())
// seed words of account, you can generate them with any wallet or using wallet.NewSeed() method
words := strings.Split("diet diet attack autumn expose honey skate lounge holiday opinion village priority major enroll romance famous motor pact hello rubber express warfare rose whisper", " ")
w, err := wallet.FromSeed(api, words, wallet.V4R2)
if err != nil {
log.Fatalln("FromSeed err:", err.Error())
return
}
log.Println("wallet address:", w.WalletAddress())
log.Println("fetching and checking proofs since config init block, it may take near a minute...")
block, err := api.CurrentMasterchainInfo(context.Background())
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!")
balance, err := w.GetBalance(ctx, block)
if err != nil {
log.Fatalln("GetBalance err:", err.Error())
return
}
if balance.Nano().Uint64() >= 3000000 {
addr := address.MustParseAddr("EQCD39VS5jcptHL8vMjEXrzGaRcCVYto7HUn4bpAOg8xqB2N")
log.Println("sending transaction and waiting for confirmation...")
// if destination wallet is not initialized (or you don't care)
// you should set bounce to false to not get money back.
// If bounce is true, money will be returned in case of not initialized destination wallet or smart-contract error
bounce := false
transfer, err := w.BuildTransfer(addr, tlb.MustFromTON("0.003"), bounce, "Hello from tonutils-go!")
if err != nil {
log.Fatalln("Transfer err:", err.Error())
return
}
tx, block, err := w.SendWaitTransaction(ctx, transfer)
if err != nil {
log.Fatalln("SendWaitTransaction err:", err.Error())
return
}
balance, err = w.GetBalance(ctx, block)
if err != nil {
log.Fatalln("GetBalance err:", err.Error())
return
}
log.Printf("transaction confirmed at block %d, hash: %s balance left: %s", block.SeqNo,
base64.StdEncoding.EncodeToString(tx.Hash), balance.String())
return
}
log.Println("not enough balance:", balance.String())
}