From fd3bb91f03c585c57691d6752551264b708432a9 Mon Sep 17 00:00:00 2001 From: Joey Sumner <47419781+Jonathansumner@users.noreply.github.com> Date: Fri, 24 May 2024 14:44:20 +0100 Subject: [PATCH] feat: ASI replace FCC issuance contract state (#356) --- cmd/fetchd/cmd/genesis-asi-upgrade.go | 81 ++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/cmd/fetchd/cmd/genesis-asi-upgrade.go b/cmd/fetchd/cmd/genesis-asi-upgrade.go index 66982cb3..8ca24f78 100644 --- a/cmd/fetchd/cmd/genesis-asi-upgrade.go +++ b/cmd/fetchd/cmd/genesis-asi-upgrade.go @@ -45,10 +45,19 @@ const ( ) var ( + // Mobix staking contract keys stakesKey = prefixStringWithLength("stakes") unbondEntriesKey = prefixStringWithLength("unbond_entries") configKey = []byte("config") + // Fcc issuance contract keys + claimsKey = prefixStringWithLength("claims") + issuanceKey = prefixStringWithLength("issuance") + issuerAddressKey = []byte("issuer_address") + sourceOfFundsAddressKey = []byte("source_of_funds_address") + cw20AddressKey = []byte("cw20_address") + issuanceFccAddressKey = []byte("issuance_fcc_address") + // Fcc Cw20 contract keys marketingInfoKey = []byte("marketing_info") tokenInfoKey = []byte("token_info") @@ -121,6 +130,9 @@ var networkInfos = map[string]NetworkConfig{ FccCw20: &FccCw20{ Addr: "fetch1s0p7pwtm8qhvh2sfpg0ajgl20hwtehr0vcztyeku0vkzzvg044xqx4t7pt", }, + FccIssuance: &FccIssuance{ + Addr: "fetch17z773v8ree3e75s5sme38vvenlcyavcfs2ct3y6w77rwa5ag3srslelug5", + }, }, }, } @@ -178,7 +190,7 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { ASIGenesisUpgradeReplaceBridgeAdmin(jsonData, networkConfig) } - // update mobix staking contract, if address present + // update mobix staking contract, if address present TODO: include all contract checks within the functions themselves if networkConfig.Contracts != nil && networkConfig.Contracts.MobixStaking != nil { ASIGenesisUpgradeUpdateMobixStakingContract(jsonData, networkConfig) } @@ -199,6 +211,11 @@ func ASIGenesisUpgradeCmd(defaultNodeHome string) *cobra.Command { ASIGenesisUpgradeReplaceANameState(jsonData, networkConfig) } + // update fcc issuance contract + if networkConfig.Contracts != nil && networkConfig.Contracts.FccIssuance != nil { + ASIGenesisUpgradeUpdateFccIssuanceContract(jsonData, networkConfig) + } + // withdraw balances from IBC channels if err = ASIGenesisUpgradeWithdrawIBCChannelsBalances(jsonData, networkConfig, &manifest); err != nil { return err @@ -334,6 +351,63 @@ func ASIGenesisUpgradeUpdateFccCw20Contract(jsonData map[string]interface{}, net } } +func ASIGenesisUpgradeUpdateFccIssuanceContract(jsonData map[string]interface{}, networkInfo NetworkConfig) { + FccIssuanceContractAddr := networkInfo.Contracts.FccIssuance.Addr + FccIssuanceContract := getContractFromAddr(FccIssuanceContractAddr, jsonData) + re := regexp.MustCompile(fmt.Sprintf(`%s%s1([%s]{%d,%d})`, OldAddrPrefix, "", Bech32Chars, AddrDataLength+AddrChecksumLength, MaxAddrDataLength)) + + replaceContractValueString := func(value string) string { + newVal := re.ReplaceAllStringFunc(value, func(match string) string { + newAddr, err := convertAddressToASI(match, AccAddressPrefix) + if err != nil { + panic(err) + } + return newAddr + }) + return base64.StdEncoding.EncodeToString([]byte(newVal)) + } + + for _, val := range FccIssuanceContract["contract_state"].([]interface{}) { + state := val.(map[string]interface{}) + hexKey := state["key"].(string) + b64Value := state["value"].(string) + + valueBytes, err := base64.StdEncoding.DecodeString(b64Value) + if err != nil { + panic(err) + } + + keyBytes, err := hex.DecodeString(hexKey) + if err != nil { + panic(err) + } + + updatedKey := hexKey + updatedValue := b64Value + + _keyBytes := Bytes(keyBytes) + switch { + case _keyBytes.StartsWith(claimsKey): + updatedKey = replaceAddressInContractStateKey(keyBytes, claimsKey) + case _keyBytes.StartsWith(issuerAddressKey): + updatedValue = replaceContractValueString(string(valueBytes)) + case _keyBytes.StartsWith(issuanceKey): + updatedKey = replaceAddressInContractStateKey(keyBytes, issuanceKey) + case _keyBytes.StartsWith(sourceOfFundsAddressKey): + updatedValue = replaceContractValueString(string(valueBytes)) + case _keyBytes.StartsWith(cw20AddressKey): + updatedValue = replaceContractValueString(string(valueBytes)) + case _keyBytes.StartsWith(issuanceFccAddressKey): + updatedValue = replaceContractValueString(string(valueBytes)) + } + + val = map[string]interface{}{ + "key": updatedKey, + "value": updatedValue, + } + } +} + func ASIGenesisUpgradeUpdateMobixStakingContract(jsonData map[string]interface{}, networkInfo NetworkConfig) { contracts := jsonData["wasm"].(map[string]interface{})["contracts"].([]interface{}) MobixStakingContractAddress := networkInfo.Contracts.MobixStaking.Addr @@ -891,6 +965,7 @@ type Contracts struct { Almanac *Almanac AName *AName MobixStaking *MobixStaking + FccIssuance *FccIssuance FccCw20 *FccCw20 } @@ -914,3 +989,7 @@ type MobixStaking struct { type FccCw20 struct { Addr string } + +type FccIssuance struct { + Addr string +}