github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/distribution/client/common/common.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 7 staking "github.com/fibonacci-chain/fbc/x/staking/types" 8 "os" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 12 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 13 "github.com/fibonacci-chain/fbc/x/distribution/types" 14 ) 15 16 // QueryParams actually queries distribution params 17 func QueryParams(cliCtx context.CLIContext, queryRoute string) (params types.Params, err error) { 18 route := fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamCommunityTax) 19 var communityTax sdk.Dec 20 var withdrawAddrEnabled bool 21 bytes, _, err := cliCtx.QueryWithData(route, []byte{}) 22 if err != nil { 23 return 24 } 25 cliCtx.Codec.MustUnmarshalJSON(bytes, &communityTax) 26 27 route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawAddrEnabled) 28 bytes, _, err = cliCtx.QueryWithData(route, []byte{}) 29 if err != nil { 30 return 31 } 32 cliCtx.Codec.MustUnmarshalJSON(bytes, &withdrawAddrEnabled) 33 34 var distributionType uint32 35 route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamDistributionType) 36 bytes, _, err = cliCtx.QueryWithData(route, []byte{}) 37 if err == nil { 38 cliCtx.Codec.MustUnmarshalJSON(bytes, &distributionType) 39 } else if !ignoreError(err.Error()) { 40 return 41 } 42 43 var withdrawRewardEnabled bool 44 route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamWithdrawRewardEnabled) 45 bytes, _, err = cliCtx.QueryWithData(route, []byte{}) 46 if err == nil { 47 cliCtx.Codec.MustUnmarshalJSON(bytes, &withdrawRewardEnabled) 48 } else if !ignoreError(err.Error()) { 49 return 50 } 51 52 var rewardTruncatePrecision int64 53 route = fmt.Sprintf("custom/%s/params/%s", queryRoute, types.ParamRewardTruncatePrecision) 54 bytes, _, err = cliCtx.QueryWithData(route, []byte{}) 55 if err == nil { 56 cliCtx.Codec.MustUnmarshalJSON(bytes, &rewardTruncatePrecision) 57 } else if !ignoreError(err.Error()) { 58 return 59 } 60 61 return types.NewParams(communityTax, withdrawAddrEnabled, distributionType, withdrawRewardEnabled, rewardTruncatePrecision), nil 62 } 63 64 func ignoreError(err string) bool { 65 type ParamsError struct { 66 Code uint32 `json:"code"` 67 } 68 var paramsError ParamsError 69 jsonErr := json.Unmarshal([]byte(err), ¶msError) 70 if jsonErr != nil { 71 return false 72 } 73 return paramsError.Code == types.CodeUnknownDistributionParamType 74 } 75 76 // QueryValidatorCommission returns a validator's commission. 77 func QueryValidatorCommission(cliCtx context.CLIContext, queryRoute string, validatorAddr sdk.ValAddress) ( 78 []byte, error) { 79 res, _, err := cliCtx.QueryWithData( 80 fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorCommission), 81 cliCtx.Codec.MustMarshalJSON(types.NewQueryValidatorCommissionParams(validatorAddr)), 82 ) 83 return res, err 84 } 85 86 // WithdrawValidatorRewardsAndCommission builds a two-message message slice to be 87 // used to withdraw both validation's commission and self-delegation reward. 88 func WithdrawValidatorRewardsAndCommission(validatorAddr sdk.ValAddress) ([]sdk.Msg, error) { 89 commissionMsg := types.NewMsgWithdrawValidatorCommission(validatorAddr) 90 if err := commissionMsg.ValidateBasic(); err != nil { 91 return nil, err 92 } 93 94 return []sdk.Msg{commissionMsg}, nil 95 } 96 97 type WrapError struct { 98 Code uint32 `json:"code"` 99 Log string `json:"log"` 100 Codespace string `json:"codespace"` 101 Changed bool `json:"-"` 102 RawError error `json:"-"` 103 } 104 105 func (e *WrapError) Error() string { 106 data, jsonErr := json.Marshal(e) 107 if jsonErr != nil { 108 fmt.Fprintf(os.Stderr, "Trans wrap error, marshal err=%v\n", jsonErr) 109 return "" 110 } 111 return string(data) 112 } 113 114 func (e *WrapError) setLog(log string) { 115 e.Log = log 116 } 117 118 func (e *WrapError) Trans(code uint32, newLog string) { 119 if e.Code == abci.CodeTypeNonceInc+code { 120 e.setLog(fmt.Sprintf("%s;%s", newLog, e.Log)) 121 e.Changed = true 122 } 123 } 124 125 func NewWrapError(err error) *WrapError { 126 if err == nil { 127 return nil 128 } 129 var wrapErr WrapError 130 jsonErr := json.Unmarshal([]byte(err.Error()), &wrapErr) 131 if jsonErr != nil { 132 fmt.Fprintf(os.Stderr, "Trans wrap error, unmarshal err=%v\n", jsonErr) 133 return nil 134 } 135 wrapErr.RawError = err 136 return &wrapErr 137 } 138 139 func IsValidator(cliCtx context.CLIContext, cdc *codec.Codec, valAddress sdk.ValAddress) bool { 140 resKVs, _, err := cliCtx.QuerySubspace(staking.ValidatorsKey, staking.StoreKey) 141 if err != nil { 142 return false 143 } 144 145 for _, kv := range resKVs { 146 if staking.MustUnmarshalValidator(cdc, kv.Value).GetOperator().Equals(valAddress) { 147 return true 148 } 149 } 150 151 return false 152 } 153 154 func IsDelegator(cliCtx context.CLIContext, cdc *codec.Codec, delAddr sdk.AccAddress) bool { 155 delegator := staking.NewDelegator(delAddr) 156 resp, _, err := cliCtx.QueryStore(staking.GetDelegatorKey(delAddr), staking.StoreKey) 157 if err != nil { 158 return false 159 } 160 if len(resp) == 0 { 161 return false 162 } 163 cdc.MustUnmarshalBinaryLengthPrefixed(resp, &delegator) 164 165 if delegator.Tokens.IsZero() { 166 return false 167 } 168 169 return true 170 }