Skip to content

Commit

Permalink
added event for redeem stake (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Apr 15, 2024
1 parent b27e712 commit 425b487
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 29 deletions.
4 changes: 2 additions & 2 deletions x/stakedym/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k Keeper) RedeemStake(ctx sdk.Context, redeemer string, stTokenAmount sdkm

// Estimate a placeholder native amount with current RedemptionRate
// this estimate will be updated when the Undelegation record is finalized
nativeAmount := sdk.NewDecFromInt(stTokenAmount).Mul(hostZone.RedemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(stTokenAmount).Mul(hostZone.RedemptionRate).TruncateInt()
if nativeAmount.GT(hostZone.DelegatedBalance) {
return nativeToken, errorsmod.Wrapf(types.ErrUnbondAmountToLarge,
"cannot unstake an amount g.t. total staked balance: %v > %v", nativeAmount, hostZone.DelegatedBalance)
Expand Down Expand Up @@ -131,7 +131,7 @@ func (k Keeper) PrepareUndelegation(ctx sdk.Context, epochNumber uint64) error {
// Keep track of the total for the unbonding record
totalNativeTokens := sdkmath.ZeroInt()
for _, redemptionRecord := range k.GetRedemptionRecordsFromUnbondingId(ctx, unbondingRecord.Id) {
nativeAmount := sdk.NewDecFromInt(redemptionRecord.StTokenAmount).Mul(redemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(redemptionRecord.StTokenAmount).Mul(redemptionRate).TruncateInt()
redemptionRecord.NativeAmount = nativeAmount
k.SetRedemptionRecord(ctx, redemptionRecord)
totalNativeTokens = totalNativeTokens.Add(nativeAmount)
Expand Down
18 changes: 9 additions & 9 deletions x/stakedym/keeper/unbonding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ func (s *KeeperTestSuite) TestRedeemStake() {
expectedUnbondingRecord: func() *types.UnbondingRecord {
_, hz, ur, _, msg := s.getDefaultTestInputs()
ur.StTokenAmount = ur.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
ur.NativeAmount = ur.NativeAmount.Add(nativeDiff)
return ur
}(),
expectedRedemptionRecord: &types.RedemptionRecord{
UnbondingRecordId: defaultUR.Id,
Redeemer: defaultMsg.Redeemer,
StTokenAmount: defaultMsg.StTokenAmount,
NativeAmount: sdk.NewDecFromInt(defaultMsg.StTokenAmount).Mul(defaultHZ.RedemptionRate).RoundInt(),
NativeAmount: sdk.NewDecFromInt(defaultMsg.StTokenAmount).Mul(defaultHZ.RedemptionRate).TruncateInt(),
},
},
{
Expand All @@ -249,14 +249,14 @@ func (s *KeeperTestSuite) TestRedeemStake() {
expectedUnbondingRecord: func() *types.UnbondingRecord {
_, hz, ur, _, msg := s.getDefaultTestInputs()
ur.StTokenAmount = ur.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
ur.NativeAmount = ur.NativeAmount.Add(nativeDiff)
return ur
}(),
expectedRedemptionRecord: func() *types.RedemptionRecord {
_, hz, _, rr, msg := s.getDefaultTestInputs()
rr.StTokenAmount = rr.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
rr.NativeAmount = rr.NativeAmount.Add(nativeDiff)
return rr
}(),
Expand Down Expand Up @@ -339,22 +339,22 @@ func (s *KeeperTestSuite) TestPrepareUndelegation() {
expectedRedemptionRecords := []types.RedemptionRecord{
// StTokenAmount: 1000 * 1.999 = 1999 Native
{UnbondingRecordId: 4, Redeemer: "A", StTokenAmount: sdkmath.NewInt(1000), NativeAmount: sdkmath.NewInt(1999)},
// StTokenAmount: 999 * 1.999 = 1997.001, Rounded down to 1997 Native
// StTokenAmount: 999 * 1.999 = 1997.001, Truncated to 1997 Native
{UnbondingRecordId: 4, Redeemer: "B", StTokenAmount: sdkmath.NewInt(999), NativeAmount: sdkmath.NewInt(1997)},
// StTokenAmount: 100 * 1.999 = 199.9, Rounded up to 200 Native
{UnbondingRecordId: 4, Redeemer: "C", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(200)},
// StTokenAmount: 100 * 1.999 = 199.9, Truncated to 199 Native
{UnbondingRecordId: 4, Redeemer: "C", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(199)},

// Different unbonding records, should be excluded
{UnbondingRecordId: 1, Redeemer: "D", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(100)},
{UnbondingRecordId: 2, Redeemer: "E", StTokenAmount: sdkmath.NewInt(200), NativeAmount: sdkmath.NewInt(200)},
{UnbondingRecordId: 3, Redeemer: "F", StTokenAmount: sdkmath.NewInt(300), NativeAmount: sdkmath.NewInt(300)},
}
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 200)
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 199)

// Create the initial records, setting the native amount to be slightly less than expected
for _, expectedUserRedemptionRecord := range expectedRedemptionRecords {
initialRedemptionRecord := expectedUserRedemptionRecord
initialRedemptionRecord.NativeAmount = sdk.NewDecFromInt(initialRedemptionRecord.StTokenAmount).Mul(oldRedemptionRate).RoundInt()
initialRedemptionRecord.NativeAmount = sdk.NewDecFromInt(initialRedemptionRecord.StTokenAmount).Mul(oldRedemptionRate).TruncateInt()
s.App.StakedymKeeper.SetRedemptionRecord(s.Ctx, initialRedemptionRecord)
}

Expand Down
17 changes: 17 additions & 0 deletions x/stakeibc/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ func EmitSuccessfulLiquidStakeEvent(ctx sdk.Context, msg *types.MsgLiquidStake,
)
}

// Emits a successful redeem stake event, and displays metadata such as the native amount
func EmitSuccessfulRedeemStakeEvent(ctx sdk.Context, msg *types.MsgRedeemStake, hostZone types.HostZone, nativeAmount, stAmount sdkmath.Int) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRedeemStakeRequest,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyRedeemer, msg.Creator),
sdk.NewAttribute(types.AttributeKeyReceiver, msg.Receiver),
sdk.NewAttribute(types.AttributeKeyHostZone, hostZone.ChainId),
sdk.NewAttribute(types.AttributeKeyNativeBaseDenom, hostZone.HostDenom),
sdk.NewAttribute(types.AttributeKeyNativeIBCDenom, hostZone.IbcDenom),
sdk.NewAttribute(types.AttributeKeyNativeAmount, nativeAmount.String()),
sdk.NewAttribute(types.AttributeKeyStTokenAmount, stAmount.String()),
),
)
}

// Builds common LSM liquid stake attribute for the event emission
func getLSMLiquidStakeEventAttributes(hostZone types.HostZone, lsmTokenDeposit recordstypes.LSMTokenDeposit) []sdk.Attribute {
return []sdk.Attribute{
Expand Down
5 changes: 3 additions & 2 deletions x/stakeibc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,8 @@ func (k msgServer) RedeemStake(goCtx context.Context, msg *types.MsgRedeemStake)
}

// construct desired unstaking amount from host zone
// TODO [cleanup]: Consider changing to truncate int
stDenom := types.StAssetDenomFromHostZoneDenom(hostZone.HostDenom)
nativeAmount := sdk.NewDecFromInt(msg.Amount).Mul(hostZone.RedemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(msg.Amount).Mul(hostZone.RedemptionRate).TruncateInt()

if nativeAmount.GT(hostZone.TotalDelegations) {
return nil, errorsmod.Wrapf(types.ErrInvalidAmount, "cannot unstake an amount g.t. staked balance on host zone: %v", msg.Amount)
Expand Down Expand Up @@ -636,6 +635,8 @@ func (k msgServer) RedeemStake(goCtx context.Context, msg *types.MsgRedeemStake)
k.RecordsKeeper.SetEpochUnbondingRecord(ctx, *updatedEpochUnbondingRecord)

k.Logger(ctx).Info(fmt.Sprintf("executed redeem stake: %s", msg.String()))
EmitSuccessfulRedeemStakeEvent(ctx, msg, hostZone, nativeAmount, msg.Amount)

return &types.MsgRedeemStakeResponse{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion x/stakeibc/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) RefreshUserRedemptionRecordNativeAmounts(
}

// Calculate the number of native tokens using the redemption rate
nativeAmount := sdk.NewDecFromInt(userRedemptionRecord.StTokenAmount).Mul(redemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(userRedemptionRecord.StTokenAmount).Mul(redemptionRate).TruncateInt()
totalNativeAmount = totalNativeAmount.Add(nativeAmount)

// Set the native amount on the record
Expand Down
8 changes: 4 additions & 4 deletions x/stakeibc/keeper/unbonding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ func (s *KeeperTestSuite) TestRefreshUserRedemptionRecordNativeAmounts() {
expectedUserRedemptionRecords := []recordtypes.UserRedemptionRecord{
// StTokenAmount: 1000 * 1.999 = 1999 Native
{Id: "A", StTokenAmount: sdkmath.NewInt(1000), NativeTokenAmount: sdkmath.NewInt(1999)},
// StTokenAmount: 999 * 1.999 = 1997.001, Rounded down to 1997 Native
// StTokenAmount: 999 * 1.999 = 1997.001, Truncated to 1997 Native
{Id: "B", StTokenAmount: sdkmath.NewInt(999), NativeTokenAmount: sdkmath.NewInt(1997)},
// StTokenAmount: 100 * 1.999 = 199.9, Rounded up to 200 Native
{Id: "C", StTokenAmount: sdkmath.NewInt(100), NativeTokenAmount: sdkmath.NewInt(200)},
// StTokenAmount: 100 * 1.999 = 199.9, Truncated to 199 Native
{Id: "C", StTokenAmount: sdkmath.NewInt(100), NativeTokenAmount: sdkmath.NewInt(199)},
}
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 200)
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 199)

// Create the initial records which do not have the end native amount
for _, expectedUserRedemptionRecord := range expectedUserRedemptionRecords {
Expand Down
3 changes: 3 additions & 0 deletions x/stakeibc/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
EventTypeRegisterZone = "register_zone"
EventTypeRedemptionRequest = "request_redemption"
EventTypeLiquidStakeRequest = "liquid_stake"
EventTypeRedeemStakeRequest = "redeem_stake"
EventTypeLSMLiquidStakeRequest = "lsm_liquid_stake"
EventTypeHostZoneHalt = "halt_zone"
EventTypeValidatorSharesToTokensRateChange = "validator_shares_to_tokens_rate_change"
Expand All @@ -31,6 +32,8 @@ const (
AttributeKeyRedemptionRate = "redemption_rate"

AttributeKeyLiquidStaker = "liquid_staker"
AttributeKeyRedeemer = "redeemer"
AttributeKeyReceiver = "receiver"
AttributeKeyNativeBaseDenom = "native_base_denom"
AttributeKeyNativeIBCDenom = "native_ibc_denom"
AttributeKeyTotalUnbondAmount = "total_unbond_amount"
Expand Down
4 changes: 2 additions & 2 deletions x/staketia/keeper/unbonding.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (k Keeper) RedeemStake(ctx sdk.Context, redeemer string, stTokenAmount sdkm

// Estimate a placeholder native amount with current RedemptionRate
// this estimate will be updated when the Undelegation record is finalized
nativeAmount := sdk.NewDecFromInt(stTokenAmount).Mul(hostZone.RedemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(stTokenAmount).Mul(hostZone.RedemptionRate).TruncateInt()
if nativeAmount.GT(hostZone.DelegatedBalance) {
return nativeToken, errorsmod.Wrapf(types.ErrUnbondAmountToLarge,
"cannot unstake an amount g.t. total staked balance: %v > %v", nativeAmount, hostZone.DelegatedBalance)
Expand Down Expand Up @@ -131,7 +131,7 @@ func (k Keeper) PrepareUndelegation(ctx sdk.Context, epochNumber uint64) error {
// Keep track of the total for the unbonding record
totalNativeTokens := sdkmath.ZeroInt()
for _, redemptionRecord := range k.GetRedemptionRecordsFromUnbondingId(ctx, unbondingRecord.Id) {
nativeAmount := sdk.NewDecFromInt(redemptionRecord.StTokenAmount).Mul(redemptionRate).RoundInt()
nativeAmount := sdk.NewDecFromInt(redemptionRecord.StTokenAmount).Mul(redemptionRate).TruncateInt()
redemptionRecord.NativeAmount = nativeAmount
k.SetRedemptionRecord(ctx, redemptionRecord)
totalNativeTokens = totalNativeTokens.Add(nativeAmount)
Expand Down
18 changes: 9 additions & 9 deletions x/staketia/keeper/unbonding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ func (s *KeeperTestSuite) TestRedeemStake() {
expectedUnbondingRecord: func() *types.UnbondingRecord {
_, hz, ur, _, msg := s.getDefaultTestInputs()
ur.StTokenAmount = ur.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
ur.NativeAmount = ur.NativeAmount.Add(nativeDiff)
return ur
}(),
expectedRedemptionRecord: &types.RedemptionRecord{
UnbondingRecordId: defaultUR.Id,
Redeemer: defaultMsg.Redeemer,
StTokenAmount: defaultMsg.StTokenAmount,
NativeAmount: sdk.NewDecFromInt(defaultMsg.StTokenAmount).Mul(defaultHZ.RedemptionRate).RoundInt(),
NativeAmount: sdk.NewDecFromInt(defaultMsg.StTokenAmount).Mul(defaultHZ.RedemptionRate).TruncateInt(),
},
},
{
Expand All @@ -249,14 +249,14 @@ func (s *KeeperTestSuite) TestRedeemStake() {
expectedUnbondingRecord: func() *types.UnbondingRecord {
_, hz, ur, _, msg := s.getDefaultTestInputs()
ur.StTokenAmount = ur.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
ur.NativeAmount = ur.NativeAmount.Add(nativeDiff)
return ur
}(),
expectedRedemptionRecord: func() *types.RedemptionRecord {
_, hz, _, rr, msg := s.getDefaultTestInputs()
rr.StTokenAmount = rr.StTokenAmount.Add(msg.StTokenAmount)
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).RoundInt()
nativeDiff := sdk.NewDecFromInt(msg.StTokenAmount).Mul(hz.RedemptionRate).TruncateInt()
rr.NativeAmount = rr.NativeAmount.Add(nativeDiff)
return rr
}(),
Expand Down Expand Up @@ -339,22 +339,22 @@ func (s *KeeperTestSuite) TestPrepareUndelegation() {
expectedRedemptionRecords := []types.RedemptionRecord{
// StTokenAmount: 1000 * 1.999 = 1999 Native
{UnbondingRecordId: 4, Redeemer: "A", StTokenAmount: sdkmath.NewInt(1000), NativeAmount: sdkmath.NewInt(1999)},
// StTokenAmount: 999 * 1.999 = 1997.001, Rounded down to 1997 Native
// StTokenAmount: 999 * 1.999 = 1997.001, Truncated to 1997 Native
{UnbondingRecordId: 4, Redeemer: "B", StTokenAmount: sdkmath.NewInt(999), NativeAmount: sdkmath.NewInt(1997)},
// StTokenAmount: 100 * 1.999 = 199.9, Rounded up to 200 Native
{UnbondingRecordId: 4, Redeemer: "C", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(200)},
// StTokenAmount: 100 * 1.999 = 199.9, Truncated to 199 Native
{UnbondingRecordId: 4, Redeemer: "C", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(199)},

// Different unbonding records, should be excluded
{UnbondingRecordId: 1, Redeemer: "D", StTokenAmount: sdkmath.NewInt(100), NativeAmount: sdkmath.NewInt(100)},
{UnbondingRecordId: 2, Redeemer: "E", StTokenAmount: sdkmath.NewInt(200), NativeAmount: sdkmath.NewInt(200)},
{UnbondingRecordId: 3, Redeemer: "F", StTokenAmount: sdkmath.NewInt(300), NativeAmount: sdkmath.NewInt(300)},
}
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 200)
expectedTotalNativeAmount := sdkmath.NewInt(1999 + 1997 + 199)

// Create the initial records, setting the native amount to be slightly less than expected
for _, expectedUserRedemptionRecord := range expectedRedemptionRecords {
initialRedemptionRecord := expectedUserRedemptionRecord
initialRedemptionRecord.NativeAmount = sdk.NewDecFromInt(initialRedemptionRecord.StTokenAmount).Mul(oldRedemptionRate).RoundInt()
initialRedemptionRecord.NativeAmount = sdk.NewDecFromInt(initialRedemptionRecord.StTokenAmount).Mul(oldRedemptionRate).TruncateInt()
s.App.StaketiaKeeper.SetRedemptionRecord(s.Ctx, initialRedemptionRecord)
}

Expand Down

0 comments on commit 425b487

Please sign in to comment.