Skip to content

Commit

Permalink
added dex hook to rewards module
Browse files Browse the repository at this point in the history
  • Loading branch information
faneaatiku committed Mar 19, 2024
1 parent 28b7083 commit b3c1b79
Show file tree
Hide file tree
Showing 17 changed files with 1,252 additions and 46 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fzipp/gocyclo v0.6.0 // indirect
github.com/getsentry/sentry-go v0.17.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-critic/go-critic v0.6.5 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
Expand All @@ -115,6 +116,7 @@ require (
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/gateway v1.1.0 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
Expand All @@ -137,6 +139,7 @@ require (
github.com/gostaticanalysis/forcetypeassert v0.1.0 // indirect
github.com/gostaticanalysis/nilerr v0.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -261,6 +262,7 @@ github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvA
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
Expand Down Expand Up @@ -395,6 +397,7 @@ github.com/getsentry/sentry-go v0.12.0/go.mod h1:NSap0JBYWzHND8oMbyi0+XZhUalc1TB
github.com/getsentry/sentry-go v0.17.0 h1:UustVWnOoDFHBS7IJUB2QK/nB5pap748ZEp0swnQJak=
github.com/getsentry/sentry-go v0.17.0/go.mod h1:B82dxtBvxG0KaPD8/hfSV+VcHD+Lg/xUS4JuQn1P4cM=
github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -487,18 +490,21 @@ github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzq
github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -633,6 +639,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
Expand All @@ -648,6 +656,7 @@ github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
Expand All @@ -657,6 +666,7 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -1314,13 +1324,15 @@ go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
Expand Down
2 changes: 2 additions & 0 deletions proto/rewards/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ message GenesisState {
repeated TradingReward trading_reward_list = 5 [(gogoproto.nullable) = false];
repeated StakingRewardParticipant staking_reward_participant_list = 6 [(gogoproto.nullable) = false];
repeated PendingUnlockParticipant pending_unlock_participant_list = 7 [(gogoproto.nullable) = false];
repeated TradingRewardLeaderboard trading_reward_leaderboard_list = 8 [(gogoproto.nullable) = false];
repeated TradingRewardCandidate trading_reward_candidate_list = 9 [(gogoproto.nullable) = false];
// this line is used by starport scaffolding # genesis/proto/state
}
18 changes: 18 additions & 0 deletions proto/rewards/trading_reward.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
syntax = "proto3";
package bze.v1.rewards;

import "gogoproto/gogo.proto";

option go_package = "github.com/bze-alphateam/bze/x/rewards/types";

message TradingReward {
Expand All @@ -12,3 +14,19 @@ message TradingReward {
uint32 slots = 6;
}

message TradingRewardLeaderboard {
string reward_id = 1;
repeated TradingRewardLeaderboardEntry list = 2 [(gogoproto.nullable) = false];
}

message TradingRewardLeaderboardEntry {
string amount = 1;
string address = 2;
int64 created_at = 3;
}

message TradingRewardCandidate {
string reward_id = 1;
string amount = 2;
string address = 3;
}
6 changes: 6 additions & 0 deletions testutil/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ func New(
app.EpochsKeeper,
)

app.TradebinKeeper.SetOnOrderFillHooks(
[]tradebintypes.OnMarketOrderFill{
app.RewardsKeeper.GetOnOrderFillHook(),
},
)

app.EpochsKeeper.SetHooks(
[]epochstypes.EpochHook{
app.RewardsKeeper.GetDistributeAllStakingRewardsHook(),
Expand Down
11 changes: 11 additions & 0 deletions x/rewards/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
// Set all the tradingReward
for _, elem := range genState.TradingRewardList {
k.SetTradingReward(ctx, elem)
k.SetMarketIdRewardId(ctx, elem)
}

for _, elem := range genState.TradingRewardLeaderboardList {
k.SetTradingRewardLeaderboard(ctx, elem)
}

for _, elem := range genState.TradingRewardCandidateList {
k.SetTradingRewardCandidate(ctx, elem)
}

// Set all the stakingRewardParticipant
Expand Down Expand Up @@ -45,6 +54,8 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.TradingRewardList = k.GetAllTradingReward(ctx)
genesis.StakingRewardParticipantList = k.GetAllStakingRewardParticipant(ctx)
genesis.PendingUnlockParticipantList = k.GetAllPendingUnlockParticipant(ctx)
genesis.TradingRewardLeaderboardList = k.GetAllTradingRewardLeaderboard(ctx)
genesis.TradingRewardCandidateList = k.GetAllTradingRewardCandidate(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
107 changes: 107 additions & 0 deletions x/rewards/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"github.com/bze-alphateam/bze/x/rewards/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"sort"
)

func (k Keeper) GetDistributeAllStakingRewardsHook() types.EpochHook {
Expand Down Expand Up @@ -38,3 +39,109 @@ func (k Keeper) GetUnlockPendingUnlockParticipantsHook() types.EpochHook {
return nil
})
}

func (k Keeper) GetOnOrderFillHook() func(ctx sdk.Context, marketId, amountTraded, userAddress string) {
return func(ctx sdk.Context, marketId, amountTraded, userAddress string) {
logger := ctx.Logger().With("market_id", marketId)
existingRewardId, found := k.GetMarketIdRewardId(ctx, marketId)
if !found {
logger.Debug("no rewards found for this market id")
return
}

reward, found := k.GetTradingReward(ctx, existingRewardId)
if !found {
logger.With("reward_id", existingRewardId).
Error("the reward id found for this market does not exist in trading reward store")
return
}
logger = logger.With("reward", reward)

candidate, found := k.GetTradingRewardCandidate(ctx, reward.RewardId, userAddress)
if !found {
logger.Debug("candidate not found. creating a new one")
candidate = types.TradingRewardCandidate{
RewardId: reward.RewardId,
Amount: "0",
Address: userAddress,
}
}
candidateAmount, ok := sdk.NewIntFromString(candidate.Amount)
if !ok {
logger.Error("could not parse candidate amount")

return
}

tradedAmount, ok := sdk.NewIntFromString(amountTraded)
if !ok {
logger.Error("could not parse traded amount")

return
}

candidateAmount = candidateAmount.Add(tradedAmount)
candidate.Amount = candidateAmount.String()
k.SetTradingRewardCandidate(ctx, candidate)
logger.Debug("trading reward candidate saved")

//try to add to leaderboard
leaderboard, found := k.GetTradingRewardLeaderboard(ctx, reward.RewardId)
if !found {
logger.Debug("leaderboard does not exist. creating new one")
leaderboard = types.TradingRewardLeaderboard{
RewardId: reward.RewardId,
List: []types.TradingRewardLeaderboardEntry{},
}
}

addedToList := false
for i, entry := range leaderboard.List {
if candidate.Address != entry.Address {
continue
}

entry.Amount = candidateAmount.String()
leaderboard.List[i] = entry
addedToList = true

logger.Debug("candidate already exists in leaderboard")

break
}

//not found in leaderboard, let's add it
if !addedToList {
logger.Debug("candidate does not exists in leaderboard. creating new entry")
newEntry := types.TradingRewardLeaderboardEntry{
Amount: candidate.Amount,
Address: candidate.Address,
CreatedAt: ctx.BlockTime().Unix(),
}
leaderboard.List = append(leaderboard.List, newEntry)
}

//sort the slice
sort.SliceStable(leaderboard.List[:], func(i, j int) bool {
iAmt, _ := sdk.NewIntFromString(amountTraded)
jAmt, _ := sdk.NewIntFromString(amountTraded)
if iAmt.GT(jAmt) {
return true
}
if iAmt.LT(jAmt) {
return false
}

return leaderboard.List[i].CreatedAt < leaderboard.List[j].CreatedAt
})

//trim slice if it's longer than the rewarded slots
if reward.Slots < uint32(len(leaderboard.List)) {
logger.Debug("trimming leaderboard list")
leaderboard.List = leaderboard.List[:reward.Slots]
}

k.SetTradingRewardLeaderboard(ctx, leaderboard)
logger.Debug("leaderboard set into storage")
}
}
1 change: 1 addition & 0 deletions x/rewards/keeper/msg_server_trading_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (k msgServer) CreateTradingReward(goCtx context.Context, msg *types.MsgCrea
ctx,
tradingReward,
)
k.SetMarketIdRewardId(ctx, tradingReward)

return &types.MsgCreateTradingRewardResponse{RewardId: tradingReward.RewardId}, nil
}
Expand Down
24 changes: 24 additions & 0 deletions x/rewards/keeper/trading_reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,27 @@ func (k Keeper) GetAllTradingReward(ctx sdk.Context) (list []types.TradingReward

return
}

// SetMarketIdRewardId save a reward id on a market id key
func (k Keeper) SetMarketIdRewardId(ctx sdk.Context, tradingReward types.TradingReward) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MarketIdRewardIdKeyPrefix))
val := []byte(tradingReward.RewardId)
store.Set(types.MarketIdRewardIdKey(tradingReward.MarketId), val)
}

// GetMarketIdRewardId get a reward id for a market id key
func (k Keeper) GetMarketIdRewardId(ctx sdk.Context, marketId string) (string, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MarketIdRewardIdKeyPrefix))
val := store.Get(types.MarketIdRewardIdKey(marketId))
if val == nil {
return "", false
}

return string(val), true
}

// RemoveMarketIdRewardId removes the reward id stored for a market id
func (k Keeper) RemoveMarketIdRewardId(ctx sdk.Context, marketId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.MarketIdRewardIdKeyPrefix))
store.Delete(types.MarketIdRewardIdKey(marketId))
}
Loading

0 comments on commit b3c1b79

Please sign in to comment.