Skip to content

Commit

Permalink
Merge pull request #13 from iqlusioninc/fix/tokenizeshare-reward-with…
Browse files Browse the repository at this point in the history
…draw

fix tokenizeshare record reward withdraw
  • Loading branch information
zmanian authored Jan 18, 2022
2 parents 6a5187f + 95c6dcc commit 377a315
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,28 @@ func (k Keeper) WithdrawSingleShareRecordReward(ctx sdk.Context, recordId uint64

// withdraw rewards into reward module account and send it to reward owner
cacheCtx, write := ctx.CacheContext()
rewards, err := k.WithdrawDelegationRewards(cacheCtx, record.GetModuleAddress(), valAddr)
_, err = k.WithdrawDelegationRewards(cacheCtx, record.GetModuleAddress(), valAddr)
if err != nil {
return err
}

err = k.bankKeeper.SendCoins(cacheCtx, record.GetModuleAddress(), owner, rewards)
if err != nil {
return err
}
write()
// apply changes when the module account has positive balance
balances := k.bankKeeper.GetAllBalances(cacheCtx, record.GetModuleAddress())
if !balances.Empty() {
err = k.bankKeeper.SendCoins(cacheCtx, record.GetModuleAddress(), owner, balances)
if err != nil {
return err
}
write()

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawTokenizeShareReward,
sdk.NewAttribute(sdk.AttributeKeyAmount, totalRewards.String()),
),
)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawTokenizeShareReward,
sdk.NewAttribute(types.AttributeKeyWithdrawAddress, owner.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, totalRewards.String()),
),
)
}
return nil
}

Expand Down Expand Up @@ -248,25 +253,29 @@ func (k Keeper) WithdrawTokenizeShareRecordReward(ctx sdk.Context, ownerAddr sdk

// withdraw rewards into reward module account and send it to reward owner
cacheCtx, write := ctx.CacheContext()
rewards, err := k.WithdrawDelegationRewards(cacheCtx, record.GetModuleAddress(), valAddr)
_, err = k.WithdrawDelegationRewards(cacheCtx, record.GetModuleAddress(), valAddr)
if err != nil {
k.Logger(ctx).Error(err.Error())
continue
}

err = k.bankKeeper.SendCoins(cacheCtx, record.GetModuleAddress(), ownerAddr, rewards)
if err != nil {
k.Logger(ctx).Error(err.Error())
continue
// apply changes when the module account has positive balance
balances := k.bankKeeper.GetAllBalances(cacheCtx, record.GetModuleAddress())
if !balances.Empty() {
err = k.bankKeeper.SendCoins(cacheCtx, record.GetModuleAddress(), ownerAddr, balances)
if err != nil {
k.Logger(ctx).Error(err.Error())
continue
}
write()
totalRewards = totalRewards.Add(balances...)
}
write()

totalRewards = totalRewards.Add(rewards...)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeWithdrawTokenizeShareReward,
sdk.NewAttribute(types.AttributeKeyWithdrawAddress, ownerAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, totalRewards.String()),
),
)
Expand Down

0 comments on commit 377a315

Please sign in to comment.