Skip to content

Commit

Permalink
accounts processing
Browse files Browse the repository at this point in the history
  • Loading branch information
MissingNO57 committed Jul 25, 2024
1 parent c2aec55 commit 84852d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 90 deletions.
22 changes: 8 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import (
icahostkeeper "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/keeper"
icahosttypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/host/types"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
transfer "github.com/cosmos/ibc-go/v3/modules/apps/transfer"
"github.com/cosmos/ibc-go/v3/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v3/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v3/modules/core"
Expand Down Expand Up @@ -755,22 +755,16 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
return app.mm.RunMigrations(ctx, cfg, fromVM)
})

app.UpgradeKeeper.SetUpgradeHandler("v0.11.4", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// "fetchd-v0.11.3-8-g929563a"
genesis, err := ReadGenesisFile(app.cudosPath)
app.UpgradeKeeper.SetUpgradeHandler("fetchd-v0.11.3-8-g929563a", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {

genesisState, _, err := genutiltypes.GenesisStateFromGenFile(app.cudosPath)
if err != nil {
return nil, err
panic(fmt.Sprintf("failed to unmarshal genesis state: %w", err))
}

// Create the map to store balances by address
balanceMap, _ := GetBankBalances(genesis)

// Print the balance map
for addr, coins := range balanceMap {
fmt.Printf("Address: %s\n", addr)
for _, coin := range coins {
fmt.Printf(" Coin: %s, Amount: %s\n", coin.Denom, coin.Amount.String())
}
err = ProcessAccounts(app, genesisState)
if err != nil {
panic(fmt.Sprintf("failed process accounts: %w", err))
}

manifest := NewUpgradeManifest()
Expand Down
85 changes: 9 additions & 76 deletions app/cudos_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,21 @@ package app

import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"io/ioutil"
"log"
"os"
"fmt"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

// ParseJSON recursively parses JSON data into a map.
func ParseJSON(data []byte) (map[string]interface{}, error) {
var result map[string]interface{}
err := json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
return result, nil
}

// ReadJSONFile reads a JSON file and returns its content as a byte slice.
func ReadJSONFile(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

byteValue, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}

return byteValue, nil
}
func ProcessAccounts(app *App, genesisState map[string]json.RawMessage) error {

func ReadGenesisFile(filePath string) (map[string]interface{}, error) {
jsonData, err := ReadJSONFile(filePath)
if err != nil {
log.Fatalf("Failed to read JSON file: %v", err)
}
authGenState := authtypes.GetGenesisStateFromAppState(app.appCodec, genesisState)

parsedData, err := ParseJSON(jsonData)
accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
log.Fatalf("Failed to parse JSON data: %v", err)
panic(fmt.Sprintf("failed to get accounts from any: %w", err))
}

return parsedData, nil
}

func GetBankBalances(genesis map[string]interface{}) (map[string][]sdk.Coin, error) {

// Create the map to store balances by address
balanceMap := make(map[string][]sdk.Coin)

// Unsafe way to access and iterate over app_state -> bank -> balances
appState := genesis["app_state"].(map[string]interface{})
bank := appState["bank"].(map[string]interface{})
balances := bank["balances"].([]interface{})

for _, res := range balances {
address := res.(map[string]interface{})["address"].(string)
balance := res.(map[string]interface{})["coins"]

// Create a list to store coins for this address
var coins []sdk.Coin

for _, coinsRes := range balance.([]interface{}) {
denom := coinsRes.(map[string]interface{})["denom"].(string)
amount := coinsRes.(map[string]interface{})["amount"].(string)
// Convert amount to sdk.Int
amountInt, ok := sdk.NewIntFromString(amount)
if !ok {
panic("Failed to convert amount to sdk.Int: %s" + amount)
}
// Append coin to the list
coin := sdk.Coin{
Denom: denom,
Amount: amountInt,
}
coins = append(coins, coin)
}

// Store the list of coins in the map
balanceMap[address] = coins
}
// Handle accounts

return balanceMap, nil
print(accs)
return nil
}

0 comments on commit 84852d0

Please sign in to comment.