Open Creator Rails SDK
TypeScript SDK for interacting with Open Creator Rails
Install
Published on npm as @chainsafe/open-creator-rails-sdk.
pnpm add @chainsafe/open-creator-rails-sdk viem
Quick start
import { createPublicClient, createWalletClient, http } from "viem";
import { sepolia } from "viem/chains";
import { OcrSdk } from "@chainsafe/open-creator-rails-sdk";
const publicClient = createPublicClient({
chain: sepolia,
transport: http(process.env.RPC_URL!),
});
const walletClient = createWalletClient({
chain: sepolia,
transport: http(process.env.RPC_URL!),
account: ,
});
const sdk = new OcrSdk({
publicClient,
walletClient,
registryAddress,
indexerUrl: process.env.INDEXER_URL,
});
API overview
The SDK exposes:
- Namespaced contract APIs
sdk.AssetRegistry.* wraps AssetRegistry contract methods
sdk.Asset.* wraps Asset contract methods
- Bound "Asset client" helper
sdk.getAsset({ assetAddress }) and sdk.getAssetById({ assetId }) return an object that remembers assetAddress.
- Indexer namespace
sdk.indexer.* exposes indexer-only queries that are not possible with simple onchain reads.
Namespaced contract usage
const assetAddress = await sdk.AssetRegistry.getAsset({ assetId });
const exists = await sdk.AssetRegistry.viewAsset({ assetId });
const owner = await sdk.Asset.owner({ assetAddress });
const token = await sdk.Asset.getTokenAddress({ assetAddress });
const asset = sdk.getAsset({ assetAddress });
await asset.setSubscriptionPrice({ newSubscriptionPrice: 123n });
Write methods (require walletClient)
All write methods require walletClient in the SDK config and walletClient.account to be set.
await sdk.AssetRegistry.updateCreatorFeeShare({ creatorFeeShare: 60n });
await sdk.Asset.setSubscriptionPrice({ assetAddress, newSubscriptionPrice: 123n });
await asset.setSubscriptionPrice({ newSubscriptionPrice: 123n });
Indexer support
If you pass indexerUrl, the SDK exposes a dedicated indexer namespace at sdk.indexer.
Indexer namespace (sdk.indexer)
if (!sdk.indexer) throw new Error("indexerUrl not configured");
const sub = await sdk.indexer.getSubscription({ assetAddress, user });
const activeSubs = await sdk.indexer.listSubscriptionsByUser({ user, activeOnly: true });
const assetEntity = await sdk.indexer.getAsset({ assetAddress });
const assetsInRegistry = await sdk.indexer.listAssetsByRegistry({ registryAddress });
Source selection
Some methods accept source?: "auto" | "onchain" | "indexer":
"auto" (default): try indexer (if configured), then fall back to onchain
"onchain": only onchain reads
"indexer": only indexer; throws if the indexer request fails
Indexer-backed reads
These methods are implemented with indexer-first + fallback behavior:
const status = await sdk.getSubscriptionStatus({ assetId, user, source: "auto" });
const status2 = await sdk.Asset.getSubscriptionStatus({ assetAddress, user, source: "auto" });
const owner2 = await sdk.Asset.getOwner({ assetAddress, source: "auto" });
Local Node (Anvil) + SDK Testing
This SDK can be tested end-to-end against a local Anvil chain using the contracts from the open-creator-rails submodule.
Prerequisites
- Install Foundry (provides
anvil + forge).
- Initialize the contracts submodule:
npm run submodule:pull
Run integration tests (recommended)
These tests start Anvil, deploy TestToken and AssetRegistry, run subscribe/getSubscriptionStatus, advance time, and then run claimCreatorFee.
npm run test:integration
Manual debugging (optional)
Start Anvil on the default port:
anvil --chain-id 31337 --port 8545
Then point your SDK clients at it (as shown in the Usage section above) by setting:
export RPC_URL=http://127.0.0.1:8545
Maintenance
Updating git submodules
This repo includes the open-creator-rails submodule (contracts + deployment JSON). When upstream main changes, update the submodule and commit the new submodule SHA value (gitlink) in this repo.
Recommended update workflow
- Pull the latest submodule commits:
npm run submodule:pull
- Commit the updated gitlink SHAs in this repository:
git status
git add open-creator-rails
git commit -m "chore: update submodules"
Publishing to npm
Package: @chainsafe/open-creator-rails-sdk (public, ChainSafe org).
pnpm install
pnpm run build
pnpm run test
pnpm run lint
npm version patch
git push && git push --tags
npm publish --access public
Requires npm login as a user with publish access to the @chainsafe scope. prepack builds dist/ automatically before pack/publish.