Skip to content

Commit

Permalink
re-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
gorgos committed Sep 9, 2021
1 parent 8d869ef commit bbf1a69
Show file tree
Hide file tree
Showing 18 changed files with 2,640 additions and 595 deletions.
2 changes: 2 additions & 0 deletions chain/exchange/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&PerpetualMarketLaunchProposal{}, "exchange/PerpetualMarketLaunchProposal", nil)
cdc.RegisterConcrete(&ExpiryFuturesMarketLaunchProposal{}, "exchange/ExpiryFuturesMarketLaunchProposal", nil)
cdc.RegisterConcrete(&DerivativeMarketParamUpdateProposal{}, "exchange/DerivativeMarketParamUpdateProposal", nil)
cdc.RegisterConcrete(&LiquidityMiningCampaignProposal{}, "exchange/LiquidityMiningCampaignProposal", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {
Expand Down Expand Up @@ -71,6 +72,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
&PerpetualMarketLaunchProposal{},
&ExpiryFuturesMarketLaunchProposal{},
&DerivativeMarketParamUpdateProposal{},
&LiquidityMiningCampaignProposal{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
41 changes: 41 additions & 0 deletions chain/exchange/types/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (d *DerivativeLimitOrderDelta) OrderHash() common.Hash {
var AuctionSubaccountID = common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111")
var ZeroSubaccountID = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")

// TempRewardsSenderAddress equals sdk.AccAddress(common.HexToAddress(AuctionSubaccountID.Hex()).Bytes())
var TempRewardsSenderAddress, _ = sdk.AccAddressFromBech32("inj1zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3t5qxqh")

func IsValidSubaccountID(subaccountID string) (*common.Address, bool) {
if len(subaccountID) != 66 {
return nil, false
Expand Down Expand Up @@ -124,10 +127,26 @@ func SdkAddressToEthAddress(addr sdk.Address) common.Address {
return common.BytesToAddress(addr.Bytes())
}

func SubaccountIDToSdkAddress(subaccountID common.Hash) sdk.Address {
return sdk.AccAddress(subaccountID[:common.AddressLength])
}

func EthAddressToSubaccountID(addr common.Address) common.Hash {
return common.BytesToHash(common.RightPadBytes(addr.Bytes(), 32))
}

func DecToDecBytes(dec sdk.Dec) []byte {
return dec.BigInt().Bytes()
}

func DecBytesToDec(bz []byte) sdk.Dec {
dec := sdk.NewDecFromBigIntWithPrec(new(big.Int).SetBytes(bz), sdk.Precision)
if dec.IsNil() {
return sdk.ZeroDec()
}
return dec
}

// PrintSpotLimitOrderbookState is a helper debugger function to print a tabular view of the spot limit orderbook fill state
func PrintSpotLimitOrderbookState(buyOrderbookState *OrderbookFills, sellOrderbookState *OrderbookFills) {
table := tablewriter.NewWriter(os.Stdout)
Expand Down Expand Up @@ -169,3 +188,25 @@ func PrintSpotLimitOrderbookState(buyOrderbookState *OrderbookFills, sellOrderbo
}
table.Render()
}

func HasDuplicates(slice []string) bool {
seen := make(map[string]bool)
for _, item := range slice {
if seen[item] {
return true
}
seen[item] = true
}
return false
}

func HasDuplicatesCoin(slice []sdk.Coin) bool {
seen := make(map[string]bool)
for _, item := range slice {
if seen[item.Denom] {
return true
}
seen[item.Denom] = true
}
return false
}
19 changes: 13 additions & 6 deletions chain/exchange/types/derivative_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type DerivativeOrderStateExpansion struct {
TotalBalanceDelta sdk.Dec
AvailableBalanceDelta sdk.Dec

AuctionFeeReward sdk.Dec
FeeRecipientReward sdk.Dec
FeeRecipient common.Address
LimitOrderFilledDelta *DerivativeLimitOrderDelta
MarketOrderFilledDelta *DerivativeMarketOrderDelta
OrderHash common.Hash
AuctionFeeReward sdk.Dec
LiquidityMiningRewardPoints sdk.Dec
FeeRecipientReward sdk.Dec
FeeRecipient common.Address
LimitOrderFilledDelta *DerivativeLimitOrderDelta
MarketOrderFilledDelta *DerivativeMarketOrderDelta
OrderHash common.Hash
}

func ApplyDeltasAndGetDerivativeOrderBatchEvent(
Expand All @@ -35,6 +36,7 @@ func ApplyDeltasAndGetDerivativeOrderBatchEvent(
funding *PerpetualMarketFunding,
stateExpansions []*DerivativeOrderStateExpansion,
depositDeltas DepositDeltas,
liquidityMiningRewards LiquidityMiningRewards,
) (batch *EventBatchDerivativeExecution, filledDeltas []*DerivativeLimitOrderDelta) {
if len(stateExpansions) == 0 {
return
Expand All @@ -61,6 +63,11 @@ func ApplyDeltasAndGetDerivativeOrderBatchEvent(
depositDeltas.ApplyUniformDelta(feeRecipientSubaccount, expansion.FeeRecipientReward)
depositDeltas.ApplyUniformDelta(AuctionSubaccountID, expansion.AuctionFeeReward)

sender := SubaccountIDToSdkAddress(expansion.SubaccountID)
if expansion.LiquidityMiningRewardPoints.IsPositive() {
liquidityMiningRewards.AddPointsForAddress(sender.String(), expansion.LiquidityMiningRewardPoints)
}

if !executionType.IsMarket() {
filledDeltas = append(filledDeltas, expansion.LimitOrderFilledDelta)
}
Expand Down
22 changes: 14 additions & 8 deletions chain/exchange/types/derivative_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type DerivativeBatchExecutionData struct {
DepositDeltas DepositDeltas
DepositSubaccountIDs []common.Hash

LiquidityMiningRewards LiquidityMiningRewards

// updated positions
Positions []*Position
PositionSubaccountIDs []common.Hash
Expand Down Expand Up @@ -106,16 +108,17 @@ func (e *DerivativeMatchingExpansionData) GetLimitMatchingDerivativeBatchExecuti
positionStates map[common.Hash]*PositionState,
) *DerivativeBatchExecutionData {
depositDeltas := NewDepositDeltas()
liquidityMiningRewards := NewLiquidityMiningRewards()

// process undermargined resting limit order forced cancellations
cancelLimitOrdersEvents, restingOrderCancelledDeltas, transientOrderCancelledDeltas := e.applyCancellationsAndGetDerivativeLimitCancelEvents(market.MarketID(), market.MakerFeeRate, market.TakerFeeRate, depositDeltas)

positions, positionSubaccountIDs := GetPositionSliceData(positionStates)

transientLimitBuyOrderBatchEvent, transientLimitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitMatchNewOrder, market, funding, e.TransientLimitBuyExpansions, depositDeltas)
restingLimitBuyOrderBatchEvent, restingLimitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitMatchRestingOrder, market, funding, e.RestingLimitBuyExpansions, depositDeltas)
transientLimitSellOrderBatchEvent, transientLimitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitMatchNewOrder, market, funding, e.TransientLimitSellExpansions, depositDeltas)
restingLimitSellOrderBatchEvent, restingLimitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitMatchRestingOrder, market, funding, e.RestingLimitSellExpansions, depositDeltas)
transientLimitBuyOrderBatchEvent, transientLimitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitMatchNewOrder, market, funding, e.TransientLimitBuyExpansions, depositDeltas, liquidityMiningRewards)
restingLimitBuyOrderBatchEvent, restingLimitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitMatchRestingOrder, market, funding, e.RestingLimitBuyExpansions, depositDeltas, liquidityMiningRewards)
transientLimitSellOrderBatchEvent, transientLimitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitMatchNewOrder, market, funding, e.TransientLimitSellExpansions, depositDeltas, liquidityMiningRewards)
restingLimitSellOrderBatchEvent, restingLimitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitMatchRestingOrder, market, funding, e.RestingLimitSellExpansions, depositDeltas, liquidityMiningRewards)

restingOrderFilledDeltas := mergeDerivativeLimitOrderFilledDeltas(restingLimitBuyFilledDeltas, restingLimitSellFilledDeltas)
transientOrderFilledDeltas := mergeDerivativeLimitOrderFilledDeltas(transientLimitBuyFilledDeltas, transientLimitSellFilledDeltas)
Expand Down Expand Up @@ -144,6 +147,7 @@ func (e *DerivativeMatchingExpansionData) GetLimitMatchingDerivativeBatchExecuti
Funding: funding,
DepositDeltas: depositDeltas,
DepositSubaccountIDs: depositDeltaKeys,
LiquidityMiningRewards: liquidityMiningRewards,
Positions: positions,
PositionSubaccountIDs: positionSubaccountIDs,
RestingLimitOrderFilledDeltas: restingOrderFilledDeltas,
Expand Down Expand Up @@ -339,6 +343,7 @@ func (e *DerivativeMarketOrderExpansionData) GetMarketDerivativeBatchExecutionDa
positionStates map[common.Hash]*PositionState,
) *DerivativeBatchExecutionData {
depositDeltas := NewDepositDeltas()
liquidityMiningRewards := NewLiquidityMiningRewards()

// process undermargined limit order forced cancellations
cancelLimitOrdersEvents, restingOrderCancelledDeltas := e.applyCancellationsAndGetDerivativeLimitCancelEvents(market.MarketID(), market.MakerFeeRate, depositDeltas)
Expand All @@ -348,10 +353,10 @@ func (e *DerivativeMarketOrderExpansionData) GetMarketDerivativeBatchExecutionDa

positions, positionSubaccountIDs := GetPositionSliceData(positionStates)

buyMarketOrderBatchEvent, _ := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_Market, market, funding, e.MarketBuyExpansions, depositDeltas)
sellMarketOrderBatchEvent, _ := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_Market, market, funding, e.MarketSellExpansions, depositDeltas)
restingLimitBuyOrderBatchEvent, limitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitFill, market, funding, e.LimitBuyExpansions, depositDeltas)
restingLimitSellOrderBatchEvent, limitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitFill, market, funding, e.LimitSellExpansions, depositDeltas)
buyMarketOrderBatchEvent, _ := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_Market, market, funding, e.MarketBuyExpansions, depositDeltas, liquidityMiningRewards)
sellMarketOrderBatchEvent, _ := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_Market, market, funding, e.MarketSellExpansions, depositDeltas, liquidityMiningRewards)
restingLimitBuyOrderBatchEvent, limitBuyFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(true, ExecutionType_LimitFill, market, funding, e.LimitBuyExpansions, depositDeltas, liquidityMiningRewards)
restingLimitSellOrderBatchEvent, limitSellFilledDeltas := ApplyDeltasAndGetDerivativeOrderBatchEvent(false, ExecutionType_LimitFill, market, funding, e.LimitSellExpansions, depositDeltas, liquidityMiningRewards)

filledDeltas := mergeDerivativeLimitOrderFilledDeltas(limitBuyFilledDeltas, limitSellFilledDeltas)

Expand All @@ -371,6 +376,7 @@ func (e *DerivativeMarketOrderExpansionData) GetMarketDerivativeBatchExecutionDa
Funding: funding,
DepositDeltas: depositDeltas,
DepositSubaccountIDs: depositDeltaKeys,
LiquidityMiningRewards: liquidityMiningRewards,
Positions: positions,
PositionSubaccountIDs: positionSubaccountIDs,
TransientLimitOrderFilledDeltas: nil,
Expand Down
6 changes: 5 additions & 1 deletion chain/exchange/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var (
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 derivative market")
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")
Expand Down Expand Up @@ -57,4 +57,8 @@ var (
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")
ErrInvalidLiquidityMiningCampaign = sdkerrors.Register(ModuleName, 55, "Invalid liquidity mining campaign")
ErrInvalidLiquidityMiningReward = sdkerrors.Register(ModuleName, 56, "Invalid liquidity mining reward")
)
Loading

0 comments on commit bbf1a69

Please sign in to comment.