Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(database): using mongo instead of postgres. #73

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions .env.example

This file was deleted.

12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,9 @@ In [PAC Zone](https://github.com/PACZone) (powered by [Dezh](https://dezh.tech))

Currently, Wrapto is a semi-centralized protocol (half decentralized) since there is no contract support on Pactus. [More details about this are here](#Documentation).

#### Our RoadMap

- [ ] We will add [wPAC](https://github.com/PACZone/wPAC) to More blockchains such as Solana, TON, Aptos, etc.
- [ ] We have a plan to update the protocol and use Pactus contracts (still decentralized and trustless but using contracts) in 2025 Q1 once contracts are available on Pactus (Wrapto Protocol V2).
- [ ] After contract support on the Pactus chain we will add wrapped other coins on Pactus too(Wrapto Protocol V2).

All V1(semi-centralized)an V2(decentralizes smart-contract) [Wrapped Tokens](https://github.com/PACZone/wPAC) minted on different networks, will be compatible with Wrapto.

# Documentation

## Wrapto Protocol V1

You can find all V1 documentations [here](./docs/V1)
You can find all documentations [here](./docs/)

# Contribution

Expand Down
9 changes: 9 additions & 0 deletions config/.develop.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Pactus
WRAPTO_PACTUS_WALLET_PASSWORD=

# Polygon
WRAPTO_POLYGON_PRIVATE_KEY=

# Database
WRAPTO_MONGO_URI=

124 changes: 42 additions & 82 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,63 @@ package config

import (
"os"
"strconv"
"strings"

"github.com/PACZone/wrapto/database"
logger "github.com/PACZone/wrapto/log"
"github.com/PACZone/wrapto/sides/pactus"
"github.com/PACZone/wrapto/sides/polygon"
"github.com/PACZone/wrapto/www/http"
"github.com/joho/godotenv"
"gopkg.in/yaml.v2"
)

type Config struct {
Environment string
Logger LoggerConfig
Pactus PactusConfig
Polygon PolygonConfig
Database DatabaseConfig
HTTPServer HTTPServerConfig
}

type PactusConfig struct {
WalletPath string
WalletPass string
LockAddr string
RPCNode string
}

type PolygonConfig struct {
PrivateKey string
ContractAddr string
RPCNode string
}

type DatabaseConfig struct {
DSN string
}

type HTTPServerConfig struct {
Port string
}

type LoggerConfig struct {
Filename string
LogLevel string
Targets []string
MaxSize int
MaxBackups int
Compress bool
Environment string `yaml:"environment"`
Logger logger.Config
Pactus pactus.Config
Polygon polygon.Config
Database database.Config
HTTPServer http.Config
}

func LoadConfig() (*Config, error) {
maxSizeStr := os.Getenv("LOG_MAX_SIZE")
maxSize, err := strconv.Atoi(maxSizeStr)
func LoadConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
return nil, Error{
reason: err.Error(),
}
}
defer file.Close()

maxBackupsStr := os.Getenv("LOG_MAX_BACKUPS")
maxBackups, err := strconv.Atoi(maxBackupsStr)
if err != nil {
return nil, err
}
config := &Config{}

compressStr := os.Getenv("LOG_COMPRESS")
compress, err := strconv.ParseBool(compressStr)
if err != nil {
return nil, err
}
decoder := yaml.NewDecoder(file)

cfg := &Config{
Environment: os.Getenv("ENVIRONMENT"),
Logger: LoggerConfig{
Filename: os.ExpandEnv("LOG_FILENAME"),
LogLevel: os.Getenv("LOG_LEVEL"),
Targets: strings.Split(os.Getenv("LOG_TARGETS"), ","),
MaxSize: maxSize,
MaxBackups: maxBackups,
Compress: compress,
},
Pactus: PactusConfig{
WalletPath: os.Getenv("PACTUS_WALLET_PATH"),
WalletPass: os.Getenv("PACTUS_WALLET_PASSWORD"),
LockAddr: os.Getenv("PACTUS_WALLET_ADDRESS"),
RPCNode: os.Getenv("PACTUS_RPC"),
},
Polygon: PolygonConfig{
PrivateKey: os.Getenv("POLYGON_PRIVATE_KEY"),
ContractAddr: os.Getenv("POLYGON_CONTRACT_ADDRESS"),
RPCNode: os.Getenv("POLYGON_RPC"),
},
Database: DatabaseConfig{
os.Getenv("DATABASE_DSN"),
},
if err := decoder.Decode(config); err != nil {
return nil, Error{
reason: err.Error(),
}
}

HTTPServer: HTTPServerConfig{
Port: os.Getenv("HTTP_PORT"),
},
if config.Environment != "prod" {
if err := godotenv.Load(); err != nil {
return nil, Error{
reason: err.Error(),
}
}
}

if err := cfg.basicCheck(); err != nil {
return nil, err
config.Database.URI = os.Getenv("WRAPTO_MONGO_URI")
config.Pactus.WalletPass = os.Getenv("WRAPTO_PACTUS_WALLET_PASSWORD")
config.Polygon.PrivateKey = os.Getenv("WRAPTO_POLYGON_PRIVATE_KEY")

if err = config.basicCheck(); err != nil {
return nil, Error{
reason: err.Error(),
}
}

return cfg, nil
return config, nil
}

func (c *Config) basicCheck() error {
Expand Down
29 changes: 29 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
environment: dev # or prod.

logger:
file_name: wrapto.log
log_level: info
targets:
- console
- file
max_size: 100
max_backups: 10
compress: true

pactus:
wallet_path: ./wallet
lock_address: pc1zgp0x33hehvczq6dggs04gywfqpzl9fea5039gh
wallet_address: pc1rtc6cgpa7zys7gv3pn5mvt2dln6u28ws2v7qqat
rpc_url: bootstrap1.pactus.org:50051

polygon:
contract_address: 0x2f77E0afAEE06970Bf860B8267b5aFECFFF6F216
rpc_url: https://polygon.drpc.org

database:
db_name: wrapto
connection_timeout_in_ms: 5000
query_timeout_in_ms: 3000

http:
port: ":3000"
9 changes: 9 additions & 0 deletions config/erros.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ type InvalidEnvironmentError struct {
func (e InvalidEnvironmentError) Error() string {
return fmt.Sprintf("environment can be `dev` or `prod` not: %s", e.Environment)
}

// Error represents an error in loading or validating config.
type Error struct {
reason string
}

func (e Error) Error() string {
return fmt.Sprintf("config error: %s\n", e.reason)
}
6 changes: 3 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Core struct {
mgr *manager.Manager
}

func NewCore(ctx context.Context, cancel context.CancelFunc) (*Core, error) {
cfg, err := config.LoadConfig()
func NewCore(ctx context.Context, cancel context.CancelFunc, path string) (*Core, error) {
cfg, err := config.LoadConfig(path)
if err != nil {
cancel()

Expand All @@ -24,7 +24,7 @@ func NewCore(ctx context.Context, cancel context.CancelFunc) (*Core, error) {

logger.InitGlobalLogger(&cfg.Logger)

db, err := database.NewDB(cfg.Database.DSN)
db, err := database.Connect(cfg.Database) //nolint
if err != nil {
cancel()

Expand Down
8 changes: 8 additions & 0 deletions database/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package database

type Config struct {
URI string
DBName string `yaml:"db_name"`
ConnectionTimeout int16 `yaml:"connection_timeout_in_ms"`
QueryTimeout int16 `yaml:"query_timeout_in_ms"`
}
Loading
Loading