-
Notifications
You must be signed in to change notification settings - Fork 4
/
trade_test.go
104 lines (87 loc) · 2.34 KB
/
trade_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
package main
import (
"encoding/hex"
"fmt"
"testing"
"math/rand"
"github.com/stretchr/testify/assert"
"github.com/vulpemventures/go-elements/network"
)
func TestTrade_ExecuteTrade(t *testing.T) {
// setup ocean wallet client
walletSvc, err := NewWalletService("localhost:18000", "default")
if err != nil {
t.Fatal("newOrder", err)
}
// Create a sample order
order, err := submitDummyOrder()
if err != nil {
t.Fatal("newOrder", err)
}
trade, err := FromFundedOrder(
walletSvc,
order,
&UTXO{
Txid: "foo",
Index: 1,
},
)
if err != nil {
t.Fatal("FromFundedOrder", err)
}
// Execute the trade
err = trade.ExecuteTrade()
// Assert that the trade was executed successfully
assert.NoError(t, err)
}
func TestTrade_CancelTrade(t *testing.T) {
// setup ocean wallet client
walletSvc, err := NewWalletService("http://localhost:18000", "default")
if err != nil {
t.Fatal("newOrder")
}
// Create a sample order
order, err := submitDummyOrder()
if err != nil {
t.Fatal("newOrder")
}
// Create a new trade
trade, err := FromFundedOrder(walletSvc, order, &UTXO{
Txid: "foo",
Index: 1,
})
if err != nil {
t.Fatal("FromFundedOrder", err)
}
// Cancel the trade
err = trade.CancelTrade()
// Assert that the trade was cancelled successfully
assert.NoError(t, err)
}
func submitDummyOrder() (*Order, error) {
// Generate random traderScript
traderScript := make([]byte, 32)
traderScriptHex := hex.EncodeToString(traderScript)
// Generate random inputCurrency and inputValue
inputCurrency := generateRandomCurrency()
inputValue := generateRandomValue()
// Generate random outputCurrency and outputValue
outputCurrency := generateRandomCurrency()
outputValue := generateRandomValue()
// Create the order with the generated values
return NewOrder(traderScriptHex, inputCurrency, inputValue, outputCurrency, outputValue, 2, &network.Testnet)
}
func generateRandomCurrency() string {
currencies := []string{"FUSD", "USDT", "L-BTC"}
randomIndex := rand.Intn(len(currencies))
return currencies[randomIndex]
}
func generateRandomValue() string {
// Generate random value using your preferred method
// For example, you can generate a random float value
// within a specific range
minValue := 1.0
maxValue := 1000.0
randomValue := minValue + rand.Float64()*(maxValue-minValue)
return fmt.Sprintf("%.2f", randomValue)
}