-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstellar_pay.go
84 lines (71 loc) · 1.82 KB
/
stellar_pay.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
package main
import (
"github.com/stellar/go/build"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/keypair"
"log"
"net/http"
)
func fillAccounts(addresses [2]string) {
for _, address := range addresses {
friendBotResp, err := http.Get("https://horizon-testnet.stellar.org/friendbot?addr=" + address)
if err != nil {
log.Fatal(err)
}
defer friendBotResp.Body.Close()
}
}
func logBalances(addresses [2]string) {
for _, address := range addresses {
account, err := horizon.DefaultTestNetClient.LoadAccount(address)
if err != nil {
log.Fatal(err)
}
log.Println("Balances for address:", address)
for _, balance := range account.Balances {
log.Println(balance)
}
}
}
func sendLumens(amount string, sourceSeed string, destinationAddress string) {
tx, err := build.Transaction(
build.SourceAccount{sourceSeed},
build.TestNetwork,
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.Payment(
build.Destination{AddressOrSeed: destinationAddress},
build.NativeAmount{Amount: amount},
),
)
if err != nil {
panic(err)
}
txe, err := tx.Sign(sourceSeed)
if err != nil {
panic(err)
}
txeB64, err := txe.Base64()
if err != nil {
panic(err)
}
resp, err := horizon.DefaultTestNetClient.SubmitTransaction(txeB64)
if err != nil {
panic(err)
}
log.Println("Successfully sent", amount, "lumens to", destinationAddress,". Hash:", resp.Hash)
}
func main() {
sourcePair, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
destinationPair, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
addresses := [2]string{sourcePair.Address(), destinationPair.Address()}
fillAccounts(addresses)
logBalances(addresses)
sendLumens("100", sourcePair.Seed(), destinationPair.Address())
logBalances(addresses)
}