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

feat: raw and process snapshot commands #6

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f4163e5
add project structure
Pantani Dec 20, 2022
d9fbd3f
Merge remote-tracking branch 'origin/main' into feat/read-state
Pantani Dec 20, 2022
90ee563
add config tests
Pantani Dec 20, 2022
a3d26e0
add gofmt
Pantani Dec 20, 2022
96088b6
add unit tests for snapshot package
Pantani Dec 20, 2022
e4af165
add unit tests
Pantani Dec 21, 2022
4ab089e
improve command decription
Pantani Dec 21, 2022
b558b7e
remove local CLI project
Pantani Dec 21, 2022
a9b4fbf
update go.sum
Pantani Dec 21, 2022
b3b7bdf
fix lint
Pantani Dec 21, 2022
0dc61c6
update genesis testdata
Pantani Dec 21, 2022
7049bbc
fix ci coverage
Pantani Dec 21, 2022
266c26c
create snapshot filter methods
Pantani Dec 21, 2022
01bd413
add formula package
Pantani Dec 22, 2022
5c39b8f
add calculate rules
Pantani Dec 22, 2022
64d521c
add fixme for the calculation
Pantani Dec 22, 2022
ff34d98
add process commands
Pantani Dec 22, 2022
8179886
fix unit tests
Pantani Dec 22, 2022
4767728
fix panic
Pantani Dec 22, 2022
407a226
move debug to cmd
Pantani Dec 22, 2022
b6fbc2c
remove unused encoding parameters
Pantani Dec 22, 2022
18b69c2
run `make format`
Pantani Dec 22, 2022
bf95ca9
fix formula calculation
Pantani Dec 22, 2022
5241e27
add unit tests and fix filter
Pantani Dec 22, 2022
67be669
fix unit tests
Pantani Dec 22, 2022
c2b99f5
fix config typo
Pantani Dec 22, 2022
717a2e6
simplify filter struct
Pantani Dec 22, 2022
e52353c
add claim records to gen state
Pantani Dec 22, 2022
11f6305
- remove liquidy module
Pantani Dec 26, 2022
832f1ba
Merge remote-tracking branch 'origin/main' into feat/read-state
Pantani Jan 9, 2023
b66bf74
fetch balances
Pantani Jan 10, 2023
504e7e3
add output genesis arg
Pantani Jan 10, 2023
9901de7
rename filter to record
Pantani Jan 10, 2023
3cd32e1
fix test name
Pantani Jan 10, 2023
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cli-plugin-airdrop
.idea
.DS_STORE
.DS_STORE
coverage.txt
coverage.out
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ FIND_ARGS := -name '*.go' -type f -not -name '*.pb.go' -not -name '*.pb.gw.go'
## format: Run gofmt and goimports.
format:
@echo Formatting...
@go install mvdan.cc/gofumpt
@go install golang.org/x/tools/cmd/goimports
@find . $(FIND_ARGS) | xargs gofumpt -w .
@find . $(FIND_ARGS) | xargs goimports -w -local github.com/ignite/cli-plugin-airdrop

## lint: Run Golang CI Lint.
lint:
@echo Running gocilint...
@go install github.com/golangci/golangci-lint/cmd/golangci-lint
@golangci-lint run --out-format=tab --issues-exit-code=0

help: Makefile
Expand Down
200 changes: 198 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,213 @@
package cmd

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"

"github.com/ignite/cli-plugin-airdrop/pkg/config"
"github.com/ignite/cli-plugin-airdrop/pkg/genesis"
"github.com/ignite/cli-plugin-airdrop/pkg/snapshot"
)

func NewAirdrop() *cobra.Command {
return &cobra.Command{
c := &cobra.Command{
Use: "airdrop",
Short: "Utility tool to create snapshots for an airdrop",
}

c.AddCommand(
NewAirdropGenerate(),
NewAirdropRaw(),
NewAirdropProcess(),
NewAirdropGenesis(),
)

return c
}

func NewAirdropRaw() *cobra.Command {
return &cobra.Command{
Use: "raw [input-genesis]",
Short: "Generate raw airdrop data based on the input genesis",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// parse the genesis input to get the current stake state
genState, err := genesis.GetGenStateFromPath(args[0])
if err != nil {
return err
}

// get only the essential info from input genesis
// to generate the snapshot the raw snapshot object
s, err := snapshot.Generate(genState.AppState)
if err != nil {
return err
}

// export snapshot json
snapshotJSON, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal snapshot: %w", err)
}

cmd.Println(string(snapshotJSON))
return nil
},
}
}

func NewAirdropProcess() *cobra.Command {
return &cobra.Command{
Use: "process [airdrop-config] [raw-snapshot]",
Short: "Process the airdrop raw data based on the config file",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var (
airdropConfig = args[0]
rawSnapshot = args[1]
)

c, err := config.ParseConfig(airdropConfig)
if err != nil {
return err
}

s, err := snapshot.ParseSnapshot(rawSnapshot)
if err != nil {
return err
}

records := make(snapshot.Records, 0)
for _, snap := range c.Snapshots {
record := s.ApplyConfig(snapshot.ConfigType(snap.Type), snap.Denom, snap.Formula, snap.Excluded)
records = append(records, record)
}
filter := records.Sum()

// export filter json
filterJSON, err := json.MarshalIndent(filter, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal snapshot: %w", err)
}

cmd.Println(string(filterJSON))
return nil
},
}
}

func NewAirdropGenesis() *cobra.Command {
return &cobra.Command{
Use: "genesis [airdrop-config] [raw-snapshot] [output-genesis]",
Short: "Generate and add quadratic airdrop claim record to the output genesis based on the raw data and airdrop config",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
var (
airdropConfig = args[0]
rawSnapshot = args[1]
outputGenesis = args[2]
)

c, err := config.ParseConfig(airdropConfig)
if err != nil {
return err
}

// parse the genesis output to get the current stake state
genState, err := genesis.GetGenStateFromPath(outputGenesis)
if err != nil {
return err
}

s, err := snapshot.ParseSnapshot(rawSnapshot)
if err != nil {
return err
}

// apply the config file to the raw data to generate the claim records
records := make(snapshot.Records, 0)
for _, snap := range c.Snapshots {
record := s.ApplyConfig(snapshot.ConfigType(snap.Type), snap.Denom, snap.Formula, snap.Excluded)
records = append(records, record)
}
record := records.Sum()

// add claim records to the output genesis
if err := genState.AddFromClaimRecord(c.AirdropToken, record.ClaimRecords()); err != nil {
return err
}

// export snapshot json
genesisJSON, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal snapshot: %w", err)
}

cmd.Println(string(genesisJSON))
return nil
},
}
}

func NewAirdropGenerate() *cobra.Command {
return &cobra.Command{
Use: "generate [airdrop-config] [input-genesis] [output-genesis]",
Short: "Generate and add quadratic airdrop claim records to the output genesis based on the input genesis stake values and airdrop config",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Hello from airdrop command")
var (
airdropConfig = args[0]
inputGenesis = args[1]
outputGenesis = args[2]
)

// parse the airdrop config yaml
c, err := config.ParseConfig(airdropConfig)
if err != nil {
return err
}

// parse the genesis input to get the current stake state
inGenState, err := genesis.GetGenStateFromPath(inputGenesis)
if err != nil {
return err
}

// parse the genesis output state to be modified
outGenState, err := genesis.GetGenStateFromPath(outputGenesis)
if err != nil {
return err
}

// get only the essential info from input genesis
// to generate the snapshot the raw snapshot object
s, err := snapshot.Generate(inGenState.AppState)
if err != nil {
return err
}

// apply the config file to the raw data to generate the claim records
records := make(snapshot.Records, 0)
for _, snap := range c.Snapshots {
record := s.ApplyConfig(snapshot.ConfigType(snap.Type), snap.Denom, snap.Formula, snap.Excluded)
records = append(records, record)
}
filter := records.Sum()

// add claim records to the output genesis
if err := outGenState.AddFromClaimRecord(c.AirdropToken, filter.ClaimRecords()); err != nil {
return err
}

// export snapshot json
genesisJSON, err := json.MarshalIndent(outGenState, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal snapshot: %w", err)
}

cmd.Println(string(genesisJSON))
return nil
},
}
Expand Down
23 changes: 23 additions & 0 deletions cmd/debug/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/ignite/cli-plugin-airdrop/cmd"
)

var rootCmd = &cobra.Command{
Use: "cli-plugin-airdrop",
Short: "debug command for CLI airdrop plugin",
}

func main() {
rootCmd.AddCommand(cmd.NewAirdrop())
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Pantani marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading