|
| 1 | +import { createMock } from '@golevelup/ts-jest'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { PimlicoBundlerService } from 'src/integration/blockchain/shared/evm/paymaster/pimlico-bundler.service'; |
| 4 | +import { PimlicoPaymasterService } from 'src/integration/blockchain/shared/evm/paymaster/pimlico-paymaster.service'; |
| 5 | +import { BlockchainRegistryService } from 'src/integration/blockchain/shared/services/blockchain-registry.service'; |
| 6 | +import { CryptoService } from 'src/integration/blockchain/shared/services/crypto.service'; |
| 7 | +import { AssetService } from 'src/shared/models/asset/asset.service'; |
| 8 | +import { TestSharedModule } from 'src/shared/utils/test.shared.module'; |
| 9 | +import { TestUtil } from 'src/shared/utils/test.util'; |
| 10 | +import { RouteService } from 'src/subdomains/core/route/route.service'; |
| 11 | +import { TransactionUtilService } from 'src/subdomains/core/transaction/transaction-util.service'; |
| 12 | +import { UserDataService } from 'src/subdomains/generic/user/models/user-data/user-data.service'; |
| 13 | +import { UserService } from 'src/subdomains/generic/user/models/user/user.service'; |
| 14 | +import { DepositService } from 'src/subdomains/supporting/address-pool/deposit/deposit.service'; |
| 15 | +import { PayInService } from 'src/subdomains/supporting/payin/services/payin.service'; |
| 16 | +import { TransactionHelper } from 'src/subdomains/supporting/payment/services/transaction-helper'; |
| 17 | +import { TransactionRequestService } from 'src/subdomains/supporting/payment/services/transaction-request.service'; |
| 18 | +import { BuyCryptoWebhookService } from '../../../process/services/buy-crypto-webhook.service'; |
| 19 | +import { BuyCryptoService } from '../../../process/services/buy-crypto.service'; |
| 20 | +import { SwapRepository } from '../swap.repository'; |
| 21 | +import { SwapService } from '../swap.service'; |
| 22 | + |
| 23 | +describe('SwapService', () => { |
| 24 | + let service: SwapService; |
| 25 | + |
| 26 | + let swapRepo: SwapRepository; |
| 27 | + let userService: UserService; |
| 28 | + let userDataService: UserDataService; |
| 29 | + let depositService: DepositService; |
| 30 | + let assetService: AssetService; |
| 31 | + let payInService: PayInService; |
| 32 | + let buyCryptoService: BuyCryptoService; |
| 33 | + let buyCryptoWebhookService: BuyCryptoWebhookService; |
| 34 | + let transactionUtilService: TransactionUtilService; |
| 35 | + let routeService: RouteService; |
| 36 | + let transactionHelper: TransactionHelper; |
| 37 | + let cryptoService: CryptoService; |
| 38 | + let transactionRequestService: TransactionRequestService; |
| 39 | + let blockchainRegistryService: BlockchainRegistryService; |
| 40 | + let pimlicoPaymasterService: PimlicoPaymasterService; |
| 41 | + let pimlicoBundlerService: PimlicoBundlerService; |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + swapRepo = createMock<SwapRepository>(); |
| 45 | + userService = createMock<UserService>(); |
| 46 | + userDataService = createMock<UserDataService>(); |
| 47 | + depositService = createMock<DepositService>(); |
| 48 | + assetService = createMock<AssetService>(); |
| 49 | + payInService = createMock<PayInService>(); |
| 50 | + buyCryptoService = createMock<BuyCryptoService>(); |
| 51 | + buyCryptoWebhookService = createMock<BuyCryptoWebhookService>(); |
| 52 | + transactionUtilService = createMock<TransactionUtilService>(); |
| 53 | + routeService = createMock<RouteService>(); |
| 54 | + transactionHelper = createMock<TransactionHelper>(); |
| 55 | + cryptoService = createMock<CryptoService>(); |
| 56 | + transactionRequestService = createMock<TransactionRequestService>(); |
| 57 | + blockchainRegistryService = createMock<BlockchainRegistryService>(); |
| 58 | + pimlicoPaymasterService = createMock<PimlicoPaymasterService>(); |
| 59 | + pimlicoBundlerService = createMock<PimlicoBundlerService>(); |
| 60 | + |
| 61 | + const module: TestingModule = await Test.createTestingModule({ |
| 62 | + imports: [TestSharedModule], |
| 63 | + providers: [ |
| 64 | + SwapService, |
| 65 | + { provide: SwapRepository, useValue: swapRepo }, |
| 66 | + { provide: UserService, useValue: userService }, |
| 67 | + { provide: UserDataService, useValue: userDataService }, |
| 68 | + { provide: DepositService, useValue: depositService }, |
| 69 | + { provide: AssetService, useValue: assetService }, |
| 70 | + { provide: PayInService, useValue: payInService }, |
| 71 | + { provide: BuyCryptoService, useValue: buyCryptoService }, |
| 72 | + { provide: BuyCryptoWebhookService, useValue: buyCryptoWebhookService }, |
| 73 | + { provide: TransactionUtilService, useValue: transactionUtilService }, |
| 74 | + { provide: RouteService, useValue: routeService }, |
| 75 | + { provide: TransactionHelper, useValue: transactionHelper }, |
| 76 | + { provide: CryptoService, useValue: cryptoService }, |
| 77 | + { provide: TransactionRequestService, useValue: transactionRequestService }, |
| 78 | + { provide: BlockchainRegistryService, useValue: blockchainRegistryService }, |
| 79 | + { provide: PimlicoPaymasterService, useValue: pimlicoPaymasterService }, |
| 80 | + { provide: PimlicoBundlerService, useValue: pimlicoBundlerService }, |
| 81 | + TestUtil.provideConfig(), |
| 82 | + ], |
| 83 | + }).compile(); |
| 84 | + |
| 85 | + service = module.get<SwapService>(SwapService); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should be defined', () => { |
| 89 | + expect(service).toBeDefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('createDepositTx', () => { |
| 93 | + const mockRequest = { |
| 94 | + id: 1, |
| 95 | + sourceId: 100, |
| 96 | + amount: 10, |
| 97 | + user: { address: '0x1234567890123456789012345678901234567890' }, |
| 98 | + }; |
| 99 | + |
| 100 | + const mockRoute = { |
| 101 | + id: 1, |
| 102 | + deposit: { address: '0x0987654321098765432109876543210987654321' }, |
| 103 | + }; |
| 104 | + |
| 105 | + const mockAsset = { |
| 106 | + id: 100, |
| 107 | + blockchain: 'Ethereum', |
| 108 | + }; |
| 109 | + |
| 110 | + const mockUnsignedTx = { |
| 111 | + to: '0x0987654321098765432109876543210987654321', |
| 112 | + data: '0xabcdef', |
| 113 | + value: '0', |
| 114 | + chainId: 1, |
| 115 | + }; |
| 116 | + |
| 117 | + beforeEach(() => { |
| 118 | + jest.spyOn(assetService, 'getAssetById').mockResolvedValue(mockAsset as any); |
| 119 | + jest.spyOn(blockchainRegistryService, 'getEvmClient').mockReturnValue({ |
| 120 | + prepareTransaction: jest.fn().mockResolvedValue({ ...mockUnsignedTx }), |
| 121 | + chainId: 1, |
| 122 | + } as any); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should NOT include eip5792 when includeEip5792 is false (default)', async () => { |
| 126 | + jest.spyOn(pimlicoPaymasterService, 'isPaymasterAvailable').mockReturnValue(true); |
| 127 | + jest.spyOn(pimlicoPaymasterService, 'getBundlerUrl').mockReturnValue('https://api.pimlico.io/test'); |
| 128 | + |
| 129 | + const result = await service.createDepositTx(mockRequest as any, mockRoute as any); |
| 130 | + |
| 131 | + expect(result).toBeDefined(); |
| 132 | + expect(result.eip5792).toBeUndefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should NOT include eip5792 when includeEip5792 is explicitly false', async () => { |
| 136 | + jest.spyOn(pimlicoPaymasterService, 'isPaymasterAvailable').mockReturnValue(true); |
| 137 | + jest.spyOn(pimlicoPaymasterService, 'getBundlerUrl').mockReturnValue('https://api.pimlico.io/test'); |
| 138 | + |
| 139 | + const result = await service.createDepositTx(mockRequest as any, mockRoute as any, false); |
| 140 | + |
| 141 | + expect(result).toBeDefined(); |
| 142 | + expect(result.eip5792).toBeUndefined(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should include eip5792 when includeEip5792 is true and paymaster available', async () => { |
| 146 | + jest.spyOn(pimlicoPaymasterService, 'isPaymasterAvailable').mockReturnValue(true); |
| 147 | + jest.spyOn(pimlicoPaymasterService, 'getBundlerUrl').mockReturnValue('https://api.pimlico.io/test'); |
| 148 | + |
| 149 | + const result = await service.createDepositTx(mockRequest as any, mockRoute as any, true); |
| 150 | + |
| 151 | + expect(result).toBeDefined(); |
| 152 | + expect(result.eip5792).toBeDefined(); |
| 153 | + expect(result.eip5792.paymasterUrl).toBe('https://api.pimlico.io/test'); |
| 154 | + expect(result.eip5792.chainId).toBe(1); |
| 155 | + expect(result.eip5792.calls).toHaveLength(1); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should NOT include eip5792 when includeEip5792 is true but paymaster not available', async () => { |
| 159 | + jest.spyOn(pimlicoPaymasterService, 'isPaymasterAvailable').mockReturnValue(false); |
| 160 | + jest.spyOn(pimlicoPaymasterService, 'getBundlerUrl').mockReturnValue(undefined); |
| 161 | + |
| 162 | + const result = await service.createDepositTx(mockRequest as any, mockRoute as any, true); |
| 163 | + |
| 164 | + expect(result).toBeDefined(); |
| 165 | + expect(result.eip5792).toBeUndefined(); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments