Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-81] Check isolated market constraints in UpdateSubaccount. #1158

Merged
merged 14 commits into from
Mar 13, 2024
2 changes: 1 addition & 1 deletion protocol/app/e2e/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestParallelAnteHandler_ClobAndOther(t *testing.T) {
},
},
constants.TestFeeCoins_5Cents,
100_000,
110_000,
ctx.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{sequenceNumber},
Expand Down
1 change: 1 addition & 0 deletions protocol/daemons/liquidation/client/grpc_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ func TestGetAllMarketPrices(t *testing.T) {
MarketPrices: []pricestypes.MarketPrice{
constants.TestMarketPrices[2],
constants.TestMarketPrices[3],
constants.TestMarketPrices[4],
},
}
mck.On("AllMarketPrices", mock.Anything, req2).Return(response2, nil)
Expand Down
12 changes: 12 additions & 0 deletions protocol/testutil/constants/perpetuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ var (
},
FundingIndex: dtypes.ZeroInt(),
}
Iso2Usd_IsolatedMarket = perptypes.Perpetual{
Params: perptypes.PerpetualParams{
Id: 4,
Ticker: "ISO2-USD",
MarketId: uint32(4),
AtomicResolution: int32(-7),
DefaultFundingPpm: int32(0),
LiquidityTier: uint32(3),
MarketType: perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED,
},
FundingIndex: dtypes.ZeroInt(),
}
)

var TestMarketPerpetuals = []perptypes.Perpetual{
Expand Down
10 changes: 10 additions & 0 deletions protocol/testutil/constants/pricefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
MarketId1 = uint32(1)
MarketId2 = uint32(2)
MarketId3 = uint32(3)
MarketId4 = uint32(4)

MarketId7 = uint32(7)
MarketId8 = uint32(8)
Expand Down Expand Up @@ -276,26 +277,35 @@ var (
Exchange3_Price3_TimeT,
},
},
{
MarketId: MarketId4,
ExchangePrices: []*api.ExchangePrice{
Exchange3_Price3_TimeT,
},
},
}
AtTimeTSingleExchangeSmoothedPrices = map[uint32]uint64{
MarketId0: Exchange0_Price4_TimeT.Price,
MarketId1: Exchange1_Price1_TimeT.Price,
MarketId2: Exchange2_Price2_TimeT.Price,
MarketId3: Exchange3_Price3_TimeT.Price,
MarketId4: Exchange3_Price3_TimeT.Price,
}

AtTimeTSingleExchangeSmoothedPricesPlus10 = map[uint32]uint64{
MarketId0: Exchange0_Price4_TimeT.Price + 10,
MarketId1: Exchange1_Price1_TimeT.Price + 10,
MarketId2: Exchange2_Price2_TimeT.Price + 10,
MarketId3: Exchange3_Price3_TimeT.Price + 10,
MarketId4: Exchange3_Price3_TimeT.Price + 10,
}

AtTimeTSingleExchangeSmoothedPricesPlus7 = map[uint32]uint64{
MarketId0: Exchange0_Price4_TimeT.Price + 7,
MarketId1: Exchange1_Price1_TimeT.Price + 7,
MarketId2: Exchange2_Price2_TimeT.Price + 7,
MarketId3: Exchange3_Price3_TimeT.Price + 7,
MarketId4: Exchange3_Price3_TimeT.Price + 7,
}

MixedTimePriceUpdate = []*api.MarketPriceUpdate{
Expand Down
26 changes: 26 additions & 0 deletions protocol/testutil/constants/prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
SolUsdPair = "SOL-USD"
LtcUsdPair = "LTC-USD"
IsoUsdPair = "ISO-USD"
Iso2UsdPair = "ISO2-USD"

BtcUsdExponent = -5
EthUsdExponent = -6
Expand All @@ -36,6 +37,7 @@ const (
SolUsdExponent = -8
LtcUsdExponent = -7
IsoUsdExponent = -8
Iso2UsdExponent = -7

CoinbaseExchangeName = "Coinbase"
BinanceExchangeName = "Binance"
Expand Down Expand Up @@ -212,6 +214,15 @@ var TestMarketExchangeConfigs = map[pricefeedclient.MarketId]string{
}
]
}`,
exchange_config.MARKET_ISO2_USD: `{
"exchanges": [
{
"exchangeName": "Binance",
"ticker": "ISO2USDT",
"adjustByMarket": "USDT-USD"
}
]
}`,
}

var TestMarketParams = []types.MarketParam{
Expand Down Expand Up @@ -247,6 +258,14 @@ var TestMarketParams = []types.MarketParam{
MinPriceChangePpm: 50,
ExchangeConfigJson: TestMarketExchangeConfigs[exchange_config.MARKET_ISO_USD],
},
{
Id: 4,
Pair: Iso2UsdPair,
Exponent: Iso2UsdExponent,
MinExchanges: 1,
MinPriceChangePpm: 50,
ExchangeConfigJson: TestMarketExchangeConfigs[exchange_config.MARKET_ISO2_USD],
},
}

var TestMarketPrices = []types.MarketPrice{
Expand All @@ -270,13 +289,19 @@ var TestMarketPrices = []types.MarketPrice{
Exponent: IsoUsdExponent,
Price: FiveBillion, // 50$ == 1 ISO
},
{
Id: 4,
Exponent: Iso2UsdExponent,
Price: ThreeBillion, // 300$ == 1 ISO2
},
}

var TestMarketIdsToExponents = map[uint32]int32{
0: BtcUsdExponent,
1: EthUsdExponent,
2: SolUsdExponent,
3: IsoUsdExponent,
4: Iso2UsdExponent,
}

var TestPricesGenesisState = types.GenesisState{
Expand All @@ -290,6 +315,7 @@ var (
types.NewMarketPriceUpdate(MarketId1, Price6),
types.NewMarketPriceUpdate(MarketId2, Price7),
types.NewMarketPriceUpdate(MarketId3, Price4),
types.NewMarketPriceUpdate(MarketId4, Price3),
}

// `MsgUpdateMarketPrices`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ const (
// MARKET_TEST_USD is the id used for the TEST-USD market pair.
MARKET_TEST_USD types.MarketId = 33

// Arbitrary isolated market
MARKET_ISO_USD types.MarketId = 999_999
// Arbitrary isolated markets
MARKET_ISO2_USD types.MarketId = 999_998
MARKET_ISO_USD types.MarketId = 999_999

// Non-trading markets.
// MARKET_USDT_USD is the id for the USDT-USD market pair.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Multiple updates": {
Expand All @@ -58,6 +59,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.Price6,
constants.MarketId2: constants.Price7,
constants.MarketId3: constants.Price4,
constants.MarketId4: constants.Price3,
},
},
"Towards index price = true (current < update < index price)": {
Expand All @@ -81,6 +83,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true (price increase), old_ticks > 1, new_ticks <= sqrt(old_ticks) = true": {
Expand All @@ -104,6 +107,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true (price decrease), old_ticks > 1, new_ticks <= sqrt(old_ticks) = true": {
Expand All @@ -127,6 +131,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true (price increase), old_ticks <= 1, new_ticks <= old_ticks = true": {
Expand All @@ -150,6 +155,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true (price decrease), old_ticks <= 1, new_ticks <= old_ticks = true": {
Expand All @@ -173,6 +179,7 @@ func TestUpdateMarketPrices_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
}
Expand Down Expand Up @@ -226,6 +233,7 @@ func TestUpdateMarketPrices_SkipNonDeterministicCheck_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price trends in the opposite direction of update price from current price, but still updates state": {
Expand All @@ -249,6 +257,7 @@ func TestUpdateMarketPrices_SkipNonDeterministicCheck_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true, old_ticks > 1, new_ticks <= sqrt(old_ticks) = false": {
Expand All @@ -272,6 +281,7 @@ func TestUpdateMarketPrices_SkipNonDeterministicCheck_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
"Index price crossing = true, old_ticks <= 1, new_ticks <= old_ticks = false": {
Expand All @@ -295,6 +305,7 @@ func TestUpdateMarketPrices_SkipNonDeterministicCheck_Valid(t *testing.T) {
constants.MarketId1: constants.ThreeBillion, // no change
constants.MarketId2: constants.FiveBillion, // no change
constants.MarketId3: constants.FiveBillion, // no change
constants.MarketId4: constants.ThreeBillion, // no change
},
},
}
Expand Down
5 changes: 4 additions & 1 deletion protocol/x/prices/keeper/smoothed_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestUpdateSmoothedPrices(t *testing.T) {
constants.MarketId1: constants.Exchange1_Price1_TimeT.Price + 7,
constants.MarketId2: constants.Exchange2_Price2_TimeT.Price + 35,
constants.MarketId3: constants.Exchange3_Price3_TimeT.Price,
constants.MarketId4: constants.Exchange3_Price3_TimeT.Price,
constants.MarketId7: constants.Price1,
},
linearInterpolateFunc: lib.Uint64LinearInterpolate,
Expand All @@ -81,7 +82,8 @@ func TestUpdateSmoothedPrices(t *testing.T) {
expectedErr: "Error updating smoothed price for market 0: error while interpolating\n" +
"Error updating smoothed price for market 1: error while interpolating\n" +
"Error updating smoothed price for market 2: error while interpolating\n" +
"Error updating smoothed price for market 3: error while interpolating",
"Error updating smoothed price for market 3: error while interpolating\n" +
"Error updating smoothed price for market 4: error while interpolating",
expectedResult: constants.AtTimeTSingleExchangeSmoothedPricesPlus10, // no change
},
"Single interpolation error - returns error, continues updating other markets": {
Expand All @@ -95,6 +97,7 @@ func TestUpdateSmoothedPrices(t *testing.T) {
constants.MarketId1: constants.AtTimeTSingleExchangeSmoothedPricesPlus10[constants.MarketId1], // no change
constants.MarketId2: constants.AtTimeTSingleExchangeSmoothedPricesPlus7[constants.MarketId2], // update
constants.MarketId3: constants.AtTimeTSingleExchangeSmoothedPricesPlus10[constants.MarketId3], // update
constants.MarketId4: constants.AtTimeTSingleExchangeSmoothedPricesPlus7[constants.MarketId4], // update
}, // no change
},
}
Expand Down
3 changes: 3 additions & 0 deletions protocol/x/prices/keeper/update_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestGetValidMarketPriceUpdates(t *testing.T) {
types.NewMarketPriceUpdate(constants.MarketId1, constants.Price1+1),
types.NewMarketPriceUpdate(constants.MarketId2, constants.Price2),
types.NewMarketPriceUpdate(constants.MarketId3, constants.Price3),
types.NewMarketPriceUpdate(constants.MarketId4, constants.Price3),
},
},
},
Expand Down Expand Up @@ -232,6 +233,7 @@ func TestGetValidMarketPriceUpdates(t *testing.T) {
types.NewMarketPriceUpdate(constants.MarketId1, constants.Price1),
types.NewMarketPriceUpdate(constants.MarketId2, constants.Price2),
types.NewMarketPriceUpdate(constants.MarketId3, constants.Price3),
types.NewMarketPriceUpdate(constants.MarketId4, constants.Price3),
},
},
},
Expand All @@ -250,6 +252,7 @@ func TestGetValidMarketPriceUpdates(t *testing.T) {
types.NewMarketPriceUpdate(constants.MarketId1, constants.Price1),
types.NewMarketPriceUpdate(constants.MarketId2, constants.Price2),
types.NewMarketPriceUpdate(constants.MarketId3, constants.Price3),
types.NewMarketPriceUpdate(constants.MarketId4, constants.Price3),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func TestGetMarketsMissingFromPriceUpdates(t *testing.T) {
smoothedIndexPrices: constants.AtTimeTSingleExchangeSmoothedPrices,
// The returned market ids must be sorted.
expectedMarketIds: []uint32{
constants.MarketId0, constants.MarketId1, constants.MarketId2, constants.MarketId3,
constants.MarketId0, constants.MarketId1, constants.MarketId2, constants.MarketId3, constants.MarketId4,
},
},
"Non-empty proposed updates, Empty local updates": {
Expand All @@ -392,6 +392,7 @@ func TestGetMarketsMissingFromPriceUpdates(t *testing.T) {
types.NewMarketPriceUpdate(constants.MarketId0, constants.Price5),
types.NewMarketPriceUpdate(constants.MarketId1, constants.Price6),
types.NewMarketPriceUpdate(constants.MarketId3, constants.Price7),
types.NewMarketPriceUpdate(constants.MarketId4, constants.Price4),
},
indexPrices: constants.AtTimeTSingleExchangePriceUpdate,
smoothedIndexPrices: constants.AtTimeTSingleExchangeSmoothedPrices,
Expand All @@ -404,7 +405,7 @@ func TestGetMarketsMissingFromPriceUpdates(t *testing.T) {
indexPrices: constants.AtTimeTSingleExchangePriceUpdate,
smoothedIndexPrices: constants.AtTimeTSingleExchangeSmoothedPrices,
// The returned market ids must be sorted.
expectedMarketIds: []uint32{constants.MarketId0, constants.MarketId2, constants.MarketId3},
expectedMarketIds: []uint32{constants.MarketId0, constants.MarketId2, constants.MarketId3, constants.MarketId4},
},
}
for name, tc := range tests {
Expand Down
Loading
Loading