-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl wip! keeper version without msg server or protos
- Loading branch information
1 parent
42e4214
commit 31d3bdb
Showing
7 changed files
with
231 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/NibiruChain/nibiru/x/common/asset" | ||
oracletypes "github.com/NibiruChain/nibiru/x/oracle/types" | ||
) | ||
|
||
// Extends the Keeper with admin functions. Admin is syntactic sugar to separate | ||
// admin calls off from the other Keeper methods. | ||
// | ||
// These Admin functions should: | ||
// 1. Not be called in other methods in the x/perp module. | ||
// 2. Only be callable from the x/sudo root or sudo contracts. | ||
// | ||
// The intention behind "admin" is to make it more obvious to the developer that | ||
// an unsafe function is being used when it's called from "OracleKeeper.Admin" | ||
type admin struct{ *Keeper } | ||
|
||
type PartialOracleParams struct { | ||
VotePeriod *sdkmath.Int `json:"vote_period,omitempty"` | ||
VoteThreshold *sdk.Dec `json:"vote_threshold,omitempty"` | ||
RewardBand *sdk.Dec `json:"reward_band,omitempty"` | ||
Whitelist []string `json:"whitelist,omitempty"` | ||
SlashFraction *sdk.Dec `json:"slash_fraction,omitempty"` | ||
SlashWindow *sdkmath.Int `json:"slash_window,omitempty"` | ||
MinValidPerWindow *sdk.Dec `json:"min_valid_per_window,omitempty"` | ||
TwapLookbackWindow *sdkmath.Int `json:"twap_lookback_window,omitempty"` | ||
MinVoters *sdkmath.Int `json:"min_voters,omitempty"` | ||
ValidatorFeeRatio *sdk.Dec `json:"validator_fee_ratio,omitempty"` | ||
} | ||
|
||
func (k admin) EditOracleParams( | ||
ctx sdk.Context, newParams PartialOracleParams, sender sdk.AccAddress, | ||
) error { | ||
if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { | ||
return err | ||
} | ||
|
||
params, err := k.Params.Get(ctx) | ||
if err != nil { | ||
// TODO: use typed error | ||
return fmt.Errorf("get oracle params error: %s", err.Error()) | ||
} | ||
|
||
mergedParams := newParams.mergeOracleParams(params) | ||
k.UpdateParams(ctx, mergedParams) | ||
return nil | ||
} | ||
|
||
// mergeOracleParams: Takes the givne oracle params and merges them into the | ||
// existing partial params, keeping any existing values that are note set in the | ||
// partial msg | ||
func (msg PartialOracleParams) mergeOracleParams( | ||
oracleParams oracletypes.Params, | ||
) oracletypes.Params { | ||
if msg.VotePeriod != nil { | ||
oracleParams.VotePeriod = msg.VotePeriod.Uint64() | ||
} | ||
|
||
if msg.VoteThreshold != nil { | ||
oracleParams.VoteThreshold = *msg.VoteThreshold | ||
} | ||
|
||
if msg.RewardBand != nil { | ||
oracleParams.RewardBand = *msg.RewardBand | ||
} | ||
|
||
if msg.Whitelist != nil { | ||
whitelist := make([]asset.Pair, len(msg.Whitelist)) | ||
for i, pair := range msg.Whitelist { | ||
whitelist[i] = asset.MustNewPair(pair) | ||
} | ||
|
||
oracleParams.Whitelist = whitelist | ||
} | ||
|
||
if msg.SlashFraction != nil { | ||
oracleParams.SlashFraction = *msg.SlashFraction | ||
} | ||
|
||
if msg.SlashWindow != nil { | ||
oracleParams.SlashWindow = msg.SlashWindow.Uint64() | ||
} | ||
|
||
if msg.MinValidPerWindow != nil { | ||
oracleParams.MinValidPerWindow = *msg.MinValidPerWindow | ||
} | ||
|
||
if msg.TwapLookbackWindow != nil { | ||
oracleParams.TwapLookbackWindow = time.Duration(msg.TwapLookbackWindow.Int64()) | ||
} | ||
|
||
if msg.MinVoters != nil { | ||
oracleParams.MinVoters = msg.MinVoters.Uint64() | ||
} | ||
|
||
if msg.ValidatorFeeRatio != nil { | ||
oracleParams.ValidatorFeeRatio = *msg.ValidatorFeeRatio | ||
} | ||
|
||
return oracleParams | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/NibiruChain/nibiru/x/common/testutil" | ||
"github.com/NibiruChain/nibiru/x/common/testutil/testapp" | ||
oraclekeeper "github.com/NibiruChain/nibiru/x/oracle/keeper" | ||
) | ||
|
||
func TestSuiteOracleExecutor_RunAll(t *testing.T) { | ||
suite.Run(t, new(SuiteOracleSudo)) | ||
} | ||
|
||
type SuiteOracleSudo struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *SuiteOracleSudo) TestEditOracleParams() { | ||
nibiru, ctx := testapp.NewNibiruTestAppAndContext() | ||
|
||
// Change to all non-defaults to test EditOracleParams as a setter . | ||
votePeriod := sdk.NewInt(1_234) | ||
voteThreshold := sdk.MustNewDecFromStr("0.4") | ||
rewardBand := sdk.MustNewDecFromStr("0.5") | ||
whitelist := []string{"aave:usdc", "sol:usdc"} | ||
slashFraction := sdk.MustNewDecFromStr("0.5") | ||
slashWindow := sdk.NewInt(2) | ||
minValidPerWindow := sdk.MustNewDecFromStr("0.5") | ||
twapLookbackWindow := sdk.NewInt(int64(time.Second * 30)) | ||
minVoters := sdk.NewInt(2) | ||
validatorFeeRatio := sdk.MustNewDecFromStr("0.7") | ||
partialParams := oraclekeeper.PartialOracleParams{ | ||
VotePeriod: &votePeriod, | ||
VoteThreshold: &voteThreshold, | ||
RewardBand: &rewardBand, | ||
Whitelist: whitelist, | ||
SlashFraction: &slashFraction, | ||
SlashWindow: &slashWindow, | ||
MinValidPerWindow: &minValidPerWindow, | ||
TwapLookbackWindow: &twapLookbackWindow, | ||
MinVoters: &minVoters, | ||
ValidatorFeeRatio: &validatorFeeRatio, | ||
} | ||
|
||
// TODO: Verify that params before were not equal | ||
|
||
invalidSender := testutil.AccAddress() | ||
err := nibiru.OracleKeeper.Admin.EditOracleParams( | ||
ctx, partialParams, invalidSender, | ||
) | ||
s.Error(err) | ||
|
||
okSender := testapp.DefaultSudoRoot() | ||
err = nibiru.OracleKeeper.Admin.EditOracleParams( | ||
ctx, partialParams, okSender, | ||
) | ||
s.NoError(err) | ||
|
||
// call admin method without err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters