Skip to content

Commit

Permalink
Merge pull request #383 from oasisprotocol/CedarMist/viem-node-hangs
Browse files Browse the repository at this point in the history
clients/viem: use interval for routine fetching, unref intervalId
  • Loading branch information
CedarMist authored Sep 10, 2024
2 parents aa5ea27 + c34b198 commit 87bba83
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion integrations/viem-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lint": "biome check .",
"format": "biome format --write .",
"clean": "rm -rf dist",
"test": "vitest --run",
"test": "vitest --run && timeout -k 5s 2s pnpm ts-node ./scripts/test.js",
"build": "npm run build:cjs && npm run build:esm && npm run build:types",
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./dist/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./dist/_cjs/package.json && node scripts/rename-cjs.js",
"build:esm": "tsc --project ./tsconfig.build.json --module es2015 --outDir ./dist/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./dist/_esm/package.json",
Expand Down
32 changes: 32 additions & 0 deletions integrations/viem-v2/scripts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
This script verifies that when Viem is used using Node via the CLI it won't
prevent Node from exiting cleanly.
See: https://github.com/oasisprotocol/sapphire-paratime/pull/383
*/

import {
createSapphireSerializer,
sapphireHttpTransport,
sapphireLocalnet,
} from "@oasisprotocol/sapphire-viem-v2";
import { createPublicClient, defineChain } from "viem";

const transport = sapphireHttpTransport();
const chain = sapphireLocalnet;
const publicClient = createPublicClient({ chain, transport });
defineChain({
id: 0x5afd,
name: "Oasis Sapphire Localnet",
network: "sapphire-localnet",
nativeCurrency: { name: "Sapphire Local Rose", symbol: "TEST", decimals: 18 },
rpcUrls: {
default: {
http: ["http://localhost:8545"],
},
},
serializers: {
transaction: await createSapphireSerializer(publicClient),
},
testnet: true,
});
14 changes: 12 additions & 2 deletions integrations/viem-v2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,23 @@ export async function createSapphireSerializer<
return originalSerializer;
}

// As the serialized is synchronous, fetching keys while running
// As the serializer is synchronous, fetching keys while running
const fetcher = new KeyFetcher();
const provider = client as EthereumProvider;
await fetcher.fetch(provider);
setTimeout(async () => {

// The fetcher runs in the background, routinely fetching the keys
// This means when the serializer requests a calldata public key one will
// have been retrieved pre-emptively.
const intervalId: NodeJS.Timeout | number = setInterval(async () => {
await fetcher.fetch(provider);
}, fetcher.timeoutMilliseconds);
// The interval ID is unreferenced to prevent Node from hanging at exit
// See discussion on https://github.com/oasisprotocol/sapphire-paratime/pull/379
// This is only available in NodeJS, and not in browsers
if (typeof intervalId.unref === "function") {
intervalId.unref();
}

const wrappedSerializer = ((tx, sig?) => {
if (!sig) {
Expand Down

0 comments on commit 87bba83

Please sign in to comment.