|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { HttpService } from 'src/shared/services/http.service'; |
| 3 | +import { RealUnitBlockchainService } from '../realunit-blockchain.service'; |
| 4 | + |
| 5 | +// Mock viem |
| 6 | +const mockReadContract = jest.fn(); |
| 7 | +jest.mock('viem', () => ({ |
| 8 | + createPublicClient: jest.fn(() => ({ |
| 9 | + readContract: mockReadContract, |
| 10 | + })), |
| 11 | + http: jest.fn(), |
| 12 | + parseAbi: jest.fn((abi) => abi), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('viem/chains', () => ({ |
| 16 | + sepolia: { id: 11155111, name: 'Sepolia' }, |
| 17 | + mainnet: { id: 1, name: 'Ethereum' }, |
| 18 | + arbitrum: { id: 42161, name: 'Arbitrum' }, |
| 19 | + optimism: { id: 10, name: 'Optimism' }, |
| 20 | + polygon: { id: 137, name: 'Polygon' }, |
| 21 | + base: { id: 8453, name: 'Base' }, |
| 22 | + bsc: { id: 56, name: 'BSC' }, |
| 23 | + gnosis: { id: 100, name: 'Gnosis' }, |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('src/config/config', () => ({ |
| 27 | + GetConfig: jest.fn(() => ({ |
| 28 | + environment: 'loc', |
| 29 | + blockchain: { |
| 30 | + realunit: { |
| 31 | + api: { |
| 32 | + url: 'https://mock-api.example.com', |
| 33 | + key: 'mock-api-key', |
| 34 | + }, |
| 35 | + }, |
| 36 | + sepolia: { |
| 37 | + sepoliaChainId: 11155111, |
| 38 | + sepoliaGatewayUrl: 'https://sepolia.example.com', |
| 39 | + sepoliaApiKey: 'mock-key', |
| 40 | + }, |
| 41 | + ethereum: { |
| 42 | + ethChainId: 1, |
| 43 | + ethGatewayUrl: 'https://mainnet.example.com', |
| 44 | + ethApiKey: 'mock-key', |
| 45 | + }, |
| 46 | + arbitrum: { arbitrumChainId: 42161 }, |
| 47 | + optimism: { optimismChainId: 10 }, |
| 48 | + polygon: { polygonChainId: 137 }, |
| 49 | + base: { baseChainId: 8453 }, |
| 50 | + bsc: { bscChainId: 56 }, |
| 51 | + gnosis: { gnosisChainId: 100 }, |
| 52 | + citrea: { citreaChainId: 0 }, |
| 53 | + citreaTestnet: { citreaTestnetChainId: 0 }, |
| 54 | + }, |
| 55 | + })), |
| 56 | + Config: jest.fn(), |
| 57 | + Environment: { |
| 58 | + DEV: 'dev', |
| 59 | + LOC: 'loc', |
| 60 | + STG: 'stg', |
| 61 | + PRD: 'prd', |
| 62 | + }, |
| 63 | +})); |
| 64 | + |
| 65 | +describe('RealUnitBlockchainService', () => { |
| 66 | + let service: RealUnitBlockchainService; |
| 67 | + let httpService: jest.Mocked<HttpService>; |
| 68 | + |
| 69 | + const MOCK_BROKERBOT_ADDRESS = '0x39c33c2fd5b07b8e890fd2115d4adff7235fc9d2'; |
| 70 | + |
| 71 | + beforeEach(async () => { |
| 72 | + const module: TestingModule = await Test.createTestingModule({ |
| 73 | + providers: [ |
| 74 | + RealUnitBlockchainService, |
| 75 | + { |
| 76 | + provide: HttpService, |
| 77 | + useValue: { |
| 78 | + post: jest.fn(), |
| 79 | + }, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }).compile(); |
| 83 | + |
| 84 | + service = module.get<RealUnitBlockchainService>(RealUnitBlockchainService); |
| 85 | + httpService = module.get(HttpService); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + jest.clearAllMocks(); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('getBrokerbotSellPrice', () => { |
| 93 | + it('should query BrokerBot contract and apply default 0.5% slippage', async () => { |
| 94 | + // BrokerBot returns 1000 ZCHF (in Wei) for 10 shares |
| 95 | + mockReadContract.mockResolvedValue(BigInt('1000000000000000000000')); |
| 96 | + |
| 97 | + const result = await service.getBrokerbotSellPrice(MOCK_BROKERBOT_ADDRESS, 10); |
| 98 | + |
| 99 | + // 1000 ZCHF * (1 - 0.005) = 995 ZCHF |
| 100 | + expect(result.zchfAmountWei).toBe(BigInt('995000000000000000000')); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should calculate correctly for 1 share', async () => { |
| 104 | + // BrokerBot returns 100 ZCHF for 1 share |
| 105 | + mockReadContract.mockResolvedValue(BigInt('100000000000000000000')); |
| 106 | + |
| 107 | + const result = await service.getBrokerbotSellPrice(MOCK_BROKERBOT_ADDRESS, 1); |
| 108 | + |
| 109 | + // 100 * 0.995 = 99.5 ZCHF |
| 110 | + expect(result.zchfAmountWei).toBe(BigInt('99500000000000000000')); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should accept custom slippage in basis points', async () => { |
| 114 | + // BrokerBot returns 1000 ZCHF for 10 shares |
| 115 | + mockReadContract.mockResolvedValue(BigInt('1000000000000000000000')); |
| 116 | + |
| 117 | + const result = await service.getBrokerbotSellPrice(MOCK_BROKERBOT_ADDRESS, 10, 100); // 1% slippage |
| 118 | + |
| 119 | + // 1000 * (1 - 0.01) = 990 ZCHF |
| 120 | + expect(result.zchfAmountWei).toBe(BigInt('990000000000000000000')); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle zero slippage', async () => { |
| 124 | + mockReadContract.mockResolvedValue(BigInt('1000000000000000000000')); |
| 125 | + |
| 126 | + const result = await service.getBrokerbotSellPrice(MOCK_BROKERBOT_ADDRESS, 10, 0); |
| 127 | + |
| 128 | + // Full amount with no slippage |
| 129 | + expect(result.zchfAmountWei).toBe(BigInt('1000000000000000000000')); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should call readContract with correct parameters', async () => { |
| 133 | + mockReadContract.mockResolvedValue(BigInt('100000000000000000000')); |
| 134 | + |
| 135 | + await service.getBrokerbotSellPrice(MOCK_BROKERBOT_ADDRESS, 5); |
| 136 | + |
| 137 | + expect(mockReadContract).toHaveBeenCalledWith( |
| 138 | + expect.objectContaining({ |
| 139 | + address: MOCK_BROKERBOT_ADDRESS, |
| 140 | + functionName: 'getSellPrice', |
| 141 | + args: [BigInt(5)], |
| 142 | + }), |
| 143 | + ); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('getBrokerbotInfo', () => { |
| 148 | + beforeEach(() => { |
| 149 | + httpService.post.mockResolvedValue({ priceInCHF: 100, priceInEUR: 92, availableShares: 500 }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return the passed addresses correctly', async () => { |
| 153 | + const result = await service.getBrokerbotInfo('0xBrokerbot', '0xREALU', '0xZCHF'); |
| 154 | + |
| 155 | + expect(result.brokerbotAddress).toBe('0xBrokerbot'); |
| 156 | + expect(result.tokenAddress).toBe('0xREALU'); |
| 157 | + expect(result.baseCurrencyAddress).toBe('0xZCHF'); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should return price from fetchPrice', async () => { |
| 161 | + httpService.post.mockResolvedValue({ priceInCHF: 123.45, priceInEUR: 114, availableShares: 200 }); |
| 162 | + |
| 163 | + const result = await service.getBrokerbotInfo('0xBB', '0xR', '0xZ'); |
| 164 | + |
| 165 | + expect(result.pricePerShare).toBe('123.45'); |
| 166 | + expect(result.availableShares).toBe(200); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should set buyingEnabled to false when availableShares is 0', async () => { |
| 170 | + httpService.post.mockResolvedValue({ priceInCHF: 100, priceInEUR: 92, availableShares: 0 }); |
| 171 | + |
| 172 | + const result = await service.getBrokerbotInfo('0xBB', '0xR', '0xZ'); |
| 173 | + |
| 174 | + expect(result.buyingEnabled).toBe(false); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should always set sellingEnabled to true', async () => { |
| 178 | + httpService.post.mockResolvedValue({ priceInCHF: 100, priceInEUR: 92, availableShares: 0 }); |
| 179 | + |
| 180 | + const result = await service.getBrokerbotInfo('0xBB', '0xR', '0xZ'); |
| 181 | + |
| 182 | + expect(result.sellingEnabled).toBe(true); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments