-
Notifications
You must be signed in to change notification settings - Fork 14
/
buy.go
233 lines (206 loc) · 5.7 KB
/
buy.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package trade
import (
"encoding/hex"
"errors"
"fmt"
tdexv1 "github.com/tdex-network/tdex-daemon/api-spec/protobuf/gen/tdex/v1"
tradeclient "github.com/tdex-network/tdex-daemon/pkg/trade/client"
trademarket "github.com/tdex-network/tdex-daemon/pkg/trade/market"
tradetype "github.com/tdex-network/tdex-daemon/pkg/trade/type"
"github.com/tdex-network/tdex-daemon/pkg/swap"
"github.com/vulpemventures/go-elements/address"
"google.golang.org/protobuf/proto"
)
var (
// ErrNullAddress ...
ErrNullAddress = errors.New("address must not be null")
// ErrInvalidAsset ...
ErrInvalidAsset = errors.New("asset must be a 32-byte array in hex format")
// ErrInvalidAddress ...
ErrInvalidAddress = errors.New("address is not valid")
// ErrNullPrivateKey ...
ErrNullPrivateKey = errors.New("private key must not be null")
// ErrNullBlindingKey ...
ErrNullBlindingKey = errors.New("blinding key must not be null")
)
// BuyOrSellOpts is the struct given to Buy/Sell method
type BuyOrSellOpts struct {
Market trademarket.Market
Amount uint64
Asset string
Address string
BlindingKey []byte
}
func (o BuyOrSellOpts) validate() error {
if err := o.Market.Validate(); err != nil {
return err
}
if o.Amount <= 0 {
return ErrInvalidAmount
}
if buf, err := hex.DecodeString(o.Asset); err != nil || len(buf) != 32 {
return ErrInvalidAsset
}
if len(o.Address) <= 0 {
return ErrNullAddress
}
if _, err := address.ToOutputScript(o.Address); err != nil {
return ErrInvalidAddress
}
if len(o.BlindingKey) <= 0 {
return ErrNullBlindingKey
}
return nil
}
// Buy creates a new trade proposal with the given arguments and sends it to
// the server which the inner client is connected to. This method returns the
// SwapAccept serialized message eventually returned by the counter-party.
func (t *Trade) Buy(opts BuyOrSellOpts) ([]byte, error) {
if err := opts.validate(); err != nil {
return nil, err
}
return t.marketOrderRequest(
opts.Market,
tradetype.Buy,
opts.Amount,
opts.Asset,
opts.Address,
opts.BlindingKey,
)
}
// BuyOrSellAndCompleteOpts is the struct given to Buy method
type BuyOrSellAndCompleteOpts struct {
Market trademarket.Market
Amount uint64
Asset string
PrivateKey []byte
BlindingKey []byte
}
func (o BuyOrSellAndCompleteOpts) validate() error {
if err := o.Market.Validate(); err != nil {
return err
}
if o.Amount <= 0 {
return ErrInvalidAmount
}
if buf, err := hex.DecodeString(o.Asset); err != nil || len(buf) != 32 {
return ErrInvalidAsset
}
if len(o.PrivateKey) <= 0 {
return ErrNullPrivateKey
}
if len(o.BlindingKey) <= 0 {
return ErrNullBlindingKey
}
return nil
}
// BuyAndComplete creates a new trade proposal with the give arguments. The
// transaction of the resulting SwapAccept message is then signed with the
// provided private key, and sent back again to the connected server, which
// will take care of finalizing and broadcasting it.
func (t *Trade) BuyAndComplete(opts BuyOrSellAndCompleteOpts) (string, error) {
if err := opts.validate(); err != nil {
return "", err
}
w := NewWalletFromKey(opts.PrivateKey, opts.BlindingKey, t.network)
swapAcceptMsg, err := t.marketOrderRequest(
opts.Market,
tradetype.Buy,
opts.Amount,
opts.Asset,
w.Address(),
opts.BlindingKey,
)
if err != nil {
return "", err
}
return t.marketOrderComplete(swapAcceptMsg, w)
}
func (t *Trade) marketOrderRequest(
market trademarket.Market,
tradeType tradetype.TradeType,
amount uint64,
asset string,
addr string,
blindingKey []byte,
) ([]byte, error) {
unspents, err := t.explorer.GetUnspents(addr, [][]byte{blindingKey})
if err != nil {
return nil, err
}
if len(unspents) <= 0 {
return nil, fmt.Errorf("address '%s' is not funded", addr)
}
preview, err := t.Preview(PreviewOpts{
Market: market,
TradeType: int(tradeType),
Amount: amount,
Asset: asset,
})
if err != nil {
return nil, err
}
outputScript, _ := address.ToOutputScript(addr)
outputScriptHex := hex.EncodeToString(outputScript)
psetBase64, err := NewSwapTx(
unspents,
preview.AssetToSend,
preview.AmountToSend,
preview.AssetToReceive,
preview.AmountToReceive,
outputScript,
)
if err != nil {
return nil, err
}
blindingKeyMap := map[string][]byte{
outputScriptHex: blindingKey,
}
swapRequestMsg, err := swap.Request(swap.RequestOpts{
AssetToSend: preview.AssetToSend,
AmountToSend: preview.AmountToSend,
AssetToReceive: preview.AssetToReceive,
AmountToReceive: preview.AmountToReceive,
PsetBase64: psetBase64,
InputBlindingKeys: blindingKeyMap,
OutputBlindingKeys: blindingKeyMap,
})
if err != nil {
return nil, err
}
reply, err := t.client.TradePropose(tradeclient.TradeProposeOpts{
Market: market,
SwapRequest: swapRequestMsg,
TradeType: tradeType,
})
if fail := reply.GetSwapFail(); fail != nil {
return nil, fmt.Errorf("trade proposal has been rejected for reason: %s", fail.GetFailureMessage())
}
return proto.Marshal(reply.GetSwapAccept())
}
func (t *Trade) marketOrderComplete(swapAcceptMsg []byte, w *Wallet) (string, error) {
swapAccept := &tdexv1.SwapAccept{}
proto.Unmarshal(swapAcceptMsg, swapAccept)
psetBase64 := swapAccept.GetTransaction()
signedPset, err := w.Sign(psetBase64)
if err != nil {
return "", err
}
_, swapCompleteMsg, err := swap.Complete(swap.CompleteOpts{
Message: swapAcceptMsg,
Transaction: signedPset,
})
if err != nil {
return "", err
}
reply, err := t.client.TradeComplete(tradeclient.TradeCompleteOpts{
SwapComplete: swapCompleteMsg,
})
if err != nil {
return "", err
}
if swapFail := reply.GetSwapFail(); swapFail != nil {
return "", errors.New(swapFail.GetFailureMessage())
}
return reply.GetTxid(), nil
}