Skip to content

Commit

Permalink
refactor(experimental): Introduce a createDefaultRpc() method to th…
Browse files Browse the repository at this point in the history
…e 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
```
  • Loading branch information
steveluscher committed Apr 2, 2023
1 parent c79035a commit baaff63
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"license": "MIT",
Expand All @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions packages/library/src/__tests__/rpc-test.ts
Original file line number Diff line number Diff line change
@@ -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<SolanaJsonRpcApi>;
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);
});
});
});
1 change: 1 addition & 0 deletions packages/library/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@solana/keys';
export * from './rpc';
9 changes: 9 additions & 0 deletions packages/library/src/rpc-default-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SolanaJsonRpcIntegerOverflowError } from './rpc-integer-overflow-error';

import { createJsonRpcTransport } from '@solana/rpc-transport';

export const DEFAULT_RPC_CONFIG: Partial<Parameters<typeof createJsonRpcTransport>[0]> = {
onIntegerOverflow(...args) {
throw new SolanaJsonRpcIntegerOverflowError(...args);
},
};
12 changes: 12 additions & 0 deletions packages/library/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -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<SolanaJsonRpcApi> {
return createJsonRpcTransport({
...DEFAULT_RPC_CONFIG,
url,
}) as Transport<SolanaJsonRpcApi>;
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit baaff63

Please sign in to comment.