Using sacrifice-utils
sacrifice-utils is the shared package of the Altar repository. The backend, the app, and these docs all use it. It holds:
- Zod schemas and types for every request and response of the API (
sacrifice-utils/api). - Points math with no database: decay steps, the rate multiplier, the early-deposit rule, and the credit points of a deposit (
sacrifice-utils/decay-calculation). - Network helpers, such as
parseNetwork('evm:1')(sacrifice-utils/network). - Campaign configuration schemas, presets, and checks (
sacrifice-utils/campaign-config).
The package is a private workspace package of the repository. Inside the repository, depend on it with "sacrifice-utils": "*". It needs zod (version 4) and viem. It exports TypeScript source, so your bundler must compile it.
Import paths
Section titled “Import paths”sacrifice-utilssacrifice-utils/idsacrifice-utils/disclaimersacrifice-utils/price-source-configsacrifice-utils/decay-calculationsacrifice-utils/usdsacrifice-utils/moneysacrifice-utils/sanitize-token-textsacrifice-utils/networksacrifice-utils/typessacrifice-utils/pointssacrifice-utils/apisacrifice-utils/campaignsacrifice-utils/campaign-configsacrifice-utils/campaign-curvesacrifice-utils/campaign-presetssacrifice-utils/campaign-dimensionssacrifice-utils/campaign-catalogsacrifice-utils/campaign-address-readinesssacrifice-utils/consentsacrifice-utils/address-setsacrifice-utils/monero-daemonsacrifice-utils/monero-daemon-pool
Examples
Section titled “Examples”The code below is the real file src/lib/sacrifice-utils-examples.ts of the docs package. Its tests run each example against the library.
/** * TypeScript examples shown on "Using sacrifice-utils". The page imports this file as raw text * and `sacrifice-utils-examples.test.ts` runs it, so each example is checked against the library. */import { ProjectsResponseSchema, type FullProject } from 'sacrifice-utils/api'import { calculateSacrificePoints, getCampaignRateMultiplier } from 'sacrifice-utils/decay-calculation'import { parseNetwork } from 'sacrifice-utils/network'
/** Read `GET /projects` and check its shape. Throws a Zod error when the server sends something else. */export const fetchProjects = async (baseUrl = 'https://altar.fund/api/v1'): Promise<FullProject[]> => { const response = await fetch(`${baseUrl}/projects`) return ProjectsResponseSchema.parse(await response.json())}
/** * The rate multiplier the app shows for a campaign at one moment, as a number such as 1.0625. * It is the inverse of the decay factor: 1.00 before the inflection, then it rises each step, * and it stops at the end time. */export const currentRateMultiplier = (project: FullProject, nowMilliseconds: number): number => { const scaled = getCampaignRateMultiplier(nowMilliseconds, project) return Number(scaled) / 1e18}
/** * Credit points of one deposit, in whole points, with the same rule the backend applies. * * @param options.scaledUsdValue - Deposit value in dollars, scaled by 10^18. * @param options.depositMilliseconds - Deposit time in milliseconds since the Unix epoch. */export const creditPointsForDeposit = (options: { project: FullProject scaledUsdValue: bigint depositMilliseconds: number}): bigint => calculateSacrificePoints({ scaledUsdValue: options.scaledUsdValue, usdToPointBaseRate: BigInt(options.project.usdToPointBaseRate), timestamp: options.depositMilliseconds, parameters: options.project, })
/** Split a network key such as `evm:1` into its family and chain identifier. */export const readNetworkKey = (key: string) => { const { network, error } = parseNetwork(key) if (error) throw new Error(error) return network}- Request shapes use
bigintString. A request schema reads{ "__type__": "bigint", "value": "123" }and gives you abigint. A response schema usually reads strings. - Points math uses scaled integers.
calculateSacrificePointstakes the dollar value and the base rate scaled by 10^18 and returns points scaled by 10^18. All functions usebigint, never floating point. - One source of truth. The backend uses the same decay and early-deposit functions to award points. A test in the backend compares
calculateSacrificePointswith the backendcalcSacrificeCreditsfor many times and settings. - Zod extension order. If you generate OpenAPI from these schemas with
@asteasolutions/zod-to-openapi, extend Zod before you import the schemas. Zod 4 attaches.openapi()when a schema is created. The Altar generator does this (seepackages/backend/src/openapi/build-document.ts).