-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstellar_assets.go
113 lines (105 loc) · 3.64 KB
/
stellar_assets.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
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"github.com/stellar/go/build"
"github.com/stellar/go/keypair"
"log"
"net/http"
"github.com/stellar/go/clients/horizon"
)
func fillAccounts(addresses []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 []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 trust(asset build.Asset, limit string, address string, seed string) {
tx, err := build.Transaction(
build.SourceAccount{address},
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.TestNetwork,
build.Trust(asset.Code, asset.Issuer, build.Limit(limit)),
)
if err != nil { log.Fatal(err) }
signAndSubmit(tx, seed, "Create orange trustline")
}
func signAndSubmit(tx *build.TransactionBuilder, seed string, privateMemo string) {
txe, err := tx.Sign(seed)
if err != nil { log.Fatal(err) }
txeB64, err := txe.Base64()
if err != nil { log.Fatal(err) }
_, err = horizon.DefaultTestNetClient.SubmitTransaction(txeB64)
if err != nil {
log.Fatal(err)
}
log.Println("Transaction successfully submitted:", privateMemo)
}
func makeOrangePurchaseOffer(rate build.Rate, amount build.Amount, address string, seed string) {
tx, err := build.Transaction(
build.SourceAccount{address},
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.TestNetwork,
build.CreateOffer(rate, amount),
)
if err != nil { log.Fatal(err) }
signAndSubmit(tx, seed, "Make purchase offer")
}
func makeOrangeSellOffer(rate build.Rate, amount build.Amount, address string, seed string) {
tx, err := build.Transaction(
build.SourceAccount{address},
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.TestNetwork,
build.CreateOffer(rate, amount),
)
if err != nil { log.Fatal(err) }
signAndSubmit(tx, seed, "Make sell offer")
}
func confirmPayment(asset build.Asset, amount string, destinationAddress string, seed string) {
tx, err := build.Transaction(
build.SourceAccount{seed},
build.TestNetwork,
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.Payment(
build.Destination{AddressOrSeed: destinationAddress},
build.CreditAmount{asset.Code, asset.Issuer, amount},
),
)
if err != nil { log.Fatal(err) }
signAndSubmit(tx, seed, "Oranges arrived")
}
func main() {
seller, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
buyer, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
addresses := []string{seller.Address(), buyer.Address()}
fillAccounts(addresses)
logBalances(addresses)
OrangeCreditAsset := build.CreditAsset("Orange", seller.Address())
trust(OrangeCreditAsset, "500", buyer.Address(), buyer.Seed())
sellOrangeRate := build.Rate{Selling: OrangeCreditAsset, Buying: build.NativeAsset(), Price: "0.5"}
buyOrangeRate := build.Rate{Selling: build.NativeAsset(), Buying: OrangeCreditAsset, Price: "2"}
makeOrangeSellOffer(sellOrangeRate, "20", seller.Address(), seller.Seed())
makeOrangePurchaseOffer(buyOrangeRate, "20", buyer.Address(), buyer.Seed())
logBalances(addresses)
confirmPayment(OrangeCreditAsset, "20", seller.Address(), buyer.Seed())
logBalances(addresses)
}