-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathoffers_test.go
146 lines (114 loc) · 4.44 KB
/
offers_test.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package microstellar
import (
"fmt"
"log"
)
// This example creates a passive offer on stellar's DEX.
func ExampleMicroStellar_CreateOffer() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Custom USD asset issued by specified issuer
USD := NewAsset("USD", "GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ", Credit4Type)
// Sell 200 USD on the DEX for lumens (at 2 lumens per USD). This is a passive
// offer. (This is equivalent to an offer to buy 400 lumens for 200 USD.)
err := ms.CreateOffer("SCSMBQYTXKZYY7CLVT6NPPYWVDQYDOQ6BB3QND4OIXC7762JYJYZ3RMK",
USD, NativeAsset, "2", "200",
Opts().MakePassive())
if err != nil {
log.Fatalf("CreateOffer: %v", err)
}
fmt.Printf("ok")
// Output: ok
}
// This example updates an existing offer on the DEX.
func ExampleMicroStellar_UpdateOffer() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Custom USD asset issued by specified issuer
USD := NewAsset("USD", "GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ", Credit4Type)
// Update Offer ID 23456 to sell 200 USD on the DEX for lumens (at 1 lumen / USD.)
err := ms.UpdateOffer("SCSMBQYTXKZYY7CLVT6NPPYWVDQYDOQ6BB3QND4OIXC7762JYJYZ3RMK",
"23456", USD, NativeAsset, "1", "200")
if err != nil {
log.Fatalf("UpdateOffer: %v", err)
}
fmt.Printf("ok")
// Output: ok
}
// This example deletes an existing offer on the DEX.
func ExampleMicroStellar_DeleteOffer() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Custom USD asset issued by specified issuer
USD := NewAsset("USD", "GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ", Credit4Type)
// Delete Offer ID 23456 on the DEX.
err := ms.DeleteOffer("SCSMBQYTXKZYY7CLVT6NPPYWVDQYDOQ6BB3QND4OIXC7762JYJYZ3RMK",
"23456", USD, NativeAsset, "0.4")
if err != nil {
log.Fatalf("DeleteOffer: %v", err)
}
fmt.Printf("ok")
// Output: ok
}
// This example creates a new offer using the ManageOffer method.
func ExampleMicroStellar_ManageOffer() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Custom USD asset issued by specified issuer
USD := NewAsset("USD", "GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ", Credit4Type)
// Create an offer to buy 200 lumens at 2 lumens/dollar.
err := ms.ManageOffer("SCSMBQYTXKZYY7CLVT6NPPYWVDQYDOQ6BB3QND4OIXC7762JYJYZ3RMK",
&OfferParams{
OfferType: OfferCreate,
SellAsset: USD,
BuyAsset: NativeAsset,
Price: "2",
SellAmount: "100",
})
if err != nil {
log.Fatalf("ManageOffer: %v", err)
}
fmt.Printf("ok")
// Output: ok
}
// This example lists the offers currently out by an address.
func ExampleMicroStellar_LoadOffers() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Get at most 10 offers made by address in descending order
offers, err := ms.LoadOffers("GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ",
Opts().WithLimit(10).WithSortOrder(SortDescending))
if err != nil {
log.Fatalf("LoadOffers: %v", err)
}
for _, o := range offers {
log.Printf("Offer ID: %v, Selling: %v, Price: %v, Amount: %v", o.ID, o.Selling.Code, o.Price, o.Amount)
}
fmt.Printf("ok")
// Output: ok
}
// This example lists all asks on the DEX between USD <-> XLM
func ExampleMicroStellar_LoadOrderBook() {
// Create a new MicroStellar client connected to a fake network. To
// use a real network replace "fake" below with "test" or "public".
ms := New("fake")
// Custom USD asset issued by specified issuer
USD := NewAsset("USD", "GAIUIQNMSXTTR4TGZETSQCGBTIF32G2L5P4AML4LFTMTHKM44UHIN6XQ", Credit4Type)
// Get at most 10 orders made between USD and XLM
orderbook, err := ms.LoadOrderBook(USD, NativeAsset,
Opts().WithLimit(10).WithSortOrder(SortDescending))
if err != nil {
log.Fatalf("LoadOrderBook: %v", err)
}
// List all the returned asks.
for _, ask := range orderbook.Asks {
log.Printf("ask: %s %s for %s %s/%s", ask.Amount, orderbook.Base.Code, ask.Price, orderbook.Counter.Code, orderbook.Base.Code)
}
fmt.Printf("ok")
// Output: ok
}