Skip to content

Commit

Permalink
re-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchon committed Oct 28, 2021
1 parent ad574c8 commit aafc8fe
Show file tree
Hide file tree
Showing 16 changed files with 1,632 additions and 794 deletions.
2 changes: 2 additions & 0 deletions chain/exchange/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgLiquidatePosition{}, "exchange/MsgLiquidatePosition", nil)

cdc.RegisterConcrete(&ExchangeEnableProposal{}, "exchange/ExchangeEnableProposal", nil)
cdc.RegisterConcrete(&BatchExchangeModificationProposal{}, "exchange/BatchExchangeModificationProposal", nil)
cdc.RegisterConcrete(&SpotMarketParamUpdateProposal{}, "exchange/SpotMarketParamUpdateProposal", nil)
cdc.RegisterConcrete(&SpotMarketLaunchProposal{}, "exchange/SpotMarketLaunchProposal", nil)
cdc.RegisterConcrete(&PerpetualMarketLaunchProposal{}, "exchange/PerpetualMarketLaunchProposal", nil)
Expand Down Expand Up @@ -69,6 +70,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations(
(*govtypes.Content)(nil),
&ExchangeEnableProposal{},
&BatchExchangeModificationProposal{},
&SpotMarketParamUpdateProposal{},
&SpotMarketLaunchProposal{},
&PerpetualMarketLaunchProposal{},
Expand Down
7 changes: 2 additions & 5 deletions chain/exchange/types/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,9 @@ func IsValidOrderHash(orderHash string) bool {
if len(orderHash) != 66 {
return false
}
orderHashBytes := common.FromHex(orderHash)

if len(orderHashBytes) != common.HashLength {
return false
}
return true
orderHashBytes := common.FromHex(orderHash)
return len(orderHashBytes) == common.HashLength
}

func BreachesMinimumTickSize(value sdk.Dec, minTickSize sdk.Dec) bool {
Expand Down
111 changes: 54 additions & 57 deletions chain/exchange/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,58 @@ import (
)

var (
ErrOrderInvalid = sdkerrors.Register(ModuleName, 1, "failed to validate order")
ErrSpotMarketNotFound = sdkerrors.Register(ModuleName, 2, "spot market not found")
ErrSpotMarketExists = sdkerrors.Register(ModuleName, 3, "spot market exists")
ErrBadField = sdkerrors.Register(ModuleName, 4, "struct field error")
ErrMarketInvalid = sdkerrors.Register(ModuleName, 5, "failed to validate market")
ErrInsufficientDeposit = sdkerrors.Register(ModuleName, 6, "subaccount has insufficient deposits")
ErrUnrecognizedOrderType = sdkerrors.Register(ModuleName, 7, "unrecognized order type")
ErrInsufficientPositionQuantity = sdkerrors.Register(ModuleName, 8, "position quantity insufficient for order")
ErrOrderHashInvalid = sdkerrors.Register(ModuleName, 9, "order hash is not valid")
ErrBadSubaccountID = sdkerrors.Register(ModuleName, 10, "subaccount id is not valid")
ErrInvalidTicker = sdkerrors.Register(ModuleName, 11, "invalid ticker")
ErrInvalidBaseDenom = sdkerrors.Register(ModuleName, 12, "invalid base denom")
ErrInvalidQuoteDenom = sdkerrors.Register(ModuleName, 13, "invalid quote denom")
ErrInvalidOracle = sdkerrors.Register(ModuleName, 14, "invalid oracle")
ErrInvalidExpiry = sdkerrors.Register(ModuleName, 15, "invalid expiry")
ErrInvalidPrice = sdkerrors.Register(ModuleName, 16, "invalid price")
ErrInvalidQuantity = sdkerrors.Register(ModuleName, 17, "invalid quantity")
ErrUnsupportedOracleType = sdkerrors.Register(ModuleName, 18, "unsupported oracle type")
ErrOrderDoesntExist = sdkerrors.Register(ModuleName, 19, "order doesnt exist")
ErrOrderbookFillInvalid = sdkerrors.Register(ModuleName, 20, "spot limit orderbook fill invalid")
ErrPerpetualMarketExists = sdkerrors.Register(ModuleName, 21, "perpetual market exists")
ErrExpiryFuturesMarketExists = sdkerrors.Register(ModuleName, 22, "expiry futures market exists")
ErrExpiryFuturesMarketExpired = sdkerrors.Register(ModuleName, 23, "expiry futures market expired")
ErrNoLiquidity = sdkerrors.Register(ModuleName, 24, "no liquidity on the orderbook!")
ErrSlippageExceedsWorstPrice = sdkerrors.Register(ModuleName, 25, "Orderbook liquidity cannot satisfy current worst price")
ErrInsufficientOrderMargin = sdkerrors.Register(ModuleName, 26, "Order has insufficient margin")
ErrDerivativeMarketNotFound = sdkerrors.Register(ModuleName, 27, "Derivative market not found")
ErrPositionNotFound = sdkerrors.Register(ModuleName, 28, "Position not found")
ErrInvalidReduceOnlyPositionDirection = sdkerrors.Register(ModuleName, 29, "Position direction does not oppose the reduce-only order")
ErrPriceSurpassesBankruptcyPrice = sdkerrors.Register(ModuleName, 30, "Price Surpasses Bankruptcy Price")
ErrPositionNotLiquidable = sdkerrors.Register(ModuleName, 31, "Position not liquidable")
ErrInvalidTriggerPrice = sdkerrors.Register(ModuleName, 32, "invalid trigger price")
ErrInvalidOracleType = sdkerrors.Register(ModuleName, 33, "invalid oracle type")
ErrInvalidPriceTickSize = sdkerrors.Register(ModuleName, 34, "invalid minimum price tick size")
ErrInvalidQuantityTickSize = sdkerrors.Register(ModuleName, 35, "invalid minimum quantity tick size")
ErrInvalidMargin = sdkerrors.Register(ModuleName, 36, "invalid minimum order margin")
ErrExceedsOrderSideCount = sdkerrors.Register(ModuleName, 37, "Exceeds order side count")
ErrDerivativeMarketOrderAlreadyExists = sdkerrors.Register(ModuleName, 38, "Subaccount cannot place a market order when a market order or limit order in the same market was already placed in same block")
ErrDerivativeLimitOrderAlreadyExists = sdkerrors.Register(ModuleName, 39, "Subaccount cannot place a limit order when a market order in the same market was already placed in the same block")
ErrMarketLaunchProposalAlreadyExists = sdkerrors.Register(ModuleName, 40, "An equivalent market launch proposal already exists.")
ErrInvalidMarketStatus = sdkerrors.Register(ModuleName, 41, "Invalid Market Status")
ErrSameDenoms = sdkerrors.Register(ModuleName, 42, "base denom cannot be same with quote denom")
ErrSameOracles = sdkerrors.Register(ModuleName, 43, "oracle base cannot be same with oracle quote")
ErrFeeRatesRelation = sdkerrors.Register(ModuleName, 44, "MakerFeeRate does not match TakerFeeRate requirements")
ErrMarginsRelation = sdkerrors.Register(ModuleName, 45, "MaintenanceMarginRatio cannot be greater than InitialMarginRatio")
ErrExceedsMaxOracleScaleFactor = sdkerrors.Register(ModuleName, 46, "OracleScaleFactor cannot be greater than MaxOracleScaleFactor")
ErrSpotExchangeNotEnabled = sdkerrors.Register(ModuleName, 47, "Spot exchange is not enabled yet")
ErrDerivativesExchangeNotEnabled = sdkerrors.Register(ModuleName, 48, "Derivatives exchange is not enabled yet")
ErrBandIBCOraclePriceDeltaExceedsThreshold = sdkerrors.Register(ModuleName, 49, "Band IBC oracle price delta exceeds threshold")
ErrInvalidHourlyInterestRate = sdkerrors.Register(ModuleName, 50, "Invalid hourly interest rate")
ErrInvalidHourlyFundingRateCap = sdkerrors.Register(ModuleName, 51, "Invalid hourly funding rate cap")
ErrInvalidMarketFundingParamUpdate = sdkerrors.Register(ModuleName, 52, "Only perpetual markets can update funding parameters")
ErrInvalidLiquidityMiningMarket = sdkerrors.Register(ModuleName, 53, "Invalid liquidity mining market")
ErrInvalidLiquidityMiningMarketWeights = sdkerrors.Register(ModuleName, 54, "Invalid liquidity mining market weights")
ErrInvalidTradingRewardCampaign = sdkerrors.Register(ModuleName, 55, "Invalid trading reward campaign")
ErrInvalidLiquidityMiningReward = sdkerrors.Register(ModuleName, 56, "Invalid liquidity mining reward")
ErrInvalidFeeDiscountSchedule = sdkerrors.Register(ModuleName, 57, "Invalid fee discount schedule")
ErrOrderInvalid = sdkerrors.Register(ModuleName, 1, "failed to validate order")
ErrSpotMarketNotFound = sdkerrors.Register(ModuleName, 2, "spot market not found")
ErrSpotMarketExists = sdkerrors.Register(ModuleName, 3, "spot market exists")
ErrBadField = sdkerrors.Register(ModuleName, 4, "struct field error")
ErrMarketInvalid = sdkerrors.Register(ModuleName, 5, "failed to validate market")
ErrInsufficientDeposit = sdkerrors.Register(ModuleName, 6, "subaccount has insufficient deposits")
ErrUnrecognizedOrderType = sdkerrors.Register(ModuleName, 7, "unrecognized order type")
ErrInsufficientPositionQuantity = sdkerrors.Register(ModuleName, 8, "position quantity insufficient for order")
ErrOrderHashInvalid = sdkerrors.Register(ModuleName, 9, "order hash is not valid")
ErrBadSubaccountID = sdkerrors.Register(ModuleName, 10, "subaccount id is not valid")
ErrInvalidTicker = sdkerrors.Register(ModuleName, 11, "invalid ticker")
ErrInvalidBaseDenom = sdkerrors.Register(ModuleName, 12, "invalid base denom")
ErrInvalidQuoteDenom = sdkerrors.Register(ModuleName, 13, "invalid quote denom")
ErrInvalidOracle = sdkerrors.Register(ModuleName, 14, "invalid oracle")
ErrInvalidExpiry = sdkerrors.Register(ModuleName, 15, "invalid expiry")
ErrInvalidPrice = sdkerrors.Register(ModuleName, 16, "invalid price")
ErrInvalidQuantity = sdkerrors.Register(ModuleName, 17, "invalid quantity")
ErrUnsupportedOracleType = sdkerrors.Register(ModuleName, 18, "unsupported oracle type")
ErrOrderDoesntExist = sdkerrors.Register(ModuleName, 19, "order doesnt exist")
ErrOrderbookFillInvalid = sdkerrors.Register(ModuleName, 20, "spot limit orderbook fill invalid")
ErrPerpetualMarketExists = sdkerrors.Register(ModuleName, 21, "perpetual market exists")
ErrExpiryFuturesMarketExists = sdkerrors.Register(ModuleName, 22, "expiry futures market exists")
ErrExpiryFuturesMarketExpired = sdkerrors.Register(ModuleName, 23, "expiry futures market expired")
ErrNoLiquidity = sdkerrors.Register(ModuleName, 24, "no liquidity on the orderbook!")
ErrSlippageExceedsWorstPrice = sdkerrors.Register(ModuleName, 25, "Orderbook liquidity cannot satisfy current worst price")
ErrInsufficientOrderMargin = sdkerrors.Register(ModuleName, 26, "Order has insufficient margin")
ErrDerivativeMarketNotFound = sdkerrors.Register(ModuleName, 27, "Derivative market not found")
ErrPositionNotFound = sdkerrors.Register(ModuleName, 28, "Position not found")
ErrInvalidReduceOnlyPositionDirection = sdkerrors.Register(ModuleName, 29, "Position direction does not oppose the reduce-only order")
ErrPriceSurpassesBankruptcyPrice = sdkerrors.Register(ModuleName, 30, "Price Surpasses Bankruptcy Price")
ErrPositionNotLiquidable = sdkerrors.Register(ModuleName, 31, "Position not liquidable")
ErrInvalidTriggerPrice = sdkerrors.Register(ModuleName, 32, "invalid trigger price")
ErrInvalidOracleType = sdkerrors.Register(ModuleName, 33, "invalid oracle type")
ErrInvalidPriceTickSize = sdkerrors.Register(ModuleName, 34, "invalid minimum price tick size")
ErrInvalidQuantityTickSize = sdkerrors.Register(ModuleName, 35, "invalid minimum quantity tick size")
ErrInvalidMargin = sdkerrors.Register(ModuleName, 36, "invalid minimum order margin")
ErrExceedsOrderSideCount = sdkerrors.Register(ModuleName, 37, "Exceeds order side count")
ErrDerivativeMarketOrderAlreadyExists = sdkerrors.Register(ModuleName, 38, "Subaccount cannot place a market order when a market order or limit order in the same market was already placed in same block")
ErrDerivativeLimitOrderAlreadyExists = sdkerrors.Register(ModuleName, 39, "Subaccount cannot place a limit order when a market order in the same market was already placed in the same block")
ErrMarketLaunchProposalAlreadyExists = sdkerrors.Register(ModuleName, 40, "An equivalent market launch proposal already exists.")
ErrInvalidMarketStatus = sdkerrors.Register(ModuleName, 41, "Invalid Market Status")
ErrSameDenoms = sdkerrors.Register(ModuleName, 42, "base denom cannot be same with quote denom")
ErrSameOracles = sdkerrors.Register(ModuleName, 43, "oracle base cannot be same with oracle quote")
ErrFeeRatesRelation = sdkerrors.Register(ModuleName, 44, "MakerFeeRate does not match TakerFeeRate requirements")
ErrMarginsRelation = sdkerrors.Register(ModuleName, 45, "MaintenanceMarginRatio cannot be greater than InitialMarginRatio")
ErrExceedsMaxOracleScaleFactor = sdkerrors.Register(ModuleName, 46, "OracleScaleFactor cannot be greater than MaxOracleScaleFactor")
ErrSpotExchangeNotEnabled = sdkerrors.Register(ModuleName, 47, "Spot exchange is not enabled yet")
ErrDerivativesExchangeNotEnabled = sdkerrors.Register(ModuleName, 48, "Derivatives exchange is not enabled yet")
ErrOraclePriceDeltaExceedsThreshold = sdkerrors.Register(ModuleName, 49, "Oracle price delta exceeds threshold")
ErrInvalidHourlyInterestRate = sdkerrors.Register(ModuleName, 50, "Invalid hourly interest rate")
ErrInvalidHourlyFundingRateCap = sdkerrors.Register(ModuleName, 51, "Invalid hourly funding rate cap")
ErrInvalidMarketFundingParamUpdate = sdkerrors.Register(ModuleName, 52, "Only perpetual markets can update funding parameters")
ErrInvalidTradingRewardCampaign = sdkerrors.Register(ModuleName, 53, "Invalid trading reward campaign")
ErrInvalidFeeDiscountSchedule = sdkerrors.Register(ModuleName, 54, "Invalid fee discount schedule")
)
4 changes: 2 additions & 2 deletions chain/exchange/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type InsuranceKeeper interface {
DepositIntoInsuranceFund(ctx sdk.Context, marketID common.Hash, amount sdk.Int) error
// WithdrawFromInsuranceFund decrements the insurance fund balance by amount and sends
WithdrawFromInsuranceFund(ctx sdk.Context, marketID common.Hash, amount sdk.Int) error
// UpdateInsuranceFundBandOracle updates the insurance fund's oracle from Band to Band IBC
UpdateInsuranceFundBandOracle(ctx sdk.Context, marketID common.Hash) error
// UpdateInsuranceFundOracleParams updates the insurance fund's oracle parameters
UpdateInsuranceFundOracleParams(ctx sdk.Context, marketID common.Hash, oracleParams *OracleParams) error
}

type GovKeeper interface {
Expand Down
8 changes: 4 additions & 4 deletions chain/exchange/types/fee_discounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (s *FeeDiscountSchedule) CalculateFeeDiscountTier(
stakedAmount sdk.Int,
tradingFeePaid sdk.Dec,
) (
discountRates *FeeDiscountRates,
feeDiscountRates *FeeDiscountRates,
tierLevel uint64,
) {
highestTierLevel := 0
Expand All @@ -29,18 +29,18 @@ func (s *FeeDiscountSchedule) CalculateFeeDiscountTier(
tierLevel = uint64(highestTierLevel)

if tierLevel == 0 {
discountRates = &FeeDiscountRates{
feeDiscountRates = &FeeDiscountRates{
MakerDiscountRate: sdk.ZeroDec(),
TakerDiscountRate: sdk.ZeroDec(),
}
} else {
discountRates = &FeeDiscountRates{
feeDiscountRates = &FeeDiscountRates{
MakerDiscountRate: s.TierInfos[highestTierLevel-1].MakerDiscountRate,
TakerDiscountRate: s.TierInfos[highestTierLevel-1].TakerDiscountRate,
}
}

return discountRates, tierLevel
return feeDiscountRates, tierLevel
}

func (s *FeeDiscountSchedule) TierOneRequirements() (
Expand Down
5 changes: 1 addition & 4 deletions chain/exchange/types/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,7 @@ const TrueByte byte = byte(1)
const FalseByte byte = byte(0)

func IsTrueByte(bz []byte) bool {
if bytes.Equal(bz, []byte{TrueByte}) {
return true
}
return false
return bytes.Equal(bz, []byte{TrueByte})
}

// MarketDirectionPrefix allows to obtain prefix against a particular marketID, direction
Expand Down
43 changes: 6 additions & 37 deletions chain/exchange/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/InjectiveLabs/sdk-go/chain/oracle/types"
)

const RouterKey = ModuleName
Expand Down Expand Up @@ -279,24 +277,9 @@ func (msg MsgInstantPerpetualMarketLaunch) ValidateBasic() error {
if msg.QuoteDenom == "" {
return sdkerrors.Wrap(ErrInvalidQuoteDenom, "quote denom should not be empty")
}
if msg.OracleBase == "" {
return sdkerrors.Wrap(ErrInvalidOracle, "oracle base should not be empty")
}
if msg.OracleQuote == "" {
return sdkerrors.Wrap(ErrInvalidOracle, "oracle quote should not be empty")
}
if msg.OracleBase == msg.OracleQuote {
return ErrSameOracles
}
switch msg.OracleType {
case types.OracleType_Band, types.OracleType_PriceFeed, types.OracleType_Coinbase, types.OracleType_Chainlink, types.OracleType_Razor,
types.OracleType_Dia, types.OracleType_API3, types.OracleType_Uma, types.OracleType_Pyth, types.OracleType_BandIBC:

default:
return sdkerrors.Wrap(ErrInvalidOracleType, msg.OracleType.String())
}
if msg.OracleScaleFactor > MaxOracleScaleFactor {
return ErrExceedsMaxOracleScaleFactor
oracleParams := NewOracleParams(msg.OracleBase, msg.OracleQuote, msg.OracleScaleFactor, msg.OracleType)
if err := oracleParams.ValidateBasic(); err != nil {
return err
}
if err := ValidateFee(msg.MakerFeeRate); err != nil {
return err
Expand Down Expand Up @@ -360,24 +343,10 @@ func (msg MsgInstantExpiryFuturesMarketLaunch) ValidateBasic() error {
if msg.QuoteDenom == "" {
return sdkerrors.Wrap(ErrInvalidQuoteDenom, "quote denom should not be empty")
}
if msg.OracleBase == "" {
return sdkerrors.Wrap(ErrInvalidOracle, "oracle base should not be empty")
}
if msg.OracleQuote == "" {
return sdkerrors.Wrap(ErrInvalidOracle, "oracle quote should not be empty")
}
if msg.OracleBase == msg.OracleQuote {
return ErrSameOracles
}
switch msg.OracleType {
case types.OracleType_Band, types.OracleType_PriceFeed, types.OracleType_Coinbase, types.OracleType_Chainlink, types.OracleType_Razor,
types.OracleType_Dia, types.OracleType_API3, types.OracleType_Uma, types.OracleType_Pyth, types.OracleType_BandIBC:

default:
return sdkerrors.Wrap(ErrInvalidOracleType, msg.OracleType.String())
}
if msg.OracleScaleFactor > MaxOracleScaleFactor {
return ErrExceedsMaxOracleScaleFactor
oracleParams := NewOracleParams(msg.OracleBase, msg.OracleQuote, msg.OracleScaleFactor, msg.OracleType)
if err := oracleParams.ValidateBasic(); err != nil {
return err
}
if msg.Expiry <= 0 {
return sdkerrors.Wrap(ErrInvalidExpiry, "expiry should not be empty")
Expand Down
2 changes: 1 addition & 1 deletion chain/exchange/types/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewPosition(isLong bool, cumulativeFundingEntry sdk.Dec) *Position {
// Formula for adjustment:
// newPayoutFromPnl = oldPayoutFromPnl * (1 - missingFundsRate)
// => Entry price adjustment for buys
// (newEntryPrice - settlementPrice) * quantity = (entryPrice - settlementPrice) * quantity * (1- missingFundsRate)
// (newEntryPrice - settlementPrice) * quantity = (entryPrice - settlementPrice) * quantity * (1 - missingFundsRate)
// newEntryPrice = entryPrice - entryPrice * haircutPercentage + settlementPrice * haircutPercentage
// => Entry price adjustment for sells
// (settlementPrice - newEntryPrice) * quantity = (settlementPrice - entryPrice) * quantity * (1 - missingFundsRate)
Expand Down
Loading

0 comments on commit aafc8fe

Please sign in to comment.