Skip to content

Commit

Permalink
rpc-web3js: Allow passing Connection class directly (#96)
Browse files Browse the repository at this point in the history
* rpc-web3js: Allow Connection class directly

* add to createUmi & allow chunk size param

* changeset

* Update rpc.md

* merge doc code blocks
  • Loading branch information
mPaella authored Nov 6, 2023
1 parent 0b99645 commit c96d94f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .changeset/hip-wasps-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@metaplex-foundation/umi-rpc-chunk-get-accounts': patch
'@metaplex-foundation/umi-bundle-defaults': patch
'@metaplex-foundation/umi-rpc-web3js': patch
---

Allow instancing umi/RPCInterface with Connection class or RPC url
9 changes: 8 additions & 1 deletion docs/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ Contacting the Solana blockchain via an RPC is an important part of any decentra

## Configuring the RPC's endpoint

When creating a new Umi instance via the default bundle, you must pass the RPC's endpoint as the first argument. Going forward, this it the endpoint that will be used every time you call a method on the RPC interface.
When creating a new Umi instance via the default bundle, you must pass the RPC's endpoint or an instance of `@solana/web3.js`'s `Connection` class as the first argument. Going forward, this is the endpoint or `Connection` that will be used every time you call a method on the RPC interface.

```ts
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { Connection } from '@solana/web3.js';

// Pass in your RPC endpoint.
const umi = createUmi("https://api.mainnet-beta.solana.com");

// Or an explicit Connection instance from web3.js.
const umi = createUmi(new Connection("https://api.mainnet-beta.solana.com"));
```

Alternatively, you may set or update the RPC implementation explicitly by the using the plugin they provide. For instance, the `web3JsRpc` plugin will set the RPC implementation to use the `@solana/web3.js` library.

```ts
import { web3JsRpc } from '@metaplex-foundation/umi-rpc-web3js';
import { Connection } from '@solana/web3.js';

umi.use(web3JsRpc("https://api.mainnet-beta.solana.com"));
umi.use(web3JsRpc(new Connection("https://api.mainnet-beta.solana.com")));
```

## Getting the RPC's endpoint and cluster
Expand Down
22 changes: 19 additions & 3 deletions packages/umi-bundle-defaults/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { Umi, createUmi as baseCreateUmi } from '@metaplex-foundation/umi';
import type { ChunkGetAccountsRpcOptions } from '@metaplex-foundation/umi-rpc-chunk-get-accounts';
import type { Web3JsRpcOptions } from '@metaplex-foundation/umi-rpc-web3js';
import type { Connection as Web3JsConnection } from '@solana/web3.js';
import { defaultPlugins } from './plugin';

export const createUmi = (
export function createUmi(
endpoint: string,
rpcOptions?: Web3JsRpcOptions
): Umi => baseCreateUmi().use(defaultPlugins(endpoint, rpcOptions));
rpcOptions?: Web3JsRpcOptions & ChunkGetAccountsRpcOptions
): Umi;
export function createUmi(
connection: Web3JsConnection,
rpcOptions?: ChunkGetAccountsRpcOptions
): Umi;
export function createUmi(
endpointOrConnection: string | Web3JsConnection,
rpcOptions?: Web3JsRpcOptions & ChunkGetAccountsRpcOptions
): Umi {
return baseCreateUmi().use(
typeof endpointOrConnection === 'string'
? defaultPlugins(endpointOrConnection, rpcOptions)
: defaultPlugins(endpointOrConnection, rpcOptions)
);
}

export * from './plugin';
48 changes: 33 additions & 15 deletions packages/umi-bundle-defaults/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,40 @@ import {
web3JsRpc,
Web3JsRpcOptions,
} from '@metaplex-foundation/umi-rpc-web3js';
import { chunkGetAccountsRpc } from '@metaplex-foundation/umi-rpc-chunk-get-accounts';
import {
chunkGetAccountsRpc,
ChunkGetAccountsRpcOptions,
} from '@metaplex-foundation/umi-rpc-chunk-get-accounts';
import { dataViewSerializer } from '@metaplex-foundation/umi-serializer-data-view';
import { web3JsTransactionFactory } from '@metaplex-foundation/umi-transaction-factory-web3js';
import type { Connection as Web3JsConnection } from '@solana/web3.js';

export const defaultPlugins = (
export function defaultPlugins(
endpoint: string,
rpcOptions?: Web3JsRpcOptions & { getAccountsChunkSize?: number }
): UmiPlugin => ({
install(umi) {
umi.use(dataViewSerializer());
umi.use(defaultProgramRepository());
umi.use(fetchHttp());
umi.use(httpDownloader());
umi.use(web3JsEddsa());
umi.use(web3JsRpc(endpoint, rpcOptions));
umi.use(chunkGetAccountsRpc(rpcOptions?.getAccountsChunkSize));
umi.use(web3JsTransactionFactory());
},
});
rpcOptions?: Web3JsRpcOptions & ChunkGetAccountsRpcOptions
): UmiPlugin;
export function defaultPlugins(
connection: Web3JsConnection,
rpcOptions?: ChunkGetAccountsRpcOptions
): UmiPlugin;
export function defaultPlugins(
endpointOrConnection: string | Web3JsConnection,
rpcOptions?: Web3JsRpcOptions & ChunkGetAccountsRpcOptions
): UmiPlugin {
return {
install(umi) {
umi.use(dataViewSerializer());
umi.use(defaultProgramRepository());
umi.use(fetchHttp());
umi.use(httpDownloader());
umi.use(web3JsEddsa());
umi.use(
typeof endpointOrConnection === 'string'
? web3JsRpc(endpointOrConnection, rpcOptions)
: web3JsRpc(endpointOrConnection)
);
umi.use(chunkGetAccountsRpc(rpcOptions?.getAccountsChunkSize));
umi.use(web3JsTransactionFactory());
},
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { RpcInterface, chunk } from '@metaplex-foundation/umi';

export interface ChunkGetAccountsRpcOptions {
getAccountsChunkSize?: number;
}

export const createChunkGetAccountsRpc = (
rpc: RpcInterface,
chunkSize = 100
Expand Down
22 changes: 18 additions & 4 deletions packages/umi-rpc-web3js/src/createWeb3JsRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,31 @@ export function createWeb3JsRpc(
context: Pick<Context, 'programs' | 'transactions'>,
endpoint: string,
rpcOptions?: Web3JsRpcOptions
): RpcInterface & { connection: Web3JsConnection };
export function createWeb3JsRpc(
context: Pick<Context, 'programs' | 'transactions'>,
connection: Web3JsConnection
): RpcInterface & { connection: Web3JsConnection };
export function createWeb3JsRpc(
context: Pick<Context, 'programs' | 'transactions'>,
endpointOrConnection: string | Web3JsConnection,
rpcOptions?: Web3JsRpcOptions
): RpcInterface & { connection: Web3JsConnection } {
const cluster = resolveClusterFromEndpoint(endpoint);

let connection: Web3JsConnection | null = null;
const getConnection = () => {
if (!connection) {
connection = new Web3JsConnection(endpoint, rpcOptions);
if (connection) {
return connection;
}
if (typeof endpointOrConnection === 'string') {
connection = new Web3JsConnection(endpointOrConnection, rpcOptions);
} else {
connection = endpointOrConnection;
}
return connection;
};

const cluster = resolveClusterFromEndpoint(getConnection().rpcEndpoint);

const getAccount = async (
publicKey: PublicKey,
options: RpcGetAccountOptions = {}
Expand Down
23 changes: 17 additions & 6 deletions packages/umi-rpc-web3js/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { UmiPlugin } from '@metaplex-foundation/umi';
import type { Connection as Web3JsConnection } from '@solana/web3.js';
import { createWeb3JsRpc, Web3JsRpcOptions } from './createWeb3JsRpc';

export const web3JsRpc = (
export function web3JsRpc(
endpoint: string,
rpcOptions?: Web3JsRpcOptions
): UmiPlugin => ({
install(umi) {
umi.rpc = createWeb3JsRpc(umi, endpoint, rpcOptions);
},
});
): UmiPlugin;
export function web3JsRpc(connection: Web3JsConnection): UmiPlugin;
export function web3JsRpc(
endpointOrConnection: string | Web3JsConnection,
rpcOptions?: Web3JsRpcOptions
): UmiPlugin {
return {
install(umi) {
umi.rpc =
typeof endpointOrConnection === 'string'
? createWeb3JsRpc(umi, endpointOrConnection, rpcOptions)
: createWeb3JsRpc(umi, endpointOrConnection);
},
};
}

0 comments on commit c96d94f

Please sign in to comment.