@@ -43,6 +43,7 @@ import {
4343 SPOT_MARKET_BALANCE_PRECISION ,
4444 SpotBalanceType ,
4545 getTokenAmount ,
46+ TWO ,
4647} from '../sdk/src' ;
4748
4849import {
@@ -780,7 +781,7 @@ describe('LP Pool', () => {
780781 ) ;
781782 } ) ;
782783
783- it ( 'will fail gracefully when trying to settle pnl from constituents to perp markets if not enough usdc in the constituent vault' , async ( ) => {
784+ it ( 'will settle gracefully when trying to settle pnl from constituents to perp markets if not enough usdc in the constituent vault' , async ( ) => {
784785 let lpPool = ( await adminClient . program . account . lpPool . fetch (
785786 lpPoolKey
786787 ) ) as LPPoolAccount ;
@@ -876,8 +877,85 @@ describe('LP Pool', () => {
876877 )
877878 )
878879 ) ;
880+ assert (
881+ adminClient
882+ . getPerpMarketAccount ( 0 )
883+ . amm . feePool . scaledBalance . eq (
884+ new BN ( constituentUSDCBalanceBefore . toString ( ) ) . mul (
885+ SPOT_MARKET_BALANCE_PRECISION . div ( QUOTE_PRECISION )
886+ )
887+ )
888+ ) ;
879889
880890 // NAV should have gone down the max that is has
881891 assert ( lpPool . lastAum . eq ( ZERO ) ) ;
882892 } ) ;
893+
894+ it ( 'perp market will not transfer with the constituent vault if it is owed from dlp' , async ( ) => {
895+ let ammCache = ( await adminClient . program . account . ammCache . fetch (
896+ getAmmCachePublicKey ( program . programId )
897+ ) ) as AmmCache ;
898+ const owedAmount = ammCache . cache [ 0 ] . quoteOwedFromLp ;
899+
900+ // Give the perp market half of its owed amount
901+ const perpMarket = adminClient . getPerpMarketAccount ( 0 ) ;
902+ perpMarket . amm . feePool . scaledBalance =
903+ perpMarket . amm . feePool . scaledBalance . add (
904+ owedAmount
905+ . div ( TWO )
906+ . mul ( SPOT_MARKET_BALANCE_PRECISION . div ( QUOTE_PRECISION ) )
907+ ) ;
908+ await overWritePerpMarket (
909+ adminClient ,
910+ bankrunContextWrapper ,
911+ perpMarket . pubkey ,
912+ perpMarket
913+ ) ;
914+
915+ await adminClient . settlePerpToLpPool ( encodeName ( lpPoolName ) , [ 0 , 1 , 2 ] ) ;
916+
917+ ammCache = ( await adminClient . program . account . ammCache . fetch (
918+ getAmmCachePublicKey ( program . programId )
919+ ) ) as AmmCache ;
920+ const constituent = ( await adminClient . program . account . constituent . fetch (
921+ getConstituentPublicKey ( program . programId , lpPoolKey , 0 )
922+ ) ) as ConstituentAccount ;
923+
924+ assert ( ammCache . cache [ 0 ] . quoteOwedFromLp . eq ( owedAmount . divn ( 2 ) ) ) ;
925+ assert ( constituent . tokenBalance . eq ( ZERO ) ) ;
926+ } ) ;
927+
928+ it ( 'perp market will transfer with the constituent vault if it should send more than its owed' , async ( ) => {
929+ let ammCache = ( await adminClient . program . account . ammCache . fetch (
930+ getAmmCachePublicKey ( program . programId )
931+ ) ) as AmmCache ;
932+ const owedAmount = ammCache . cache [ 0 ] . quoteOwedFromLp ;
933+
934+ // Give the perp market half of its owed amount
935+ const perpMarket = adminClient . getPerpMarketAccount ( 0 ) ;
936+ perpMarket . amm . feePool . scaledBalance =
937+ perpMarket . amm . feePool . scaledBalance . add (
938+ owedAmount
939+ . mul ( TWO )
940+ . mul ( SPOT_MARKET_BALANCE_PRECISION . div ( QUOTE_PRECISION ) )
941+ ) ;
942+ await overWritePerpMarket (
943+ adminClient ,
944+ bankrunContextWrapper ,
945+ perpMarket . pubkey ,
946+ perpMarket
947+ ) ;
948+
949+ await adminClient . settlePerpToLpPool ( encodeName ( lpPoolName ) , [ 0 , 1 , 2 ] ) ;
950+
951+ ammCache = ( await adminClient . program . account . ammCache . fetch (
952+ getAmmCachePublicKey ( program . programId )
953+ ) ) as AmmCache ;
954+ const constituent = ( await adminClient . program . account . constituent . fetch (
955+ getConstituentPublicKey ( program . programId , lpPoolKey , 0 )
956+ ) ) as ConstituentAccount ;
957+
958+ assert ( ammCache . cache [ 0 ] . quoteOwedFromLp . eq ( ZERO ) ) ;
959+ assert ( constituent . tokenBalance . eq ( owedAmount ) ) ;
960+ } ) ;
883961} ) ;
0 commit comments