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

Feature/nft module #1

Merged
merged 19 commits into from
Nov 7, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.idea/
build/
build/

config.local.yaml
# Coverage
coverage.*
vendor
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ RUN git config --global url."https://olegfomenkodev:${CI_ACCESS_TOKEN}@github.co

COPY . .

RUN go mod vendor
RUN go build -mod=vendor -o /usr/local/bin/callisto /go/src/github.com/forbole/callisto/cmd/bdjuno


Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ test-unit: start-docker-test
###############################################################################
golangci_lint_cmd=github.com/golangci/golangci-lint/cmd/golangci-lint

##TODO fix linter
lint:
@echo "--> Running linter"
@go run $(golangci_lint_cmd) run --timeout=10m
Expand Down
2 changes: 2 additions & 0 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/forbole/juno/v4/modules/messages"

migratecmd "github.com/forbole/bdjuno/v4/cmd/migrate"
migratedbcmd "github.com/forbole/bdjuno/v4/cmd/migrate_db"
parsecmd "github.com/forbole/bdjuno/v4/cmd/parse"

"github.com/forbole/bdjuno/v4/types/config"
Expand Down Expand Up @@ -40,6 +41,7 @@ func main() {
parsecmd.NewParseCmd(cfg.GetParseConfig()),
migratecmd.NewMigrateCmd(cfg.GetName(), cfg.GetParseConfig()),
startcmd.NewStartCmd(cfg.GetParseConfig()),
migratedbcmd.NewMigrateDBCmd(cfg.GetParseConfig()),
)

executor := cmd.PrepareRootCmd(cfg.GetName(), rootCmd)
Expand Down
95 changes: 95 additions & 0 deletions cmd/migrate_db/migrate_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package migrate_db

import (
"database/sql"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/forbole/bdjuno/v4/database"
parsecmdtypes "github.com/forbole/juno/v4/cmd/parse/types"
"github.com/forbole/juno/v4/logging"
"github.com/forbole/juno/v4/types/config"
migrate "github.com/rubenv/sql-migrate"

"github.com/spf13/cobra"
)

var migrations = &migrate.EmbedFileSystemMigrationSource{
FileSystem: database.Migrations,
Root: "schema",
}

func NewMigrateDBCmd(parseCfg *parsecmdtypes.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate-db",
Short: "Migrate the database schema",
PersistentPreRunE: runPersistentPreRuns(parsecmdtypes.ReadConfigPreRunE(parseCfg)),
}

cmd.AddCommand(
&cobra.Command{
Use: "up",
Short: "migrate db up",
RunE: func(cmd *cobra.Command, args []string) error {
context, err := parsecmdtypes.GetParserContext(config.Cfg, parseCfg)
if err != nil {
return err
}
db := database.Cast(context.Database)
return migrateUp(db.SQL.DB, context.Logger)
},
},
)

cmd.AddCommand(
&cobra.Command{
Use: "down",
Short: "migrate db down",
RunE: func(cmd *cobra.Command, args []string) error {
context, err := parsecmdtypes.GetParserContext(config.Cfg, parseCfg)
if err != nil {
return err
}
db := database.Cast(context.Database)
return migrateDown(db.SQL.DB, context.Logger)
},
},
)

return cmd
}

func migrateUp(rawDB *sql.DB, log logging.Logger) error {
applied, err := migrate.Exec(rawDB, "postgres", migrations, migrate.Up)
if err != nil {
return errors.Wrap(err, "failed to apply migrations")
}
log.Info("migrations applied", map[string]interface{}{
"applied": applied,
})
return nil
}

func migrateDown(rawDB *sql.DB, log logging.Logger) error {
applied, err := migrate.Exec(rawDB, "postgres", migrations, migrate.Down)
if err != nil {
return errors.Wrap(err, "failed to apply migrations")
}
log.Info("migrations applied", map[string]interface{}{
"applied": applied,
})
return nil
}

func runPersistentPreRuns(preRun func(_ *cobra.Command, _ []string) error) func(_ *cobra.Command, _ []string) error {
return func(cmd *cobra.Command, args []string) error {
if root := cmd.Root(); root != nil {
if root.PersistentPreRunE != nil {
err := root.PersistentPreRunE(root, args)
if err != nil {
return err
}
}
}

return preRun(cmd, args)
}
}
53 changes: 53 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
actions:
port: 8000
chain:
bech32_prefix: bridge
modules:
- modules
- messages
- auth
- bank
- consensus
- feegrant
- gov
- mint
- slashing
- staking
- distribution
- actions
- upgrade
- nft
- accumulator
- bridge
database:
host: scan-pg
max_idle_connections: 10
max_open_connections: 20
name: scan
partition_batch: 1000
partition_size: 100000
password: scan
port: 5432
url: postgres://postgres:postgres@localhost:5432/juno?sslmode=disable
user: scan
logging:
format: text
level: debug
node:
config:
grpc:
address: http://localhost:9090
insecure: true
rpc:
address: http://localhost:26657
client_name: juno
max_connections: 10
type: remote
parsing:
average_block_time: 5s
genesis_file_path: genesis.json
listen_new_blocks: true
parse_genesis: false
parse_old_blocks: false
start_height: 1
workers: 1
61 changes: 61 additions & 0 deletions database/accumulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package database

import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
dbtypes "github.com/forbole/bdjuno/v4/database/types"
"github.com/forbole/bdjuno/v4/types"
"github.com/lib/pq"
)

// SaveAdmin allows to save new Admin
func (db *Db) SaveAdmin(address string, vestingCount, lastVestingTime, vestingPeriod int64, rewardPerPeriod sdk.Coin, denom string) error {
query := `
INSERT INTO admins_vesting(address, vesting_period, reward_per_period, last_vesting_time, vesting_counter, denom)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (address) DO UPDATE
SET vesting_counter = excluded.vesting_counter,
last_vesting_time = excluded.last_vesting_time
WHERE admins_vesting.address <= excluded.address
`
_, err := db.SQL.Exec(query, address, vestingPeriod, pq.Array(dbtypes.NewDbCoins(sdk.NewCoins(rewardPerPeriod))), lastVestingTime, vestingCount, denom)
if err != nil {
return fmt.Errorf("error while storing admin vesting info: %s", err)
}

return nil
}

// -------------------------------------------------------------------------------------------------------------------

// SaveAccumulatorParams allows to store the given params inside the database
func (db *Db) SaveAccumulatorParams(params *types.AccumulatorParams) error {
paramsBz, err := json.Marshal(&params.Params)
if err != nil {
return fmt.Errorf("error while marshaling accumulator params: %s", err)
}

stmt := `
INSERT INTO accumulator_params (params, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET params = excluded.params,
height = excluded.height
WHERE accumulator_params.height <= excluded.height
`

_, err = db.SQL.Exec(stmt, string(paramsBz), params.Height)
if err != nil {
return fmt.Errorf("error while storing accumulator params: %s", err)
}

return nil
}

// GetAdmins returns all the admins that are currently stored inside the database.
func (db *Db) GetAdmins() ([]dbtypes.AdminVestingRow, error) {
var rows []dbtypes.AdminVestingRow
err := db.Sqlx.Select(&rows, `SELECT * FROM admins_vesting WHERE to_timestamp(last_vesting_time) + INTERVAL '1 second' * last_vesting_time < NOW();`)
return rows, err
}
Loading
Loading