Skip to content

Commit

Permalink
feat(oracle): remove tobin tax (#816)
Browse files Browse the repository at this point in the history
* change: remove tobin tax mentions from oracle protos

* chore: rebuild protos

* change: adjust code in genesis, pair list and params

* change: remove query tobin taxes CLI cmd

* remove: querier tobin taxes related functionality

* change: rename tobin tax to pair

* change: adjust code to not use TobinTax but Pair instead

* change: fixes and test adjustments

* change: adjust test

* chore: increase coverage of ApplyWhitelist

* chore: tobin tax=>pair renamings

* chore: lint

* chore: update CHANGELOG.md

* chore: lint

* Update CHANGELOG.md

Co-authored-by: AgentSmithMatrix <98403347+AgentSmithMatrix@users.noreply.github.com>
  • Loading branch information
testinginprod and AgentSmithMatrix authored Aug 15, 2022
1 parent fc260a5 commit 5f42aac
Show file tree
Hide file tree
Showing 37 changed files with 422 additions and 1,784 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

* [#791](https://github.com/NibiruChain/nibiru/pull/791) Add the x/oracle module
- [#813](https://github.com/NibiruChain/nibiru/pull/813) - (vpool): Expose mark price, mark TWAP, index price, and k (swap invariant) in the all-pools query
* [#816](https://github.com/NibiruChain/nibiru/pull/816) - Remove tobin tax from x/oracle
* [#813](https://github.com/NibiruChain/nibiru/pull/813) - (vpool): Expose mark price, mark TWAP, index price, and k (swap invariant) in the all-pools query
* [#810](https://github.com/NibiruChain/nibiru/pull/810) - feat(x/perp): expose 'marginRatioIndex' and block number on QueryTraderPosition

## [v0.12.1](https://github.com/NibiruChain/nibiru/releases/tag/v0.12.1) - 2022-08-04
Expand Down
10 changes: 1 addition & 9 deletions proto/oracle/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ message GenesisState {
repeated MissCounter miss_counters = 4 [(gogoproto.nullable) = false];
repeated AggregateExchangeRatePrevote aggregate_exchange_rate_prevotes = 5 [(gogoproto.nullable) = false];
repeated AggregateExchangeRateVote aggregate_exchange_rate_votes = 6 [(gogoproto.nullable) = false];
repeated TobinTax tobin_taxes = 7 [(gogoproto.nullable) = false];
repeated Pair pairs = 7 [(gogoproto.nullable) = false];
}

// FeederDelegation is the address for where oracle feeder authority are
Expand All @@ -33,11 +33,3 @@ message MissCounter {
string validator_address = 1;
uint64 miss_counter = 2;
}

// TobinTax defines a pair and tobin_tax pair used in
// oracle module's genesis state
message TobinTax {
string pair = 1;
string tobin_tax = 2
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}
5 changes: 0 additions & 5 deletions proto/oracle/v1beta1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ message Pair {
option (gogoproto.goproto_stringer) = false;

string name = 1 [(gogoproto.moretags) = "yaml:\"name\""];
string tobin_tax = 2 [
(gogoproto.moretags) = "yaml:\"tobin_tax\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// struct for aggregate prevoting on the ExchangeRateVote.
Expand Down
41 changes: 0 additions & 41 deletions proto/oracle/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ service Query {
option (google.api.http).get = "/nibiru/oracle/v1beta1/pairs/exchange_rates";
}

// TobinTax returns tobin tax of a pair
rpc TobinTax(QueryTobinTaxRequest) returns (QueryTobinTaxResponse) {
option (google.api.http).get = "/nibiru/oracle/v1beta1/pairs/{pair}/tobin_tax";
}

// TobinTaxes returns tobin taxes of all pairs
rpc TobinTaxes(QueryTobinTaxesRequest) returns (QueryTobinTaxesResponse) {
option (google.api.http).get = "/nibiru/oracle/v1beta1/pairs/tobin_taxes";
}

// Actives returns all active pairs
rpc Actives(QueryActivesRequest) returns (QueryActivesResponse) {
option (google.api.http).get = "/nibiru/oracle/v1beta1/pairs/actives";
Expand Down Expand Up @@ -106,37 +96,6 @@ message QueryExchangeRatesResponse {
];
}

// QueryTobinTaxRequest is the request type for the Query/TobinTax RPC method.
message QueryTobinTaxRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// pair defines the asset to query for.
string pair = 1;
}

// QueryTobinTaxResponse is response type for the
// Query/TobinTax RPC method.
message QueryTobinTaxResponse {
// tobin_taxe defines the tobin tax of a pair
string tobin_tax = 1
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// QueryTobinTaxesRequest is the request type for the Query/TobinTaxes RPC method.
message QueryTobinTaxesRequest {}

// QueryTobinTaxesResponse is response type for the
// Query/TobinTaxes RPC method.
message QueryTobinTaxesResponse {
// tobin_taxes defines a list of the tobin tax of all whitelisted pairs
repeated Pair tobin_taxes = 1 [
(gogoproto.moretags) = "yaml:\"tobin_taxes\"",
(gogoproto.castrepeated) = "PairList",
(gogoproto.nullable) = false
];
}

// QueryActivesRequest is the request type for the Query/Actives RPC method.
message QueryActivesRequest {}

Expand Down
15 changes: 7 additions & 8 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
}
}

// Pair-TobinTax map
pairTobinTaxMap := make(map[string]sdk.Dec)
k.IterateTobinTaxes(ctx, func(pair string, tobinTax sdk.Dec) bool {
pairTobinTaxMap[pair] = tobinTax
pairsMap := make(map[string]struct{})
k.IteratePairs(ctx, func(pair string) bool {
pairsMap[pair] = struct{}{}
return false
})

Expand All @@ -55,7 +54,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
// NOTE: **Make abstain votes to have zero vote power**
pairBallotMap := k.OrganizeBallotByPair(ctx, validatorClaimMap)

if referencePair := PickReferencePair(ctx, k, pairTobinTaxMap, pairBallotMap); referencePair != "" {
if referencePair := PickReferencePair(ctx, k, pairsMap, pairBallotMap); referencePair != "" {
// make voteMap of reference pair to calculate cross exchange rates
referenceBallot := pairBallotMap[referencePair]
referenceValidatorExchangeRateMap := referenceBallot.ToMap()
Expand Down Expand Up @@ -83,7 +82,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {

//---------------------------
// Do miss counting & slashing
voteTargetsLen := len(pairTobinTaxMap)
voteTargetsLen := len(pairsMap)
for _, claim := range validatorClaimMap {
// Skip abstain & valid voters
if int(claim.WinCount) == voteTargetsLen {
Expand All @@ -99,15 +98,15 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
ctx,
(int64)(params.VotePeriod),
(int64)(params.RewardDistributionWindow),
pairTobinTaxMap,
pairsMap,
validatorClaimMap,
)

// Clear the ballot
k.ClearBallots(ctx, params.VotePeriod)

// Update vote targets and tobin tax
k.ApplyWhitelist(ctx, params.Whitelist, pairTobinTaxMap)
k.ApplyWhitelist(ctx, params.Whitelist, pairsMap)
}

// Do slash who did miss voting over threshold and
Expand Down
52 changes: 21 additions & 31 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ func TestOracleRewardDistribution(t *testing.T) {
func TestOracleRewardBand(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// clear tobin tax to reset vote targets
input.OracleKeeper.ClearTobinTaxes(input.Ctx)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
// clear pairs to reset vote targets
input.OracleKeeper.ClearPairs(input.Ctx)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())

rewardSpread := randomExchangeRate.Mul(input.OracleKeeper.RewardBand(input.Ctx).QuoInt64(2))

Expand Down Expand Up @@ -461,9 +461,9 @@ func TestOracleExchangeRateVal5(t *testing.T) {
func TestInvalidVotesSlashing(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())

votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
slashFraction := input.OracleKeeper.SlashFraction(input.Ctx)
Expand Down Expand Up @@ -542,12 +542,12 @@ func TestWhitelistSlashing(t *testing.T) {
func TestNotPassedBallotSlashing(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// clear tobin tax to reset vote targets
input.OracleKeeper.ClearTobinTaxes(input.Ctx)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
input.OracleKeeper.ClearPairs(input.Ctx)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())

input.Ctx = input.Ctx.WithBlockHeight(input.Ctx.BlockHeight() + 1)

Expand All @@ -563,12 +563,12 @@ func TestNotPassedBallotSlashing(t *testing.T) {
func TestAbstainSlashing(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// clear tobin tax to reset vote targets
input.OracleKeeper.ClearTobinTaxes(input.Ctx)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
input.OracleKeeper.ClearPairs(input.Ctx)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())

votePeriodsPerWindow := sdk.NewDec(int64(input.OracleKeeper.SlashWindow(input.Ctx))).QuoInt64(int64(input.OracleKeeper.VotePeriod(input.Ctx))).TruncateInt64()
minValidPerWindow := input.OracleKeeper.MinValidPerWindow(input.Ctx)
Expand Down Expand Up @@ -596,12 +596,12 @@ func TestAbstainSlashing(t *testing.T) {
func TestVoteTargets(t *testing.T) {
input, h := setup(t)
params := input.OracleKeeper.GetParams(input.Ctx)
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}, {Name: common.PairBTCStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}, {Name: common.PairBTCStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// clear tobin tax to reset vote targets
input.OracleKeeper.ClearTobinTaxes(input.Ctx)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
input.OracleKeeper.ClearPairs(input.Ctx)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())

// govstable
makeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: common.PairGovStable.String(), ExchangeRate: randomExchangeRate}}, 0)
Expand All @@ -618,13 +618,8 @@ func TestVoteTargets(t *testing.T) {
// vote targets are {govstable, btcstable}
require.Equal(t, []string{common.PairBTCStable.String(), common.PairGovStable.String()}, input.OracleKeeper.GetVoteTargets(input.Ctx))

// tobin tax must be exists for btcstable
btcStableTax, err := input.OracleKeeper.GetTobinTax(input.Ctx, common.PairBTCStable.String())
require.NoError(t, err)
require.Equal(t, types.DefaultTobinTax, btcStableTax)

// delete btcstable
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: types.DefaultTobinTax}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// govstable, missing
Expand All @@ -641,11 +636,11 @@ func TestVoteTargets(t *testing.T) {
// btcstable must be deleted
require.Equal(t, []string{common.PairGovStable.String()}, input.OracleKeeper.GetVoteTargets(input.Ctx))

_, err = input.OracleKeeper.GetTobinTax(input.Ctx, common.PairBTCStable.String())
require.Error(t, err)
exists := input.OracleKeeper.PairExists(input.Ctx, common.PairBTCStable.String())
require.False(t, exists)

// change govstable tobin tax
params.Whitelist = types.PairList{{Name: common.PairGovStable.String(), TobinTax: sdk.ZeroDec()}}
params.Whitelist = types.PairList{{Name: common.PairGovStable.String()}}
input.OracleKeeper.SetParams(input.Ctx, params)

// govstable, no missing
Expand All @@ -658,19 +653,14 @@ func TestVoteTargets(t *testing.T) {
require.Equal(t, uint64(1), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[0]))
require.Equal(t, uint64(1), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[1]))
require.Equal(t, uint64(1), input.OracleKeeper.GetMissCounter(input.Ctx, keeper.ValAddrs[2]))

// govstable tobin tax must be 0
tobinTax, err := input.OracleKeeper.GetTobinTax(input.Ctx, common.PairGovStable.String())
require.NoError(t, err)
require.True(t, sdk.ZeroDec().Equal(tobinTax))
}

func TestAbstainWithSmallStakingPower(t *testing.T) {
input, h := setupWithSmallVotingPower(t)

// clear tobin tax to reset vote targets
input.OracleKeeper.ClearTobinTaxes(input.Ctx)
input.OracleKeeper.SetTobinTax(input.Ctx, common.PairGovStable.String(), types.DefaultTobinTax)
input.OracleKeeper.ClearPairs(input.Ctx)
input.OracleKeeper.SetPair(input.Ctx, common.PairGovStable.String())
makeAggregatePrevoteAndVote(t, input, h, 0, types.ExchangeRateTuples{{Pair: common.PairGovStable.String(), ExchangeRate: sdk.ZeroDec()}}, 0)

oracle.EndBlocker(input.Ctx, input.OracleKeeper)
Expand Down
54 changes: 0 additions & 54 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryAggregatePrevote(),
GetCmdQueryAggregateVote(),
GetCmdQueryVoteTargets(),
GetCmdQueryTobinTaxes(),
)

return oracleQueryCmd
Expand Down Expand Up @@ -365,56 +364,3 @@ func GetCmdQueryVoteTargets() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryTobinTaxes implements the query params command.
func GetCmdQueryTobinTaxes() *cobra.Command {
cmd := &cobra.Command{
Use: "tobin-taxes [pair]",
Args: cobra.RangeArgs(0, 1),
Short: "Query the current Oracle tobin taxes.",
Long: strings.TrimSpace(`
Query the current Oracle tobin taxes.
$ nibid query oracle tobin-taxes
Or, can filter with pair
$ nibid query oracle tobin-taxes nibi:usd
Or, can
`),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

if len(args) == 0 {
res, err := queryClient.TobinTaxes(
context.Background(),
&types.QueryTobinTaxesRequest{},
)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}

pair := args[0]
res, err := queryClient.TobinTax(
context.Background(),
&types.QueryTobinTaxRequest{Pair: pair},
)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
16 changes: 8 additions & 8 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState
keeper.SetAggregateExchangeRateVote(ctx, valAddr, av)
}

if len(data.TobinTaxes) > 0 {
for _, tt := range data.TobinTaxes {
keeper.SetTobinTax(ctx, tt.Pair, tt.TobinTax)
if len(data.Pairs) > 0 {
for _, tt := range data.Pairs {
keeper.SetPair(ctx, tt.Name)
}
} else {
for _, item := range data.Params.Whitelist {
keeper.SetTobinTax(ctx, item.Name, item.TobinTax)
keeper.SetPair(ctx, item.Name)
}
}

Expand Down Expand Up @@ -117,9 +117,9 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
return false
})

tobinTaxes := []types.TobinTax{}
keeper.IterateTobinTaxes(ctx, func(pair string, tobinTax sdk.Dec) (stop bool) {
tobinTaxes = append(tobinTaxes, types.TobinTax{Pair: pair, TobinTax: tobinTax})
pairs := []types.Pair{}
keeper.IteratePairs(ctx, func(pair string) (stop bool) {
pairs = append(pairs, types.Pair{Name: pair})
return false
})

Expand All @@ -129,5 +129,5 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
missCounters,
aggregateExchangeRatePrevotes,
aggregateExchangeRateVotes,
tobinTaxes)
pairs)
}
4 changes: 2 additions & 2 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestExportInitGenesis(t *testing.T) {
input.OracleKeeper.SetExchangeRate(input.Ctx, "denom", sdk.NewDec(123))
input.OracleKeeper.SetAggregateExchangeRatePrevote(input.Ctx, keeper.ValAddrs[0], types.NewAggregateExchangeRatePrevote(types.AggregateVoteHash{123}, keeper.ValAddrs[0], uint64(2)))
input.OracleKeeper.SetAggregateExchangeRateVote(input.Ctx, keeper.ValAddrs[0], types.NewAggregateExchangeRateVote(types.ExchangeRateTuples{{Pair: "foo", ExchangeRate: sdk.NewDec(123)}}, keeper.ValAddrs[0]))
input.OracleKeeper.SetTobinTax(input.Ctx, "denom", sdk.NewDecWithPrec(123, 3))
input.OracleKeeper.SetTobinTax(input.Ctx, "denom2", sdk.NewDecWithPrec(123, 3))
input.OracleKeeper.SetPair(input.Ctx, "pair1:pair1")
input.OracleKeeper.SetPair(input.Ctx, "pair2:pair2")
input.OracleKeeper.SetMissCounter(input.Ctx, keeper.ValAddrs[0], 10)
genesis := oracle.ExportGenesis(input.Ctx, input.OracleKeeper)

Expand Down
Loading

0 comments on commit 5f42aac

Please sign in to comment.