@@ -24,6 +24,8 @@ import {
2424 TOKEN_DECIMALS ,
2525 U64_MAX ,
2626 U32_MAX ,
27+ MIN_SQRT_PRICE ,
28+ MAX_SQRT_PRICE ,
2729} from "../common" ;
2830
2931import ZapIDL from "../../target/idl/zap.json" ;
@@ -409,6 +411,151 @@ describe("Zap In damm V2", () => {
409411 expect ( result ) . instanceOf ( TransactionMetadata ) ;
410412 } ) ;
411413
414+ it ( "zap in when sqrt_price is at sqrt_min_price (single-sided A)" , async ( ) => {
415+ const pool = await createDammV2Pool (
416+ svm ,
417+ admin ,
418+ tokenAMint ,
419+ tokenBMint ,
420+ new BN ( LAMPORTS_PER_SOL ) ,
421+ undefined ,
422+ undefined ,
423+ MIN_SQRT_PRICE
424+ ) ;
425+
426+ const poolState = getDammV2Pool ( svm , pool ) ;
427+
428+ expect ( poolState . sqrtPrice . eq ( MIN_SQRT_PRICE ) ) . to . be . true ;
429+
430+ const { position, positionNftAccount } = await createDammV2Position (
431+ svm ,
432+ user ,
433+ pool
434+ ) ;
435+
436+ const initializeLedgerTx = await initializeLedgerAccount ( user . publicKey ) ;
437+
438+ const totalAmountA = new BN ( LAMPORTS_PER_SOL / 2 ) ;
439+ const setLedgerBalanceTx = await setLedgerBalance (
440+ user . publicKey ,
441+ totalAmountA ,
442+ true
443+ ) ;
444+
445+ const tokenBAccount = getAssociatedTokenAddressSync (
446+ tokenBMint ,
447+ user . publicKey
448+ ) ;
449+ const preTokenBBalance = getTokenBalance ( svm , tokenBAccount ) ;
450+ const updateLedgerBalanceAfterSwapTx = await updateLedgerBalanceAfterSwap (
451+ user . publicKey ,
452+ tokenBAccount ,
453+ preTokenBBalance ,
454+ U64_MAX ,
455+ false
456+ ) ;
457+
458+ const zapInTx = await zapInDammv2 ( {
459+ svm,
460+ user : user . publicKey ,
461+ pool,
462+ position,
463+ positionNftAccount,
464+ preSqrtPrice : poolState . sqrtPrice ,
465+ maxSqrtPriceChangeBps : U32_MAX . toNumber ( ) ,
466+ } ) ;
467+
468+ const closeLedgerTx = await closeLedgerAccount ( user . publicKey ) ;
469+
470+ const finalTx = new Transaction ( )
471+ . add ( initializeLedgerTx )
472+ . add ( setLedgerBalanceTx )
473+ . add ( updateLedgerBalanceAfterSwapTx )
474+ . add ( zapInTx )
475+ . add ( closeLedgerTx ) ;
476+ finalTx . recentBlockhash = svm . latestBlockhash ( ) ;
477+ finalTx . sign ( user ) ;
478+
479+ const result = svm . sendTransaction ( finalTx ) ;
480+ if ( result instanceof FailedTransactionMetadata ) {
481+ console . log ( result . meta ( ) . logs ( ) ) ;
482+ }
483+ expect ( result ) . instanceOf ( TransactionMetadata ) ;
484+ } ) ;
485+
486+ it ( "zap in when sqrt_price is at sqrt_max_price (single-sided B)" , async ( ) => {
487+ const pool = await createDammV2Pool (
488+ svm ,
489+ admin ,
490+ tokenAMint ,
491+ tokenBMint ,
492+ undefined ,
493+ new BN ( LAMPORTS_PER_SOL ) ,
494+ undefined ,
495+ MAX_SQRT_PRICE
496+ ) ;
497+
498+ const poolState = getDammV2Pool ( svm , pool ) ;
499+
500+ expect ( poolState . sqrtPrice . eq ( MAX_SQRT_PRICE ) ) . to . be . true ;
501+
502+ const { position, positionNftAccount } = await createDammV2Position (
503+ svm ,
504+ user ,
505+ pool
506+ ) ;
507+
508+ const initializeLedgerTx = await initializeLedgerAccount ( user . publicKey ) ;
509+
510+ const totalAmountB = new BN ( LAMPORTS_PER_SOL / 2 ) ;
511+ const setLedgerBalanceTx = await setLedgerBalance (
512+ user . publicKey ,
513+ totalAmountB ,
514+ false
515+ ) ;
516+
517+ const tokenAAccount = getAssociatedTokenAddressSync (
518+ tokenAMint ,
519+ user . publicKey
520+ ) ;
521+ const preTokenABalance = getTokenBalance ( svm , tokenAAccount ) ;
522+ const updateLedgerBalanceAfterSwapTx = await updateLedgerBalanceAfterSwap (
523+ user . publicKey ,
524+ tokenAAccount ,
525+ preTokenABalance ,
526+ U64_MAX ,
527+ true
528+ ) ;
529+
530+ const zapInTx = await zapInDammv2 ( {
531+ svm,
532+ user : user . publicKey ,
533+ pool,
534+ position,
535+ positionNftAccount,
536+ preSqrtPrice : poolState . sqrtPrice ,
537+ maxSqrtPriceChangeBps : U32_MAX . toNumber ( ) ,
538+ } ) ;
539+
540+ const closeLedgerTx = await closeLedgerAccount ( user . publicKey ) ;
541+
542+ const finalTx = new Transaction ( )
543+ . add ( initializeLedgerTx )
544+ . add ( setLedgerBalanceTx )
545+ . add ( updateLedgerBalanceAfterSwapTx )
546+ . add ( zapInTx )
547+ . add ( closeLedgerTx ) ;
548+
549+ finalTx . recentBlockhash = svm . latestBlockhash ( ) ;
550+ finalTx . sign ( user ) ;
551+
552+ const result = svm . sendTransaction ( finalTx ) ;
553+ if ( result instanceof FailedTransactionMetadata ) {
554+ console . log ( result . meta ( ) . logs ( ) ) ;
555+ }
556+ expect ( result ) . instanceOf ( TransactionMetadata ) ;
557+ } ) ;
558+
412559 it ( "zap in without external swap with rate limiter and remaining accounts" , async ( ) => {
413560 const baseFee = encodeFeeRateLimiterParams (
414561 new BN ( 10_000_00 ) , // 1% cliff fee
0 commit comments