-
Notifications
You must be signed in to change notification settings - Fork 3
/
dex.go
84 lines (70 loc) · 2.67 KB
/
dex.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 dex
import (
"github.com/pkg/errors"
//"log"
ticker "github.com/Varunram/essentials/exchangetickers"
utils "github.com/Varunram/essentials/utils"
xlm "github.com/Varunram/essentials/xlm"
stablecoin "github.com/Varunram/essentials/xlm/stablecoin"
build "github.com/stellar/go/txnbuild"
)
// package dex contains functions for interfacing with the stellar dex
// NewBuyOrder creates a new buy order on the stellar dex
func NewBuyOrder(seed string, assetName string, issuer string,
amount string, price string) (int32, string, error) {
sourceAccount, mykp, err := xlm.ReturnSourceAccount(seed)
if err != nil {
return -1, "", errors.Wrap(err, "could not load client details, quitting")
}
buyOffer := build.ManageBuyOffer{
Selling: build.NativeAsset{},
Buying: build.CreditAsset{assetName, issuer},
Amount: amount,
Price: price,
OfferID: 0,
}
memo := "buy"
// once the offer is completed, we need to send a follow up tx to send funds to the requested address
return xlm.SendTx(mykp, &sourceAccount, memo, build.Operation(&buyOffer))
}
// NewSellOrder creates a new sell order on the stellar dex
func NewSellOrder(seed string, assetName string, issuer string, amount string,
price string) (int32, string, error) {
sourceAccount, mykp, err := xlm.ReturnSourceAccount(seed)
if err != nil {
return -1, "", errors.Wrap(err, "could not load client details, quitting")
}
sellOffer := build.ManageBuyOffer{
Selling: build.CreditAsset{assetName, issuer},
Buying: build.NativeAsset{},
Amount: amount,
Price: price,
OfferID: 0,
}
memo := "sell"
return xlm.SendTx(mykp, &sourceAccount, memo, build.Operation(&sellOffer))
}
// StableCoinBuy gets the price from an oracle and places an order on the DEX to buy AnchorUSD
func StableCoinBuy(seed string, amount string) (int32, string, error) {
assetName := "USD"
issuer := stablecoin.AnchorUSDAddress
price, err := ticker.BinanceTicker()
if err != nil {
return -1, "", errors.New("could not fetch price form binance, quitting")
}
price = price * 1.02 // a small premium to get the order fulfilled immediately
ftss, _ := utils.ToString(price)
return NewBuyOrder(seed, assetName, issuer, amount, ftss)
}
// StableCoinSell places a sell order for STABLEUSD on the Stellar dex
func StableCoinSell(seed string, amount string) (int32, string, error) {
assetName := "USD"
issuer := stablecoin.AnchorUSDAddress
price, err := ticker.BinanceTicker()
if err != nil {
return -1, "", errors.New("could not fetch price form binance, quitting")
}
price = price * 1.02 // a small premium to get the order fulfilled immediately
ftss, _ := utils.ToString(price)
return NewSellOrder(seed, assetName, issuer, amount, ftss)
}