generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from axone-protocol/feat/extract-command
Feat/extract command
- Loading branch information
Showing
11 changed files
with
244 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target/ | |
.idea/ | ||
.vscode/ | ||
.DS_Store | ||
data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/axone-protocol/wallet-extractor/pkg/delegators" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
flagChainName = "chain-name" | ||
) | ||
|
||
var extractDelegatorsCmd = &cobra.Command{ | ||
Use: "delegators [source] [dest]", | ||
Short: "Extract all delegators into CSV files", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
chainName, _ := cmd.Flags().GetString(flagChainName) | ||
|
||
return delegators.Extract(chainName, args[0], args[1]) | ||
}, | ||
} | ||
|
||
func init() { | ||
extractCmd.AddCommand(extractDelegatorsCmd) | ||
|
||
extractDelegatorsCmd.Flags().StringP(flagChainName, "n", "cosmos", "Name of the chain") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var extractCmd = &cobra.Command{ | ||
Use: "extract", | ||
Short: "Extract data from a chain (snapshot)", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(extractCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package delegators | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/axone-protocol/wallet-extractor/pkg/keeper" | ||
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
"github.com/gocarina/gocsv" | ||
|
||
"cosmossdk.io/log" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/bech32" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func Extract(chainName, src, dst string) error { | ||
logger := log.NewLogger(os.Stderr) | ||
|
||
logger.Info("Extracting wallets", "source", src, "destination", dst) | ||
|
||
keepers, err := keeper.OpenStore(src, logger) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := sdk.NewContext(keepers.Store, cmtproto.Header{}, false, keepers.Logger) | ||
|
||
validators, err := keepers.Staking.GetAllValidators(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
keepers.Logger.Info("Analyzing validators", "count", len(validators)) | ||
|
||
err = extractChainMetadata(ctx, chainName, keepers, dst) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return extractDelegators(ctx, chainName, keepers, validators, dst) | ||
} | ||
|
||
func extractDelegators( | ||
ctx sdk.Context, chainName string, keepers *keeper.Keepers, validators []stakingtypes.Validator, destination string, | ||
) error { | ||
file, err := os.OpenFile(path.Join(destination, "delegations.csv"), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
writer := bufio.NewWriter(file) | ||
defer writer.Flush() | ||
|
||
prefix, err := guessPrefixFromValoper(validators[0].OperatorAddress) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config := sdk.GetConfig() | ||
|
||
keepers.Bank.IterateAllBalances(ctx, func(addr sdk.AccAddress, _ sdk.Coin) (stop bool) { | ||
for _, val := range validators { | ||
if config.GetBech32AccountAddrPrefix() != prefix { | ||
config.SetBech32PrefixForValidator( | ||
fmt.Sprintf("%svaloper", prefix), | ||
fmt.Sprintf("%svaloperpub", prefix), | ||
) | ||
} | ||
|
||
valAddr, err := sdk.ValAddressFromBech32(val.OperatorAddress) | ||
if err != nil { | ||
panic(err) | ||
} | ||
delegation, err := keepers.Staking.GetDelegation(ctx, addr, valAddr) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
record := Delegations{ | ||
ChainName: chainName, | ||
DelegatorNativeAddr: delegation.DelegatorAddress, | ||
DelegatorCosmosAddr: convertAndEncodeMust("cosmos", delegation.DelegatorAddress), | ||
DelegatorAxoneAddr: convertAndEncodeMust("axone", delegation.DelegatorAddress), | ||
ValidatorAddr: delegation.ValidatorAddress, | ||
Shares: delegation.Shares.String(), | ||
} | ||
|
||
v, err := gocsv.MarshalStringWithoutHeaders(&[]Delegations{record}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = writer.WriteString(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
return false | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
func extractChainMetadata( | ||
_ sdk.Context, chainName string, keepers *keeper.Keepers, destination string, | ||
) error { | ||
file, err := os.OpenFile(path.Join(destination, "metadatas.csv"), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
writer := bufio.NewWriter(file) | ||
defer writer.Flush() | ||
|
||
record := Chains{ | ||
Name: chainName, | ||
StoreVersion: fmt.Sprintf("%d", keepers.Store.LastCommitID().Version), | ||
StoreHash: fmt.Sprintf("%X", keepers.Store.LastCommitID().Hash), | ||
} | ||
|
||
v, err := gocsv.MarshalStringWithoutHeaders(&[]Chains{record}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = writer.WriteString(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func convertAndEncodeMust(hrp string, bech string) string { | ||
_, bytes, err := bech32.DecodeAndConvert(bech) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
encoded, err := bech32.ConvertAndEncode(hrp, bytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return encoded | ||
} | ||
|
||
func guessPrefixFromValoper(valoper string) (string, error) { | ||
if idx := strings.Index(valoper, "valoper"); idx != -1 { | ||
return valoper[:idx], nil | ||
} | ||
return "", fmt.Errorf("valoper not found in operator address: %s", valoper) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package delegators | ||
|
||
type Chains struct { | ||
Name string `csv:"name"` | ||
StoreVersion string `csv:"store_version"` | ||
StoreHash string `csv:"store_hash"` | ||
} | ||
|
||
type Delegations struct { | ||
ChainName string `csv:"chain_name"` | ||
DelegatorNativeAddr string `csv:"delegator_native_addr"` | ||
DelegatorCosmosAddr string `csv:"delegator_cosmos_addr"` | ||
DelegatorAxoneAddr string `csv:"delegator_axone_addr"` | ||
|
||
ValidatorAddr string `csv:"validator_addr"` | ||
Shares string `csv:"shares"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters