Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the rewards collects for bid #2539

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"];
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
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()
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
r.Bidder = bid.Bidder
r.Bid = coin.UmeeInt(bid.Amount)
}
rewards, _ := q.Keeper(&ctx).getRewardsAuction(msg.Id)
Expand Down
31 changes: 19 additions & 12 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 @@ -30,7 +28,11 @@ func (k Keeper) FinalizeRewardsAuction() error {
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, err := sdk.AccAddressFromBech32(bid.Bidder)
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
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 Down Expand Up @@ -79,26 +81,33 @@ 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, err := sdk.AccAddressFromBech32(lastBid.Bidder)
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
if err = k.sendFromModule(bidderAccAddr, returned); err != nil {
return err
}
}
}

sender, err := sdk.AccAddressFromBech32(msg.Sender)
gsk967 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

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 +121,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 +131,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
Loading