Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ contracts. The addresses emitted by the deploy script are authoritative.
preconditions are the first lines a reader sees.
- **Doc comments.** Every public entrypoint has one, in OpenZeppelin's style: a
one-line summary, then `# Arguments` with units (asset base units, share base
units, WAD prices, seconds or ledgers), `# Errors`, `# Events` and
units, 14-decimal prices, seconds or ledgers), `# Errors`, `# Events` and
`# Authorization`. Every error variant's doc says when it happens. Keep them
short: they are stored in the Wasm as part of the contract spec, which keeps
1,024 bytes of each doc and drops the rest without a warning.
Expand Down
6 changes: 3 additions & 3 deletions app-lib/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ describe("formatExact", () => {

describe("formatExact with a minimum fraction width", () => {
it("pads a short price to the minimum", () => {
const value = 11n * 10n ** 17n
const value = 11n * 10n ** 13n
expect(formatExact(value as Price, PRICE_DECIMALS, 4)).toBe("1.1000")
})

it("keeps every significant digit beyond the minimum", () => {
const value = 1_041_234_560_000_000_000n
const value = 104_123_456_000_000n
expect(formatExact(value as Price, PRICE_DECIMALS, 4)).toBe("1.04123456")
})
})
Expand Down Expand Up @@ -226,7 +226,7 @@ describe("formatDayMonth", () => {
})

it("rejects a Price formatted with AMOUNT_DECIMALS at compile time", () => {
const price = 1_000000000000000000n as Price
const price = 1_00000000000000n as Price
// @ts-expect-error a Price must be formatted with PRICE_DECIMALS, not AMOUNT_DECIMALS
expect(typeof formatUnits(price, AMOUNT_DECIMALS)).toBe("string")
})
6 changes: 4 additions & 2 deletions app-lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ export function networkStatus(
}

export const AMOUNT_DECIMALS = 7 as const
export const PRICE_DECIMALS = 18 as const
export const PRICE_DECIMALS = 14 as const
/** A share price of one asset base unit per share base unit. */
export const PRICE_SCALE = 10n ** BigInt(PRICE_DECIMALS)
export type Decimals = typeof AMOUNT_DECIMALS | typeof PRICE_DECIMALS

declare const scale: unique symbol
/** A bigint branded with its decimal scale, so an `Amount` and a `Price` can't be swapped by mistake. */
export type Scaled<D extends Decimals> = bigint & { readonly [scale]: D }
/** Asset units and share counts, scaled by `AMOUNT_DECIMALS`. */
export type Amount = Scaled<typeof AMOUNT_DECIMALS>
/** WAD share price, scaled by `PRICE_DECIMALS`. */
/** Share price, scaled by `PRICE_DECIMALS`. */
export type Price = Scaled<typeof PRICE_DECIMALS>

/**
Expand Down
10 changes: 5 additions & 5 deletions app/src/components/admin/ConfigurationSection.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PRICE_SCALE } from "@stellar-scaffold/app-lib"
import { act, fireEvent, render, screen, within } from "@testing-library/react"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { type Grant } from "../../hooks/useAdminAuthority"
Expand Down Expand Up @@ -75,7 +76,6 @@ const CUSTODIAN = "CACAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAINCW"
const COMPLIANCE = "CACQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQLC2U"
const VERIFIER = "CADAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMSST"
const DAY = 86_400n
const PRICE_ONE = 10n ** 18n

const grantOf = (role: string): Grant => ({
role,
Expand Down Expand Up @@ -103,8 +103,8 @@ const READY: ConfigurationState = {
cooldown: 3_600n,
maxUpBps: 1_000,
maxDownBps: 500,
min: PRICE_ONE / 2n,
max: 2n * PRICE_ONE,
min: PRICE_SCALE / 2n,
max: 2n * PRICE_SCALE,
},
compliance: COMPLIANCE,
identityVerifier: VERIFIER,
Expand Down Expand Up @@ -370,8 +370,8 @@ describe("ConfigurationSection", () => {
cooldown_secs: 3_600n,
max_up_bps: 500,
max_down_bps: 500,
min_answer: PRICE_ONE / 2n,
max_answer: 2n * PRICE_ONE,
min_answer: PRICE_SCALE / 2n,
max_answer: 2n * PRICE_SCALE,
},
oracleAdmin,
)
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/admin/CycleSection.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Amount, type Price } from "@stellar-scaffold/app-lib"
import { type Amount, type Price, PRICE_SCALE } from "@stellar-scaffold/app-lib"
import { act, fireEvent, render, screen, within } from "@testing-library/react"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { type ActivateWindDownStatus } from "../../hooks/useActivateWindDown"
Expand Down Expand Up @@ -108,7 +108,7 @@ vi.mock("../../hooks/useDepositBalance", () => ({
}))

const amount = (whole: bigint) => (whole * 10_000_000n) as Amount
const price = (whole: bigint) => (whole * 10n ** 18n) as Price
const price = (whole: bigint) => (whole * PRICE_SCALE) as Price

const sealed: EpochRecord = {
id: 4n,
Expand Down Expand Up @@ -540,7 +540,7 @@ describe("CycleSection", () => {

expect(attestMock.submit).toHaveBeenCalledWith(
{
price: 1_040_000_000_000_000_000n,
price: 104_000_000_000_000n,
freshness: 86_400n,
proofHash: PROOF_HASH,
proofUri: "https://example.com/nav.pdf",
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/vault/transactionCopy/cycle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe("TransactionModal, pricing an epoch", () => {
it("shows the share price the vault settled at once confirmed, to four places", () => {
renderFulfillEpochModal({
status: "confirmed",
sharePrice: 1_250_000_000_000_000_000n as Price,
sharePrice: 125_000_000_000_000n as Price,
hash: "b".repeat(64),
})

Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useAttest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ vi.mock("../config/clients", () => ({
}))

const attestation = {
price: 1_040_000_000_000_000_000n as Price,
price: 104_000_000_000_000n as Price,
freshness: 86_400n,
proofHash: "ab".repeat(32),
proofUri: "https://example.com/nav.pdf",
Expand Down
13 changes: 7 additions & 6 deletions app/src/hooks/useCycleState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type NavReport,
type OracleState,
type Price,
PRICE_SCALE,
} from "@stellar-scaffold/app-lib"
import { waitFor } from "@testing-library/react"
import { beforeEach, describe, expect, it, vi } from "vitest"
Expand Down Expand Up @@ -76,7 +77,7 @@ const unreadable = { kind: "unreadable" } as const

const report: NavReport = {
expires_at: 1_700_136_000n,
nav_per_share: 10n ** 18n,
nav_per_share: PRICE_SCALE,
accepted_at: 1_700_050_000n,
proof_hash: Buffer.alloc(32, 0xab),
proof_uri: "https://example.com/nav.pdf",
Expand All @@ -87,8 +88,8 @@ const config = {
cooldown_secs: 3_600n,
max_up_bps: 500,
max_down_bps: 500,
min_answer: 5n * 10n ** 17n,
max_answer: 2n * 10n ** 18n,
min_answer: PRICE_SCALE / 2n,
max_answer: 2n * PRICE_SCALE,
}

const state = (tag: OracleState["tag"]): ContractRead<OracleState> =>
Expand All @@ -98,7 +99,7 @@ describe("toOracle", () => {
it("carries the report, the limits and no NAV pause while the price is valid", () => {
expect(toOracle(state("Valid"), value(report), value(config))).toEqual({
state: "valid",
price: (10n ** 18n) as Price,
price: PRICE_SCALE as Price,
attestedAt: 1_700_050_000n,
expiresAt: 1_700_136_000n,
proof: { hash: "ab".repeat(32), uri: "https://example.com/nav.pdf" },
Expand All @@ -109,8 +110,8 @@ describe("toOracle", () => {
cooldown: 3_600n,
maxUpBps: 500,
maxDownBps: 500,
min: 5n * 10n ** 17n,
max: 2n * 10n ** 18n,
min: PRICE_SCALE / 2n,
max: 2n * PRICE_SCALE,
},
})
})
Expand Down
4 changes: 2 additions & 2 deletions app/src/hooks/useEpochHistory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const fulfilled: EpochInfo = {
status: { tag: "Fulfilled", values: undefined },
total_deposited: 200_0000000n,
total_shares_redeeming: 20_0000000n,
share_price: 1_500_000_000_000_000_000n,
share_price: 150_000_000_000_000n,
closed_at: 1_700_000_000n,
priceable_at: 1_700_003_600n,
}
Expand Down Expand Up @@ -106,7 +106,7 @@ describe("fetchEpochHistory", () => {
status: "Fulfilled",
totalDeposited: 200_0000000n,
totalSharesRedeeming: 20_0000000n,
sharePrice: 1_500_000_000_000_000_000n as Price,
sharePrice: 150_000_000_000_000n as Price,
closedAt: 1_700_000_000n,
priceableAt: 1_700_003_600n,
},
Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useFulfillEpoch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ vi.mock("../config/clients", () => ({
}))

const epochId = 4n
const sharePrice = 1_250_000_000_000_000_000n
const sharePrice = 125_000_000_000_000n

const renderFulfillEpoch = () => renderWithWallet(useFulfillEpoch)

Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useInvestorRequests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const fulfilledEpoch: EpochInfo = {
status: { tag: "Fulfilled", values: undefined },
total_deposited: 200_0000000n,
total_shares_redeeming: 20_0000000n,
share_price: 1_500_000_000_000_000_000n,
share_price: 150_000_000_000_000n,
closed_at: 1_700_000_000n,
priceable_at: 1_700_003_600n,
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/hooks/useNavPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const paused: ContractRead<OracleState> = {
const unreadableState: ContractRead<OracleState> = { kind: "unreadable" }

const report: NavReport = {
nav_per_share: 1500000000000000000n,
nav_per_share: 150000000000000n,
expires_at: 1800000000n,
accepted_at: 1757900000n,
proof_hash: Buffer.alloc(32, 0xab),
Expand Down
21 changes: 10 additions & 11 deletions app/src/pages/VaultPreview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
networkPassphrase,
PRICE_DECIMALS,
type Price,
PRICE_SCALE,
shortAddress,
} from "@stellar-scaffold/app-lib"
import type * as AppLib from "@stellar-scaffold/app-lib"
Expand Down Expand Up @@ -73,7 +74,7 @@ const {
1n,
{
status: { tag: "Fulfilled" },
share_price: 1_000000000000000000n,
share_price: 1_00000000000000n,
priceable_at: 1_700_003_600n,
},
],
Expand Down Expand Up @@ -195,8 +196,6 @@ const defaultCancelRedeemImpl = async ({
}
}

const WAD_SCALE = 1_000000000000000000n

const defaultClaimDepositImpl = async ({
epoch_id,
}: {
Expand All @@ -207,7 +206,7 @@ const defaultClaimDepositImpl = async ({
const sharePrice = mockRequests.epochs.get(epoch_id)?.share_price ?? 0n
const shares =
request !== undefined && sharePrice > 0n
? (request.amount * WAD_SCALE) / sharePrice
? (request.amount * PRICE_SCALE) / sharePrice
: 0n
return {
simulation: undefined,
Expand Down Expand Up @@ -235,7 +234,7 @@ const defaultClaimRedeemImpl = async ({
const request = mockRequests.redeems.get(epoch_id)
const sharePrice = mockRequests.epochs.get(epoch_id)?.share_price ?? 0n
const assets =
request !== undefined ? (request.shares * sharePrice) / WAD_SCALE : 0n
request !== undefined ? (request.shares * sharePrice) / PRICE_SCALE : 0n
return {
simulation: undefined,
signAndSend: async ({
Expand Down Expand Up @@ -320,7 +319,7 @@ afterEach(() => {
resetMockRequests()
})

const price = 1500000000000000000n as Price
const price = 150000000000000n as Price
const attestedAt = 1757900000n

vi.mock("../config/contracts", () => {
Expand Down Expand Up @@ -376,7 +375,7 @@ vi.mock("../config/clients", () => {
state: async () => ({ result: { tag: "Valid", values: undefined } }),
latest: async () => ({
result: {
nav_per_share: 1500000000000000000n,
nav_per_share: 150000000000000n,
expires_at: 1800000000n,
accepted_at: 1757900000n,
proof_hash: Buffer.alloc(32, 0xab),
Expand Down Expand Up @@ -1488,7 +1487,7 @@ describe("VaultPreview", () => {
mockRequests.currentEpoch = readyDepositEpoch
mockRequests.epochs.set(readyDepositEpoch, {
status: { tag: "Fulfilled" },
share_price: 1_000000000000000000n,
share_price: 1_00000000000000n,
priceable_at: 1_700_003_600n,
})
mockRequests.deposits.set(readyDepositEpoch, {
Expand Down Expand Up @@ -1594,7 +1593,7 @@ describe("VaultPreview", () => {
mockRequests.currentEpoch = 4n
mockRequests.epochs.set(4n, {
status: { tag: "Fulfilled" },
share_price: 1_000000000000000000n,
share_price: 1_00000000000000n,
priceable_at: 1_700_003_600n,
})
mockRequests.deposits.set(4n, { amount: 150_0000000n, claimed: false })
Expand Down Expand Up @@ -1718,7 +1717,7 @@ describe("VaultPreview", () => {
mockRequests.currentEpoch = readyRedeemEpoch
mockRequests.epochs.set(readyRedeemEpoch, {
status: { tag: "Fulfilled" },
share_price: 1_000000000000000000n,
share_price: 1_00000000000000n,
priceable_at: 1_700_003_600n,
})
mockRequests.redeems.set(readyRedeemEpoch, {
Expand Down Expand Up @@ -1764,7 +1763,7 @@ describe("VaultPreview", () => {
mockRequests.currentEpoch = epoch
mockRequests.epochs.set(epoch, {
status: { tag: "Fulfilled" },
share_price: 1_000000000000000000n,
share_price: 1_00000000000000n,
priceable_at: 1_700_003_600n,
})
mockRequests.redeems.set(epoch, { shares: 952380952n, claimed: false })
Expand Down
11 changes: 5 additions & 6 deletions app/src/pages/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Amount } from "@stellar-scaffold/app-lib"
import { type Amount, PRICE_SCALE } from "@stellar-scaffold/app-lib"
import { describe, expect, it } from "vitest"
import { type Grant } from "../hooks/useAdminAuthority"
import { type ConfigurationState } from "../hooks/useConfigurationState"
Expand All @@ -16,7 +16,6 @@ const CUSTODIAN = "CACAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAINCW"
const COMPLIANCE = "CACQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQLC2U"
const VERIFIER = "CADAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMBQGAYDAMSST"
const DAY = 86_400n
const PRICE_ONE = 10n ** 18n

const grantOf = (role: string, overrides: Partial<Grant> = {}): Grant => ({
role,
Expand Down Expand Up @@ -47,8 +46,8 @@ const ready = (
cooldown: 3_600n,
maxUpBps: 1_000,
maxDownBps: 500,
min: PRICE_ONE / 2n,
max: 2n * PRICE_ONE,
min: PRICE_SCALE / 2n,
max: 2n * PRICE_SCALE,
},
compliance: COMPLIANCE,
identityVerifier: VERIFIER,
Expand Down Expand Up @@ -639,8 +638,8 @@ describe("toConfigurationActions", () => {
cooldown_secs: 3_600n,
max_up_bps: 500,
max_down_bps: 500,
min_answer: PRICE_ONE / 2n,
max_answer: 2n * PRICE_ONE,
min_answer: PRICE_SCALE / 2n,
max_answer: 2n * PRICE_SCALE,
})
})

Expand Down
Loading
Loading