@ensdomains/ccip-read-router
A lightweight package for routing CCIP-Read requests, using itty-router.
Getting started
Install with:
bun add @ensdomains/ccip-read-router itty-router viem
Basic usage
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
const router = CcipReadRouter();
router.add({
type: "function bar(uint256) pure returns (uint256)",
handle: async ([x]) => {
return [x * 2n];
},
});
Customisation
Most customisation is inherited from itty-router.
Base URL
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
const router = CcipReadRouter({ base: "/ccip-read" });
CORS
From itty-router docs:
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
import { cors } from "itty-router";
const { preflight, corsify } = cors();
const router = CcipReadRouter({
before: [preflight],
finally: [corsify],
});
Runtimes
Cloudflare Workers
Just export the router from your worker file.
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
const router = CcipReadRouter();
router.add({
type: "function bar(uint256) pure returns (uint256)",
handle: async ([x]) => {
return [x * 2n];
},
});
export default { ...router };
Bun
Initialise the router with the port you want to use, and export.
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
const router = CcipReadRouter({ port: 3001 });
router.add({
type: "function bar(uint256) pure returns (uint256)",
handle: async ([x]) => {
return [x * 2n];
},
});
export default router;
Node usage
Use the @whatwg-node/server adapter.
import { CcipReadRouter } from "@ensdomains/ccip-read-router";
import { createServerAdapter } from "@whatwg-node/server";
import { createServer } from "http";
const router = CcipReadRouter();
router.add({
type: "function bar(uint256) pure returns (uint256)",
handle: async ([x]) => {
return [x * 2n];
},
});
const ccipReadServer = createServerAdapter(router.fetch);
const httpServer = createServer(ccipReadServer);
httpServer.listen(3001);
Other runtimes
See the runtime guides section of the itty-router docs.