|
| 1 | +import BigNumber from 'bignumber.js'; |
| 2 | +import { Response } from 'cross-fetch'; |
| 3 | +import { mocked } from 'ts-jest/utils'; |
| 4 | + |
| 5 | +import { ResponseError } from '../../../../src/basic/request/exceptions'; |
| 6 | +import { RestfulRequest } from '../../../../src/basic/request/restful'; |
| 7 | +import { BlockBook } from '../../../../src/provider/chains/btc'; |
| 8 | +import { TransactionStatus } from '../../../../src/types/provider'; |
| 9 | + |
| 10 | +export const okResponse = (data: unknown) => |
| 11 | + Promise.resolve(new Response(JSON.stringify(data))); |
| 12 | + |
| 13 | +jest.mock('../../../../src/basic/request/restful'); |
| 14 | +const mockRestfulClass = mocked(RestfulRequest, true); |
| 15 | + |
| 16 | +const blockbook = new BlockBook('https://myblockbook.com/api'); |
| 17 | +const [restful] = mockRestfulClass.mock.instances as Array<any>; |
| 18 | + |
| 19 | +test('getInfo', async () => { |
| 20 | + restful.get.mockReturnValueOnce( |
| 21 | + okResponse({ |
| 22 | + backend: { |
| 23 | + blocks: 724263, |
| 24 | + }, |
| 25 | + }), |
| 26 | + ); |
| 27 | + |
| 28 | + await expect(blockbook.getInfo()).resolves.toStrictEqual({ |
| 29 | + bestBlockNumber: 724263, |
| 30 | + isReady: true, |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +test('getAddress', async () => { |
| 35 | + restful.get.mockReturnValueOnce( |
| 36 | + okResponse({ |
| 37 | + address: '12dRugNcdxK39288NjcDV4GX7rMsKCGn6B', |
| 38 | + unconfirmedBalance: '1000', |
| 39 | + balance: '35000000000', |
| 40 | + txs: 12044, |
| 41 | + }), |
| 42 | + ); |
| 43 | + |
| 44 | + await expect( |
| 45 | + blockbook.getAddress('12dRugNcdxK39288NjcDV4GX7rMsKCGn6B'), |
| 46 | + ).resolves.toStrictEqual({ |
| 47 | + balance: new BigNumber('35000001000'), |
| 48 | + existing: true, |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +test('getFeePricePerUnit', async () => { |
| 53 | + restful.get |
| 54 | + .mockReturnValueOnce( |
| 55 | + okResponse({ |
| 56 | + result: '0.000024', |
| 57 | + }), |
| 58 | + ) |
| 59 | + .mockReturnValueOnce( |
| 60 | + okResponse({ |
| 61 | + result: '0.000036', |
| 62 | + }), |
| 63 | + ) |
| 64 | + .mockReturnValueOnce( |
| 65 | + okResponse({ |
| 66 | + result: '0.000012', |
| 67 | + }), |
| 68 | + ); |
| 69 | + |
| 70 | + await expect(blockbook.getFeePricePerUnit()).resolves.toStrictEqual({ |
| 71 | + normal: { price: new BigNumber('2.4'), waitingBlock: 5 }, |
| 72 | + others: [ |
| 73 | + { price: new BigNumber('1.2'), waitingBlock: 20 }, |
| 74 | + { price: new BigNumber('3.6'), waitingBlock: 1 }, |
| 75 | + ], |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +test('getFeePricePerUnit - planB', async () => { |
| 80 | + restful.get |
| 81 | + .mockReturnValueOnce( |
| 82 | + okResponse({ |
| 83 | + result: '0.000024', |
| 84 | + }), |
| 85 | + ) |
| 86 | + .mockRejectedValueOnce(undefined) |
| 87 | + .mockRejectedValueOnce(undefined); |
| 88 | + |
| 89 | + await expect(blockbook.getFeePricePerUnit()).resolves.toStrictEqual({ |
| 90 | + normal: { price: new BigNumber('2.4'), waitingBlock: 5 }, |
| 91 | + others: [ |
| 92 | + { price: new BigNumber('1.44'), waitingBlock: 20 }, |
| 93 | + { price: new BigNumber('3.84'), waitingBlock: 1 }, |
| 94 | + ], |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +test('getTransactionStatus', async () => { |
| 99 | + restful.get |
| 100 | + .mockReturnValueOnce( |
| 101 | + okResponse({ |
| 102 | + txid: 'txid1', |
| 103 | + confirmations: 1, |
| 104 | + }), |
| 105 | + ) |
| 106 | + .mockReturnValueOnce( |
| 107 | + okResponse({ |
| 108 | + txid: 'txid2', |
| 109 | + confirmations: 0, |
| 110 | + }), |
| 111 | + ) |
| 112 | + .mockReturnValueOnce( |
| 113 | + Promise.reject( |
| 114 | + new ResponseError( |
| 115 | + '', |
| 116 | + new Response( |
| 117 | + JSON.stringify({ |
| 118 | + error: 'txid3 not found', |
| 119 | + }), |
| 120 | + { status: 404 }, |
| 121 | + ), |
| 122 | + ), |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + await expect(blockbook.getTransactionStatus('txid1')).resolves.toBe( |
| 127 | + TransactionStatus.CONFIRM_AND_SUCCESS, |
| 128 | + ); |
| 129 | + await expect(blockbook.getTransactionStatus('txid2')).resolves.toBe( |
| 130 | + TransactionStatus.PENDING, |
| 131 | + ); |
| 132 | + await expect(blockbook.getTransactionStatus('txid3')).resolves.toBe( |
| 133 | + TransactionStatus.NOT_FOUND, |
| 134 | + ); |
| 135 | +}); |
| 136 | + |
| 137 | +test('broadcastTransaction', async () => { |
| 138 | + restful.get |
| 139 | + .mockReturnValueOnce( |
| 140 | + okResponse({ |
| 141 | + result: |
| 142 | + '2222222222222222222222222222222222222222222222222222222222222222', |
| 143 | + }), |
| 144 | + ) |
| 145 | + .mockReturnValueOnce( |
| 146 | + Promise.reject( |
| 147 | + new ResponseError( |
| 148 | + '', |
| 149 | + new Response( |
| 150 | + JSON.stringify({ |
| 151 | + error: 'Transaction already in block chain', |
| 152 | + }), |
| 153 | + { status: 400 }, |
| 154 | + ), |
| 155 | + ), |
| 156 | + ), |
| 157 | + ); |
| 158 | + |
| 159 | + await expect(blockbook.broadcastTransaction('rawTx1')).resolves.toBe(true); |
| 160 | + await expect(blockbook.broadcastTransaction('rawTx2')).rejects.toThrow( |
| 161 | + 'Transaction already in block', |
| 162 | + ); |
| 163 | +}); |
0 commit comments