From 38eb20820b7af09868925e28506188d66da0f954 Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Mon, 22 Apr 2024 19:56:55 +0100 Subject: [PATCH 1/4] feat: handle supply increase and transfer overflow to admin addr --- ...enasiupgrade.go => genesis-asi-upgrade.go} | 80 ++++++++++++++++--- 1 file changed, 71 insertions(+), 9 deletions(-) rename cmd/fetchd/cmd/{genasiupgrade.go => genesis-asi-upgrade.go} (81%) diff --git a/cmd/fetchd/cmd/genasiupgrade.go b/cmd/fetchd/cmd/genesis-asi-upgrade.go similarity index 81% rename from cmd/fetchd/cmd/genasiupgrade.go rename to cmd/fetchd/cmd/genesis-asi-upgrade.go index 355f938c..adbebc4e 100644 --- a/cmd/fetchd/cmd/genasiupgrade.go +++ b/cmd/fetchd/cmd/genesis-asi-upgrade.go @@ -26,9 +26,9 @@ import ( ) const ( - BridgeContractAddress = "fetch1qxxlalvsdjd07p07y3rc5fu6ll8k4tmetpha8n" - NewBridgeContractAdmin = "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw" - + BridgeContractAddress = "fetch1qxxlalvsdjd07p07y3rc5fu6ll8k4tmetpha8n" + NewBridgeContractAdmin = "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw" + NewSupplyOverflowAddress = "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw" IbcWithdrawAddress = "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x" /* "asi1rhrlzsx9z865dqen8t4v47r99dw6y4vaw76rd9" */ ReconciliationWithdrawAddress = "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x" @@ -51,6 +51,11 @@ const ( OldAddrPrefix = "fetch" ) +const ( + flagUpdatedSupplyValue = "updated-supply-value" + defaultUpdatedSupplyValue = "2000000000000000000000000000" +) + //go:embed reconciliation_data.csv var reconciliationData []byte @@ -64,10 +69,10 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { - The native coin denom will be updated to "asi" - The denom metadata will be updated to the new ASI token - The address prefix will be updated to "asi" - - The old fetch addresses will be updated to the new asi addresses + - The old fetch addresses will be updated to the new asi addresses, e.g. asivaloper1, asivalcons1, asi1, etc. - The bridge contract admin will be updated to the new address - - The IBC withdrawal address will be updated to the new address - - The reconciliation withdrawal address will be updated to the new address + - The IBC channel funds will be transferred to the IBC withdrawal address + - The reconciliation withdrawal funds will be transferred to the reconciliation withdrawal address `, Args: cobra.ExactArgs(0), @@ -78,6 +83,11 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { config.SetRoot(clientCtx.HomeDir) + updatedSupplyVal, err := cmd.Flags().GetString(flagUpdatedSupplyValue) + if err != nil { + return fmt.Errorf("failed to get flag %q: %w", flagUpdatedSupplyValue, err) + } + genFile := config.GenesisFile() _, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile) @@ -112,6 +122,9 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { // set denom metadata in bank module ASIGenesisUpgradeReplaceDenomMetadata(jsonData) + // supplement the genesis supply + ASIGenesisUpgradeASISupply(updatedSupplyVal, jsonData) + // replace addresses across the genesis file ASIGenesisUpgradeReplaceAddresses(jsonData) @@ -125,8 +138,8 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { }, } + cmd.Flags().String(flagUpdatedSupplyValue, defaultUpdatedSupplyValue, "The amount of ASI to add to the genesis supply, accounting for merged tokens") cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") - cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") flags.AddQueryFlagsToCmd(cmd) return cmd @@ -256,7 +269,7 @@ func ASIGenesisUpgradeWithdrawIBCChannelsBalances(jsonData map[string]interface{ withdrawalBalanceIdx, ok := (*balanceMap)[IbcWithdrawAddress] if !ok { - fmt.Println("failed to find Ibc withdrawal address in genesis balances - have addresses already been converted?") + fmt.Println("failed to find ibc withdrawal address in genesis balances - have addresses already been converted?") return nil } @@ -377,6 +390,55 @@ func ASIGenesisUpgradeWithdrawReconciliationBalances(jsonData map[string]interfa return nil } +func ASIGenesisUpgradeASISupply(newSupplyValStr string, jsonData map[string]interface{}) { + newSupplyValue, ok := sdk.NewIntFromString(newSupplyValStr) + if !ok { + panic("asi upgrade update supply: failed to convert new supply value to int") + } + + if newSupplyValue.LT(sdk.ZeroInt()) { + panic("asi upgrade update supply: new supply value is negative") + } + + bank := jsonData[banktypes.ModuleName].(map[string]interface{}) + supply := bank["supply"].([]interface{}) + balances := bank["balances"].([]interface{}) + balancesMap := getGenesisBalancesMap(bank["balances"].([]interface{})) + + var curSupply sdk.Int + var curSupplyIdx int + for idx, coin := range supply { + coinData := coin.(map[string]interface{}) + if coinData["denom"] == NewDenom { + curSupplyIdx = idx + curSupply, ok = sdk.NewIntFromString(coinData["amount"].(string)) + if !ok { + panic("asi upgrade update supply: failed to convert coin amount to int") + } + break + } + } + + overflowAddressBalance := balances[(*balancesMap)[NewSupplyOverflowAddress]] + overflowAddressBalanceCoins := getCoinsFromInterfaceSlice(overflowAddressBalance) + + newSupplyCoins := sdk.NewCoin(NewDenom, newSupplyValue) + curSupplyCoin := sdk.NewCoin(NewDenom, curSupply) + + // calculate the difference between the new supply and the current supply + supplyDiffCoin := newSupplyCoins.Sub(curSupplyCoin) + if supplyDiffCoin.IsNegative() { + panic("asi upgrade update supply: new supply is less than current supply") + } + + // add the supply diff to the overflow address balance + overflowAddressBalanceCoins = overflowAddressBalanceCoins.Add(supplyDiffCoin) + + // update the supply in the bank module + supply[curSupplyIdx].(map[string]interface{})["amount"] = newSupplyCoins.Amount.String() + balances[(*balancesMap)[NewSupplyOverflowAddress]].(map[string]interface{})["coins"] = getInterfaceSliceFromCoins(overflowAddressBalanceCoins) +} + func convertAddressToASI(addr string, addressPrefix string) (string, error) { _, decodedAddrData, err := bech32.Decode(addr) if err != nil { @@ -438,7 +500,7 @@ func getCoinsFromInterfaceSlice(data interface{}) sdk.Coins { coinDenom := coinData["denom"].(string) coinAmount, ok := sdk.NewIntFromString(coinData["amount"].(string)) if !ok { - panic("IBC withdraw: failed to convert coin amount to int") + panic("ibc withdraw: failed to convert coin amount to int") } balanceCoins = append(balanceCoins, sdk.NewCoin(coinDenom, coinAmount)) } From 9ea74f6675bad62e533cbd23b875d8e1b2a517f0 Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Tue, 23 Apr 2024 13:40:50 +0100 Subject: [PATCH 2/4] feat: close ibc channels & tested --- cmd/fetchd/cmd/genesis-asi-upgrade.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/fetchd/cmd/genesis-asi-upgrade.go b/cmd/fetchd/cmd/genesis-asi-upgrade.go index fb503ec8..778d7562 100644 --- a/cmd/fetchd/cmd/genesis-asi-upgrade.go +++ b/cmd/fetchd/cmd/genesis-asi-upgrade.go @@ -116,15 +116,15 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { return err } - // supplement the genesis supply - ASIGenesisUpgradeASISupply(updatedSupplyVal, jsonData) - // set denom metadata in bank module ASIGenesisUpgradeReplaceDenomMetadata(jsonData) // replace denom across the genesis file ASIGenesisUpgradeReplaceDenom(jsonData) + // supplement the genesis supply + ASIGenesisUpgradeASISupply(updatedSupplyVal, jsonData) + // replace addresses across the genesis file ASIGenesisUpgradeReplaceAddresses(jsonData) @@ -278,8 +278,12 @@ func ASIGenesisUpgradeWithdrawIBCChannelsBalances(jsonData map[string]interface{ ibcChannels := channelGenesis["channels"].([]interface{}) for _, channel := range ibcChannels { - channelId := channel.(map[string]interface{})["channel_id"].(string) - portId := channel.(map[string]interface{})["port_id"].(string) + channelMap := channel.(map[string]interface{}) + channelId := channelMap["channel_id"].(string) + portId := channelMap["port_id"].(string) + + // close channel + channelMap["state"] = "STATE_CLOSED" rawAddr := ibctransfertypes.GetEscrowAddress(portId, channelId) channelAddr, err := sdk.Bech32ifyAddressBytes(OldAddrPrefix+AccAddressPrefix, rawAddr) @@ -312,15 +316,17 @@ func getGenesisAccountSequenceMap(accounts []interface{}) *map[string]int { accountMap := make(map[string]int) for _, acc := range accounts { - accType := acc.(map[string]interface{})["@type"] + accMap := acc.(map[string]interface{}) + accType := accMap["@type"] accData := acc if accType == ModuleAccount { - accData = acc.(map[string]interface{})["base_account"] + accData = accMap["base_account"] } - addr := accData.(map[string]interface{})["address"].(string) - sequence := accData.(map[string]interface{})["sequence"].(string) + accDataMap := accData.(map[string]interface{}) + addr := accDataMap["address"].(string) + sequence := accDataMap["sequence"].(string) sequenceInt, ok := strconv.Atoi(sequence) if ok != nil { From fdcdffb997259faf9c040ecc01b3543aa5527ca7 Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Thu, 16 May 2024 12:07:36 +0100 Subject: [PATCH 3/4] feat: update additional supply logic --- cmd/fetchd/cmd/genesis-asi-upgrade.go | 45 ++++++++++++++------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/cmd/fetchd/cmd/genesis-asi-upgrade.go b/cmd/fetchd/cmd/genesis-asi-upgrade.go index 8a7a6b59..5c74e873 100644 --- a/cmd/fetchd/cmd/genesis-asi-upgrade.go +++ b/cmd/fetchd/cmd/genesis-asi-upgrade.go @@ -43,15 +43,17 @@ var ReconciliationTargetAddr = "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x" var networkInfos = map[string]NetworkConfig{ "fetchhub-4": { - AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this - UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this - NewChainID: "asi-1", - NewDescription: "ASI token", // TODO(JS): confirm this + NewChainID: "asi-1", + NewDescription: "ASI token", // TODO(JS): confirm this DenomInfo: DenomInfo{ NewBaseDenom: "asi", NewDenom: "aasi", OldDenom: "afet", }, + SupplyInfo: SupplyInfo{ + AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this + UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this + }, IbcTargetAddr: "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x", // TODO(JS): amend this ReconciliationTargetAddr: &ReconciliationTargetAddr, // TODO(JS): amend this Contracts: &Contracts{ @@ -63,15 +65,17 @@ var networkInfos = map[string]NetworkConfig{ }, "dorado-1": { - AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this - UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this - NewChainID: "asi-1", // TODO(JS): likely amend this - NewDescription: "Test ASI token", // TODO(JS): confirm this + NewChainID: "asi-1", // TODO(JS): likely amend this + NewDescription: "Test ASI token", // TODO(JS): confirm this DenomInfo: DenomInfo{ NewBaseDenom: "testasi", NewDenom: "atestasi", OldDenom: "atestfet", }, + SupplyInfo: SupplyInfo{ + AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this + UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this + }, IbcTargetAddr: "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x", // TODO(JS): amend this }, } @@ -433,13 +437,13 @@ func ASIGenesisUpgradeWithdrawReconciliationBalances(jsonData map[string]interfa func ASIGenesisUpgradeASISupply(jsonData map[string]interface{}, networkInfo NetworkConfig) { denomInfo := networkInfo.DenomInfo supplyInfo := networkInfo.SupplyInfo - newSupplyValue, ok := sdk.NewIntFromString(supplyInfo.AdditionalSupplyValue) + additionalSupply, ok := sdk.NewIntFromString(supplyInfo.AdditionalSupplyValue) if !ok { panic("asi upgrade update supply: failed to convert new supply value to int") } - if newSupplyValue.LT(sdk.ZeroInt()) { - panic("asi upgrade update supply: new supply value is negative") + if additionalSupply.LT(sdk.ZeroInt()) { + panic("asi upgrade update supply: additional supply value is negative") } bank := jsonData[banktypes.ModuleName].(map[string]interface{}) @@ -461,24 +465,21 @@ func ASIGenesisUpgradeASISupply(jsonData map[string]interface{}, networkInfo Net } } - overflowAddressBalance := balances[(*balancesMap)[supplyInfo.NewSupplyOverflowAddress]] + overflowAddressBalance := balances[(*balancesMap)[supplyInfo.UpdatedSupplyOverflowAddr]] overflowAddressBalanceCoins := getCoinsFromInterfaceSlice(overflowAddressBalance) - newSupplyCoins := sdk.NewCoin(NewDenom, newSupplyValue) - curSupplyCoin := sdk.NewCoin(NewDenom, curSupply) + additionalSupplyCoin := sdk.NewCoin(denomInfo.NewDenom, additionalSupply) + curSupplyCoin := sdk.NewCoin(denomInfo.NewDenom, curSupply) - // calculate the difference between the new supply and the current supply - supplyDiffCoin := newSupplyCoins.Sub(curSupplyCoin) - if supplyDiffCoin.IsNegative() { - panic("asi upgrade update supply: new supply is less than current supply") - } + // add new coins to the current supply + newSupplyCoins := additionalSupplyCoin.Add(curSupplyCoin) - // add the supply diff to the overflow address balance - overflowAddressBalanceCoins = overflowAddressBalanceCoins.Add(supplyDiffCoin) + // add the additional coins to the overflow address balance + overflowAddressBalanceCoins = overflowAddressBalanceCoins.Add(additionalSupplyCoin) // update the supply in the bank module supply[curSupplyIdx].(map[string]interface{})["amount"] = newSupplyCoins.Amount.String() - balances[(*balancesMap)[NewSupplyOverflowAddress]].(map[string]interface{})["coins"] = getInterfaceSliceFromCoins(overflowAddressBalanceCoins) + balances[(*balancesMap)[supplyInfo.UpdatedSupplyOverflowAddr]].(map[string]interface{})["coins"] = getInterfaceSliceFromCoins(overflowAddressBalanceCoins) } func convertAddressToASI(addr string, addressPrefix string) (string, error) { From 05cca9e33679a931b8ae1af2aaa69ebbfc0aa078 Mon Sep 17 00:00:00 2001 From: jonathansumner Date: Fri, 17 May 2024 11:02:33 +0100 Subject: [PATCH 4/4] refact: renaming and tidying --- cmd/fetchd/cmd/genesis-asi-upgrade.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/fetchd/cmd/genesis-asi-upgrade.go b/cmd/fetchd/cmd/genesis-asi-upgrade.go index 5c74e873..79b4c7ac 100644 --- a/cmd/fetchd/cmd/genesis-asi-upgrade.go +++ b/cmd/fetchd/cmd/genesis-asi-upgrade.go @@ -51,7 +51,7 @@ var networkInfos = map[string]NetworkConfig{ OldDenom: "afet", }, SupplyInfo: SupplyInfo{ - AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this + SupplyToMint: "100000000000000000000000000", // TODO(JS): likely amend this UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this }, IbcTargetAddr: "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x", // TODO(JS): amend this @@ -73,7 +73,7 @@ var networkInfos = map[string]NetworkConfig{ OldDenom: "atestfet", }, SupplyInfo: SupplyInfo{ - AdditionalSupplyValue: "100000000000000000000000000", // TODO(JS): likely amend this + SupplyToMint: "100000000000000000000000000", // TODO(JS): likely amend this UpdatedSupplyOverflowAddr: "fetch15p3rl5aavw9rtu86tna5lgxfkz67zzr6ed4yhw", // TODO(JS): likely amend this }, IbcTargetAddr: "fetch1rhrlzsx9z865dqen8t4v47r99dw6y4va4uph0x", // TODO(JS): amend this @@ -437,7 +437,7 @@ func ASIGenesisUpgradeWithdrawReconciliationBalances(jsonData map[string]interfa func ASIGenesisUpgradeASISupply(jsonData map[string]interface{}, networkInfo NetworkConfig) { denomInfo := networkInfo.DenomInfo supplyInfo := networkInfo.SupplyInfo - additionalSupply, ok := sdk.NewIntFromString(supplyInfo.AdditionalSupplyValue) + additionalSupply, ok := sdk.NewIntFromString(supplyInfo.SupplyToMint) if !ok { panic("asi upgrade update supply: failed to convert new supply value to int") } @@ -472,7 +472,7 @@ func ASIGenesisUpgradeASISupply(jsonData map[string]interface{}, networkInfo Net curSupplyCoin := sdk.NewCoin(denomInfo.NewDenom, curSupply) // add new coins to the current supply - newSupplyCoins := additionalSupplyCoin.Add(curSupplyCoin) + newSupplyCoins := curSupplyCoin.Add(additionalSupplyCoin) // add the additional coins to the overflow address balance overflowAddressBalanceCoins = overflowAddressBalanceCoins.Add(additionalSupplyCoin) @@ -573,7 +573,7 @@ type NetworkConfig struct { type SupplyInfo struct { UpdatedSupplyOverflowAddr string - AdditionalSupplyValue string + SupplyToMint string } type DenomInfo struct {