dero-swaps/clients/clients.go

78 lines
1.4 KiB
Go
Raw Normal View History

2024-04-20 22:21:02 +02:00
package clients
import (
"log"
2024-04-20 22:23:48 +02:00
"swap-client/coin"
2024-04-20 22:21:02 +02:00
"github.com/lesismal/nbio/nbhttp/websocket"
)
2024-04-20 22:23:48 +02:00
func IsExternalSwapAvailable(pair string, amount float64) (ok bool, client *websocket.Conn) {
2024-04-20 22:21:02 +02:00
Clients.Range(func(key any, value any) bool {
c := value.(ClientInfo)
for _, p := range c.PairInfo {
2024-04-20 22:23:48 +02:00
if p.Pair == pair && p.Balance >= amount {
2024-04-20 22:21:02 +02:00
ok = true
client = key.(*websocket.Conn)
return false
}
}
return true
})
return
}
2024-04-20 22:23:48 +02:00
func PrepareExternalSwap(pair string, amount float64) (bool, *websocket.Conn) {
2024-04-20 22:21:02 +02:00
// only XMR swaps
if pair != coin.XMRDERO && pair != coin.DEROXMR {
log.Println("Only 3rd party XMR swaps")
return false, nil
}
2024-04-20 22:23:48 +02:00
ok, conn := IsExternalSwapAvailable(pair, amount)
2024-04-20 22:21:02 +02:00
if !ok {
log.Println("No 3rd party swaps available")
return false, nil
}
return true, conn
}
func (c *SwapState) ChangeClientState(mode uint, conn *websocket.Conn) {
c.Lock()
defer c.Unlock()
if mode == LOCK {
c.Client[conn] = true
} else {
c.Client[conn] = false
}
}
func (c *SwapState) CheckClientState(conn *websocket.Conn) bool {
c.RLock()
defer c.RUnlock()
return c.Client[conn]
}
func (c *SwapState) AddOrigin(conn *websocket.Conn, target *websocket.Conn) {
c.Lock()
defer c.Unlock()
c.Result[conn] = target
}
func (c *SwapState) GetOrigin(conn *websocket.Conn) *websocket.Conn {
c.RLock()
defer c.RUnlock()
return c.Result[conn]
}