Skip to content

Commit

Permalink
assert isValidator when assertLowMargin
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Sep 3, 2023
1 parent 22b3bb8 commit 9f659b2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 31 deletions.
5 changes: 5 additions & 0 deletions precompile/contracts/bibliophile/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BibliophileClient interface {
GetShortOpenOrdersAmount(trader common.Address, ammIndex *big.Int) *big.Int
GetReduceOnlyAmount(trader common.Address, ammIndex *big.Int) *big.Int
IsTradingAuthority(trader, senderOrSigner common.Address) bool
IsValidator(senderOrSigner common.Address) bool
// Limit Order
GetBlockPlaced(orderHash [32]byte) *big.Int
GetOrderFilledAmount(orderHash [32]byte) *big.Int
Expand Down Expand Up @@ -118,6 +119,10 @@ func (b *bibliophileClient) IsTradingAuthority(trader, senderOrSigner common.Add
return IsTradingAuthority(b.accessibleState.GetStateDB(), trader, senderOrSigner)
}

func (b *bibliophileClient) IsValidator(senderOrSigner common.Address) bool {
return IsValidator(b.accessibleState.GetStateDB(), senderOrSigner)
}

func (b *bibliophileClient) IOC_GetExpirationCap() *big.Int {
return iocGetExpirationCap(b.accessibleState.GetStateDB())
}
Expand Down
22 changes: 18 additions & 4 deletions precompile/contracts/bibliophile/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions precompile/contracts/bibliophile/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
const (
ORDERBOOK_GENESIS_ADDRESS = "0x0300000000000000000000000000000000000000"
ORDER_INFO_SLOT int64 = 53
IS_VALIDATOR_SLOT int64 = 54
REDUCE_ONLY_AMOUNT_SLOT int64 = 55
IS_TRADING_AUTHORITY_SLOT int64 = 61
LONG_OPEN_ORDERS_SLOT int64 = 65
Expand Down Expand Up @@ -79,6 +80,11 @@ func IsTradingAuthority(stateDB contract.StateDB, trader, senderOrSigner common.
return stateDB.GetState(common.HexToAddress(ORDERBOOK_GENESIS_ADDRESS), common.BytesToHash(tradingAuthorityMappingSlot)).Big().Cmp(big.NewInt(1)) == 0
}

func IsValidator(stateDB contract.StateDB, senderOrSigner common.Address) bool {
isValidatorMappingSlot := crypto.Keccak256(append(common.LeftPadBytes(senderOrSigner.Bytes(), 32), common.LeftPadBytes(big.NewInt(IS_VALIDATOR_SLOT).Bytes(), 32)...))
return stateDB.GetState(common.HexToAddress(ORDERBOOK_GENESIS_ADDRESS), common.BytesToHash(isValidatorMappingSlot)).Big().Cmp(big.NewInt(1)) == 0
}

// Business Logic

func ValidateOrdersAndDetermineFillPrice(stateDB contract.StateDB, inputStruct *ValidateOrdersAndDetermineFillPriceInput) (*ValidateOrdersAndDetermineFillPriceOutput, error) {
Expand Down
2 changes: 1 addition & 1 deletion precompile/contracts/juror/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func validateCancelLimitOrder(accessibleState contract.AccessibleState, caller c
}
// CUSTOM CODE STARTS HERE
bibliophile := bibliophile.NewBibliophileClient(accessibleState)
output := ValidateCancelLimitOrderV2(bibliophile, &inputStruct)
output := ValidateCancelLimitOrderV2(bibliophile, &inputStruct, new(big.Int).SetUint64(accessibleState.GetBlockContext().Timestamp()))
packedOutput, err := PackValidateCancelLimitOrderOutput(*output)
if err != nil {
return nil, remainingGas, err
Expand Down
40 changes: 20 additions & 20 deletions precompile/contracts/juror/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1636,14 +1636,14 @@ func TestValidateCancelLimitOrder(t *testing.T) {
order := getOrder(ammIndex, trader, longBaseAssetQuantity, price, salt, reduceOnly, postOnly)
input := getValidateCancelLimitOrderInput(order, sender, assertLowMargin)
mockBibliophile.EXPECT().IsTradingAuthority(order.Trader, sender).Return(false).Times(1)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, ErrNoTradingAuthority.Error(), output.Err)
})
t.Run("it returns error for a short order", func(t *testing.T) {
order := getOrder(ammIndex, trader, shortBaseAssetQuantity, price, salt, reduceOnly, postOnly)
input := getValidateCancelLimitOrderInput(order, sender, assertLowMargin)
mockBibliophile.EXPECT().IsTradingAuthority(order.Trader, sender).Return(false).Times(1)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, ErrNoTradingAuthority.Error(), output.Err)
})
})
Expand All @@ -1655,7 +1655,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(longOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Invalid)).Times(1)
input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Invalid", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1666,7 +1666,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(shortOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Invalid)).Times(1)
input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Invalid", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1679,7 +1679,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(longOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Cancelled)).Times(1)
input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Cancelled", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1690,7 +1690,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(shortOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Cancelled)).Times(1)
input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Cancelled", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1703,7 +1703,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(longOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Filled)).Times(1)
input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Filled", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1714,7 +1714,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
orderHash := getOrderV2Hash(shortOrder)
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Filled)).Times(1)
input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Filled", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1734,7 +1734,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Placed)).Times(1)
mockBibliophile.EXPECT().GetAvailableMargin(longOrder.Trader).Return(big.NewInt(0)).Times(1)
input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Not Low Margin", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1747,7 +1747,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Placed)).Times(1)
mockBibliophile.EXPECT().GetAvailableMargin(shortOrder.Trader).Return(big.NewInt(0)).Times(1)
input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)

assert.Equal(t, "Not Low Margin", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
Expand All @@ -1764,7 +1764,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Placed)).Times(1)
mockBibliophile.EXPECT().GetAvailableMargin(longOrder.Trader).Return(newMargin).Times(1)
input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Not Low Margin", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1777,7 +1777,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetOrderStatus(orderHash).Return(int64(Placed)).Times(1)
mockBibliophile.EXPECT().GetAvailableMargin(shortOrder.Trader).Return(newMargin).Times(1)
input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "Not Low Margin", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, common.Address{}, output.Res.Amm)
Expand All @@ -1797,7 +1797,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(longOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1813,7 +1813,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(shortOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1832,7 +1832,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(longOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1850,7 +1850,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(shortOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1872,7 +1872,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(longOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1887,7 +1887,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(shortOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1905,7 +1905,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(longOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(longOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand All @@ -1922,7 +1922,7 @@ func TestValidateCancelLimitOrder(t *testing.T) {
mockBibliophile.EXPECT().GetMarketAddressFromMarketID(shortOrder.AmmIndex.Int64()).Return(ammAddress).Times(1)

input := getValidateCancelLimitOrderInput(shortOrder, trader, assertLowMargin)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input)
output := ValidateCancelLimitOrderV2(mockBibliophile, &input, nil)
assert.Equal(t, "", output.Err)
assert.Equal(t, orderHash, common.BytesToHash(output.OrderHash[:]))
assert.Equal(t, ammAddress, output.Res.Amm)
Expand Down
23 changes: 17 additions & 6 deletions precompile/contracts/juror/logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ func GetBaseQuote(bibliophile b.BibliophileClient, ammAddress common.Address, qu
}

// Limit Orders V2
func ValidateCancelLimitOrderV2(bibliophile b.BibliophileClient, inputStruct *ValidateCancelLimitOrderInput) *ValidateCancelLimitOrderOutput {
errorString, orderHash, ammAddress, unfilledAmount := validateCancelLimitOrderV2(bibliophile, inputStruct.Order, inputStruct.Trader, inputStruct.AssertLowMargin)
func ValidateCancelLimitOrderV2(bibliophile b.BibliophileClient, inputStruct *ValidateCancelLimitOrderInput, blockTimestamp *big.Int) *ValidateCancelLimitOrderOutput {
errorString, orderHash, ammAddress, unfilledAmount := validateCancelLimitOrderV2(bibliophile, inputStruct.Order, inputStruct.Trader, inputStruct.AssertLowMargin, blockTimestamp)
return &ValidateCancelLimitOrderOutput{
Err: errorString,
OrderHash: orderHash,
Expand All @@ -526,12 +526,23 @@ func ValidateCancelLimitOrderV2(bibliophile b.BibliophileClient, inputStruct *Va
}
}

func validateCancelLimitOrderV2(bibliophile b.BibliophileClient, order ILimitOrderBookOrderV2, sender common.Address, assertLowMargin bool) (errorString string, orderHash [32]byte, ammAddress common.Address, unfilledAmount *big.Int) {
// Sunday, 3 September 2023 10:35:00 UTC
var V4ActivationDate *big.Int = new(big.Int).SetInt64(1693737300)

func validateCancelLimitOrderV2(bibliophile b.BibliophileClient, order ILimitOrderBookOrderV2, sender common.Address, assertLowMargin bool, blockTimestamp *big.Int) (errorString string, orderHash [32]byte, ammAddress common.Address, unfilledAmount *big.Int) {
unfilledAmount = big.NewInt(0)
trader := order.Trader
if trader != sender && !bibliophile.IsTradingAuthority(trader, sender) {
errorString = ErrNoTradingAuthority.Error()
return
if blockTimestamp != nil && blockTimestamp.Cmp(V4ActivationDate) == 1 {
if (!assertLowMargin && trader != sender && !bibliophile.IsTradingAuthority(trader, sender)) ||
(assertLowMargin && !bibliophile.IsValidator(sender)) {
errorString = ErrNoTradingAuthority.Error()
return
}
} else {
if trader != sender && !bibliophile.IsTradingAuthority(trader, sender) {
errorString = ErrNoTradingAuthority.Error()
return
}
}
orderHash, err := GetLimitOrderV2Hash(&order)
if err != nil {
Expand Down

0 comments on commit 9f659b2

Please sign in to comment.