Skip to content

Commit

Permalink
Move ImportGenesisAccountsFromSnapshotCmd to cmd/nibirud/airdrop.go (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
padi-dev-hoangpa authored Jan 11, 2022
1 parent 8cc6258 commit d27e70b
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 227 deletions.
218 changes: 218 additions & 0 deletions cmd/nibirud/cmd/airdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/cosmos-gaminghub/nibiru/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ibctransfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types"
"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/state"
Expand Down Expand Up @@ -326,3 +330,217 @@ func exportSnapShotFromGenesisFile(clientCtx client.Context, genesisFile string,
fmt.Printf("gameTotalSupply: %s\n", totalBalance.String())
return snapshot
}

func ImportGenesisAccountsFromSnapshotCmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "import-genesis-accounts-from-snapshot [input-snapshot-file] [input-games-file]",
Short: "Import genesis accounts from fairdrop snapshot.json and an games.json",
Long: `Import genesis accounts from fairdrop snapshot.json
20% of airdrop amount is liquid in accounts.
The remaining is placed in the claims module.
Must also pass in an games.json file to airdrop genesis games
Example:
nibirud import-genesis-accounts-from-snapshot ../snapshot.json ../games.json
- Check input genesis:
file is at ~/.nibirud/config/genesis.json
`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {

// aminoCodec := clientCtx.LegacyAmino.Amino

clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)

config := serverCtx.Config

config.SetRoot(clientCtx.HomeDir)

genFile := config.GenesisFile()
appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile)
if err != nil {
return fmt.Errorf("failed to unmarshal genesis state: %w", err)
}

authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)

accs, err := authtypes.UnpackAccounts(authGenState.Accounts)
if err != nil {
return fmt.Errorf("failed to get accounts from any: %w", err)
}

// Read snapshot file
snapshotInput := args[0]
snapshotJSON, err := os.Open(snapshotInput)
if err != nil {
return err
}
defer snapshotJSON.Close()
byteValue, _ := ioutil.ReadAll(snapshotJSON)
snapshot := Snapshot{}
err = json.Unmarshal(byteValue, &snapshot)
if err != nil {
return err
}

// Read ions file
gameInput := args[1]
gameJSON, err := os.Open(gameInput)
if err != nil {
return err
}
defer gameJSON.Close()
byteValue2, _ := ioutil.ReadAll(gameJSON)
var gameAmts map[string]int64
err = json.Unmarshal(byteValue2, &gameAmts)
if err != nil {
return err
}

// get genesis params
genesisParams := MainnetGenesisParams()
nonAirdropAccs := make(map[string]sdk.Coins)

for _, acc := range genesisParams.DistributedAccounts {
nonAirdropAccs[acc.Address] = acc.GetCoins()
}

for addr, amt := range gameAmts {
// set atom bech32 prefixes
bech32Addr, err := app.ConvertBech32(addr)
if err != nil {
return err
}

address, err := sdk.AccAddressFromBech32(bech32Addr)
if err != nil {
return err
}

if val, ok := nonAirdropAccs[address.String()]; ok {
nonAirdropAccs[address.String()] = val.Add(sdk.NewCoin("game", sdk.NewInt(amt).MulRaw(1_000_000)))
} else {
nonAirdropAccs[address.String()] = sdk.NewCoins(sdk.NewCoin("game", sdk.NewInt(amt).MulRaw(1_000_000)))
}
}

// figure out normalizationFactor to normalize snapshot balances to desired airdrop supply
normalizationFactor := genesisParams.AirdropSupply.ToDec().QuoInt(snapshot.TotalGameAirdropAmount)
fmt.Printf("normalization factor: %s\n", normalizationFactor)

// for each account in the snapshot
bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState)
liquidBalances := bankGenState.Balances
supply := bankGenState.Supply
for _, acc := range snapshot.Accounts {
// read address from snapshot
bech32Addr, err := app.ConvertBech32(acc.AtomAddress)
if err != nil {
return err
}

address, err := sdk.AccAddressFromBech32(bech32Addr)
if err != nil {
return err
}

// initial liquid amounts
// We consistently round down to the nearest uosmo
liquidCoins := sdk.NewCoins(sdk.NewCoin(genesisParams.NativeCoinMetadatas[0].Base, acc.GameBalance))

if coins, ok := nonAirdropAccs[address.String()]; ok {
liquidCoins = liquidCoins.Add(coins...)
delete(nonAirdropAccs, address.String())
}

liquidBalances = append(liquidBalances, banktypes.Balance{
Address: address.String(),
Coins: liquidCoins,
})
supply = supply.Add(liquidCoins...)

// Add the new account to the set of genesis accounts
baseAccount := authtypes.NewBaseAccount(address, nil, 0, 0)
if err := baseAccount.Validate(); err != nil {
return fmt.Errorf("failed to validate new genesis account: %w", err)
}
accs = append(accs, baseAccount)

}

// distribute remaining game to accounts not in fairdrop
for addr, coin := range nonAirdropAccs {
// read address from snapshot
address, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return err
}

liquidBalances = append(liquidBalances, banktypes.Balance{
Address: address.String(),
Coins: coin,
})
supply = supply.Add(coin...)

// Add the new account to the set of genesis accounts
baseAccount := authtypes.NewBaseAccount(address, nil, 0, 0)
if err := baseAccount.Validate(); err != nil {
return fmt.Errorf("failed to validate new genesis account: %w", err)
}
accs = append(accs, baseAccount)
}
// auth module genesis
accs = authtypes.SanitizeGenesisAccounts(accs)
genAccs, err := authtypes.PackAccounts(accs)
if err != nil {
return fmt.Errorf("failed to convert accounts into any's: %w", err)
}
authGenState.Accounts = genAccs
authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState)
if err != nil {
return fmt.Errorf("failed to marshal auth genesis state: %w", err)
}
appState[authtypes.ModuleName] = authGenStateBz

// bank module genesis
bankGenState.Balances = banktypes.SanitizeGenesisBalances(liquidBalances)
bankGenState.Supply = supply
bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
}
appState[banktypes.ModuleName] = bankGenStateBz

byteIBCTransfer, err := appState[ibctransfertypes.ModuleName].MarshalJSON()
if err != nil {
return fmt.Errorf("Error marshal ibc transfer: %w", err)
}

var ibcGenState ibctransfertypes.GenesisState
err = ibctransfertypes.ModuleCdc.UnmarshalJSON(byteIBCTransfer, &ibcGenState)
if err != nil {
return fmt.Errorf("Error unmarshal ibc transfer: %w", err)
}
ibcGenState.Params = ibctransfertypes.NewParams(false, false)
ibcGenStateBz, err := clientCtx.Codec.MarshalJSON(&ibcGenState)
if err != nil {
return fmt.Errorf("failed to marshal ibc genesis state: %w", err)
}
appState[ibctransfertypes.ModuleName] = ibcGenStateBz

appStateJSON, err := json.Marshal(appState)
if err != nil {
return fmt.Errorf("failed to marshal application genesis state: %w", err)
}
genDoc.AppState = appStateJSON

err = genutil.ExportGenesisFile(genDoc, genFile)
return err
},
}

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
flags.AddQueryFlagsToCmd(cmd)

return cmd
}
Loading

0 comments on commit d27e70b

Please sign in to comment.