Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/sdk-coin-sui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@grpc/grpc-js": "^1.14.4",
"@mysten/bcs": "^0.7.0",
"@mysten/hashi": "0.7.1",
"@mysten/sui": "2.20.0",
"@mysten/sui": "3.1.1",
"@protobuf-ts/grpc-transport": "2.11.1",
"bignumber.js": "^9.0.0",
"bs58": "^4.0.1",
Expand Down
42 changes: 20 additions & 22 deletions modules/sdk-coin-sui/src/lib/grpcClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Structural surface of the GrpcCoreClient namespace of @mysten/sui that the
* Sui SDK's node interactions rely on. Shapes are verified against
* @mysten/sui@2.20.0 (src/grpc/core.ts); the exact version pin in package.json
* @mysten/sui@3.1.1 (dist/grpc/core.d.mts); the exact version pin in package.json
* keeps them stable across upgrades, so no field is silently defaulted —
* helpers below hard-throw when expected fields are missing.
*/
Expand Down Expand Up @@ -77,18 +77,14 @@ function isBrowserEnvironment(): boolean {
return typeof window !== 'undefined' && typeof window.document !== 'undefined';
}

// protobuf-ts service clients hold their transport under a private `_transport`
// field that the structural SuiGrpcClient type cannot express; the shape is
// fixed at the pinned @mysten/sui version.
function transportOf(client: SuiGrpcClient): unknown {
const transportHolder = client as unknown as { transactionExecutionService: { _transport: unknown } };
return transportHolder.transactionExecutionService._transport;
}
// SuiGrpcClient wraps the transport it is given, and @mysten/sui 3.x hands its service clients a
// plain object without `close`, so the SDK keeps a reference to the transport it built.
const clientTransports = new WeakMap<SuiGrpcClient, unknown>();
const clientCache = new Map<string, { client: SuiGrpcClient }>();

/**
* Creates a Sui gRPC client. Node: native transport; browser: gRPC-web
* (SuiGrpcClient's default fetch transport, given the URL as base). One
* (the gRPC-web transport from @mysten/sui/grpc, given the URL as base). One
* client (and connection) is cached per url+network and reused across calls.
*/
export async function createSuiGrpcClient(url: string, network: 'mainnet' | 'testnet'): Promise<SuiGrpcClient> {
Expand All @@ -100,32 +96,29 @@ export async function createSuiGrpcClient(url: string, network: 'mainnet' | 'tes

// TS cannot resolve this ESM-only exports subpath under CJS resolution.
const grpcModule = require('@mysten/sui/grpc') as {
SuiGrpcClient: new (options: {
network: 'mainnet' | 'testnet';
transport?: unknown;
baseUrl?: string;
}) => SuiGrpcClient;
SuiGrpcClient: new (options: { network: 'mainnet' | 'testnet'; transport: unknown }) => SuiGrpcClient;
/** gRPC-web transport; `baseUrl` must be an absolute http(s) URL. */
GrpcWebFetchTransport: new (options: { baseUrl: string }) => unknown;
};

let client: SuiGrpcClient;
let transport: unknown;
if (isBrowserEnvironment()) {
const { SuiGrpcClient } = grpcModule;
client = new SuiGrpcClient({ network, baseUrl: url });
transport = new grpcModule.GrpcWebFetchTransport({ baseUrl: url });
} else {
const [{ GrpcTransport }, { ChannelCredentials }] = await Promise.all([
import('@protobuf-ts/grpc-transport'),
import('@grpc/grpc-js'),
]);
const isTls = !url.startsWith('http://');
const host = url.replace(/^https?:\/\//i, '');
const transport = new GrpcTransport({
transport = new GrpcTransport({
host,
channelCredentials: isTls ? ChannelCredentials.createSsl() : ChannelCredentials.createInsecure(),
timeout: SUI_GRPC_CALL_TIMEOUT_MS,
});
const { SuiGrpcClient: NativeSuiGrpcClient } = grpcModule;
client = new NativeSuiGrpcClient({ network, transport });
}
const client = new grpcModule.SuiGrpcClient({ network, transport });
clientTransports.set(client, transport);
clientCache.set(key, { client });
return client;
}
Expand All @@ -141,9 +134,9 @@ export function closeSuiGrpcClient(client: SuiGrpcClient): void {
clientCache.delete(key);
}
}
const transport = transportOf(client) as { close?: () => void };
const transport = clientTransports.get(client) as { close?: () => void } | undefined;
try {
transport.close?.();
transport?.close?.();
} catch {
// transport already closed
}
Expand All @@ -156,3 +149,8 @@ export function __resetSuiGrpcClientCacheForTest(): void {
}
clientCache.clear();
}

/** Test-only: the transport a client was built with. */
export function __getSuiGrpcTransportForTest(client: SuiGrpcClient): unknown {
return clientTransports.get(client);
}
24 changes: 14 additions & 10 deletions modules/sdk-coin-sui/test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,22 +456,22 @@ describe('Sui util library', function () {
const grpcClientModule = require('../../src/lib/grpcClient');
grpcClientModule.__resetSuiGrpcClientCacheForTest();
});
it('uses the gRPC-web default transport in browser environments', async function () {
const { createSuiGrpcClient } = require('../../src/lib/grpcClient');
it('uses the gRPC-web transport in browser environments', async function () {
const { createSuiGrpcClient, __getSuiGrpcTransportForTest } = require('../../src/lib/grpcClient');
const { GrpcWebFetchTransport } = require('@mysten/sui/grpc');
(global as any).window = { document: {} };
try {
const client = await createSuiGrpcClient('fullnode.testnet.sui.io:443', 'testnet');
const transportName = (client as any).transactionExecutionService._transport.constructor.name;
transportName.should.equal('GrpcWebFetchTransport');
const client = await createSuiGrpcClient('https://fullnode.testnet.sui.io:443', 'testnet');
should(__getSuiGrpcTransportForTest(client)).be.instanceOf(GrpcWebFetchTransport);
} finally {
delete (global as any).window;
}
});
it('uses the native transport in Node environments', async function () {
const { createSuiGrpcClient } = require('../../src/lib/grpcClient');
const { createSuiGrpcClient, __getSuiGrpcTransportForTest } = require('../../src/lib/grpcClient');
const { GrpcTransport } = require('@protobuf-ts/grpc-transport');
const client = await createSuiGrpcClient('fullnode.testnet.sui.io:443', 'testnet');
const transportName = (client as any).transactionExecutionService._transport.constructor.name;
transportName.should.equal('GrpcTransport');
should(__getSuiGrpcTransportForTest(client)).be.instanceOf(GrpcTransport);
});

it('caches one client per url and network', async function () {
Expand All @@ -485,9 +485,13 @@ describe('Sui util library', function () {
});

it('closeSuiGrpcClient evicts the cache and closes the transport', async function () {
const { createSuiGrpcClient, closeSuiGrpcClient } = require('../../src/lib/grpcClient');
const {
createSuiGrpcClient,
closeSuiGrpcClient,
__getSuiGrpcTransportForTest,
} = require('../../src/lib/grpcClient');
const client = await createSuiGrpcClient('fullnode.testnet.sui.io:443', 'testnet');
const transport = (client as any).transactionExecutionService._transport;
const transport = __getSuiGrpcTransportForTest(client);
const closeSpy = sinon.spy(transport, 'close');
closeSuiGrpcClient(client);
sinon.assert.calledOnce(closeSpy);
Expand Down
Loading