202 lines
5.0 KiB
Go
202 lines
5.0 KiB
Go
|
package cfg
|
||
|
|
||
|
import (
|
||
|
"dero-swap/coin"
|
||
|
"dero-swap/dero"
|
||
|
"dero-swap/monero"
|
||
|
"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
|
||
|
}
|
||
|
|
||
|
dero.Dero_Daemon = jsonrpc.NewClient("http://" + Settings.Dero_daemon + "/json_rpc")
|
||
|
dero.Dero_Wallet = jsonrpc.NewClient("http://" + Settings.Dero_wallet + "/json_rpc")
|
||
|
monero.Monero_Wallet = jsonrpc.NewClient("http://" + Settings.Monero_wallet + "/json_rpc")
|
||
|
|
||
|
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
|
||
|
|
||
|
// 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 {
|
||
|
|
||
|
if Settings.Dero_daemon == "" || Settings.Dero_wallet == "" {
|
||
|
log.Println("Dero Daemon or Dero Wallet is not set")
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for p := range coin.Pairs {
|
||
|
switch p {
|
||
|
case coin.BTCDERO, coin.DEROBTC:
|
||
|
if Settings.BTC_daemon == "" || Settings.BTC_dir == "" {
|
||
|
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
|
||
|
return false
|
||
|
} else {
|
||
|
coin.BTC_dir = Settings.BTC_dir
|
||
|
}
|
||
|
case coin.LTCDERO, coin.DEROLTC:
|
||
|
if Settings.LTC_daemon == "" || Settings.LTC_dir == "" {
|
||
|
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
|
||
|
return false
|
||
|
} else {
|
||
|
coin.LTC_dir = Settings.LTC_dir
|
||
|
}
|
||
|
case coin.ARRRDERO, coin.DEROARRR:
|
||
|
if Settings.ARRR_daemon == "" || Settings.ARRR_dir == "" {
|
||
|
log.Printf("%s pair is set, but daemon or directory is not set\n", p)
|
||
|
return false
|
||
|
} else {
|
||
|
coin.ARRR_dir = Settings.ARRR_dir
|
||
|
}
|
||
|
case coin.XMRDERO, coin.DEROXMR:
|
||
|
if Settings.Monero_wallet == "" {
|
||
|
log.Printf("%s pair is set, but wallet is not set\n", p)
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if dero.GetHeight() == 0 || dero.CheckBlockHeight() == 0 {
|
||
|
log.Println("Dero daemon or wallet is not available")
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|