Skip to content

Commit

Permalink
Move SA module address transfers to use perpetual based SA accounts
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
  • Loading branch information
shrenujb committed Mar 6, 2024
1 parent ee4bba3 commit 8f36ca0
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 186 deletions.
7 changes: 7 additions & 0 deletions protocol/testutil/constants/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ var (
PerpetualId: 1,
Quantums: dtypes.NewIntFromBigInt(BigNegMaxUint64()), // 18,446,744,070 ETH, -$55,340,232,210,000 notional.
}
// Long position for arbitrary isolated market
PerpetualPosition_OneISOLong = satypes.PerpetualPosition{
PerpetualId: 3,
Quantums: dtypes.NewInt(100_000_000),
FundingIndex: dtypes.NewInt(0),
}

// Asset Positions
Usdc_Asset_0 = satypes.AssetPosition{
AssetId: 0,
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ func TestPrepareCheckState(t *testing.T) {
constants.Usdc.Denom,
).Return(sdk.NewCoin(constants.Usdc.Denom, sdkmath.NewIntFromBigInt(new(big.Int))))
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down
28 changes: 21 additions & 7 deletions protocol/x/clob/keeper/liquidations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,25 @@ func TestPlacePerpetualLiquidation(t *testing.T) {
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.Anything,
).Return(nil)
mockBankKeeper.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
perptypes.InsuranceFundModuleAddress,
mock.Anything,
).Return(nil)
// Fee collector does not have any funds.
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
authtypes.FeeCollectorName,
satypes.ModuleName,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
satypes.ModuleAddress,
mock.Anything,
).Return(sdkerrors.ErrInsufficientFunds)
// Give the insurance fund a 1M USDC balance.
Expand Down Expand Up @@ -793,6 +793,13 @@ func TestPlacePerpetualLiquidation_PreexistingLiquidation(t *testing.T) {
mock.Anything,
mock.Anything,
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)
bk.On(
"GetBalance",
mock.Anything,
Expand Down Expand Up @@ -833,6 +840,13 @@ func TestPlacePerpetualLiquidation_PreexistingLiquidation(t *testing.T) {
mock.Anything,
mock.Anything,
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)
bk.On(
"GetBalance",
mock.Anything,
Expand Down
7 changes: 7 additions & 0 deletions protocol/x/clob/keeper/mev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,13 @@ func TestGetMidPrices(t *testing.T) {
mock.Anything,
mock.Anything,
).Return(nil)
mockBankKeeper.On(
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)

ks := keepertest.NewClobKeepersTestContext(t, memclob, mockBankKeeper, indexer_manager.NewIndexerEventManagerNoop())
ctx := ks.Ctx.WithIsCheckTx(true)
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func TestPlaceShortTermOrder(t *testing.T) {
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -875,7 +875,7 @@ func TestAddPreexistingStatefulOrder(t *testing.T) {
memClob := memclob.NewMemClobPriceTimePriority(false)
mockBankKeeper := &mocks.BankKeeper{}
mockBankKeeper.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
mock.Anything,
Expand Down
94 changes: 47 additions & 47 deletions protocol/x/clob/keeper/process_operations_liquidations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,17 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(10_000_000)),
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Subaccount pays $250 to insurance fund for liquidating 1 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(250_000_000)),
).Return(nil).Once()
Expand Down Expand Up @@ -271,10 +271,10 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(10_100_000)),
).Return(nil)
bk.On(
Expand All @@ -286,8 +286,8 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
authtypes.NewModuleAddress(satypes.ModuleName),
perptypes.InsuranceFundModuleAddress,
satypes.ModuleAddress,
// Insurance fund covers $1 loss for liquidating 1 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(1_000_000)),
).Return(nil).Once()
Expand Down Expand Up @@ -357,17 +357,17 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(2_500_000)),
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Subaccount pays $62.5 to insurance fund for liquidating 0.25 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(62_500_000)),
).Return(nil).Twice()
Expand Down Expand Up @@ -455,10 +455,10 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(2_525_000)),
).Return(nil)
bk.On(
Expand All @@ -470,8 +470,8 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
authtypes.NewModuleAddress(satypes.ModuleName),
perptypes.InsuranceFundModuleAddress,
satypes.ModuleAddress,
// Insurance fund covers $0.25 loss for liquidating 0.25 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(250_000)),
).Return(nil).Twice()
Expand Down Expand Up @@ -560,10 +560,10 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.Anything,
).Return(nil)
bk.On(
Expand All @@ -575,16 +575,16 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Pays insurance fund $0.75 for liquidating 0.75 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(750_000)),
).Return(nil).Once()
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
authtypes.NewModuleAddress(satypes.ModuleName),
perptypes.InsuranceFundModuleAddress,
satypes.ModuleAddress,
// Insurance fund covers $0.25 loss for liquidating 0.25 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(250_000)),
).Return(nil).Once()
Expand Down Expand Up @@ -664,10 +664,10 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.Anything,
).Return(nil)
bk.On(
Expand All @@ -679,17 +679,17 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Pays insurance fund $0.378735 (capped by MaxLiquidationFeePpm)
// for liquidating 0.75 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(378_735)),
).Return(nil).Once()
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Pays insurance fund $0.121265.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(121_265)),
).Return(nil).Once()
Expand Down Expand Up @@ -777,17 +777,17 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(5_000_000)),
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
// Subaccount pays $125 to insurance fund for liquidating 0.5 BTC.
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(125_000_000)),
).Return(nil).Once()
Expand Down Expand Up @@ -890,17 +890,17 @@ func TestProcessProposerMatches_Liquidation_Success(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
satypes.ModuleName,
authtypes.FeeCollectorName,
satypes.ModuleAddress,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(1)),
).Return(nil)
bk.On(
"SendCoins",
mock.Anything,
authtypes.NewModuleAddress(satypes.ModuleName),
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
satypes.ModuleAddress,
perptypes.InsuranceFundModuleAddress,
mock.MatchedBy(testutil_bank.MatchUsdcOfAmount(25)),
).Return(nil)
},
Expand Down Expand Up @@ -1263,17 +1263,17 @@ func TestProcessProposerMatches_Liquidation_Failure(t *testing.T) {
},
setupMockBankKeeper: func(bk *mocks.BankKeeper) {
bk.On(
"SendCoinsFromModuleToModule",
"SendCoins",
mock.Anything,
mock.Anything,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(authtypes.FeeCollectorName),
mock.Anything,
).Return(fmt.Errorf("transfer failed"))
bk.On(
"SendCoins",
mock.Anything,
mock.Anything,
authtypes.NewModuleAddress(perptypes.InsuranceFundName),
perptypes.InsuranceFundModuleAddress,
mock.Anything,
).Return(nil)
},
Expand Down
Loading

0 comments on commit 8f36ca0

Please sign in to comment.