-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
92 lines (75 loc) · 3.06 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
package main
import (
"fmt"
"os"
"github.com/DefiantLabs/RedpointSwap/api"
"github.com/DefiantLabs/RedpointSwap/api/middleware"
"github.com/DefiantLabs/RedpointSwap/config"
"github.com/DefiantLabs/RedpointSwap/osmosis"
"github.com/DefiantLabs/RedpointSwap/zenith"
sdk "github.com/cosmos/cosmos-sdk/types"
"go.uber.org/zap"
)
func main() {
conf := "config.toml"
if len(os.Args) > 1 {
conf = os.Args[1]
}
var err error
config.Conf, err = config.GetConfig(conf)
if err != nil {
fmt.Println("Error getting config file. Err: ", err)
os.Exit(1)
}
logLevel := config.Conf.Api.LogLevel
logPath := config.Conf.Api.LogPath
config.DoConfigureLogger([]string{logPath, "stdout"}, logLevel)
disallowedExampleSecretKey := "example-key-do-not-use-this-k3y-1n-producti0n"
//It is insecure to configure a SHA256 key with less than a 32 byte secret key
if len(config.Conf.JWT.SecretKey) < 32 || string(config.Conf.JWT.SecretKey) == disallowedExampleSecretKey {
config.Logger.Fatal("Insecure JWT configuration", zap.Int("Secret key length", len(config.Conf.JWT.SecretKey)))
}
//Initialize the codecs for Osmosis
osmosis.Initialize()
//Chain tx and query client
txClient, err := osmosis.GetOsmosisTxClient(config.Conf.Api.ChainID, config.Conf.GetApiRpcSearchTxEndpoint(),
config.Conf.Api.KeyringHomeDir, config.Conf.Api.KeyringBackend, config.Conf.Api.HotWalletKey)
if err != nil {
config.Logger.Fatal("GetOsmosisTxClient", zap.Error(err))
}
//get the bech32 address for the given key
addr, err := osmosis.GetKeyAddressForKey(config.Conf.Api.ChainID, config.Conf.GetApiRpcSearchTxEndpoint(),
config.Conf.Api.KeyringHomeDir, config.Conf.Api.KeyringBackend, config.Conf.Api.HotWalletKey)
if err != nil {
config.Logger.Fatal("GetKeyAddressForKey", zap.Error(err))
}
config.HotWalletAddress = addr
//Make sure the hot wallet has funds
hotWalletBalances, err := osmosis.GetAccountBalances(txClient, config.HotWalletAddress)
if err != nil {
config.Logger.Fatal("GetAccountBalances", zap.Error(err))
}
arbWalletBalanceRequired := sdk.NewCoin(config.Conf.Api.ArbitrageDenom, sdk.NewInt(config.Conf.Api.ArbitrageDenomMinAmount))
arbWalletBalanceActual := osmosis.GetTokenBalance(config.Conf.Api.ArbitrageDenom, hotWalletBalances)
if !arbWalletBalanceActual.GTE(arbWalletBalanceRequired.Amount) {
config.Logger.Fatal("Hot wallet insufficient balance", zap.String("Required balance", arbWalletBalanceRequired.String()))
}
config.HotWalletArbBalance = arbWalletBalanceActual
newBlocks := make(chan int64)
done := make(chan struct{})
//Detect when new blocks are produced on the chain
go func() {
defer close(done)
osmosis.TrackBlocks(config.Conf.GetApiWebsocketEndpoint(), newBlocks)
}()
//Track average time between blocks and notify Zenith when a new block is available
go func() {
defer close(done)
osmosis.ProcessNewBlock(newBlocks, []func(int64, int64){zenith.ZenithBlockNotificationHandler, api.AuthzBlockNotificationHandler, api.ExecuteQueuedZenith, api.ParseZenithCommittedTxs})
}()
go func() {
defer close(done)
middleware.InitializeRestApi()
}()
<-done
}