2022-04-25 17:29:38 +02:00
|
|
|
package main
|
2022-05-03 17:54:51 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-11 11:32:11 +02:00
|
|
|
"fmt"
|
2022-05-19 08:52:34 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
batch_balance "github.com/grassrootseconomics/cic-go/batch_balance"
|
2022-06-02 10:24:58 +02:00
|
|
|
"github.com/grassrootseconomics/cic-go/meta"
|
2022-05-11 11:32:11 +02:00
|
|
|
cic_net "github.com/grassrootseconomics/cic-go/net"
|
2022-05-19 08:52:34 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/grassrootseconomics/cic-go/provider"
|
2022-05-03 17:54:51 +02:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
"github.com/knadh/koanf"
|
|
|
|
"github.com/knadh/koanf/parsers/toml"
|
|
|
|
"github.com/knadh/koanf/providers/env"
|
|
|
|
"github.com/knadh/koanf/providers/file"
|
|
|
|
"github.com/nleof/goyesql"
|
|
|
|
)
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
type config struct {
|
|
|
|
Db struct {
|
|
|
|
Postgres string `koanf:"postgres"`
|
|
|
|
Redis string `koanf:"redis"`
|
|
|
|
}
|
2022-05-11 15:57:56 +02:00
|
|
|
Server struct {
|
2022-05-12 11:32:33 +02:00
|
|
|
Address string `koanf:"address"`
|
|
|
|
Cors []string `koanf:"cors"`
|
2022-05-11 15:57:56 +02:00
|
|
|
}
|
2022-05-05 14:01:34 +02:00
|
|
|
Chain struct {
|
2022-05-19 08:52:34 +02:00
|
|
|
RpcProvider string `koanf:"rpc"`
|
|
|
|
TokenRegistry string `koanf:"index"`
|
|
|
|
BalanceResolver string `koanf:"balances_resolver"`
|
2022-05-05 14:01:34 +02:00
|
|
|
}
|
2022-05-19 15:24:26 +02:00
|
|
|
Syncer struct {
|
|
|
|
Enabled bool `koanf:"enabled"`
|
|
|
|
}
|
|
|
|
Api struct {
|
|
|
|
Enabled bool `koan:"enabled"`
|
|
|
|
}
|
2022-05-05 14:01:34 +02:00
|
|
|
Syncers map[string]string `koanf:"syncers"`
|
2022-06-02 10:24:58 +02:00
|
|
|
Meta struct {
|
2022-06-04 12:27:41 +02:00
|
|
|
Endpoint string `koanf:"endpoint"`
|
2022-06-02 10:24:58 +02:00
|
|
|
}
|
|
|
|
Jwt struct {
|
|
|
|
Secret string `koanf:"secret"`
|
|
|
|
}
|
2022-05-05 14:01:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-11 11:32:11 +02:00
|
|
|
type queries struct {
|
|
|
|
core goyesql.Queries
|
|
|
|
dashboard goyesql.Queries
|
2022-05-14 10:15:45 +02:00
|
|
|
public goyesql.Queries
|
2022-06-02 10:24:58 +02:00
|
|
|
admin goyesql.Queries
|
2022-05-11 11:32:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
func loadConfig(configFilePath string, k *koanf.Koanf) error {
|
2022-05-03 17:54:51 +02:00
|
|
|
confFile := file.Provider(configFilePath)
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := k.Load(confFile, toml.Parser()); err != nil {
|
2022-05-03 17:54:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-05-05 14:01:34 +02:00
|
|
|
if err := k.Load(env.Provider("", ".", func(s string) string {
|
2022-05-03 17:54:51 +02:00
|
|
|
return strings.ReplaceAll(strings.ToLower(
|
2022-05-05 14:01:34 +02:00
|
|
|
strings.TrimPrefix(s, "")), "_", ".")
|
2022-05-03 17:54:51 +02:00
|
|
|
}), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
err := k.UnmarshalWithConf("", &conf, koanf.UnmarshalConf{Tag: "koanf"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
func connectDb(dsn string) error {
|
2022-05-05 14:01:34 +02:00
|
|
|
var err error
|
|
|
|
db, err = pgxpool.Connect(context.Background(), dsn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-05 18:58:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:52:34 +02:00
|
|
|
func parseRedis(dsn string) error {
|
|
|
|
var err error
|
|
|
|
rClient, err = asynq.ParseRedisURI(dsn)
|
2022-05-05 18:58:27 +02:00
|
|
|
if err != nil {
|
2022-05-19 08:52:34 +02:00
|
|
|
return err
|
2022-05-05 18:39:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 08:52:34 +02:00
|
|
|
return nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 08:52:34 +02:00
|
|
|
func loadProvider(rpcEndpoint string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
rpcProvider, err = provider.NewRpcProvider(rpcEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadCicNet(tokenIndex common.Address) error {
|
2022-05-05 14:01:34 +02:00
|
|
|
var err error
|
|
|
|
|
2022-05-06 15:53:57 +02:00
|
|
|
cicnetClient, err = cic_net.NewCicNet(rpcProvider, tokenIndex)
|
2022-05-03 17:54:51 +02:00
|
|
|
if err != nil {
|
2022-05-05 14:01:34 +02:00
|
|
|
return err
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:01:34 +02:00
|
|
|
return nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-06-02 10:24:58 +02:00
|
|
|
func loadCicMeta(metaEndpoint string) {
|
|
|
|
metaClient = meta.NewCicMeta(metaEndpoint)
|
|
|
|
}
|
|
|
|
|
2022-05-19 08:52:34 +02:00
|
|
|
func loadBatchBalance(balanceResolver common.Address) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
batchBalance, err = batch_balance.NewBatchBalance(rpcProvider, balanceResolver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:32:11 +02:00
|
|
|
func loadQueries(sqlFilesPath string) error {
|
|
|
|
coreQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/core.sql", sqlFilesPath))
|
2022-05-03 17:54:51 +02:00
|
|
|
if err != nil {
|
2022-05-05 14:01:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:32:11 +02:00
|
|
|
dashboardQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/dashboard.sql", sqlFilesPath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 10:15:45 +02:00
|
|
|
publicQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/public.sql", sqlFilesPath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-02 10:24:58 +02:00
|
|
|
adminQueries, err := goyesql.ParseFile(fmt.Sprintf("%s/admin.sql", sqlFilesPath))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-11 11:32:11 +02:00
|
|
|
preparedQueries = &queries{
|
|
|
|
core: coreQueries,
|
|
|
|
dashboard: dashboardQueries,
|
2022-05-14 10:15:45 +02:00
|
|
|
public: publicQueries,
|
2022-06-02 10:24:58 +02:00
|
|
|
admin: adminQueries,
|
2022-05-11 11:32:11 +02:00
|
|
|
}
|
2022-05-03 17:54:51 +02:00
|
|
|
|
2022-05-11 11:32:11 +02:00
|
|
|
return nil
|
2022-05-03 17:54:51 +02:00
|
|
|
}
|