Skip to content

Commit

Permalink
added back in native token amount during redemption and removed from …
Browse files Browse the repository at this point in the history
…callbacks
  • Loading branch information
sampocs committed Jan 10, 2024
1 parent 9f56e82 commit 9151887
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 52 deletions.
2 changes: 1 addition & 1 deletion x/records/types/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import sdkmath "cosmossdk.io/math"

// Helper function to evaluate if a host zone unbonding record still needs to be initiated
func (r HostZoneUnbonding) ShouldInitiateUnbonding() bool {
return r.Status == HostZoneUnbonding_UNBONDING_QUEUE && r.StTokenAmount.GT(sdkmath.ZeroInt())
return r.Status == HostZoneUnbonding_UNBONDING_QUEUE && r.NativeTokenAmount.GT(sdkmath.ZeroInt())
}
1 change: 1 addition & 0 deletions x/stakeibc/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (k Keeper) BeforeEpochStart(ctx sdk.Context, epochInfo epochstypes.EpochInf
k.Logger(ctx).Error(fmt.Sprintf("Unable to update epoch tracker, err: %s", err.Error()))
return
}

// Day Epoch - Process Unbondings
if epochInfo.Identifier == epochstypes.DAY_EPOCH {
// Initiate unbondings from any hostZone where it's appropriate
Expand Down
5 changes: 0 additions & 5 deletions x/stakeibc/keeper/icacallbacks_undelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ func (k Keeper) UndelegateCallback(ctx sdk.Context, packet channeltypes.Packet,
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Undelegate,
icacallbackstypes.AckResponseStatus_FAILURE, packet))

// Set NativeTokenAmounts on these HZUs to 0
if err := k.ResetUnbondingNativeTokenAmounts(ctx, chainId, undelegateCallback.EpochUnbondingRecordIds); err != nil {
return err
}

// Reset unbondings record status
if err := k.RecordsKeeper.SetHostZoneUnbondings(
ctx,
Expand Down
6 changes: 3 additions & 3 deletions x/stakeibc/keeper/msg_server_redeem_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ func (k msgServer) RedeemStake(goCtx context.Context, msg *types.MsgRedeemStake)
}
// - Creator owns at least "amount" stAssets
balance := k.bankKeeper.GetBalance(ctx, sender, stDenom)
k.Logger(ctx).Info(fmt.Sprintf("Redemption issuer stDenom balance: %v%s", balance.Amount, balance.Denom))
k.Logger(ctx).Info(fmt.Sprintf("Redemption requested stDenom amount: %v%s", msg.Amount, stDenom))
if balance.Amount.LT(msg.Amount) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "balance is lower than redemption amount. redemption amount: %v, balance %v: ", msg.Amount, balance.Amount)
}
Expand All @@ -85,12 +83,13 @@ func (k msgServer) RedeemStake(goCtx context.Context, msg *types.MsgRedeemStake)
// Add the unbonded amount to the UserRedemptionRecord
// The record is set below
userRedemptionRecord.StTokenAmount = userRedemptionRecord.StTokenAmount.Add(msg.Amount)
userRedemptionRecord.Amount = userRedemptionRecord.Amount.Add(nativeAmount)
} else {
// First time a user is redeeming this epoch
userRedemptionRecord = recordstypes.UserRedemptionRecord{
Id: redemptionId,
Receiver: msg.Receiver,
Amount: sdk.ZeroInt(),
Amount: nativeAmount,
Denom: hostZone.HostDenom,
HostZoneId: hostZone.ChainId,
EpochNumber: epochTracker.EpochNumber,
Expand All @@ -117,6 +116,7 @@ func (k msgServer) RedeemStake(goCtx context.Context, msg *types.MsgRedeemStake)
// Only append a UserRedemptionRecord to the HZU if it wasn't previously appended
hostZoneUnbonding.UserRedemptionRecords = append(hostZoneUnbonding.UserRedemptionRecords, userRedemptionRecord.Id)
}
hostZoneUnbonding.NativeTokenAmount = hostZoneUnbonding.NativeTokenAmount.Add(nativeAmount)

// Escrow user's balance
redeemCoin := sdk.NewCoins(sdk.NewCoin(stDenom, msg.Amount))
Expand Down
9 changes: 1 addition & 8 deletions x/stakeibc/keeper/msg_server_restore_interchain_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,8 @@ func (k msgServer) RestoreInterchainAccount(goCtx context.Context, msg *types.Ms
epochNumberForPendingTransferRecords = append(epochNumberForPendingTransferRecords, epochUnbondingRecord.EpochNumber)
}
}
// Set UNBONDING_IN_PROGRESS records NativeTokenAmounts to 0
err := k.SetNativeTokensToZeroInUnbondingRecords(ctx, hostZone.ChainId, epochNumberForPendingUnbondingRecords)
if err != nil {
errMsg := fmt.Sprintf("unable to set native token amounts to 0 for chainId: %s and epochUnbondingRecordIds: %v, err: %s")
k.Logger(ctx).Error(errMsg)
// TODO Question: should we return an error here, or just proceed?
}
// Revert UNBONDING_IN_PROGRESS records to UNBONDING_QUEUE
err = k.RecordsKeeper.SetHostZoneUnbondings(ctx, hostZone.ChainId, epochNumberForPendingUnbondingRecords, recordtypes.HostZoneUnbonding_UNBONDING_QUEUE)
err := k.RecordsKeeper.SetHostZoneUnbondings(ctx, hostZone.ChainId, epochNumberForPendingUnbondingRecords, recordtypes.HostZoneUnbonding_UNBONDING_QUEUE)
if err != nil {
errMsg := fmt.Sprintf("unable to update host zone unbonding record status to %s for chainId: %s and epochUnbondingRecordIds: %v, err: %s",
recordtypes.HostZoneUnbonding_UNBONDING_QUEUE.String(), hostZone.ChainId, epochNumberForPendingUnbondingRecords, err)
Expand Down
46 changes: 11 additions & 35 deletions x/stakeibc/keeper/unbonding_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ func (k Keeper) CreateEpochUnbondingRecord(ctx sdk.Context, epochNumber uint64)
func (k Keeper) GetQueuedHostZoneUnbondingRecords(
ctx sdk.Context,
chainId string,
) (epochNumbers []uint64, records map[uint64]recordstypes.HostZoneUnbonding) {
) (epochNumbers []uint64, hostZoneUnbondingMap map[uint64]recordstypes.HostZoneUnbonding) {
hostZoneUnbondingMap = map[uint64]recordstypes.HostZoneUnbonding{}
for _, epochUnbonding := range k.RecordsKeeper.GetAllEpochUnbondingRecord(ctx) {
hostZoneRecord, found := k.RecordsKeeper.GetHostZoneUnbondingByChainId(ctx, epochUnbonding.EpochNumber, chainId)
if !found {
Expand All @@ -91,10 +92,10 @@ func (k Keeper) GetQueuedHostZoneUnbondingRecords(

if hostZoneRecord.ShouldInitiateUnbonding() {
epochNumbers = append(epochNumbers, epochUnbonding.EpochNumber)
records[epochUnbonding.EpochNumber] = *hostZoneRecord
hostZoneUnbondingMap[epochUnbonding.EpochNumber] = *hostZoneRecord
}
}
return epochNumbers, records
return epochNumbers, hostZoneUnbondingMap
}

// Gets the total unbonded amount for a host zone by looping through the epoch unbonding records
Expand All @@ -109,7 +110,7 @@ func (k Keeper) GetTotalUnbondAmount(ctx sdk.Context, hostZoneUnbondingRecords m

// Given a list of redemption record IDs and a redemption rate, sets the native token amount on each record and returns the total
// The native amount is calculated using the redemption rate. If a zero redemption rate is passed, set the amount to zero
func (k Keeper) SetUserRedemptionRecordNativeAmounts(
func (k Keeper) RefreshUserRedemptionRecordNativeAmounts(
ctx sdk.Context,
chainId string,
redemptionRecordIds []string,
Expand All @@ -126,10 +127,7 @@ func (k Keeper) SetUserRedemptionRecordNativeAmounts(

// A zero redemption rate is used to indicate that the native amount should be set to zero
// If the redemption rate is non-zero, calculate the number of native tokens
nativeAmount := sdkmath.ZeroInt()
if !redemptionRate.IsZero() {
nativeAmount = sdk.NewDecFromInt(userRedemptionRecord.StTokenAmount).Mul(redemptionRate).RoundInt()
}
nativeAmount := sdk.NewDecFromInt(userRedemptionRecord.StTokenAmount).Mul(redemptionRate).RoundInt()
totalNativeAmount = totalNativeAmount.Add(nativeAmount)

// Set the native amount on the record
Expand All @@ -140,7 +138,7 @@ func (k Keeper) SetUserRedemptionRecordNativeAmounts(
}

// Sets the native token amount unbonded on the host zone unbonding record and the associated user redemption records
func (k Keeper) SetHostZoneUnbondingNativeTokenAmounts(
func (k Keeper) RefreshHostZoneUnbondingNativeTokenAmounts(
ctx sdk.Context,
epochNumber uint64,
hostZoneUnbondingRecord recordstypes.HostZoneUnbonding,
Expand All @@ -154,41 +152,19 @@ func (k Keeper) SetHostZoneUnbondingNativeTokenAmounts(

// Set all native token amount on each user redemption record
redemptionRecordIds := hostZoneUnbondingRecord.UserRedemptionRecords
totalNativeAmount := k.SetUserRedemptionRecordNativeAmounts(ctx, chainId, redemptionRecordIds, hostZone.RedemptionRate)
totalNativeAmount := k.RefreshUserRedemptionRecordNativeAmounts(ctx, chainId, redemptionRecordIds, hostZone.RedemptionRate)

// Then set the total on the host zone unbonding record
hostZoneUnbondingRecord.NativeTokenAmount = totalNativeAmount
return k.RecordsKeeper.SetHostZoneUnbondingRecord(ctx, epochNumber, chainId, hostZoneUnbondingRecord)
}

// Resets the native token amounts back to zero on the host zone unbonding record and user redemption records
// This is done if the unbonding callback fails, used to indicate that it still needs to be processed
func (k Keeper) ResetUnbondingNativeTokenAmounts(ctx sdk.Context, chainId string, epochUnbondingRecordIds []uint64) error {
for _, epochNumber := range epochUnbondingRecordIds {
k.Logger(ctx).Info(fmt.Sprintf("Resetting native token amount on host zone unbondings on EpochUnbondingRecord %d to zero", epochNumber))

hostZoneUnbondingRecord, found := k.RecordsKeeper.GetHostZoneUnbondingByChainId(ctx, epochNumber, chainId)
if !found {
return errorsmod.Wrapf(recordstypes.ErrHostUnbondingRecordNotFound, "chain %d, epoch %d", chainId, epochNumber)
}

// Reset the user redemption records back to zero
redemptionRecordIds := hostZoneUnbondingRecord.UserRedemptionRecords
k.SetUserRedemptionRecordNativeAmounts(ctx, chainId, redemptionRecordIds, sdk.ZeroDec())

// Reset the host zone unbonding record back to zero
hostZoneUnbondingRecord.NativeTokenAmount = sdkmath.ZeroInt()
return k.RecordsKeeper.SetHostZoneUnbondingRecord(ctx, epochNumber, chainId, *hostZoneUnbondingRecord)
}
return nil
}

// Given a mapping of epoch unbonding record IDs to host zone unbonding records,
// sets the native token amount across all epoch unbonding records, host zone unbonding records,
// and user redemption records, using the most updated redemption rate
func (k Keeper) SetUnbondingNativeTokenAmounts(ctx sdk.Context, hostZoneUnbondings map[uint64]recordstypes.HostZoneUnbonding) error {
func (k Keeper) RefreshUnbondingNativeTokenAmounts(ctx sdk.Context, hostZoneUnbondings map[uint64]recordstypes.HostZoneUnbonding) error {
for epochNumber, hostZoneUnbondingRecord := range hostZoneUnbondings {
if err := k.SetHostZoneUnbondingNativeTokenAmounts(ctx, epochNumber, hostZoneUnbondingRecord); err != nil {
if err := k.RefreshHostZoneUnbondingNativeTokenAmounts(ctx, epochNumber, hostZoneUnbondingRecord); err != nil {
return err
}
}
Expand Down Expand Up @@ -469,7 +445,7 @@ func (k Keeper) UnbondFromHostZone(ctx sdk.Context, hostZone types.HostZone) err

// Update the native unbond amount on all relevant records
// The native amount is calculated from the stTokens
if err := k.SetUnbondingNativeTokenAmounts(ctx, epochNumberToHostZoneUnbondingMap); err != nil {
if err := k.RefreshUnbondingNativeTokenAmounts(ctx, epochNumberToHostZoneUnbondingMap); err != nil {
return err
}

Expand Down

0 comments on commit 9151887

Please sign in to comment.