From baaff631052d423aa5d8274c6c2bfd528b7230bf Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Sun, 2 Apr 2023 17:34:34 +0000 Subject: [PATCH] refactor(experimental): Introduce a `createDefaultRpc()` method to the library ## Summary You can use this to create a transport with default config. Supply only a URL. ## Test Plan ``` pnpm test:unit:browser pnpm test:unit:node ``` --- packages/library/package.json | 8 +++-- packages/library/src/__tests__/rpc-test.ts | 34 ++++++++++++++++++++++ packages/library/src/index.ts | 1 + packages/library/src/rpc-default-config.ts | 9 ++++++ packages/library/src/rpc.ts | 12 ++++++++ pnpm-lock.yaml | 6 ++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 packages/library/src/__tests__/rpc-test.ts create mode 100644 packages/library/src/rpc-default-config.ts create mode 100644 packages/library/src/rpc.ts diff --git a/packages/library/package.json b/packages/library/package.json index 810308a01e70..b757d048ae4e 100644 --- a/packages/library/package.json +++ b/packages/library/package.json @@ -44,7 +44,9 @@ "test:treeshakability:browser": "agadoo dist/index.browser.js", "test:treeshakability:native": "agadoo dist/index.node.js", "test:treeshakability:node": "agadoo dist/index.native.js", - "test:typecheck": "tsc --noEmit" + "test:typecheck": "tsc --noEmit", + "test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent", + "test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent" }, "author": "Solana Labs Maintainers ", "license": "MIT", @@ -60,10 +62,12 @@ "maintained node versions" ], "dependencies": { - "@solana/keys": "workspace:*" + "@solana/keys": "workspace:*", + "@solana/rpc-transport": "workspace:*" }, "devDependencies": { "@solana/eslint-config-solana": "^0.0.4", + "@solana/rpc-core": "workspace:*", "@swc/core": "^1.3.18", "@swc/jest": "^0.2.23", "@types/jest": "^29.5.0", diff --git a/packages/library/src/__tests__/rpc-test.ts b/packages/library/src/__tests__/rpc-test.ts new file mode 100644 index 000000000000..ec4273336270 --- /dev/null +++ b/packages/library/src/__tests__/rpc-test.ts @@ -0,0 +1,34 @@ +import { createDefaultRpc } from '../rpc'; +import { SolanaJsonRpcIntegerOverflowError } from '../rpc-integer-overflow-error'; + +import { SolanaJsonRpcApi } from '@solana/rpc-core'; +import { Transport } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-transport-types'; + +describe('RPC', () => { + let transport: Transport; + beforeEach(() => { + transport = createDefaultRpc('fake://url'); + }); + describe('with respect to integer overflows', () => { + it('does not throw when called with a value up to `Number.MAX_SAFE_INTEGER`', () => { + expect(() => { + transport.getBlocks(BigInt(Number.MAX_SAFE_INTEGER)); + }).not.toThrow(); + }); + it('does not throw when called with a value up to `-Number.MAX_SAFE_INTEGER`', () => { + expect(() => { + transport.getBlocks(BigInt(-Number.MAX_SAFE_INTEGER)); + }).not.toThrow(); + }); + it('throws when called with a value greater than `Number.MAX_SAFE_INTEGER`', () => { + expect(() => { + transport.getBlocks(BigInt(Number.MAX_SAFE_INTEGER) + 1n); + }).toThrow(SolanaJsonRpcIntegerOverflowError); + }); + it('throws when called with a value less than `-Number.MAX_SAFE_INTEGER`', () => { + expect(() => { + transport.getBlocks(BigInt(-Number.MAX_SAFE_INTEGER) - 1n); + }).toThrow(SolanaJsonRpcIntegerOverflowError); + }); + }); +}); diff --git a/packages/library/src/index.ts b/packages/library/src/index.ts index a7ef1d3903b9..30b54a8f0702 100644 --- a/packages/library/src/index.ts +++ b/packages/library/src/index.ts @@ -1 +1,2 @@ export * from '@solana/keys'; +export * from './rpc'; diff --git a/packages/library/src/rpc-default-config.ts b/packages/library/src/rpc-default-config.ts new file mode 100644 index 000000000000..46d36dd2f0a8 --- /dev/null +++ b/packages/library/src/rpc-default-config.ts @@ -0,0 +1,9 @@ +import { SolanaJsonRpcIntegerOverflowError } from './rpc-integer-overflow-error'; + +import { createJsonRpcTransport } from '@solana/rpc-transport'; + +export const DEFAULT_RPC_CONFIG: Partial[0]> = { + onIntegerOverflow(...args) { + throw new SolanaJsonRpcIntegerOverflowError(...args); + }, +}; diff --git a/packages/library/src/rpc.ts b/packages/library/src/rpc.ts new file mode 100644 index 000000000000..5645244e7c9f --- /dev/null +++ b/packages/library/src/rpc.ts @@ -0,0 +1,12 @@ +import { DEFAULT_RPC_CONFIG } from './rpc-default-config'; + +import { SolanaJsonRpcApi } from '@solana/rpc-core'; +import { createJsonRpcTransport } from '@solana/rpc-transport'; +import { Transport } from '@solana/rpc-transport/dist/types/json-rpc-transport/json-rpc-transport-types'; + +export function createDefaultRpc(url: string): Transport { + return createJsonRpcTransport({ + ...DEFAULT_RPC_CONFIG, + url, + }) as Transport; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 99aefee2d5a5..b9f12e7a93cc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -197,10 +197,16 @@ importers: '@solana/keys': specifier: workspace:* version: link:../keys + '@solana/rpc-transport': + specifier: workspace:* + version: link:../rpc-transport devDependencies: '@solana/eslint-config-solana': specifier: ^0.0.4 version: 0.0.4(@typescript-eslint/eslint-plugin@5.50.0)(@typescript-eslint/parser@5.49.0)(eslint-plugin-jest@27.2.1)(eslint-plugin-react-hooks@4.6.0)(eslint-plugin-sort-keys-fix@1.1.2)(eslint@8.33.0)(typescript@4.9.4) + '@solana/rpc-core': + specifier: workspace:* + version: link:../rpc-core '@swc/core': specifier: ^1.3.18 version: 1.3.32