@@ -10,11 +10,8 @@ import type {
1010 VaultInfo ,
1111 VaultBalance ,
1212} from '@mycelium-sdk/core/types/protocols/general' ;
13- import type {
14- ProxyVaults ,
15- ProxyBalance ,
16- OperationCallDataType ,
17- } from '@mycelium-sdk/core/types/protocols/proxy' ;
13+ import type { ProxyVaults , ProxyBalance } from '@mycelium-sdk/core/types/protocols/proxy' ;
14+ import type { TransactionData } from '@mycelium-sdk/core/types/transaction' ;
1815import { encodeFunctionData , erc20Abi , parseUnits , type Address , type Hash } from 'viem' ;
1916
2017// Mock viem functions
@@ -178,7 +175,7 @@ describe('ProxyProtocol integration tests', () => {
178175 BigInt ( '2000000000' ) , // Allowance is greater than deposit amount
179176 ) ;
180177
181- const mockOperationData : OperationCallDataType = {
178+ const mockOperationData : TransactionData = {
182179 to : mockVaultInfo . vaultAddress ,
183180 data : '0x1234' as `0x${string } `,
184181 } ;
@@ -220,7 +217,7 @@ describe('ProxyProtocol integration tests', () => {
220217 const mockApproveData = '0xhash123' as `0x${string } `;
221218 vi . mocked ( encodeFunctionData ) . mockReturnValue ( mockApproveData ) ;
222219
223- const mockOperationData : OperationCallDataType = {
220+ const mockOperationData : TransactionData = {
224221 to : mockVaultInfo . vaultAddress ,
225222 data : '0x1234' as `0x${string } `,
226223 } ;
@@ -289,7 +286,7 @@ describe('ProxyProtocol integration tests', () => {
289286 BigInt ( '2000000000' ) ,
290287 ) ;
291288
292- const mockOperationData : OperationCallDataType = {
289+ const mockOperationData : TransactionData = {
293290 to : mockVaultInfo . vaultAddress ,
294291 data : '0x1234' as `0x${string } `,
295292 } ;
@@ -318,6 +315,63 @@ describe('ProxyProtocol integration tests', () => {
318315 } ) ,
319316 ) ;
320317 } ) ;
318+
319+ it ( 'should deposit with paymaster token when paymaster token equals deposit token' , async ( ) => {
320+ const paymasterToken = mockVaultInfo . tokenAddress ;
321+ const mockPublicClient = chainManager . getPublicClient ( 8453 ) ;
322+
323+ // Mock balance check (for gas reserve) and allowance check
324+ vi . mocked ( mockPublicClient . readContract as ReturnType < typeof vi . fn > )
325+ . mockResolvedValueOnce ( BigInt ( '2000000000' ) )
326+ . mockResolvedValueOnce ( BigInt ( '2000000000' ) ) ;
327+
328+ const mockOperationData : TransactionData = {
329+ to : mockVaultInfo . vaultAddress ,
330+ data : '0x1234' as `0x${string } `,
331+ } ;
332+
333+ ( apiClient . sendRequest as ReturnType < typeof vi . fn > )
334+ . mockResolvedValueOnce ( {
335+ success : true ,
336+ data : mockOperationData ,
337+ } )
338+ . mockResolvedValueOnce ( {
339+ success : true ,
340+ } ) ;
341+
342+ ( smartWallet . sendBatch as ReturnType < typeof vi . fn > ) . mockResolvedValue ( '0xhash123' as Hash ) ;
343+
344+ const result = await proxyProtocol . deposit ( mockVaultInfo , '1000' , smartWallet , {
345+ paymasterToken,
346+ } ) ;
347+
348+ expect ( mockPublicClient . readContract ) . toHaveBeenCalledWith ( {
349+ address : mockVaultInfo . tokenAddress ,
350+ abi : erc20Abi ,
351+ functionName : 'balanceOf' ,
352+ args : [ expect . any ( String ) ] ,
353+ } ) ;
354+
355+ expect ( smartWallet . sendBatch ) . toHaveBeenCalledWith ( [ mockOperationData ] , 8453 , {
356+ paymasterToken,
357+ } ) ;
358+ expect ( result . success ) . toBe ( true ) ;
359+ expect ( result . hash ) . toBe ( '0xhash123' ) ;
360+ } ) ;
361+
362+ it ( 'should throw error when deposit amount exceeds balance minus gas reserve' , async ( ) => {
363+ const paymasterToken = mockVaultInfo . tokenAddress ;
364+ const mockPublicClient = chainManager . getPublicClient ( 8453 ) ;
365+
366+ // Mock balance that's too low for gas reserve
367+ vi . mocked ( mockPublicClient . readContract as ReturnType < typeof vi . fn > ) . mockResolvedValue (
368+ BigInt ( '500000000' ) ,
369+ ) ;
370+
371+ await expect (
372+ proxyProtocol . deposit ( mockVaultInfo , '1000' , smartWallet , { paymasterToken } ) ,
373+ ) . rejects . toThrow ( 'Insufficient balance. Must reserve tokens for gas payment.' ) ;
374+ } ) ;
321375 } ) ;
322376
323377 describe ( 'withdraw' , ( ) => {
@@ -335,7 +389,7 @@ describe('ProxyProtocol integration tests', () => {
335389
336390 vi . mocked ( smartWallet . getEarnBalances ) . mockResolvedValue ( mockEarningBalances ) ;
337391
338- const mockOperationData : OperationCallDataType = {
392+ const mockOperationData : TransactionData = {
339393 to : mockVaultInfo . vaultAddress ,
340394 data : '0xhash123' as `0x${string } `,
341395 } ;
@@ -374,7 +428,7 @@ describe('ProxyProtocol integration tests', () => {
374428
375429 vi . mocked ( smartWallet . getEarnBalances ) . mockResolvedValue ( mockEarningBalances ) ;
376430
377- const mockOperationData : OperationCallDataType = {
431+ const mockOperationData : TransactionData = {
378432 to : mockVaultInfo . vaultAddress ,
379433 data : '0xhash123' as `0x${string } `,
380434 } ;
@@ -454,7 +508,7 @@ describe('ProxyProtocol integration tests', () => {
454508
455509 vi . mocked ( smartWallet . getEarnBalances ) . mockResolvedValue ( mockEarningBalances ) ;
456510
457- const mockOperationData : OperationCallDataType = {
511+ const mockOperationData : TransactionData = {
458512 to : mockVaultInfo . vaultAddress ,
459513 data : '0xhash123' as `0x${string } `,
460514 } ;
@@ -483,6 +537,78 @@ describe('ProxyProtocol integration tests', () => {
483537 } ) ,
484538 ) ;
485539 } ) ;
540+
541+ it ( 'should withdraw with paymaster token when paymaster token equals withdraw token' , async ( ) => {
542+ const paymasterToken = mockVaultInfo . tokenAddress ;
543+ const mockPublicClient = chainManager . getPublicClient ( 8453 ) ;
544+ const mockEarningBalances : VaultBalance [ ] = [
545+ {
546+ vaultInfo : mockVaultInfo ,
547+ balance : mockProxyBalance ,
548+ } ,
549+ ] ;
550+
551+ vi . mocked ( smartWallet . getEarnBalances ) . mockResolvedValue ( mockEarningBalances ) ;
552+
553+ // Mock balance check for gas reserve
554+ vi . mocked ( mockPublicClient . readContract as ReturnType < typeof vi . fn > ) . mockResolvedValue (
555+ BigInt ( '2000000000' ) ,
556+ ) ;
557+
558+ const mockOperationData : TransactionData = {
559+ to : mockVaultInfo . vaultAddress ,
560+ data : '0xhash123' as `0x${string } `,
561+ } ;
562+
563+ ( apiClient . sendRequest as ReturnType < typeof vi . fn > )
564+ . mockResolvedValueOnce ( {
565+ success : true ,
566+ data : mockOperationData ,
567+ } )
568+ . mockResolvedValueOnce ( {
569+ success : true ,
570+ } ) ;
571+
572+ ( smartWallet . send as ReturnType < typeof vi . fn > ) . mockResolvedValue ( '0xhash456' as Hash ) ;
573+
574+ const result = await proxyProtocol . withdraw ( mockVaultInfo , '500' , smartWallet , {
575+ paymasterToken,
576+ } ) ;
577+
578+ // Verify balance check was performed
579+ expect ( mockPublicClient . readContract ) . toHaveBeenCalledWith ( {
580+ address : mockVaultInfo . tokenAddress ,
581+ abi : erc20Abi ,
582+ functionName : 'balanceOf' ,
583+ args : [ expect . any ( String ) ] ,
584+ } ) ;
585+
586+ expect ( smartWallet . send ) . toHaveBeenCalledWith ( mockOperationData , 8453 , { paymasterToken } ) ;
587+ expect ( result . success ) . toBe ( true ) ;
588+ expect ( result . hash ) . toBe ( '0xhash456' ) ;
589+ } ) ;
590+
591+ it ( 'should throw error when wallet balance is insufficient for gas payment' , async ( ) => {
592+ const paymasterToken = mockVaultInfo . tokenAddress ;
593+ const mockPublicClient = chainManager . getPublicClient ( 8453 ) ;
594+ const mockEarningBalances : VaultBalance [ ] = [
595+ {
596+ vaultInfo : mockVaultInfo ,
597+ balance : mockProxyBalance ,
598+ } ,
599+ ] ;
600+
601+ vi . mocked ( smartWallet . getEarnBalances ) . mockResolvedValue ( mockEarningBalances ) ;
602+
603+ // Mock balance that's too low for gas reserve
604+ vi . mocked ( mockPublicClient . readContract as ReturnType < typeof vi . fn > ) . mockResolvedValue (
605+ BigInt ( '0' ) ,
606+ ) ;
607+
608+ await expect (
609+ proxyProtocol . withdraw ( mockVaultInfo , '500' , smartWallet , { paymasterToken } ) ,
610+ ) . rejects . toThrow ( 'Insufficient wallet balance for gas payment' ) ;
611+ } ) ;
486612 } ) ;
487613
488614 describe ( 'getBalances' , ( ) => {
0 commit comments