/
main.go
129 lines (104 loc) · 2.9 KB
/
main.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
package main
import (
_ "github.com/joho/godotenv/autoload"
)
import (
"bytes"
"encoding/json"
"fmt"
"github.com/HydroProtocol/hydro-sdk-backend/sdk/ethereum"
"github.com/HydroProtocol/hydro-sdk-backend/utils"
"io/ioutil"
"math"
"math/rand"
"net/http"
"os"
"time"
)
var apiURL = os.Getenv("HSK_API_URL")
func randomNumber(min, max, decimals float64) float64 {
r := rand.Float64()*(max-min) + min
pow := math.Pow(10, float64(decimals))
return math.Floor(r*pow) / pow
}
// ethereum-test-node
// maker pk and address
// https://github.com/HydroProtocol/ethereum-test-node
const pk = "0xa6553a3cbade744d6c6f63e557345402abd93e25cd1f1dba8bb0d374de2fcf4f"
const address = "0x126aa4ef50a6e546aa5ecd1eb83c060fb780891a"
func getHydroAuthenticationHeader() string {
message := "HYDRO-AUTHENTICATION"
signature, _ := ethereum.PersonalSign([]byte(message), pk)
return fmt.Sprintf("%s#%s#%s", address, message, utils.Bytes2HexP(signature))
}
func setReqHeader(req *http.Request) {
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Hydro-Authentication", getHydroAuthenticationHeader())
}
func placeOrder() {
// random price
price := randomNumber(1, 1.5, 2)
// random amount
amount := randomNumber(1, 5, 4)
// side
var side string
if randomNumber(0, 1, 1) > 0.5 {
side = "buy"
} else {
side = "sell"
}
marketID := "HOT-DAI"
body, _ := json.Marshal(map[string]interface{}{
"amount": fmt.Sprintf("%f", amount),
"price": fmt.Sprintf("%f", price),
"side": side,
"orderType": "limit",
"marketID": marketID,
"isMakerOnly": false,
})
req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/orders/build", apiURL), bytes.NewReader(body))
setReqHeader(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
utils.Error("build order req error: %v", err)
}
utils.Info("build order success %s", body)
resBytes, _ := ioutil.ReadAll(res.Body)
var buildOrderRes struct {
Status int `json:"status"`
Data struct {
Order struct {
ID string `json:"id"`
} `json:"order"`
} `json:"data"`
}
_ = json.Unmarshal(resBytes, &buildOrderRes)
signature, _ := ethereum.PersonalSign(
utils.Hex2Bytes(buildOrderRes.Data.Order.ID),
pk,
)
placeOrderRequestBody, _ := json.Marshal(map[string]interface{}{
"orderID": buildOrderRes.Data.Order.ID,
"signature": utils.Bytes2HexP(toOrderSignature(signature)),
"method": 0,
})
req, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/orders", apiURL), bytes.NewReader(placeOrderRequestBody))
setReqHeader(req)
res, err = http.DefaultClient.Do(req)
if err != nil {
utils.Error("place order req error: %v", err)
}
utils.Info("place order success %s", buildOrderRes.Data.Order.ID)
}
func toOrderSignature(sign []byte) []byte {
var res [96]byte
copy(res[:], []byte{sign[64] + 27})
copy(res[32:], sign[:64])
return res[:]
}
func main() {
for {
placeOrder()
time.Sleep(3 * time.Second)
}
}