From 8a1b9fd760ddc26c84795ffde29c34dd3b501953 Mon Sep 17 00:00:00 2001 From: puneetmahajan Date: Thu, 11 Jan 2024 22:08:48 +0530 Subject: [PATCH] refactor ica_tx.go. --- x/ratesync/keeper/ica_tx.go | 78 ++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/x/ratesync/keeper/ica_tx.go b/x/ratesync/keeper/ica_tx.go index 0c5cbc6be..0980a4331 100644 --- a/x/ratesync/keeper/ica_tx.go +++ b/x/ratesync/keeper/ica_tx.go @@ -102,32 +102,9 @@ func (k *Keeper) ExecuteLiquidStakeRateTx(ctx sdk.Context, feature types.LiquidS mintDenom, hostDenom string, cValue sdk.Dec, hostchainId uint64, connectionID string, icaAccount liquidstakeibctypes.ICAAccount) error { if feature.AllowsDenom(mintDenom) { - contractMsg := types.ExecuteLiquidStakeRate{ - LiquidStakeRate: types.LiquidStakeRate{ - DefaultBondDenom: hostDenom, - StkDenom: mintDenom, - CValue: cValue, - ControllerChainTime: ctx.BlockTime().Unix(), - }, - } - contractBz, err := json.Marshal(contractMsg) - if err != nil { - return err - } - msg := &wasmtypes.MsgExecuteContract{ - Sender: icaAccount.Address, - Contract: feature.ContractAddress, - Msg: contractBz, - Funds: nil, - } - memo := types.ICAMemo{ - FeatureType: feature.FeatureType, - HostChainID: hostchainId, - } - memoBz, err := json.Marshal(memo) + msg, memoBz, err := GenerateExecuteLiquidStakeRateTxMsg(ctx.BlockTime().Unix(), feature, mintDenom, hostDenom, cValue, hostchainId, icaAccount) if err != nil { return err - } _, err = k.GenerateAndExecuteICATx(ctx, connectionID, icaAccount.Owner, []proto.Message{msg}, string(memoBz)) if err != nil { @@ -137,15 +114,58 @@ func (k *Keeper) ExecuteLiquidStakeRateTx(ctx sdk.Context, feature types.LiquidS return nil } +func GenerateExecuteLiquidStakeRateTxMsg(blockTime int64, feature types.LiquidStake, + mintDenom, hostDenom string, cValue sdk.Dec, hostchainId uint64, + icaAccount liquidstakeibctypes.ICAAccount) (sdk.Msg, []byte, error) { + contractMsg := types.ExecuteLiquidStakeRate{ + LiquidStakeRate: types.LiquidStakeRate{ + DefaultBondDenom: hostDenom, + StkDenom: mintDenom, + CValue: cValue, + ControllerChainTime: blockTime, + }, + } + contractBz, err := json.Marshal(contractMsg) + if err != nil { + return nil, nil, err + } + msg := &wasmtypes.MsgExecuteContract{ + Sender: icaAccount.Address, + Contract: feature.ContractAddress, + Msg: contractBz, + Funds: nil, + } + memo := types.ICAMemo{ + FeatureType: feature.FeatureType, + HostChainID: hostchainId, + } + memoBz, err := json.Marshal(memo) + if err != nil { + return nil, nil, err + + } + return msg, memoBz, nil +} + func (k *Keeper) InstantiateLiquidStakeContract(ctx sdk.Context, icaAccount liquidstakeibctypes.ICAAccount, feature types.LiquidStake, id uint64, connectionID string) error { // generate contract msg{msg} + msg, memoBz, err := GenerateInstantiateLiquidStakeContractMsg(icaAccount, feature, id) + _, err = k.GenerateAndExecuteICATx(ctx, connectionID, icaAccount.Owner, []proto.Message{msg}, string(memoBz)) + if err != nil { + return err + } + return nil +} + +func GenerateInstantiateLiquidStakeContractMsg(icaAccount liquidstakeibctypes.ICAAccount, + feature types.LiquidStake, id uint64) (sdk.Msg, []byte, error) { contractMsg := types.InstantiateLiquidStakeRateContract{ Admin: icaAccount.Address, } contractMsgBz, err := json.Marshal(contractMsg) if err != nil { - return errorsmod.Wrapf(err, "unable to marshal InstantiateLiquidStakeRateContract") + return nil, nil, errorsmod.Wrapf(err, "unable to marshal InstantiateLiquidStakeRateContract") } msg := &wasmtypes.MsgInstantiateContract{ @@ -162,11 +182,7 @@ func (k *Keeper) InstantiateLiquidStakeContract(ctx sdk.Context, icaAccount liqu } memobz, err := json.Marshal(memo) if err != nil { - return err + return nil, nil, err } - _, err = k.GenerateAndExecuteICATx(ctx, connectionID, icaAccount.Owner, []proto.Message{msg}, string(memobz)) - if err != nil { - return err - } - return nil + return msg, memobz, nil }