Skip to content

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.

  • sacrifice-utils
  • sacrifice-utils/id
  • sacrifice-utils/disclaimer
  • sacrifice-utils/price-source-config
  • sacrifice-utils/decay-calculation
  • sacrifice-utils/usd
  • sacrifice-utils/money
  • sacrifice-utils/sanitize-token-text
  • sacrifice-utils/network
  • sacrifice-utils/types
  • sacrifice-utils/points
  • sacrifice-utils/api
  • sacrifice-utils/campaign
  • sacrifice-utils/campaign-config
  • sacrifice-utils/campaign-curve
  • sacrifice-utils/campaign-presets
  • sacrifice-utils/campaign-dimensions
  • sacrifice-utils/campaign-catalog
  • sacrifice-utils/campaign-address-readiness
  • sacrifice-utils/consent
  • sacrifice-utils/address-set
  • sacrifice-utils/monero-daemon
  • sacrifice-utils/monero-daemon-pool

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.

sacrifice-utils-examples.ts
/**
* 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 a bigint. A response schema usually reads strings.
  • Points math uses scaled integers. calculateSacrificePoints takes the dollar value and the base rate scaled by 10^18 and returns points scaled by 10^18. All functions use bigint, 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 calculateSacrificePoints with the backend calcSacrificeCredits for 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 (see packages/backend/src/openapi/build-document.ts).