@@ -5,6 +5,7 @@ import type {
55 SuiTransactionBlockResponse ,
66 SuiTransactionBlockKind ,
77 PaginatedTransactionResponse ,
8+ SuiObjectResponse ,
89} from "@mysten/sui/client" ;
910import { BigNumber } from "bignumber.js" ;
1011import coinConfig from "../config" ;
@@ -2273,3 +2274,96 @@ describe("getCoinsForAmount", () => {
22732274 } ) ;
22742275 } ) ;
22752276} ) ;
2277+
2278+ describe ( "withBatchedMultiGetObjects" , ( ) => {
2279+ const createMockClient = ( ) => {
2280+ const multiGetObjects = jest . fn (
2281+ async ( params : { ids : string [ ] ; options ?: Record < string , boolean > } ) => {
2282+ if ( params . ids . length > 50 ) {
2283+ throw new Error ( "Input exceeds limit of 50" ) ;
2284+ }
2285+ return params . ids . map (
2286+ id =>
2287+ ( {
2288+ data : {
2289+ objectId : id ,
2290+ version : "1" ,
2291+ digest : `digest-${ id } ` ,
2292+ } ,
2293+ } ) as SuiObjectResponse ,
2294+ ) ;
2295+ } ,
2296+ ) ;
2297+ return { client : { multiGetObjects } as unknown as SuiClient , multiGetObjects } ;
2298+ } ;
2299+
2300+ it ( "should pass through when <= 50 objects" , async ( ) => {
2301+ // GIVEN
2302+ const { client, multiGetObjects } = createMockClient ( ) ;
2303+ const ids = Array . from ( { length : 30 } , ( _ , i ) => `0xobj${ i } ` ) ;
2304+
2305+ // WHEN
2306+ const batched = sdk . withBatchedMultiGetObjects ( client ) ;
2307+ const result = await batched . multiGetObjects ( { ids, options : { showBcs : true } } ) ;
2308+
2309+ // THEN
2310+ expect ( multiGetObjects ) . toHaveBeenCalledTimes ( 1 ) ;
2311+ expect ( result ) . toHaveLength ( 30 ) ;
2312+ } ) ;
2313+
2314+ it ( "should batch when > 50 objects" , async ( ) => {
2315+ // GIVEN
2316+ const { client, multiGetObjects } = createMockClient ( ) ;
2317+ const ids = Array . from ( { length : 120 } , ( _ , i ) => `0xobj${ i } ` ) ;
2318+
2319+ // WHEN
2320+ const batched = sdk . withBatchedMultiGetObjects ( client ) ;
2321+ const result = await batched . multiGetObjects ( { ids, options : { showBcs : true } } ) ;
2322+
2323+ // THEN
2324+ expect ( multiGetObjects ) . toHaveBeenCalledTimes ( 3 ) ;
2325+ expect ( multiGetObjects ) . toHaveBeenNthCalledWith ( 1 , {
2326+ ids : ids . slice ( 0 , 50 ) ,
2327+ options : { showBcs : true } ,
2328+ } ) ;
2329+ expect ( multiGetObjects ) . toHaveBeenNthCalledWith ( 2 , {
2330+ ids : ids . slice ( 50 , 100 ) ,
2331+ options : { showBcs : true } ,
2332+ } ) ;
2333+ expect ( multiGetObjects ) . toHaveBeenNthCalledWith ( 3 , {
2334+ ids : ids . slice ( 100 , 120 ) ,
2335+ options : { showBcs : true } ,
2336+ } ) ;
2337+ expect ( result ) . toHaveLength ( 120 ) ;
2338+ expect ( result [ 0 ] . data ?. objectId ) . toBe ( "0xobj0" ) ;
2339+ expect ( result [ 119 ] . data ?. objectId ) . toBe ( "0xobj119" ) ;
2340+ } ) ;
2341+
2342+ it ( "should handle exactly 50 objects without batching" , async ( ) => {
2343+ // GIVEN
2344+ const { client, multiGetObjects } = createMockClient ( ) ;
2345+ const ids = Array . from ( { length : 50 } , ( _ , i ) => `0xobj${ i } ` ) ;
2346+
2347+ // WHEN
2348+ const batched = sdk . withBatchedMultiGetObjects ( client ) ;
2349+ const result = await batched . multiGetObjects ( { ids } ) ;
2350+
2351+ // THEN
2352+ expect ( multiGetObjects ) . toHaveBeenCalledTimes ( 1 ) ;
2353+ expect ( result ) . toHaveLength ( 50 ) ;
2354+ } ) ;
2355+
2356+ it ( "should handle exactly 51 objects with batching" , async ( ) => {
2357+ // GIVEN
2358+ const { client, multiGetObjects } = createMockClient ( ) ;
2359+ const ids = Array . from ( { length : 51 } , ( _ , i ) => `0xobj${ i } ` ) ;
2360+
2361+ // WHEN
2362+ const batched = sdk . withBatchedMultiGetObjects ( client ) ;
2363+ const result = await batched . multiGetObjects ( { ids } ) ;
2364+
2365+ // THEN
2366+ expect ( multiGetObjects ) . toHaveBeenCalledTimes ( 2 ) ;
2367+ expect ( result ) . toHaveLength ( 51 ) ;
2368+ } ) ;
2369+ } ) ;
0 commit comments