1- import { describe , it , beforeAll } from "vitest" ;
1+ import { describe , it , beforeAll , expect } from "vitest" ;
22import type { Address } from "@solana/kit" ;
33import { setupAta , setupMint } from "./utils/token" ;
44import {
@@ -21,6 +21,7 @@ import assert from "assert";
2121import {
2222 getInitializableTickIndex ,
2323 priceToTickIndex ,
24+ tickIndexToPrice ,
2425} from "@orca-so/whirlpools-core" ;
2526import { resetPositionRangeInstructions } from "../src/resetPositionRange" ;
2627import { decreaseLiquidityInstructions } from "../src/decreaseLiquidity" ;
@@ -165,6 +166,73 @@ describe("Reset Position Range Instructions", () => {
165166 ) ;
166167 } ;
167168
169+ const testResetPositionRangeShouldFailWhenPositionIsNotEmpty = async (
170+ poolName : string ,
171+ positionName : string ,
172+ ) => {
173+ const positionMintAddress = positions . get ( positionName ) ! ;
174+
175+ // Try to reset the position range with new price range, and assert will be executed.
176+ await expect (
177+ resetPositionRangeInstructions (
178+ rpc ,
179+ positionMintAddress ,
180+ 1000 ,
181+ 5000 ,
182+ signer ,
183+ ) ,
184+ ) . rejects . toThrow ( "Position must be empty" ) ;
185+ } ;
186+
187+ const testResetPositionRangeShouldFailWithSameTickIndex = async (
188+ poolName : string ,
189+ positionName : string ,
190+ ) => {
191+ const positionMintAddress = positions . get ( positionName ) ! ;
192+ const positionAddress = await getPositionAddress ( positionMintAddress ) ;
193+ const position = await fetchPosition ( rpc , positionAddress [ 0 ] ) ;
194+ const whirlpool = await fetchWhirlpool ( rpc , position . data . whirlpool ) ;
195+ const [ mintA , mintB ] = await fetchAllMint ( rpc , [
196+ whirlpool . data . tokenMintA ,
197+ whirlpool . data . tokenMintB ,
198+ ] ) ;
199+
200+ // 1. Try to reset the position range with same tick index
201+ const lowerPrice = tickIndexToPrice (
202+ position . data . tickLowerIndex ,
203+ mintA . data . decimals ,
204+ mintB . data . decimals ,
205+ ) ;
206+ const upperPrice = tickIndexToPrice (
207+ position . data . tickUpperIndex ,
208+ mintA . data . decimals ,
209+ mintB . data . decimals ,
210+ ) ;
211+ const { instructions : resetInstructions } =
212+ await resetPositionRangeInstructions (
213+ rpc ,
214+ positionMintAddress ,
215+ lowerPrice ,
216+ upperPrice ,
217+ signer ,
218+ ) ;
219+
220+ // 2. Send the transaction and expect it to fail
221+ await expect ( sendTransaction ( resetInstructions ) ) . rejects . toThrow ( "0x17ac" ) ;
222+ } ;
223+
224+ for ( const poolName of poolTypes . keys ( ) ) {
225+ for ( const positionTypeName of positionTypes . keys ( ) ) {
226+ const positionName = `${ poolName } ${ positionTypeName } ` ;
227+ it ( `Should fail to reset a position for ${ positionName } when position is not empty` , async ( ) => {
228+ await testResetPositionRangeShouldFailWhenPositionIsNotEmpty (
229+ poolName ,
230+ positionName ,
231+ ) ;
232+ } ) ;
233+ }
234+ }
235+
168236 for ( const poolName of poolTypes . keys ( ) ) {
169237 for ( const positionTypeName of positionTypes . keys ( ) ) {
170238 const positionName = `${ poolName } ${ positionTypeName } ` ;
@@ -173,4 +241,16 @@ describe("Reset Position Range Instructions", () => {
173241 } ) ;
174242 }
175243 }
244+
245+ for ( const poolName of poolTypes . keys ( ) ) {
246+ for ( const positionTypeName of positionTypes . keys ( ) ) {
247+ const positionName = `${ poolName } ${ positionTypeName } ` ;
248+ it ( `Should fail to reset a position for ${ positionName } when tick index is the same` , async ( ) => {
249+ await testResetPositionRangeShouldFailWithSameTickIndex (
250+ poolName ,
251+ positionName ,
252+ ) ;
253+ } ) ;
254+ }
255+ }
176256} ) ;
0 commit comments