Skip to content

Commit

Permalink
remove uneeded casting, refractor transfer from mint (#461)
Browse files Browse the repository at this point in the history
Co-authored-by: Son Trinh <sontrinh@Sons-MacBook-Pro.local>
  • Loading branch information
sontrinh16 and Son Trinh authored Dec 22, 2022
1 parent e9351a2 commit af53f06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
22 changes: 10 additions & 12 deletions x/stakeibc/keeper/host_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,14 @@ func (k Keeper) AddDelegationToValidator(ctx sdk.Context, hostZone types.HostZon
if amount.GTE(sdk.ZeroInt()) {
validator.DelegationAmt = validator.DelegationAmt.Add(amount)
return true
} else {
absAmt := amount.Abs()
if absAmt.GT(validator.DelegationAmt) {
k.Logger(ctx).Error(fmt.Sprintf("Delegation amount %v is greater than validator %s delegation amount %v", absAmt, validatorAddress, validator.DelegationAmt))
return false
}
validator.DelegationAmt = validator.DelegationAmt.Sub(absAmt)
return true
}
absAmt := amount.Abs()
if absAmt.GT(validator.DelegationAmt) {
k.Logger(ctx).Error(fmt.Sprintf("Delegation amount %v is greater than validator %s delegation amount %v", absAmt, validatorAddress, validator.DelegationAmt))
return false
}
validator.DelegationAmt = validator.DelegationAmt.Sub(absAmt)
return true
}
}

Expand Down Expand Up @@ -193,11 +192,10 @@ func (k Keeper) RemoveValidatorFromHostZone(ctx sdk.Context, chainId string, val
hostZone.Validators = append(hostZone.Validators[:i], hostZone.Validators[i+1:]...)
k.SetHostZone(ctx, hostZone)
return nil
} else {
errMsg := fmt.Sprintf("Validator (%s) has non-zero delegation (%v) or weight (%d)", validatorAddress, val.DelegationAmt, val.Weight)
k.Logger(ctx).Error(errMsg)
return errors.New(errMsg)
}
errMsg := fmt.Sprintf("Validator (%s) has non-zero delegation (%v) or weight (%d)", validatorAddress, val.DelegationAmt, val.Weight)
k.Logger(ctx).Error(errMsg)
return errors.New(errMsg)
}
}
errMsg := fmt.Sprintf("Validator address (%s) not found on host zone (%s)", validatorAddress, chainId)
Expand Down
6 changes: 4 additions & 2 deletions x/stakeibc/keeper/msg_server_liquid_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (k msgServer) LiquidStake(goCtx context.Context, msg *types.MsgLiquidStake)
}
// mint user `amount` of the corresponding stAsset
// NOTE: We should ensure that denoms are unique - we don't want anyone spoofing denoms
err = k.MintStAsset(ctx, sender, msg.Amount, msg.HostDenom)
err = k.MintStAssetAndTransfer(ctx, sender, msg.Amount, msg.HostDenom)
if err != nil {
k.Logger(ctx).Error("failed to send tokens from Account to Module")
return nil, sdkerrors.Wrapf(err, "failed to mint %s stAssets to user", msg.HostDenom)
Expand All @@ -93,7 +93,7 @@ func (k msgServer) LiquidStake(goCtx context.Context, msg *types.MsgLiquidStake)
return &types.MsgLiquidStakeResponse{}, nil
}

func (k msgServer) MintStAsset(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Int, denom string) error {
func (k msgServer) MintStAssetAndTransfer(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Int, denom string) error {
stAssetDenom := types.StAssetDenomFromHostZoneDenom(denom)

// TODO(TEST-7): Add an exchange rate here! What object should we store the exchange rate on?
Expand All @@ -114,12 +114,14 @@ func (k msgServer) MintStAsset(ctx sdk.Context, sender sdk.AccAddress, amount sd
k.Logger(ctx).Error("Failed to mint coins")
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to mint coins")
}

// transfer those coins to the user
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, stCoins)
if err != nil {
k.Logger(ctx).Error("Failed to send coins from module to account")
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to send %s from module to account", stCoins.GetDenomByIndex(0))
}

k.Logger(ctx).Info(fmt.Sprintf("[MINT ST ASSET] success on %s.", hz.GetChainId()))
return nil
}

0 comments on commit af53f06

Please sign in to comment.