|
| 1 | +import { createJsonRpcTransport } from '@solana/rpc-transport'; |
| 2 | +import type { SolanaJsonRpcErrorCode } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-errors'; |
| 3 | +import type { Transport } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-transport-types'; |
| 4 | +import fetchMock from 'jest-fetch-mock-fork'; |
| 5 | +import { createSolanaRpcApi, SolanaRpcMethods } from '../../index'; |
| 6 | +import { Commitment } from '../common'; |
| 7 | +import { Base58EncodedAddress } from '@solana/keys'; |
| 8 | + |
| 9 | +describe('getBalance', () => { |
| 10 | + let transport: Transport<SolanaRpcMethods>; |
| 11 | + beforeEach(() => { |
| 12 | + fetchMock.resetMocks(); |
| 13 | + fetchMock.dontMock(); |
| 14 | + transport = createJsonRpcTransport({ |
| 15 | + api: createSolanaRpcApi(), |
| 16 | + url: 'http://127.0.0.1:8899', |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + (['confirmed', 'finalized', 'processed'] as Commitment[]).forEach(commitment => { |
| 21 | + describe(`when called with \`${commitment}\` commitment`, () => { |
| 22 | + it('returns block production data', async () => { |
| 23 | + expect.assertions(1); |
| 24 | + const blockProductionPromise = transport.getBlockProduction({ commitment }).send(); |
| 25 | + await expect(blockProductionPromise).resolves.toMatchObject({ |
| 26 | + value: expect.objectContaining({ |
| 27 | + byIdentity: expect.toBeObject(), |
| 28 | + range: expect.objectContaining({ |
| 29 | + firstSlot: expect.any(BigInt), |
| 30 | + lastSlot: expect.any(BigInt), |
| 31 | + }), |
| 32 | + }), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('has the latest context slot as the last slot', async () => { |
| 37 | + expect.assertions(1); |
| 38 | + const blockProductionPromise = transport.getBlockProduction({ commitment }).send(); |
| 39 | + await expect(blockProductionPromise).resolves.toSatisfy( |
| 40 | + rpcResponse => rpcResponse.context.slot === rpcResponse.value.range.lastSlot |
| 41 | + ); |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('when called with a single identity', () => { |
| 47 | + // Currently this call always returns just one identity in tests, so no way to meaningfully test this |
| 48 | + it.todo('returns data for just that identity'); |
| 49 | + |
| 50 | + it('returns an empty byIdentity if the identity is not a block producer', async () => { |
| 51 | + expect.assertions(1); |
| 52 | + // Randomly generated address, assumed not to be a block producer |
| 53 | + const identity = '9NmqDDZa7mH1DBM4zeq9cm7VcRn2un1i2TwuMvjBoVhU' as Base58EncodedAddress; |
| 54 | + const blockProductionPromise = transport.getBlockProduction({ identity }).send(); |
| 55 | + await expect(blockProductionPromise).resolves.toMatchObject({ |
| 56 | + value: expect.objectContaining({ |
| 57 | + byIdentity: expect.toBeEmptyObject(), |
| 58 | + }), |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('when called with a `lastSlot` higher than the highest slot available', () => { |
| 64 | + it('throws an error', async () => { |
| 65 | + expect.assertions(1); |
| 66 | + const blockProductionPromise = transport |
| 67 | + .getBlockProduction({ |
| 68 | + range: { |
| 69 | + firstSlot: 0n, |
| 70 | + lastSlot: 2n ** 63n - 1n, // u64:MAX; safe bet it'll be too high. |
| 71 | + }, |
| 72 | + }) |
| 73 | + .send(); |
| 74 | + await expect(blockProductionPromise).rejects.toMatchObject({ |
| 75 | + code: -32602 satisfies (typeof SolanaJsonRpcErrorCode)['JSON_RPC_SERVER_ERROR_LAST_SLOT_TOO_LARGE'], |
| 76 | + message: expect.any(String), |
| 77 | + name: 'SolanaJsonRpcError', |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments