dero-swaps/server.go

180 lines
3.8 KiB
Go
Raw Normal View History

2024-04-11 14:35:17 +02:00
package main
import (
2024-04-20 22:23:48 +02:00
"crypto/tls"
2024-04-11 14:35:17 +02:00
"fmt"
"log"
2024-04-20 22:23:48 +02:00
"net/url"
2024-04-11 14:35:17 +02:00
"strings"
2024-04-20 22:21:02 +02:00
"time"
2024-04-11 14:35:17 +02:00
"github.com/deroproject/derohe/globals"
2024-04-20 22:23:48 +02:00
"github.com/gorilla/websocket"
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
"swap-client/cfg"
"swap-client/coin"
"swap-client/dero"
2024-04-11 14:35:17 +02:00
)
type WS_Message struct {
ID uint64 `json:"id"`
Method string `json:"method"`
Params any `json:"params,omitempty"`
Result any `json:"result"`
}
2024-04-20 22:23:48 +02:00
var Connection *websocket.Conn
2024-04-11 14:35:17 +02:00
// swap other coins to Dero
func Dero_Swap(request coin.Swap_Request) (response coin.Swap_Response) {
var err error
// check if destination wallet is valid. Registered usernames can also be used.
if strings.HasPrefix(request.DeroAddr, "dero1") || strings.HasPrefix(request.DeroAddr, "deroi") {
_, err = globals.ParseValidateAddress(request.DeroAddr)
} else {
if addr := dero.CheckAddress(request.DeroAddr); addr != "" {
request.DeroAddr = addr
} else {
err = fmt.Errorf("invalid address")
}
}
// basic checks
if request.Amount == 0 || err != nil {
response.Error = "invalid request"
return
}
// prevent users from creating too many swap requests
if Delay.CheckUser(request.DeroAddr) {
response.Error = "2 minutes wait time triggered"
return
}
// check if pair is enabled and available
pair := request.Pair
if !coin.IsPairEnabled(pair) || !IsPairAvailable[pair] {
response.Error = fmt.Sprintf("%s swap currently not possible", pair)
return
}
// create swap
err = XTCSwap(pair, request.DeroAddr, coin.RoundFloat(request.Amount, 5), &response)
if err != nil {
response.Error = err.Error()
log.Println(err)
} else {
Delay.AddUser(request.DeroAddr)
}
response.Request = request
return response
}
// swap Dero to other coins
func Reverse_Swap(request coin.Swap_Request) (response coin.Swap_Response) {
var err error
// prevent users from creating too many swap requests
if Delay.CheckUser(request.DeroAddr) {
response.Error = "2 minutes wait time triggered"
return
}
// check if pair is enabled and available
pair := request.Pair
if !coin.IsPairEnabled(pair) || !IsPairAvailable[pair] {
response.Error = fmt.Sprintf("%s swap currently not possible", pair)
return
}
response.Deposit = coin.RoundFloat(request.Amount, 5)
// create swap
err = DeroXTCSwap(pair, request.DeroAddr, response.Deposit, &response)
if err != nil {
response.Error = err.Error()
log.Println(err)
} else {
Delay.AddUser(request.DeroAddr)
}
response.Request = request
return response
}
2024-04-20 22:23:48 +02:00
func StartClient(server url.URL) {
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
var err error
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
for {
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
var in WS_Message
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
dialer := websocket.DefaultDialer
dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
Connection, _, err = websocket.DefaultDialer.Dial(server.String(), nil)
2024-04-11 14:35:17 +02:00
if err != nil {
2024-04-20 22:23:48 +02:00
log.Println("Websocket error, re-connect in 10 seconds")
time.Sleep(time.Second * 10)
continue
2024-04-11 14:35:17 +02:00
}
2024-04-20 22:23:48 +02:00
log.Printf("Connected to server %s\n", cfg.Settings.ServerAddress)
UpdateMarkets()
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
for {
if err := Connection.ReadJSON(&in); err != nil {
break
2024-04-20 22:21:02 +02:00
}
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
var out WS_Message
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
out.ID = in.ID
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
switch in.Method {
case "swap":
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
var request coin.Swap_Request
var response coin.Swap_Response
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
p := in.Params.(map[string]any)
out.Method = "client_ok"
2024-04-11 14:35:17 +02:00
2024-04-20 22:23:48 +02:00
if d, ok := p["pair"]; ok {
request.Pair = d.(string)
}
if d, ok := p["amount"]; ok {
request.Amount = d.(float64)
}
if d, ok := p["dero_address"]; ok {
request.DeroAddr = d.(string)
}
2024-04-20 22:21:02 +02:00
2024-04-20 22:23:48 +02:00
switch request.Pair {
2024-04-26 16:05:47 +02:00
case coin.XMRDERO, coin.LTCDERO:
2024-04-20 22:23:48 +02:00
response = Dero_Swap(request)
2024-04-26 16:05:47 +02:00
case coin.DEROXMR, coin.DEROLTC:
2024-04-20 22:23:48 +02:00
response = Reverse_Swap(request)
default:
return
}
2024-04-20 22:21:02 +02:00
2024-04-20 22:23:48 +02:00
out.Result = response
2024-04-20 22:21:02 +02:00
2024-04-20 22:23:48 +02:00
Connection.WriteJSON(out)
UpdateMarkets()
2024-04-20 22:21:02 +02:00
}
2024-04-11 14:35:17 +02:00
}
2024-04-20 22:23:48 +02:00
log.Println("Websocket error, re-connect in 10 seconds")
time.Sleep(time.Second * 10)
2024-04-11 14:35:17 +02:00
}
}