dero-swaps/cfg/config.go

220 lines
5.6 KiB
Go
Raw Normal View History

2024-04-11 13:35:17 +01:00
package cfg
import (
"dero-swap/coin"
"dero-swap/dero"
"dero-swap/monero"
2024-04-20 21:21:02 +01:00
"encoding/base64"
2024-04-11 13:35:17 +01:00
"encoding/json"
"log"
"os"
"strings"
"github.com/ybbus/jsonrpc/v3"
)
// load config file
func LoadConfig() {
fd, err := os.ReadFile("config.json")
if err != nil {
log.Printf("Error loading config file: %v\n", err)
return
}
err = json.Unmarshal(fd, &Settings)
if err != nil {
log.Printf("Error parsing config file: %v\n", err)
return
}
2024-04-20 21:21:02 +01:00
dero.Dero_Daemon = jsonrpc.NewClient("http://" + Settings.Dero_Daemon + "/json_rpc")
if Settings.Dero_Login != "" {
dero.RPC_Login = base64.StdEncoding.EncodeToString([]byte(Settings.Dero_Login))
dero.Dero_Wallet = jsonrpc.NewClientWithOpts("http://"+Settings.Dero_Wallet+"/json_rpc", &jsonrpc.RPCClientOpts{
CustomHeaders: map[string]string{
"Authorization": "Basic " + dero.RPC_Login,
},
})
} else {
dero.Dero_Wallet = jsonrpc.NewClient("http://" + Settings.Dero_Wallet + "/json_rpc")
log.Println("Dero Wallet: No RPC authorization specified")
}
monero.Monero_Wallet = jsonrpc.NewClient("http://" + Settings.Monero_Wallet + "/json_rpc")
2024-04-11 13:35:17 +01:00
2024-04-20 21:21:02 +01:00
coin.XTC_URL[coin.BTCDERO] = "http://" + Settings.BTC_Daemon
coin.XTC_URL[coin.LTCDERO] = "http://" + Settings.LTC_Daemon
coin.XTC_URL[coin.ARRRDERO] = "http://" + Settings.ARRR_Daemon
2024-04-11 13:35:17 +01:00
// check if pair is "supported"
for _, p := range Settings.Pairs {
supported := false
for i := range coin.Supported_pairs {
if p == coin.Supported_pairs[i] {
supported = true
break
}
}
if supported {
coin.Pairs[p] = true
} else {
log.Printf("%s is not a supported pair\n", p)
}
}
log.Printf("Config successfully loaded\n")
LoadFees()
}
// load fees file
func LoadFees() {
fd, err := os.ReadFile("fees.json")
if err != nil {
log.Printf("Error loading fees file: %v\n", err)
return
}
err = json.Unmarshal(fd, &SwapFees)
if err != nil {
log.Printf("Error parsing fees file: %v\n", err)
return
}
log.Printf("%-14s: Buy: %.2f%% / Sell: %.2f%%\n", "Fees", SwapFees.Swap.Ask, SwapFees.Swap.Bid)
}
// basic config check
func CheckConfig() bool {
2024-04-20 21:21:02 +01:00
if Settings.Dero_Daemon == "" || Settings.Dero_Wallet == "" {
2024-04-11 13:35:17 +01:00
log.Println("Dero Daemon or Dero Wallet is not set")
return false
}
for p := range coin.Pairs {
switch p {
case coin.BTCDERO, coin.DEROBTC:
2024-04-20 21:21:02 +01:00
if Settings.BTC_Daemon == "" || Settings.BTC_Dir == "" {
2024-04-11 13:35:17 +01:00
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
return false
} else {
2024-04-20 21:21:02 +01:00
coin.BTC_Dir = Settings.BTC_Dir
2024-04-11 13:35:17 +01:00
}
case coin.LTCDERO, coin.DEROLTC:
2024-04-20 21:21:02 +01:00
if Settings.LTC_Daemon == "" || Settings.LTC_Dir == "" {
2024-04-11 13:35:17 +01:00
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
return false
} else {
2024-04-20 21:21:02 +01:00
coin.LTC_Dir = Settings.LTC_Dir
2024-04-11 13:35:17 +01:00
}
case coin.ARRRDERO, coin.DEROARRR:
2024-04-20 21:21:02 +01:00
if Settings.ARRR_Daemon == "" || Settings.ARRR_Dir == "" {
2024-04-11 13:35:17 +01:00
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
return false
} else {
2024-04-20 21:21:02 +01:00
coin.ARRR_Dir = Settings.ARRR_Dir
2024-04-11 13:35:17 +01:00
}
case coin.XMRDERO, coin.DEROXMR:
2024-04-20 21:21:02 +01:00
if Settings.Monero_Wallet == "" {
2024-04-11 13:35:17 +01:00
log.Printf("%s pair is set, but wallet is not set\n", p)
return false
}
}
}
2024-04-20 21:21:02 +01:00
if dero.RPC_Login != "" {
log.Printf("%-14s: %s\n", "Dero Wallet", "Using RPC authorization")
}
dero_wallet := dero.GetAddress()
if dero.GetHeight() == 0 || dero.CheckBlockHeight() == 0 || dero_wallet == "" {
2024-04-11 13:35:17 +01:00
log.Println("Dero daemon or wallet is not available")
return false
}
2024-04-20 21:21:02 +01:00
log.Printf("%-14s: %s\n", "Dero Wallet", dero_wallet)
2024-04-11 13:35:17 +01:00
return true
}
// TODO: optimization needed
func LoadWallets() {
for p := range coin.Pairs {
switch p {
case coin.ARRRDERO, coin.DEROARRR:
addr := coin.ARRR_GetAddress()
if !coin.XTCValidateAddress(p, addr) {
log.Printf("Disable pair \"%s\": wallet not available or other error\n", p)
delete(coin.Pairs, p)
} else {
if coin.ARRR_address == "" {
coin.ARRR_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "ARRR Wallet", addr)
}
}
case coin.XMRDERO, coin.DEROXMR:
addr := monero.GetAddress()
if !monero.ValidateAddress(addr) {
log.Printf("Disable pair \"%s\": wallet not available or other error\n", p)
delete(coin.Pairs, p)
} else {
if coin.XMR_address == "" {
coin.XMR_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "XMR Wallet", addr)
}
}
case coin.LTCDERO, coin.DEROLTC:
ok, err := coin.XTCLoadWallet(p)
if !ok && !strings.Contains(err, "is already loaded") {
ok, err := coin.XTCNewWallet(p)
if !ok {
log.Printf("Disable pair \"%s\": %s\n", p, err)
delete(coin.Pairs, p)
} else {
addr := coin.XTCGetAddress(p)
if coin.LTC_address == "" {
coin.LTC_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "LTC Wallet", addr)
}
}
} else {
addr := coin.XTCGetAddress(p)
if coin.LTC_address == "" {
coin.LTC_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "LTC Wallet", addr)
}
}
case coin.BTCDERO, coin.DEROBTC:
ok, err := coin.XTCLoadWallet(p)
if !ok && !strings.Contains(err, "is already loaded") {
ok, err := coin.XTCNewWallet(p)
if !ok {
log.Printf("Disable pair \"%s\": %s\n", p, err)
delete(coin.Pairs, p)
} else {
addr := coin.XTCGetAddress(p)
if coin.BTC_address == "" {
coin.BTC_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "BTC Wallet", addr)
}
}
} else {
addr := coin.XTCGetAddress(p)
if coin.BTC_address == "" {
coin.BTC_address = addr
coin.SimplePairs[p] = true
log.Printf("%-14s: %s\n", "BTC Wallet", addr)
}
}
default:
continue
}
}
}