This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
/
transfer.go
94 lines (81 loc) · 2.36 KB
/
transfer.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
package main
import (
"flag"
"fmt"
"log"
"strings"
"github.com/stellar/go/network"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/clients/horizonclient"
"github.com/threefoldtech/rivine/research/stellar/examples/config"
)
func main() {
var accountname string
flag.StringVar(&accountname, "from", "default", "The name of the account to make the payment from")
var destination string
var assetString string
flag.StringVar(&destination, "destination", "", "Destination address")
flag.StringVar(&assetString, "asset", "", "The asset to transfer in case of non native XLM, format: `code:issuer`")
var amount string
flag.StringVar(&amount, "amount", "10", "The amount of topkens to transfer")
flag.Parse()
pair, err := config.GetKeyPairFromConfig(accountname)
if err != nil {
log.Fatal(err)
}
log.Println("From Address", pair.Address())
sourceAccount, err := getAccountDetails(pair.Address())
if err != nil {
log.Fatal(err)
}
var asset txnbuild.Asset
if assetString == "" {
asset = txnbuild.NativeAsset{}
} else {
assetparts := strings.SplitN(assetString, ":", 2)
if len(assetparts) != 2 {
log.Fatalln("Invalid asset format")
}
asset = txnbuild.CreditAsset{
Code: assetparts[0],
Issuer: assetparts[1],
}
}
payment := txnbuild.Payment{
Destination: destination,
Amount: amount,
Asset: asset,
}
tx := txnbuild.Transaction{
SourceAccount: &sourceAccount,
Operations: []txnbuild.Operation{&payment},
Timebounds: txnbuild.NewInfiniteTimeout(), // Use a real timeout in production!
Network: network.TestNetworkPassphrase,
}
txe, err := tx.BuildSignEncode(pair)
if err != nil {
panic(err)
}
// And finally, send it off to Stellar!
resp, err := horizon.DefaultTestNetClient.SubmitTransaction(txe)
if err != nil {
he := err.(*horizon.Error)
log.Println(he.Problem.Detail)
resultcodes := he.ResultCodes
log.Println(resultcodes)
for _, ex := range he.Problem.Extras {
log.Printf("%s\n", ex)
}
panic(err)
}
fmt.Println("Successful Transaction:")
fmt.Println("Ledger:", resp.Ledger)
fmt.Println("Hash:", resp.Hash)
}
func getAccountDetails(address string) (account horizon.Account, err error) {
client := horizonclient.DefaultTestNetClient
ar := horizonclient.AccountRequest{AccountID: address}
account, err = client.AccountDetail(ar)
return
}