Skip to content

Commit

Permalink
fix: fix the rewards collects for bid (#2539)
Browse files Browse the repository at this point in the history
* fix the amount collect to reward auction fee collector
  • Loading branch information
gsk967 authored Jun 6, 2024
1 parent 221e817 commit 01a3e1a
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 111 deletions.
2 changes: 1 addition & 1 deletion proto/umee/auction/v1/auction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ message RewardsParams {

// Bid records a user bid
message Bid {
bytes bidder = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string bidder = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string amount = 2 [(cosmos_proto.scalar) = "cosmos.Int", (gogoproto.customtype) = "cosmossdk.io/math.Int", (gogoproto.nullable) = false];
}

Expand Down
3 changes: 2 additions & 1 deletion proto/umee/auction/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package umee.auction.v1;

import "google/protobuf/timestamp.proto";
import "google/api/annotations.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "umee/auction/v1/auction.proto";
import "cosmos/base/v1beta1/coin.proto";
Expand Down Expand Up @@ -42,7 +43,7 @@ message QueryRewardsAuction {
message QueryRewardsAuctionResponse {
uint32 id = 1;
// highest bidder
string bidder = 2;
string bidder = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
cosmos.base.v1beta1.Coin bid = 3 [(gogoproto.nullable) = false];
repeated cosmos.base.v1beta1.Coin rewards = 4 [(gogoproto.nullable) = false];
google.protobuf.Timestamp ends_at = 5 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
Expand Down
70 changes: 34 additions & 36 deletions x/auction/auction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions x/auction/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestGenesis(t *testing.T) {
{4, auction.Rewards{newCoin(412313), randTime()}},
}
gs.RewardsBids = []auction.BidKV{
{1, auction.Bid{accs.Alice, sdkmath.NewInt(153252)}},
{2, auction.Bid{accs.Alice, sdkmath.NewInt(8521)}},
{1, auction.Bid{accs.Alice.String(), sdkmath.NewInt(153252)}},
{2, auction.Bid{accs.Alice.String(), sdkmath.NewInt(8521)}},
}
check(gs)
}
2 changes: 1 addition & 1 deletion x/auction/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (q Querier) RewardsAuction(goCtx context.Context, msg *auction.QueryRewards
bid, id := q.Keeper(&ctx).getRewardsBid(msg.Id)
r := &auction.QueryRewardsAuctionResponse{Id: id}
if bid != nil {
r.Bidder = sdk.AccAddress(bid.Bidder).String()
r.Bidder = bid.Bidder
r.Bid = coin.UmeeInt(bid.Amount)
}
rewards, _ := q.Keeper(&ctx).getRewardsAuction(msg.Id)
Expand Down
30 changes: 12 additions & 18 deletions x/auction/keeper/rewards.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package keeper

import (
"bytes"
"errors"
"fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/umee-network/umee/v6/util"
"github.com/umee-network/umee/v6/util/coin"
"github.com/umee-network/umee/v6/util/sdkutil"
"github.com/umee-network/umee/v6/util/store"
Expand All @@ -27,10 +25,10 @@ func (k Keeper) FinalizeRewardsAuction() error {
return nil
}

newCoins := k.bank.GetAllBalances(*k.ctx, k.accs.RewardsCollect)
bid, _ := k.getRewardsBid(id)
if bid != nil && len(bid.Bidder) != 0 {
err := k.sendCoins(k.accs.RewardsCollect, bid.Bidder, a.Rewards)
bidderAccAddr := sdk.MustAccAddressFromBech32(bid.Bidder)
err := k.sendCoins(k.accs.RewardsCollect, bidderAccAddr, a.Rewards)
if err != nil {
return fmt.Errorf("can't send coins to finalize the auction [%w]", err)
}
Expand All @@ -42,12 +40,10 @@ func (k Keeper) FinalizeRewardsAuction() error {
Id: id,
Bidder: sdk.AccAddress(bid.Bidder).String(),
})
} else if len(a.Rewards) != 0 {
// rollover the past rewards if there was no bidder
newCoins = newCoins.Add(a.Rewards...)
}

return k.initNewAuction(id+1, newCoins)
remainingRewards := k.bank.GetAllBalances(*k.ctx, k.accs.RewardsCollect)
return k.initNewAuction(id+1, remainingRewards)
}

func (k Keeper) initNewAuction(id uint32, rewards sdk.Coins) error {
Expand Down Expand Up @@ -79,26 +75,26 @@ func (k Keeper) rewardsBid(msg *auction.MsgRewardsBid) error {
return err
}

sender, err := sdk.AccAddressFromBech32(msg.Sender)
util.Panic(err)

toAuction := msg.Amount
if lastBid != nil {
if bytes.Equal(sender, lastBid.Bidder) {
if msg.Sender == lastBid.Bidder {
// bidder updates his last bid: send only diff
toAuction = msg.Amount.SubAmount(lastBid.Amount)
} else {
returned := coin.UmeeInt(lastBid.Amount)
if err = k.sendFromModule(lastBid.Bidder, returned); err != nil {
bidderAccAddr := sdk.MustAccAddressFromBech32(lastBid.Bidder)
if err := k.sendFromModule(bidderAccAddr, returned); err != nil {
return err
}
}
}
if err = k.sendToModule(sender, toAuction); err != nil {

sender := sdk.MustAccAddressFromBech32(msg.Sender)
if err := k.sendToModule(sender, toAuction); err != nil {
return err
}

bid := auction.Bid{Bidder: sender, Amount: msg.Amount.Amount}
bid := auction.Bid{Bidder: msg.Sender, Amount: msg.Amount.Amount}
return store.SetValue(k.store, key, &bid, keyMsg)
}

Expand All @@ -112,8 +108,7 @@ func (k Keeper) getRewardsBid(id uint32) (*auction.Bid, uint32) {
}

func (k Keeper) getAllRewardsBids() ([]auction.BidKV, error) {
elems, err := store.LoadAllKV[*store.Uint32, store.Uint32, *auction.Bid](
k.store, keyPrefixRewardsBid)
elems, err := store.LoadAllKV[*store.Uint32, store.Uint32, *auction.Bid](k.store, keyPrefixRewardsBid)
if err != nil {
return nil, err
}
Expand All @@ -123,7 +118,6 @@ func (k Keeper) getAllRewardsBids() ([]auction.BidKV, error) {
bids[i].Bid = elems[i].Val
}
return bids, nil

}

func (k Keeper) storeAllRewardsBids(elems []auction.BidKV) error {
Expand Down
19 changes: 0 additions & 19 deletions x/auction/module/genesis.go

This file was deleted.

67 changes: 35 additions & 32 deletions x/auction/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/leverage/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (k Keeper) fundModules(ctx sdk.Context, toOracle, toAuction sdk.Coins) erro
}
if !toAuctionCheck.IsZero() {
auction.EmitFundRewardsAuction(&ctx, toOracleCheck)
return send(ctx, types.ModuleName, auction.ModuleName, toAuctionCheck)
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, k.rewardsAuction, toAuctionCheck)
}

return nil
Expand Down

0 comments on commit 01a3e1a

Please sign in to comment.