Skip to content

Commit

Permalink
Fixed t2 balance moving from old tminus1 to new tminus1 on t0 deletion.
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles committed Nov 3, 2023
1 parent b868e64 commit d655d49
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions tokenomics/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func (s *usersTableSource) replaceUser(ctx context.Context, usr *users.User) err
model.BlockchainAccountAddressField
model.DeserializedUsersKey
model.IDT0Field
model.IDTMinus1Field
model.HideRankingField
model.BalanceForTMinus1Field
}
)
dbUser, err := storage.Get[user](ctx, s.db, model.SerializedUsersKey(internalID))
Expand Down Expand Up @@ -200,12 +202,12 @@ func (s *usersTableSource) replaceUser(ctx context.Context, usr *users.User) err

return multierror.Append( //nolint:wrapcheck // Not Needed.
errors.Wrapf(err, "failed to replace user:%#v", usr),
errors.Wrapf(s.updateReferredBy(ctx, internalID, dbUser[0].IDT0, usr.ID, usr.ReferredBy), "failed to updateReferredBy for user:%#v", usr),
errors.Wrapf(s.updateReferredBy(ctx, internalID, dbUser[0].IDT0, dbUser[0].IDTMinus1, usr.ID, usr.ReferredBy, dbUser[0].BalanceForTMinus1), "failed to updateReferredBy for user:%#v", usr),
errors.Wrapf(s.updateUsernameKeywords(ctx, internalID, dbUser[0].Username, usr.Username), "failed to updateUsernameKeywords for oldUser:%#v, user:%#v", dbUser, usr), //nolint:lll // .
).ErrorOrNil()
}

func (s *usersTableSource) updateReferredBy(ctx context.Context, id, oldIDT0 int64, userID, referredBy string) error {
func (s *usersTableSource) updateReferredBy(ctx context.Context, id, oldIDT0, oldTMinus1 int64, userID, referredBy string, balanceForTMinus1 float64) error {
if referredBy == userID ||
referredBy == "" ||
referredBy == "bogus" ||
Expand Down Expand Up @@ -239,6 +241,42 @@ func (s *usersTableSource) updateReferredBy(ctx context.Context, id, oldIDT0 int
return errors.Wrapf(err3, "failed to get users entry for tMinus1ID:%v", t0Referral[0].IDT0)
} else if len(tMinus1Referral) == 1 {
newPartialState.IDTMinus1 = -tMinus1Referral[0].ID

if balanceForTMinus1 > 0.0 {
results, err4 := s.db.TxPipelined(ctx, func(pipeliner redis.Pipeliner) error {
if oldTMinus1 < 0 {
oldTMinus1 *= -1
}
if oldIdTMinus1Key := model.SerializedUsersKey(oldTMinus1); oldIdTMinus1Key != "" {
if err = pipeliner.HIncrByFloat(ctx, oldIdTMinus1Key, "balance_t2_pending", -balanceForTMinus1).Err(); err != nil {
return err
}
}
newTMinus1 := tMinus1Referral[0].ID
if newTMinus1 < 0 {
newTMinus1 *= -1
}
if newIdTMinus1Key := model.SerializedUsersKey(newTMinus1); newIdTMinus1Key != "" {
if err = pipeliner.HIncrByFloat(ctx, newIdTMinus1Key, "balance_t2_pending", balanceForTMinus1).Err(); err != nil {
return err
}
}

return nil
})
if err4 != nil {
return errors.Wrapf(err4, "failed to move t2 balance from:%v to:%v", oldTMinus1, newPartialState.IDTMinus1)
}
errs := make([]error, 0, len(results))
for _, result := range results {
if err = result.Err(); err != nil {
errs = append(errs, errors.Wrapf(err, "failed to run `%#v`", result.FullName()))
}
}
if errs := multierror.Append(nil, errs...); errs.ErrorOrNil() != nil {
return errors.Wrapf(errs.ErrorOrNil(), "failed to move t2 balances for id:%v,id:%v", userID, id)
}
}
}
}
}
Expand Down

0 comments on commit d655d49

Please sign in to comment.