@chipsonchips/shared

v0.1.1

Published

Shared wallet-bridge protocol, hub connector, and ChipsBank contract bindings for ChipsOnChips games.

Readme

@chipsonchips/shared

Shared code for the ChipsOnChips hub and its games.

Source: https://github.com/chipsonchips/chips-shared Package: https://www.npmjs.com/package/@chipsonchips/shared

Installation

pnpm add @chipsonchips/shared

Consumers pin a version range (currently ^0.1.0) in their own package.json rather than linking to this repo locally — see Publishing below for how version bumps flow out to games.

Modules

  • @chipsonchips/shared/wallet-bridge — the postMessage EIP-1193 protocol (protocol.ts) and the chipsHubConnector wagmi connector used by games. The hub's WalletBridgeHost also imports the protocol types from here. Deliberately does not export a useHubAutoConnect hook: this package bundles its own copy of wagmi/react, so a hook that calls useAccount/useConnect from here would read a different WagmiContext than the one each game's own <WagmiProvider> creates — the classic duplicate-module-instance problem, and it fails at runtime, not at typecheck. Each game keeps a small local useHubAutoConnect (see below) that calls its own wagmi's hooks and only imports the context-free bits (CHIPS_HUB_CONNECTOR_ID, getHubBridgeProvider) from here.
  • @chipsonchips/shared/chains — chain ids, default USDC addresses, explorer URLs. Deployed ChipsBank addresses stay in each app's own env vars (they differ per environment).
  • @chipsonchips/shared/abi — CHIPS_BANK_ABI, generated from the Foundry build artifact in chipschips/contracts. Regenerate when the contract changes; don't hand-edit.

Usage in a game (Vite or Next.js)

import { chipsHubConnector } from "@chipsonchips/shared/wallet-bridge";

// wagmi config
connectors: [
  chipsHubConnector({ allowedOrigins: [/* your hub origin(s) */] }),
  // ...your other connectors
],

allowedOrigins is read by the caller, not this package, because env var access differs by bundler (Next.js process.env.NEXT_PUBLIC_* vs Vite import.meta.env.VITE_*).

Pin viem and typescript to the exact versions this package uses (currently [email protected], [email protected] — see this package's devDependencies), not semver ranges. chipsHubConnector's return type is a wagmi CreateConnectorFn generic over viem's Chain/Client types; since this package is not part of a workspace with its consumers, each consumer gets its own physical copy of viem, and TypeScript treats two differently-versioned (even patch-level) or differently-toolchain-compiled copies as structurally incompatible ("Two different types with this name exist, but they are unrelated"). Exact version + exact typescript version match keeps them structurally identical. wagmi itself has not shown this issue in practice but pin it exactly too if it ever does.

For auto-connect, keep a small local hook per game (copy from any existing game, e.g. spaceship/frontend/components/auth/HubAutoConnect.tsx) that imports useAccount/useConnect from your own wagmi and only CHIPS_HUB_CONNECTOR_ID / getHubBridgeProvider from this package:

import { useAccount, useConnect } from "wagmi"; // your app's own wagmi
import {
  CHIPS_HUB_CONNECTOR_ID,
  getHubBridgeProvider,
} from "@chipsonchips/shared/wallet-bridge";

Usage in the hub

import {
  BRIDGE_SOURCE,
  BRIDGE_VERSION,
  isBridgeMessage,
  type BridgeRequest,
  type ParentToChildMessage,
} from "@chipsonchips/shared/wallet-bridge";

Regenerating the ABI

From chipschips/contracts, after forge build:

python3 -c "
import json
abi = json.load(open('out/ChipsBank.sol/ChipsBank.json'))['abi']
print(json.dumps(abi, indent=2))
"

Paste the result into src/abi/chipsBank.ts (wrapped in export const CHIPS_BANK_ABI = [...] as const;), bump the package version, publish, and bump the version in each consumer.

Publishing

Requires membership in the chipsonchips npm organization (create it once at npmjs.com if it doesn't exist yet — free tier supports public scoped packages) and npm login on the publishing machine.

pnpm build
npm publish --access public

--access public is required the first time: scoped packages (@scope/name) default to private, which requires a paid org plan. Subsequent publishes of an already-public package don't need the flag, but it doesn't hurt to keep it.

Bump version in package.json first (semver). Consumers pin a version range in their own package.json — a ChipsBank redeploy or protocol change becomes an explicit pnpm up @chipsonchips/shared in each repo instead of a silent copy-paste that can drift.