Skip to content

Commit

Permalink
Merge branch 'main' into realu/sg-perp2
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Jan 2, 2024
2 parents a9408cb + c2a96d5 commit 5d4a0f6
Show file tree
Hide file tree
Showing 32 changed files with 851 additions and 344 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1573](https://github.com/NibiruChain/nibiru/pull/1573) - feat(perp): Close markets and compute settlement price
* [#1705](https://github.com/NibiruChain/nibiru/pull/1705) - feat(perp): Add oracle pair to market object
* [#1718](https://github.com/NibiruChain/nibiru/pull/1718) - fix: fees does not require additional funds
* [#1734](https://github.com/NibiruChain/nibiru/pull/1734) - feat(perp): MsgDonateToPerpFund
* [#1734](https://github.com/NibiruChain/nibiru/pull/1734) - feat(perp): MsgDonateToPerpFund sudo call as part of #1642
* [#1752](https://github.com/NibiruChain/nibiru/pull/1752) - feat(oracle): MsgEditOracleParams sudo tx msg as part of #1642
* [#1755](https://github.com/NibiruChain/nibiru/pull/1755) - feat(oracle): Add more events on validator's performance

### Non-breaking/Compatible Improvements

Expand All @@ -81,10 +82,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1723](https://github.com/NibiruChain/nibiru/pull/1723) - ci(e2e-wasm.yml): rm unused workflow
* [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs
* [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs
* [#1736](https://github.com/NibiruChain/nibiru/pull/1736) - test(sim): add sim genesis state for all cusom modules

### Dependencies
- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
- Bump `github.com/cometbft/cometbft-db` from 0.9.0 to 0.9.1 ([#1760](https://github.com/NibiruChain/nibiru/pull/1760))

* Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))
* Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
* Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))
* Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724))
* Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730))
Expand Down
84 changes: 65 additions & 19 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,39 @@ import (
wasmvm "github.com/CosmWasm/wasmvm/types"
)

// YieldStargateMsgs parses the JSON and sends wasmvm.StargateMsg objects to a channel
func YieldStargateMsgs(jsonBz []byte) ([]wasmvm.StargateMsg, error) {
// YieldStargateMsgs takes a byte slice of JSON data and converts it into a slice
// of wasmvm.StargateMsg objects. This function is essential for processing
// JSON-formatted messages that contain base64-encoded protobuf messages.
//
// Args:
// - jsonBz []byte: A byte slice containing the JSON data to be parsed.
//
// Returns:
// - sgMsgs []wasmvm.StargateMsg: A slice of wasmvm.StargateMsg objects parsed
// from the provided JSON data.
// - err error: An error object, which is nil if the operation is successful.
func YieldStargateMsgs(jsonBz []byte) (sgMsgs []wasmvm.StargateMsg, err error) {
var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return nil, err
return sgMsgs, err
}

Check warning on line 33 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L32-L33

Added lines #L32 - L33 were not covered by tests

var msgs []wasmvm.StargateMsg
parseStargateMsgs(data, &msgs)
return msgs, nil
parseStargateMsgs(data, &sgMsgs)
return sgMsgs, nil
}

// parseStargateMsgs is a recursive function used by YieldStargateMsgs to
// traverse the JSON data, filter for any protobuf.Any messages in the
// "WasmVM.StargateMsg" format and decode them from base64 back to human-readable
// form as JSON objects.
//
// Args:
// - jsonData any: JSON data to parse. According to the JSON specification,
// possible value types are:
// Null, Bool, Number(f64), String, Array, or Object(Map<String, Value>)
// - msgs *[]wasmvm.StargateMsg: Mutable reference to a slice of protobuf
// messages. These are potentially altered in place if the value is an
// encoded base 64 string.
func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) {
switch v := jsonData.(type) {
case map[string]interface{}:
Expand All @@ -48,33 +69,51 @@ func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) {
}
}

// StargateMsgDecoded is a struct designed to hold a decoded version of a
// "wasmvm.StargateMsg".
type StargateMsgDecoded struct {
TypeURL string `json:"type_url"`
Value string `json:"value"`
}

// DecodeBase64StargateMsgs decodes a series of base64-encoded
// wasmvm.StargateMsg objects from the provided JSON byte slice (jsonBz).
// This function is vital for extracting and interpreting the contents of these
// protobuf messages.
//
// Args:
// - jsonBz []byte: JSON data containing potential base64-encoded messages.
// - clientCtx client.Context: Context for the `nibid` CLI.
//
// Returns:
// - newSgMsgs []StargateMsgDecoded: The decoded stargate messages.
// - err error: An error object, which is nil if the operation is successful.
func DecodeBase64StargateMsgs(
jsonBz []byte, context client.Context,
jsonBz []byte, clientCtx client.Context,
) (newSgMsgs []StargateMsgDecoded, err error) {
codec := context.Codec
codec := clientCtx.Codec

var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return []StargateMsgDecoded{}, err
return newSgMsgs, fmt.Errorf(
"failed to decode stargate msgs due to invalid JSON: %w", err)
}

sgMsgs, err := YieldStargateMsgs(jsonBz)
if err != nil {
return
return newSgMsgs, err
}

Check warning on line 105 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L104-L105

Added lines #L104 - L105 were not covered by tests
for _, sgMsg := range sgMsgs {
valueStr := string(sgMsg.Value)
value := strings.Replace(string(sgMsg.Value), `\"`, `"`, -1)
value = strings.Replace(value, `"{`, `{`, -1)
value = strings.Replace(value, `}"`, `}`, -1)
replacer := strings.NewReplacer(
`\"`, `"`, // old, new
`"{`, `{`,
`}"`, `}`,
)
value := replacer.Replace(string(sgMsg.Value))

if _, err := base64.StdEncoding.DecodeString(valueStr); err == nil {
protoMsg, err := context.InterfaceRegistry.Resolve(sgMsg.TypeURL)
protoMsg, err := clientCtx.InterfaceRegistry.Resolve(sgMsg.TypeURL)
if err != nil {
return newSgMsgs, err
}

Check warning on line 119 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L118-L119

Added lines #L118 - L119 were not covered by tests
Expand All @@ -92,9 +131,13 @@ func DecodeBase64StargateMsgs(
return newSgMsgs, err
}

Check warning on line 132 in cmd/nibid/cmd/decode_base64.go

View check run for this annotation

Codecov / codecov/patch

cmd/nibid/cmd/decode_base64.go#L131-L132

Added lines #L131 - L132 were not covered by tests

newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)})
newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)},
)
} else if _, err := json.Marshal(value); err == nil {
newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)})
newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)},
)
} else {
return newSgMsgs, fmt.Errorf(
"parse error: encountered wasmvm.StargateMsg with unexpected format: %s", sgMsg)
Expand All @@ -103,7 +146,9 @@ func DecodeBase64StargateMsgs(
return newSgMsgs, nil
}

// DecodeBase64Cmd creates a cobra command for base64 decoding.
// DecodeBase64Cmd creates a Cobra command used to decode base64-encoded protobuf
// messages from a JSON input. This function enables users to input arbitrary
// JSON strings and parse the contents of base-64 encoded protobuf.Any messages.
func DecodeBase64Cmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "base64-decode",
Expand All @@ -114,8 +159,9 @@ The input should be a JSON object with 'type_url' and 'value' fields.`,
clientCtx := client.GetClientContextFromCmd(cmd)

outMessage, err := DecodeBase64StargateMsgs([]byte(args[0]), clientCtx)
fmt.Println(outMessage)

if err == nil {
fmt.Println(outMessage)
}
return err
},
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/NibiruChain/collections v0.4.0
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.9.0
github.com/cometbft/cometbft-db v0.9.1
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/go-bip39 v1.0.0
Expand All @@ -35,7 +35,7 @@ require (
github.com/stretchr/testify v1.8.4
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17
google.golang.org/grpc v1.60.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ github.com/coinbase/rosetta-sdk-go v0.7.9 h1:lqllBjMnazTjIqYrOGv8h8jxjg9+hJazIGZ
github.com/coinbase/rosetta-sdk-go v0.7.9/go.mod h1:0/knutI7XGVqXmmH4OQD8OckFrbQ8yMsUZTG7FXCR2M=
github.com/cometbft/cometbft v0.37.2 h1:XB0yyHGT0lwmJlFmM4+rsRnczPlHoAKFX6K8Zgc2/Jc=
github.com/cometbft/cometbft v0.37.2/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/cometbft/cometbft-db v0.9.0 h1:J+7haCZCddpM8DBESe6R7/ScnqigdQKph8V2yucApx8=
github.com/cometbft/cometbft-db v0.9.0/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/cometbft/cometbft-db v0.9.1 h1:MIhVX5ja5bXNHF8EYrThkG9F7r9kSfv8BX4LWaxWJ4M=
github.com/cometbft/cometbft-db v0.9.1/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4=
github.com/confio/ics23/go v0.9.0/go.mod h1:4LPZ2NYqnYIVRklaozjNR1FScgDJ2s5Xrp+e/mYVRak=
github.com/consensys/bavard v0.1.8-0.20210406032232-f3452dc9b572/go.mod h1:Bpd0/3mZuaj6Sj+PqrmIquiOKy397AKGThQPaGzNXAQ=
Expand Down Expand Up @@ -1878,8 +1878,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
22 changes: 22 additions & 0 deletions proto/nibiru/oracle/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@ message EventAggregatePrevote {
// transaction messages on behalf of the voting validator.
string feeder = 2;
}


message EventValidatorPerformance{
// Validator is the Bech32 address to which the vote will be credited.
string validator = 1;

// Tendermint consensus voting power
int64 voting_power = 2;

// RewardWeight: Weight of rewards the validator should receive in units of
// consensus power.
int64 reward_weight = 3;

// Number of valid votes for which the validator will be rewarded
int64 win_count = 4;

// Number of abstained votes for which there will be no reward or punishment
int64 abstain_count = 5;

// Number of invalid/punishable votes
int64 miss_count = 6;
}
2 changes: 1 addition & 1 deletion proto/nibiru/oracle/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ message MsgDelegateFeedConsent {
message MsgDelegateFeedConsentResponse {}

// MsgEditOracleParams: gRPC tx message for updating the x/oracle module params
// [[SUDO][SUDO][SUDO][SUDO][SUDO]] O[SUDO]ly callable by su[SUDO]oers.
// [SUDO] Only callable by sudoers.
message MsgEditOracleParams {
string sender = 1;

Expand Down
13 changes: 7 additions & 6 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ message MsgWithdrawEpochRebatesResponse {

// -------------------------- ShiftPegMultiplier --------------------------

// ShiftPegMultiplier: gRPC tx msg for changing the peg multiplier.
// [SUDO] Only callable by sudoers.
// MsgShiftPegMultiplier: gRPC tx msg for changing the peg multiplier.
// [SUDO] Only callable sudoers.
message MsgShiftPegMultiplier {
string sender = 1;
string pair = 2 [
Expand All @@ -418,8 +418,8 @@ message MsgShiftPegMultiplierResponse {}

// -------------------------- ShiftSwapInvariant --------------------------

// ShiftSwapInvariant: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable by sudoers.
// MsgShiftSwapInvariant: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable sudoers.
message MsgShiftSwapInvariant {
string sender = 1;
string pair = 2 [
Expand All @@ -437,14 +437,15 @@ message MsgShiftSwapInvariantResponse {}

// -------------------------- WithdrawFromPerpFund --------------------------

// WithdrawFromPerpFund: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable by sudoers.
// MsgWithdrawFromPerpFund: gRPC tx msg for changing the swap invariant.
// [SUDO] Only callable sudoers.
message MsgWithdrawFromPerpFund {
string sender = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
// Optional denom in case withdrawing assets aside from NUSD.
string denom = 3;
string to_addr = 4;
}
Expand Down
8 changes: 0 additions & 8 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type NibiruMsg struct {
// bindings-perp ExecuteMsg enum types
// MultiLiquidate *MultiLiquidate `json:"multi_liquidate,omitempty"` // TODO
SetMarketEnabled *SetMarketEnabled `json:"set_market_enabled,omitempty"`
CreateMarket *CreateMarket `json:"create_market,omitempty"`

EditOracleParams *EditOracleParams `json:"edit_oracle_params,omitempty"`

Expand All @@ -41,13 +40,6 @@ type SetMarketEnabled struct {
Enabled bool `json:"enabled"`
}

type CreateMarket struct {
Pair string `json:"pair"`
PegMult sdk.Dec `json:"peg_mult,omitempty"`
SqrtDepth sdk.Dec `json:"sqrt_depth,omitempty"`
MarketParams *MarketParams `json:"market_params,omitempty"`
}

type MarketParams struct {
Pair string
Enabled bool `json:"enabled,omitempty"`
Expand Down
45 changes: 0 additions & 45 deletions wasmbinding/exec_perp.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package wasmbinding

import (
"time"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -35,46 +33,3 @@ func (exec *ExecutorPerp) SetMarketEnabled(

return exec.PerpV2.Sudo().CloseMarket(ctx, pair)
}

func (exec *ExecutorPerp) CreateMarket(
cwMsg *bindings.CreateMarket, ctx sdk.Context,
) (err error) {
if cwMsg == nil {
return wasmvmtypes.InvalidRequest{Err: "null msg"}
}

pair, err := asset.TryNewPair(cwMsg.Pair)
if err != nil {
return err
}

var market perpv2types.Market
if cwMsg.MarketParams == nil {
market = perpv2types.DefaultMarket(pair)
} else {
mp := cwMsg.MarketParams
market = perpv2types.Market{
Pair: pair,
Enabled: true,
MaintenanceMarginRatio: mp.MaintenanceMarginRatio,
MaxLeverage: mp.MaxLeverage,
LatestCumulativePremiumFraction: mp.LatestCumulativePremiumFraction,
ExchangeFeeRatio: mp.ExchangeFeeRatio,
EcosystemFundFeeRatio: mp.EcosystemFundFeeRatio,
LiquidationFeeRatio: mp.LiquidationFeeRatio,
PartialLiquidationRatio: mp.PartialLiquidationRatio,
FundingRateEpochId: mp.FundingRateEpochId,
MaxFundingRate: mp.MaxFundingRate,
TwapLookbackWindow: time.Duration(mp.TwapLookbackWindow.Int64()),
PrepaidBadDebt: sdk.NewCoin(pair.QuoteDenom(), sdk.ZeroInt()),
OraclePair: asset.MustNewPair(mp.OraclePair),
}
}

return exec.PerpV2.Sudo().CreateMarket(ctx, perpv2keeper.ArgsCreateMarket{
Pair: pair,
PriceMultiplier: cwMsg.PegMult,
SqrtDepth: cwMsg.SqrtDepth,
Market: &market,
})
}
Loading

0 comments on commit 5d4a0f6

Please sign in to comment.